import { useState, useEffect, useCallback, useRef } from 'react'; import { useParams } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { type Project } from '@/lib/ProjectsManager'; import { useProjectsManager } from '@/hooks/useProjectsManager'; import { ChatPane, type ChatPaneRef } from '@/components/Shakespeare/ChatPane'; import { PreviewPane } from '@/components/Shakespeare/PreviewPane'; import { ProjectSidebar } from '@/components/ProjectSidebar'; import { ProjectDetailsDialog } from '@/components/ProjectDetailsDialog'; import { ResizablePanelGroup, ResizablePanel, ResizableHandle } from '@/components/ui/resizable'; import { Button } from '@/components/ui/button'; import { Skeleton } from '@/components/ui/skeleton'; import { ArrowLeft, MessageSquare, Eye, Code, Menu } from 'lucide-react'; import { useNavigate } from 'react-router-dom'; import { useIsMobile } from '@/hooks/useIsMobile'; import { ProjectTitleMenu } from '@/components/ProjectTitleMenu'; import { GitHistoryDialog } from '@/components/ai/GitHistoryDialog'; import { GitDialog } from '@/components/GitDialog'; import { DeployDialog } from '@/components/DeployDialog'; import { DuplicateProjectDialog } from '@/components/DuplicateProjectDialog'; import { GitStatusIndicator } from '@/components/GitStatusIndicator'; import { StarButton } from '@/components/StarButton'; import { useBuildProject } from '@/hooks/useBuildProject'; import { useIsProjectPreviewable } from '@/hooks/useIsProjectPreviewable'; import { useConsoleError } from '@/hooks/useConsoleError'; import { useAutoBuild } from '@/hooks/useAutoBuild'; import { cn } from '@/lib/utils'; export function ProjectView() { const { projectId } = useParams<{ projectId: string }>(); const { t } = useTranslation(); const [project, setProject] = useState(null); const [isLoading, setIsLoading] = useState(true); const [activeTab, setActiveTab] = useState<'preview' | 'code'>('preview'); const [mobileView, setMobileView] = useState<'chat' | 'preview' | 'code'>('chat'); const [isAILoading, setIsAILoading] = useState(false); const [isProjectDetailsOpen, setIsProjectDetailsOpen] = useState(false); const [gitHistoryOpen, setGitHistoryOpen] = useState(false); const [gitDialogOpen, setGitDialogOpen] = useState(false); const [deployDialogOpen, setDeployDialogOpen] = useState(false); const [duplicateDialogOpen, setDuplicateDialogOpen] = useState(false); // Use console error state from provider const { consoleError, dismissConsoleError, clearErrors } = useConsoleError(); const projectsManager = useProjectsManager(); const chatPaneRef = useRef(null); const navigate = useNavigate(); const isMobile = useIsMobile(); const [isSidebarVisible, setIsSidebarVisible] = useState(!isMobile); const build = useBuildProject(projectId!); const { data: isPreviewable = false } = useIsProjectPreviewable(projectId!); // Enable auto-build for previewable projects useAutoBuild({ projectId: projectId!, enabled: isPreviewable, debounceMs: 2000, }); const loadProject = useCallback(async () => { if (!projectId) return; try { const projectData = await projectsManager.getProject(projectId); setProject(projectData); // Clear console messages and error state when switching projects clearErrors(); } catch (error) { console.error('Failed to load project:', error); } finally { setIsLoading(false); } }, [projectId, projectsManager, clearErrors]); useEffect(() => { loadProject(); }, [loadProject]); // Reset view state when projectId changes useEffect(() => { if (projectId) { // Reset to preview tab if the new project is previewable, otherwise code setActiveTab(isPreviewable ? 'preview' : 'code'); // Reset mobile view to chat when switching projects setMobileView('chat'); } }, [projectId, isPreviewable]); // Switch away from preview mode if project is not previewable useEffect(() => { if (mobileView === 'preview' && !isPreviewable) { setMobileView('code'); } if (activeTab === 'preview' && !isPreviewable) { setActiveTab('code'); } }, [isPreviewable, mobileView, activeTab]); const handleNewChat = () => { // Reset to chat view on mobile when starting new chat if (isMobile) { setMobileView('chat'); } // Call the ChatPane's startNewSession function if (chatPaneRef.current) { chatPaneRef.current.startNewSession(); } }; const handleAILoadingChange = (loading: boolean) => { setIsAILoading(loading); }; // Handle first user interaction to enable audio context const handleFirstInteraction = () => { // This will be handled automatically by the useKeepAlive hook // when isAILoading becomes true after user interaction }; const handleDismissConsoleError = () => { dismissConsoleError(); }; const handleProjectDeleted = async () => { setProject(null); navigate('/'); }; const isAnyLoading = isAILoading || build.isPending; if (!project && !isLoading) { return (

Project Not Found

); } if (isMobile) { return (
{project ? ( setGitHistoryOpen(true)} onGitDialog={() => setGitDialogOpen(true)} onDeploy={() => setDeployDialogOpen(true)} onDuplicate={() => setDuplicateDialogOpen(true)} onProjectDetails={() => setIsProjectDetailsOpen(true)} isAILoading={isAILoading} isAnyLoading={isAnyLoading} className="text-sm" /> ) : ( )}
{project ? ( ) : ( )}
{/* Mobile Sidebar Overlay */} {isSidebarVisible && (
{/* Backdrop - only covers the area not occupied by the sidebar */}
setIsSidebarVisible(false)} /> {/* Sidebar - positioned above the backdrop */}
{ setProject(selectedProject); setIsSidebarVisible(false); // Always collapse sidebar on mobile navigation if (selectedProject) { setMobileView('chat'); // Switch to chat view when selecting a project navigate(`/project/${selectedProject.id}`); } else { navigate('/'); } }} onClose={() => setIsSidebarVisible(false)} />
)}
{mobileView === 'chat' && ( project ? ( ) : (
) )} {(mobileView === 'preview' || mobileView === 'code') && ( project ? ( ) : (
) )}
{isPreviewable && ( )}
{/* Dialogs */} {project && ( <> )}
); } return (
{/* Fixed Sidebar */}
{ setProject(selectedProject); if (selectedProject) { navigate(`/project/${selectedProject.id}`); } else { navigate('/'); } }} onToggleSidebar={() => setIsSidebarVisible(!isSidebarVisible)} className="h-full" />
{/* Main Content Area */}
{/* Chat Panel */}
{/* Chat Header */}
{/* Left side - Sidebar toggle (when collapsed) */}
{!isSidebarVisible && ( )} {/* Project title */}
{project ? ( setGitHistoryOpen(true)} onGitDialog={() => setGitDialogOpen(true)} onDeploy={() => setDeployDialogOpen(true)} onDuplicate={() => setDuplicateDialogOpen(true)} onProjectDetails={() => setIsProjectDetailsOpen(true)} isAILoading={isAILoading} isAnyLoading={isAnyLoading} className="text-lg" /> ) : ( )}
{/* Right side - Star button only */}
{project ? ( ) : ( )}
{/* Chat Content */}
{project ? ( ) : (
)}
{/* Resizable Handle */} {/* Preview/Code Panel */}
{project ? ( setActiveTab(activeTab === 'preview' ? 'code' : 'preview') : undefined} projectName={project.name} onFirstInteraction={handleFirstInteraction} isPreviewable={isPreviewable} /> ) : (
)}
{/* Dialogs */} {project && ( <> )}
); }