"use client"; import { useState, useEffect } from "react"; import { Clock, History, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle, SheetTrigger, } from "@/components/ui/sheet"; import { ScrollArea } from "@/components/ui/scroll-area"; import { auth, db } from "@/lib/firebase/config"; import { doc, getDoc, collection, query, where, orderBy, getDocs } from "firebase/firestore"; import { formatDistanceToNow } from "date-fns"; interface MissionRevision { id: string; content: string; updatedAt: Date; updatedBy: string; source: 'ai' | 'user'; } interface MissionIdeaSectionProps { projectId: string; } export function MissionIdeaSection({ projectId }: MissionIdeaSectionProps) { const [loading, setLoading] = useState(true); const [content, setContent] = useState(""); const [lastUpdated, setLastUpdated] = useState(null); const [revisions, setRevisions] = useState([]); const [loadingRevisions, setLoadingRevisions] = useState(false); useEffect(() => { if (projectId) { fetchMissionIdea(); } }, [projectId]); const fetchMissionIdea = async () => { setLoading(true); try { const user = auth.currentUser; if (!user) return; // Fetch current mission idea from project document const projectRef = doc(db, 'projects', projectId); const projectSnap = await getDoc(projectRef); if (projectSnap.exists()) { const data = projectSnap.data(); setContent( data.missionIdea || "Help solo founders build and launch their products 10x faster by turning conversations into production-ready code, designs, and marketing." ); setLastUpdated(data.missionIdeaUpdatedAt?.toDate() || null); } } catch (error) { console.error('Error fetching mission idea:', error); } finally { setLoading(false); } }; const fetchRevisions = async () => { setLoadingRevisions(true); try { const user = auth.currentUser; if (!user) return; // Fetch revision history const revisionsRef = collection(db, 'missionRevisions'); const revisionsQuery = query( revisionsRef, where('projectId', '==', projectId), orderBy('updatedAt', 'desc') ); const revisionsSnap = await getDocs(revisionsQuery); const revisionsList: MissionRevision[] = []; revisionsSnap.forEach((doc) => { const data = doc.data(); revisionsList.push({ id: doc.id, content: data.content, updatedAt: data.updatedAt?.toDate(), updatedBy: data.updatedBy || 'AI', source: data.source || 'ai', }); }); setRevisions(revisionsList); } catch (error) { console.error('Error fetching revisions:', error); } finally { setLoadingRevisions(false); } }; if (loading) { return (
); } return (
{/* Content Card */}

{content}

{/* Meta Information */}
{lastUpdated ? `Last updated ${formatDistanceToNow(lastUpdated, { addSuffix: true })} by AI` : 'Not yet updated'}
{/* Revision History */} Revision History See how your mission idea has evolved over time {loadingRevisions ? (
) : revisions.length === 0 ? (

No revision history yet

) : (
{revisions.map((revision, index) => (
{revision.source === 'ai' ? 'AI Update' : 'Manual Edit'} {index === 0 && ( Current )}
{formatDistanceToNow(revision.updatedAt, { addSuffix: true })}

{revision.content}

{revision.updatedAt.toLocaleString()}
))}
)}
); }