"use client"; import { useState, useRef, useEffect, useCallback } from "react"; import { IconRail, type PanelId } from "./icon-rail"; import { Pencil, Moon, Sun, Monitor } from "lucide-react"; import { motion } from "framer-motion"; import { Sidebar, SidebarContent, SidebarHeader, } from "@/components/ui/primitives/sidebar"; import { cn } from "@/lib/utils"; import { SettingsPanel } from "./panels/settings-panel"; import { SitePanel } from "./panels/site-panel"; import { useProjectStore } from "@/features/community/lib/projects/store"; import { updateProjectName } from "@/features/community/lib/projects/actions"; import { useViewer } from "@pascal-app/viewer"; export function AppSidebar() { const [activePanel, setActivePanel] = useState("site"); const activeProject = useProjectStore((s) => s.activeProject); const theme = useViewer((state) => state.theme); const setTheme = useViewer((state) => state.setTheme); const [mounted, setMounted] = useState(false); const [isEditingTitle, setIsEditingTitle] = useState(false); const [titleValue, setTitleValue] = useState(""); const inputRef = useRef(null); useEffect(() => { setMounted(true); }, []); useEffect(() => { if (isEditingTitle) { setTitleValue(activeProject?.name || "Untitled Project"); setTimeout(() => { if (inputRef.current) { inputRef.current.focus(); inputRef.current.select(); } }, 0); } }, [isEditingTitle, activeProject?.name]); const handleSaveTitle = useCallback(async () => { const trimmed = titleValue.trim(); if (trimmed && activeProject && trimmed !== activeProject.name) { // Optimistic update useProjectStore.setState((state) => ({ activeProject: state.activeProject ? { ...state.activeProject, name: trimmed } : null, projects: state.projects.map((p) => p.id === activeProject.id ? { ...p, name: trimmed } : p) })); // Server update try { await updateProjectName(activeProject.id, trimmed); } catch (error) { console.error("Failed to update project name:", error); } } setIsEditingTitle(false); }, [titleValue, activeProject]); const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault(); handleSaveTitle(); } else if (e.key === "Escape") { e.preventDefault(); setIsEditingTitle(false); } }; const renderPanelContent = () => { switch (activePanel) { case "site": return ; case "settings": return ; default: return null; } }; const getPanelTitle = () => { switch (activePanel) { case "site": return "Site"; case "settings": return "Settings"; default: return ""; } }; return (
{/* Icon Rail */} {/* Panel Content */}
{isEditingTitle ? ( setTitleValue(e.target.value)} onKeyDown={handleKeyDown} onBlur={handleSaveTitle} placeholder="Untitled Project" className="w-full bg-transparent text-foreground outline-none border-b border-primary/50 focus:border-primary rounded-none px-0 py-0 m-0 h-7 font-semibold text-lg" /> ) : (
setIsEditingTitle(true)} >

{activeProject?.name || "Untitled Project"}

)}
{mounted && ( )}
{getPanelTitle()}
{renderPanelContent()}
); }