"use client"; import { useState, useEffect } from "react"; import { useParams, usePathname } from "next/navigation"; import { Target, Users, AlertCircle, TrendingUp, Lightbulb, Plus, Edit, Search, Loader2, Layout, CheckCircle, DollarSign, Link as LinkIcon, } from "lucide-react"; import { PageTemplate, PageSection, PageCard, PageGrid, PageEmptyState, } from "@/components/layout/page-template"; import { Button } from "@/components/ui/button"; import { MissionContextTree } from "@/components/mission/mission-context-tree"; import { MissionIdeaSection } from "@/components/mission/mission-idea-section"; import { auth } from "@/lib/firebase/config"; import { toast } from "sonner"; const MISSION_NAV_ITEMS = [ { title: "Target Customer", icon: Users, href: "/mission" }, { title: "Existing Solutions", icon: Layout, href: "/mission#solutions" }, ]; interface MissionFramework { targetCustomer: { primaryAudience: string; theirSituation: string; relatedMarkets?: string[]; }; existingSolutions: Array<{ category: string; description: string; products: Array<{ name: string; url?: string; }>; }>; innovations: Array<{ title: string; description: string; }>; ideaValidation: Array<{ title: string; description: string; }>; financialSuccess: { subscribers: number; pricePoint: number; retentionRate: number; }; } export default function MissionPage() { const params = useParams(); const pathname = usePathname(); const workspace = params.workspace as string; const projectId = params.projectId as string; const [researchingMarket, setResearchingMarket] = useState(false); const [framework, setFramework] = useState(null); const [loading, setLoading] = useState(true); const [generating, setGenerating] = useState(false); // Fetch mission framework on mount useEffect(() => { fetchFramework(); }, [projectId]); const fetchFramework = async () => { setLoading(true); try { // Fetch project data from Firestore to get the saved framework const user = auth.currentUser; const headers: HeadersInit = {}; if (user) { const token = await user.getIdToken(); headers.Authorization = `Bearer ${token}`; } const response = await fetch(`/api/projects/${projectId}`, { headers, }); if (response.ok) { const data = await response.json(); if (data.project?.phaseData?.missionFramework) { setFramework(data.project.phaseData.missionFramework); console.log('[Mission] Loaded saved framework'); } else { console.log('[Mission] No saved framework found'); } } } catch (error) { console.error('[Mission] Error fetching framework:', error); } finally { setLoading(false); } }; const handleGenerateFramework = async () => { setGenerating(true); try { const user = auth.currentUser; const headers: HeadersInit = { 'Content-Type': 'application/json', }; if (user) { const token = await user.getIdToken(); headers.Authorization = `Bearer ${token}`; } const response = await fetch(`/api/projects/${projectId}/mission/generate`, { method: 'POST', headers, }); if (!response.ok) { throw new Error('Failed to generate mission framework'); } const data = await response.json(); setFramework(data.framework); toast.success('Mission framework generated successfully!'); } catch (error) { console.error('Error generating framework:', error); toast.error('Failed to generate mission framework'); } finally { setGenerating(false); } }; const handleResearchMarket = async () => { setResearchingMarket(true); try { const user = auth.currentUser; if (!user) { toast.error('Please sign in'); return; } const token = await user.getIdToken(); const response = await fetch(`/api/projects/${projectId}/research/market`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json', }, }); if (!response.ok) { throw new Error('Failed to conduct market research'); } const data = await response.json(); toast.success( `Market research complete! Found ${data.research.targetNiches.length} niches, ` + `${data.research.competitors.length} competitors, and ${data.research.marketGaps.length} gaps.` ); // Regenerate framework with new insights await handleGenerateFramework(); } catch (error) { console.error('Error conducting market research:', error); toast.error('Failed to conduct market research'); } finally { setResearchingMarket(false); } }; // Build sidebar items with full hrefs and active states const sidebarItems = MISSION_NAV_ITEMS.map((item) => { const fullHref = `/${workspace}/project/${projectId}${item.href}`; return { ...item, href: fullHref, isActive: pathname === fullHref || pathname.startsWith(fullHref), }; }); if (loading) { return ( , }} >
); } if (!framework) { return ( , }} >

No Mission Framework Yet

Generate your mission framework based on your project's insights and knowledge

); } return ( , }} > {/* Target Customer */} {generating ? ( ) : ( )} Regenerate } >

Primary Audience

{framework.targetCustomer.primaryAudience}

Their Situation

{framework.targetCustomer.theirSituation}

{framework.targetCustomer.relatedMarkets && framework.targetCustomer.relatedMarkets.length > 0 && (

Related Markets

    {framework.targetCustomer.relatedMarkets.map((market, idx) => (
  • {market}
  • ))}
)}
{/* Existing Solutions */} {researchingMarket ? ( <> Researching... ) : ( <> Research Market )} } >
{framework.existingSolutions.map((solution, idx) => (

{solution.category}

{solution.products && solution.products.length > 0 && (
{solution.products.map((product, prodIdx) => (
{product.url ? ( {product.name} ) : ( {product.name} )}
))}
)}
))}
); }