import { useState, useEffect, useCallback, useRef } from 'react'; import { useParams } from 'react-router-dom'; 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 { ResizablePanelGroup, ResizablePanel, ResizableHandle } from '@/components/ui/resizable'; import { Button } from '@/components/ui/button'; import { ArrowLeft, MessageSquare, Eye, Code, Menu, Columns2 } from 'lucide-react'; import { useNavigate } from 'react-router-dom'; import { ActionsMenu } from '@/components/ActionsMenu'; import { GitStatusIndicator } from '@/components/GitStatusIndicator'; import { useIsMobile } from '@/hooks/useIsMobile'; import { useFS } from '@/hooks/useFS'; import { useJSRuntime } from '@/hooks/useJSRuntime'; import { useKeepAlive } from '@/hooks/useKeepAlive'; import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools'; import { bytesToHex } from 'nostr-tools/utils'; import { buildProject } from "@/lib/build"; import { copyDirectory } from '@/lib/copyFiles'; export function ProjectView() { const { projectId } = useParams<{ projectId: string }>(); 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 [isBuildLoading, setIsBuildLoading] = useState(false); const [isDeployLoading, setIsDeployLoading] = useState(false); const [isAILoading, setIsAILoading] = useState(false); const projectsManager = useProjectsManager(); const chatPaneRef = useRef(null); const navigate = useNavigate(); const isMobile = useIsMobile(); const { fs: browserFS } = useFS(); const { runtime } = useJSRuntime(); const [isSidebarVisible, setIsSidebarVisible] = useState(() => { // Check if we're on mobile immediately to set correct initial state if (typeof window !== 'undefined') { return window.innerWidth >= 768; // Desktop starts open, mobile starts closed } return false; // Default to closed for SSR }); // Keep-alive functionality to prevent tab throttling during AI processing const { updateMetadata } = useKeepAlive({ enabled: isAILoading || isBuildLoading || isDeployLoading, title: 'Shakespeare', artist: project ? `Working on ${project.name}...` : 'Working...', artwork: [ { src: '/favicon.png', sizes: '512x512', type: 'image/png' } ] }); const loadProject = useCallback(async () => { if (!projectId) return; try { const projectData = await projectsManager.getProject(projectId); setProject(projectData); } catch (error) { console.error('Failed to load project:', error); } finally { setIsLoading(false); } }, [projectId, projectsManager]); useEffect(() => { loadProject(); }, [loadProject]); const runBuild = async () => { if (isBuildLoading || !project || !browserFS) return; setIsBuildLoading(true); updateMetadata('Shakespeare', `Building ${project.name}...`); try { const dist = await buildProject({ fs: browserFS, projectPath: `/projects/${project.id}`, domParser: new DOMParser(), target: "esnext", }); console.log(dist); // Delete all existing files in "dist" directory try { for (const file of await browserFS.readdir(`/projects/${project.id}/dist`)) { await browserFS.unlink(`/projects/${project.id}/dist/${file}`); } } catch { // Ignore errors (e.g., directory doesn't exist) } await browserFS.mkdir(`/projects/${project.id}/dist`, { recursive: true }); for (const [path, contents] of Object.entries(dist)) { await browserFS.writeFile(`/projects/${project.id}/dist/${path}`, contents); } } catch (error) { console.error('Build failed:', error); } finally { setIsBuildLoading(false); } }; const runDeploy = async () => { if (isDeployLoading || !project || !browserFS || !runtime) return; setIsDeployLoading(true); updateMetadata('Shakespeare', `Deploying ${project.name}...`); try { const runtimeFS = await runtime.fs(); const sk = generateSecretKey(); const pubkey = getPublicKey(sk); const npub = nip19.npubEncode(pubkey); const projectUrl = `https://${npub}.nostrdeploy.com`; console.log('Running deploy for project:', project.id, 'at', projectUrl); await runtimeFS.mkdir('dist', { recursive: true }); await runtimeFS.writeFile('.env.nostr-deploy.local', `# Nostr Deploy CLI Configuration # This file contains sensitive information - do not commit to version control # Nostr Authentication NOSTR_PRIVATE_KEY=${bytesToHex(sk)} NOSTR_PUBLIC_KEY=${pubkey} NOSTR_RELAYS=wss://relay.nostr.band,wss://nostrue.com,wss://purplerelay.com,wss://relay.primal.net,wss://ditto.pub/relay # Blossom File Storage BLOSSOM_SERVERS=https://cdn.hzrd149.com,https://blossom.primal.net,https://blossom.band,https://blossom.f7z.io # Deployment Settings BASE_DOMAIN=nostrdeploy.com`); // Copy "dist" files to runtime filesystem const distPath = `/projects/${project.id}/dist`; try { await copyDirectory(browserFS, runtimeFS, distPath, 'dist'); console.log('Successfully copied dist to runtime'); } catch (error) { console.error('Failed to copy dist to runtime:', error); return; } const proc = await runtime.spawn('npx', ["-y", "nostr-deploy-cli", "deploy"]); await proc.exit; console.log('Project deployed:', projectUrl); } catch (error) { console.error('Deploy failed:', error); } finally { setIsDeployLoading(false); } }; 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 }; if (isLoading) { return (
); } if (!project) { return (

Project Not Found

); } if (isMobile) { return (

{project.name}

{/* 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('/'); } }} />
)}
{mobileView === 'chat' && ( )} {(mobileView === 'preview' || mobileView === 'code') && ( )}
); } return (
{/* Fixed Sidebar */}
{ setProject(selectedProject); if (selectedProject) { navigate(`/project/${selectedProject.id}`); } else { navigate('/'); } }} className="h-full" />
{/* Main Content Area */}
{/* Chat Panel */}
{/* Chat Header */}
{/* Left side - Sidebar toggle */}
{/* Center - Project title */}

{project.name}

{/* Right side - Actions Menu */}
{/* Chat Content */}
{/* Resizable Handle */} {/* Preview/Code Panel */}
{/* Preview Header */}
{/* Preview Content */}
); }