import * as React from 'react'; import { Sidebar } from './components/Sidebar'; import { PageBuilder } from './components/PageBuilder'; import AIAssistant from './components/AIAssistant'; import PageAnalytics from './components/PageAnalytics'; import InteractionAnalytics from './components/InteractionAnalytics'; import ShortLinks from './components/ShortLinks'; import EnterpriseHosting from './components/EnterpriseHosting'; import SEOServices from './components/SEOServices'; import VideoCreator from './components/VideoCreator'; import NewsCreator from './components/NewsCreator'; import { ContentScheduler } from './components/ContentScheduler'; import { CRM } from './components/CRM'; import { GreenPage, AIGCSettings, FormSubmission } from './types'; import { createNewPage } from './services/mockDataService'; import PageManagementModal from './components/PageManagementModal'; import ShareModal from './components/ShareModal'; import Showcase from './components/Showcase'; import Auth from './components/Auth'; import { authService } from './services/authService'; import { LanguageProvider, useTranslation } from './hooks/useI18n'; const MainApp = () => { const { t } = useTranslation(); const [currentUser, setCurrentUser] = React.useState(() => authService.getCurrentUser()); const [pages, setPages] = React.useState([]); const [activePageId, setActivePageId] = React.useState(null); const [activeNavKey, setActiveNavKey] = React.useState('Page.Link'); const [isPageManagerOpen, setIsPageManagerOpen] = React.useState(false); const [isShareModalOpen, setIsShareModalOpen] = React.useState(false); const [theme, setTheme] = React.useState<'light' | 'dark'>('dark'); // Load user pages on login/app load, and handle multi-tab logout React.useEffect(() => { const loadUserData = () => { const user = authService.getCurrentUser(); if (user) { if (user !== currentUser) { setCurrentUser(user); } const userPages = authService.getUserPages(); if (userPages) { setPages(userPages); if (userPages.length > 0) { // Ensure activePageId is valid, otherwise default to the first page const currentActive = userPages.find(p => p.id === activePageId); if (!currentActive) { setActivePageId(userPages[0].id); } } else { setActivePageId(null); } } } else { setCurrentUser(null); setPages([]); setActivePageId(null); } }; loadUserData(); // Listen for storage changes to handle login/logout from other tabs window.addEventListener('storage', loadUserData); return () => { window.removeEventListener('storage', loadUserData); }; }, [currentUser, activePageId]); // Save pages whenever they change React.useEffect(() => { if (currentUser && pages.length > 0) { authService.saveUserPages(pages); } }, [pages, currentUser]); React.useEffect(() => { if (theme === 'dark') { document.documentElement.classList.add('dark'); } else { document.documentElement.classList.remove('dark'); } }, [theme]); const activePage = React.useMemo(() => pages.find(p => p.id === activePageId), [pages, activePageId]); const handleAuthSuccess = () => { const user = authService.getCurrentUser(); setCurrentUser(user); }; const handleLogout = () => { authService.logout(); setCurrentUser(null); }; const handleUpdateActivePage = (updates: Partial) => { setPages(currentPages => currentPages.map(page => page.id === activePageId ? { ...page, ...updates } : page ) ); }; const handleUpdatePage = (id: string, newName: string) => { setPages(currentPages => currentPages.map(page => page.id === id ? { ...page, name: newName, slug: newName.toLowerCase().replace(/\s+/g, '-') } : page ) ); }; const handleCreatePage = (name: string) => { if (!name.trim()) return; const newPage = createNewPage(name); setPages(prev => [...prev, newPage]); setActivePageId(newPage.id); setActiveNavKey('Page.Link'); setIsPageManagerOpen(false); }; const handleImportShowcasePage = (pageData: GreenPage) => { // This is a deep copy to prevent state mutation issues const newPage: GreenPage = JSON.parse(JSON.stringify(pageData)); // Make it unique for the user's list newPage.id = `page_${Date.now()}_${Math.random()}`; newPage.name = `${pageData.name} (Copy)`; newPage.slug = `${pageData.slug}-copy-${Math.floor(Math.random() * 1000)}`; setPages(prev => [...prev, newPage]); setActivePageId(newPage.id); setActiveNavKey('Page.Link'); // Switch to the page builder alert(t('app.import_success', { name: pageData.name })); }; const handleVideosGenerated = (newVideos: any[]) => { if (!activePage) return; const updatedAIGCSettings = { ...activePage.aigcSettings, videos: [...activePage.aigcSettings.videos, ...newVideos] }; handleUpdateActivePage({ aigcSettings: updatedAIGCSettings }); alert(t('app.videos_generated_success', { count: newVideos.length })); setActiveNavKey('AIGC.Scheduler'); }; const handleScheduleUpdate = (newSchedule: any[]) => { if (!activePage) return; const updatedAIGCSettings = { ...activePage.aigcSettings, schedule: newSchedule }; handleUpdateActivePage({ aigcSettings: updatedAIGCSettings }); }; const handleUpdateAIGCSettings = (newSettings: AIGCSettings) => { handleUpdateActivePage({ aigcSettings: newSettings }); }; const handleFormSubmission = (submission: FormSubmission) => { if (!activePage) return; const updatedAnalyticsData = { ...activePage.analyticsData, formSubmissions: [...(activePage.analyticsData.formSubmissions || []), submission] }; handleUpdateActivePage({ analyticsData: updatedAnalyticsData }); alert(t('app.form_submission_success')); }; const renderContent = () => { if (activeNavKey.startsWith('Showcase.')) { const tab = (activeNavKey.split('.')[1] || 'Personal').toLowerCase(); return ; } if (!activePage) { return (

{t('app.welcome')}

{t('app.no_pages_prompt')}

); } if (activeNavKey.startsWith('Page.')) { const tab = (activeNavKey.split('.')[1] || '').toLowerCase(); return handleUpdateActivePage({ pageSettings: newSettings })} aigcVideos={activePage.aigcSettings.videos} aigcArticles={activePage.aigcSettings.articles} initialTab={tab as any || 'link'} onOpenShareModal={() => setIsShareModalOpen(true)} onFormSubmit={handleFormSubmission} /> } if (activeNavKey.startsWith('AI Assistant.')) { const tab = (activeNavKey.split('.')[1] || 'Persona').toLowerCase(); return handleUpdateActivePage({ aiAssistantSettings: newSettings })} initialTab={tab as any} /> } if (activeNavKey.startsWith('SEO.')) { const tab = (activeNavKey.split('.')[1] || 'ShortLinks'); switch (tab) { case 'ShortLinks': return handleUpdateActivePage({ slug })} /> case 'Hosting': return case 'Services': return default: return handleUpdateActivePage({ slug })}/> } } switch (activeNavKey) { case 'Analytics.Page': return case 'Analytics.Interactions': return handleUpdateActivePage({ analyticsData: { ...activePage.analyticsData, conversations } })} /> case 'Analytics.CRM': return case 'AIGC.Creator': return case 'AIGC.News': return case 'AIGC.Scheduler': return default: return handleUpdateActivePage({ pageSettings: newSettings })} aigcVideos={activePage.aigcSettings.videos} aigcArticles={activePage.aigcSettings.articles} initialTab="link" onOpenShareModal={() => setIsShareModalOpen(true)} onFormSubmit={handleFormSubmission} /> } }; if (!currentUser) { return ; } return (
setIsPageManagerOpen(true)} activeNavKey={activeNavKey} setActiveNavKey={setActiveNavKey} theme={theme} setTheme={setTheme} currentUser={currentUser} onLogout={handleLogout} />
{renderContent()}
{isPageManagerOpen && ( { setActivePageId(id); setIsPageManagerOpen(false); }} onCreatePage={handleCreatePage} onUpdatePage={handleUpdatePage} onClose={() => setIsPageManagerOpen(false)} /> )} {isShareModalOpen && activePage && ( setIsShareModalOpen(false)} /> )}
); }; const App = () => { return ( ); }; export default App;