'use client' import { useEffect, useState } from 'react' import { useSearchParams, useRouter } from 'next/navigation' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { Separator } from '@/components/ui/separator' import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table' import { Search, Loader2, ExternalLink, RefreshCw, Users, Target, Zap, TrendingUp, Lightbulb, AlertCircle, CheckCircle2, BarChart3, Sparkles } from 'lucide-react' import type { EnhancedProductAnalysis, Opportunity } from '@/lib/types' export default function DashboardPage() { const router = useRouter() const searchParams = useSearchParams() const productName = searchParams.get('product') const [analysis, setAnalysis] = useState(null) const [opportunities, setOpportunities] = useState([]) const [loadingOpps, setLoadingOpps] = useState(false) const [stats, setStats] = useState(null) useEffect(() => { const stored = localStorage.getItem('productAnalysis') const storedStats = localStorage.getItem('analysisStats') if (stored) { setAnalysis(JSON.parse(stored)) } if (storedStats) { setStats(JSON.parse(storedStats)) } }, []) async function findOpportunities() { if (!analysis) return setLoadingOpps(true) try { const response = await fetch('/api/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ analysis }), }) if (!response.ok) throw new Error('Failed to search') const data = await response.json() setOpportunities(data.data.opportunities) } catch (error) { console.error('Search error:', error) } finally { setLoadingOpps(false) } } function getScoreColor(score: number) { if (score >= 0.8) return 'bg-green-500/20 text-green-400 border-green-500/30' if (score >= 0.6) return 'bg-amber-500/20 text-amber-400 border-amber-500/30' return 'bg-red-500/20 text-red-400 border-red-500/30' } function getIntentIcon(intent: string) { switch (intent) { case 'frustrated': return case 'alternative': return case 'comparison': return case 'problem-solving': return default: return } } // Separate keywords by type for prioritization const differentiatorKeywords = analysis?.keywords.filter(k => k.type === 'differentiator' || k.term.includes('vs') || k.term.includes('alternative') || k.term.includes('better') ) || [] const otherKeywords = analysis?.keywords.filter(k => !differentiatorKeywords.includes(k) ) || [] // Sort: single words first, then short phrases const sortedKeywords = [...differentiatorKeywords, ...otherKeywords].sort((a, b) => { const aWords = a.term.split(/\s+/).length const bWords = b.term.split(/\s+/).length return aWords - bWords || a.term.length - b.term.length }) if (!analysis) { return (

Dashboard

No product analyzed yet.

) } return (
{/* Centered container */}
{/* Header */}

{analysis.productName}

{analysis.tagline}

{/* Stats Cards */} {stats && (
{stats.features}
Features
{stats.keywords}
Keywords
{stats.personas}
Personas
{stats.useCases}
Use Cases
{stats.competitors}
Competitors
{stats.dorkQueries}
Queries
)} {/* Main Tabs */} Overview Features Personas Keywords Opportunities {opportunities.length > 0 && `(${opportunities.length})`} {/* Overview Tab */} Product Description

{analysis.description}

{/* Problems Solved */} Problems Solved {analysis.problemsSolved.slice(0, 5).map((problem, i) => (
{problem.problem} {problem.severity}

Feeling: {problem.emotionalImpact}

))}
{/* Competitors */} Competitive Landscape {analysis.competitors.slice(0, 4).map((comp, i) => (
{comp.name}

Your edge: {comp.differentiator}

Their strength: {comp.theirStrength}

))}
{/* Features Tab */} {analysis.features.map((feature, i) => ( {feature.name} {feature.description}

Benefits

    {feature.benefits.map((b, j) => (
  • {b}
  • ))}

Use Cases

    {feature.useCases.map((u, j) => (
  • • {u}
  • ))}
))}
{/* Personas Tab */} {analysis.personas.map((persona, i) => (
{persona.name} {persona.role} • {persona.companySize}
{persona.techSavvy} tech

Pain Points

{persona.painPoints.map((p, j) => ( {p} ))}

Goals

    {persona.goals.map((g, j) => (
  • • {g}
  • ))}

Search Behavior

{persona.searchBehavior.map((s, j) => (
"{s}"
))}
))}
{/* Keywords Tab */} {/* Differentiation Keywords First */} {differentiatorKeywords.length > 0 && ( Differentiation Keywords Keywords that highlight your competitive advantage
{differentiatorKeywords.map((kw, i) => ( {kw.term} ))}
)} {/* All Keywords */} All Keywords ({sortedKeywords.length}) Sorted by word count - single words first
{sortedKeywords.map((kw, i) => ( {kw.term} ))}
{/* Keywords by Type */}
{['product', 'problem', 'solution', 'feature', 'competitor'].map(type => { const typeKeywords = analysis.keywords.filter(k => k.type === type) if (typeKeywords.length === 0) return null return ( {type}
{typeKeywords.slice(0, 20).map((kw, i) => ( {kw.term} ))}
) })}
{/* Opportunities Tab */} {opportunities.length > 0 ? ( <> {opportunities.map((opp) => (

{opp.title}

{Math.round(opp.relevanceScore * 100)}%
{opp.source} {getIntentIcon(opp.intent)} {opp.intent} {opp.matchedPersona && ( {opp.matchedPersona} )}

{opp.snippet}

{opp.matchedKeywords.length > 0 && (
{opp.matchedKeywords.map((k, i) => ( {k} ))}
)}

Suggested Approach

{opp.suggestedApproach}

View Post
))} ) : (

No opportunities yet

Click "Find Opportunities" to search for potential customers

)}
) }