|
| 1 | +import { useEffect } from 'react'; |
| 2 | + |
| 3 | +interface UseSegmentationSetupProps { |
| 4 | + authChecked: boolean; |
| 5 | + onOpenPreferences: () => void; |
| 6 | + onOpenLogin: () => void; |
| 7 | + onOpenRegister: () => void; |
| 8 | + onOpenProfile: (userId?: string) => void; |
| 9 | + onOpenHelp: () => void; |
| 10 | +} |
| 11 | + |
| 12 | +export const useSegmentationSetup = ({ |
| 13 | + authChecked, |
| 14 | + onOpenPreferences, |
| 15 | + onOpenLogin, |
| 16 | + onOpenRegister, |
| 17 | + onOpenProfile, |
| 18 | + onOpenHelp |
| 19 | +}: UseSegmentationSetupProps) => { |
| 20 | + useEffect(() => { |
| 21 | + if (!authChecked) return; |
| 22 | + |
| 23 | + const isDebugMode = window.location.search.includes('debug=1') || window.location.hostname === 'localhost'; |
| 24 | + |
| 25 | + if (isDebugMode) { |
| 26 | + console.log('🚀 IRIS Segmentation: React SPA initialized'); |
| 27 | + } |
| 28 | + |
| 29 | + // Check if we should auto-open preferences (from admin navigation) |
| 30 | + const urlParams = new URLSearchParams(window.location.search); |
| 31 | + if (urlParams.get('openPreferences') === 'true') { |
| 32 | + onOpenPreferences(); |
| 33 | + // Clean up URL parameter |
| 34 | + urlParams.delete('openPreferences'); |
| 35 | + const newUrl = window.location.pathname + (urlParams.toString() ? '?' + urlParams.toString() : ''); |
| 36 | + window.history.replaceState({}, '', newUrl); |
| 37 | + } |
| 38 | + |
| 39 | + // Wait for legacy JavaScript to load, then check auth and initialize |
| 40 | + const waitForLegacyScripts = setInterval(() => { |
| 41 | + if (window.init_segmentation) { |
| 42 | + clearInterval(waitForLegacyScripts); |
| 43 | + |
| 44 | + if (isDebugMode) console.log('✅ Legacy scripts loaded'); |
| 45 | + |
| 46 | + // Check authentication BEFORE calling init_segmentation |
| 47 | + fetch('/user/get/current') |
| 48 | + .then(response => { |
| 49 | + if (response.status === 403) { |
| 50 | + if (isDebugMode) console.log('❌ Not authenticated, showing React login modal'); |
| 51 | + onOpenLogin(); |
| 52 | + } else { |
| 53 | + if (isDebugMode) console.log('✅ Authenticated, calling init_segmentation()'); |
| 54 | + window.init_segmentation(); |
| 55 | + } |
| 56 | + }) |
| 57 | + .catch(error => { |
| 58 | + console.error('Auth check failed:', error); |
| 59 | + window.init_segmentation(); |
| 60 | + }); |
| 61 | + } |
| 62 | + }, 50); |
| 63 | + |
| 64 | + // Timeout waiting for scripts after 5 seconds |
| 65 | + setTimeout(() => { |
| 66 | + clearInterval(waitForLegacyScripts); |
| 67 | + if (!window.init_segmentation) { |
| 68 | + console.error('❌ Legacy scripts failed to load - init_segmentation not found'); |
| 69 | + } |
| 70 | + }, 5000); |
| 71 | + |
| 72 | + // Override global dialogue functions to use React modals |
| 73 | + (window as any).dialogue_config = onOpenPreferences; |
| 74 | + window.openUserProfile = onOpenProfile; |
| 75 | + window.openLogin = onOpenLogin; |
| 76 | + window.openRegister = onOpenRegister; |
| 77 | + |
| 78 | + // Expose logout function for React |
| 79 | + (window as any).reactLogout = async (callback?: () => void) => { |
| 80 | + await fetch('/user/logout'); |
| 81 | + if (callback) { |
| 82 | + callback(); |
| 83 | + } else { |
| 84 | + window.location.reload(); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + // Expose React functions for legacy JavaScript integration |
| 89 | + window.irisReactApp = { |
| 90 | + openHelpModal: onOpenHelp, |
| 91 | + openUserProfile: onOpenProfile, |
| 92 | + openPreferences: onOpenPreferences, |
| 93 | + }; |
| 94 | + }, [authChecked, onOpenPreferences, onOpenLogin, onOpenRegister, onOpenProfile, onOpenHelp]); |
| 95 | +}; |
0 commit comments