"use client"; import { useEffect, useState } from "react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Loader2, Save, FolderOpen, AlertCircle } from "lucide-react"; import { useParams, useRouter } from "next/navigation"; import { db, auth } from "@/lib/firebase/config"; import { doc, getDoc, updateDoc, serverTimestamp } from "firebase/firestore"; import { toast } from "sonner"; import { Alert, AlertDescription, AlertTitle, } from "@/components/ui/alert"; interface Project { id: string; name: string; productName: string; productVision?: string; workspacePath?: string; workspaceName?: string; githubRepo?: string; chatgptUrl?: string; projectType: string; status: string; } export default function ProjectSettingsPage() { const params = useParams(); const router = useRouter(); const projectId = params.projectId as string; const workspace = params.workspace as string; const [project, setProject] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [orphanedSessionsCount, setOrphanedSessionsCount] = useState(0); // Form state const [productName, setProductName] = useState(""); const [productVision, setProductVision] = useState(""); const [workspacePath, setWorkspacePath] = useState(""); useEffect(() => { const fetchProject = async () => { try { const user = auth.currentUser; if (!user) { toast.error('Please sign in'); router.push('/auth'); return; } const projectDoc = await getDoc(doc(db, 'projects', projectId)); if (!projectDoc.exists()) { toast.error('Project not found'); router.push(`/${workspace}/projects`); return; } const projectData = projectDoc.data() as Project; setProject({ ...projectData, id: projectDoc.id }); // Set form values setProductName(projectData.productName); setProductVision(projectData.productVision || ""); setWorkspacePath(projectData.workspacePath || ""); // Check for orphaned sessions from old workspace path if (projectData.workspacePath) { // This would require checking sessions - we'll implement this in the API // For now, just show the UI } } catch (err: any) { console.error('Error fetching project:', err); toast.error('Failed to load project'); } finally { setLoading(false); } }; const unsubscribe = auth.onAuthStateChanged((user) => { if (user) { fetchProject(); } else { router.push('/auth'); } }); return () => unsubscribe(); }, [projectId, workspace, router]); const handleSave = async () => { setSaving(true); try { const user = auth.currentUser; if (!user) { toast.error('Please sign in'); return; } // Get the directory name from the path const workspaceName = workspacePath ? workspacePath.split('/').pop() || '' : ''; await updateDoc(doc(db, 'projects', projectId), { productName, productVision, workspacePath, workspaceName, updatedAt: serverTimestamp(), }); toast.success('Project settings saved!'); // Refresh project data const projectDoc = await getDoc(doc(db, 'projects', projectId)); if (projectDoc.exists()) { setProject({ ...projectDoc.data() as Project, id: projectDoc.id }); } } catch (error) { console.error('Error saving project:', error); toast.error('Failed to save settings'); } finally { setSaving(false); } }; const handleSelectDirectory = async () => { try { // Check if File System Access API is supported if ('showDirectoryPicker' in window) { const dirHandle = await (window as any).showDirectoryPicker({ mode: 'read', }); if (dirHandle?.name) { // Provide a path hint (browsers don't expose full paths for security) const pathHint = `~/projects/${dirHandle.name}`; setWorkspacePath(pathHint); toast.info('Update the path to match your actual folder location', { description: 'You can get the full path from Finder/Explorer or your terminal' }); } } else { toast.error('Directory picker not supported in this browser', { description: 'Please enter the path manually or use Chrome/Edge' }); } } catch (error: any) { // User cancelled or denied permission if (error.name !== 'AbortError') { console.error('Error selecting directory:', error); toast.error('Failed to select directory'); } } }; if (loading) { return (
); } if (!project) { return (

Project not found

); } return (
{/* Header */}

Project Settings

Manage your project configuration and workspace settings

{/* Content */}
{/* General Settings */} General Information Basic details about your project
setProductName(e.target.value)} placeholder="My Awesome Product" />