'use client'; import Link from 'next/link'; import { useCallback, useEffect, useMemo, useState } from 'react'; import { format } from 'date-fns'; import { ArrowLeft, BrainCircuit, RefreshCcw } from 'lucide-react'; import { useParams } from 'next/navigation'; import { AppShell } from '@/components/shell/app-shell'; import { useAuthGuard } from '@/hooks/use-auth-guard'; import { getCompanyAiReport } from '@/lib/api'; import type { CompanyAiReportDetail } from '@/lib/types'; import { Button } from '@/components/ui/button'; import { Panel } from '@/components/ui/panel'; function formatFilingDate(value: string) { const date = new Date(value); if (Number.isNaN(date.getTime())) { return 'Unknown'; } return format(date, 'MMM dd, yyyy'); } export default function AnalysisReportPage() { const { isPending, isAuthenticated } = useAuthGuard(); const params = useParams<{ ticker: string; accessionNumber: string }>(); const tickerFromRoute = useMemo(() => { const value = typeof params.ticker === 'string' ? params.ticker : ''; return value.toUpperCase(); }, [params.ticker]); const accessionNumber = useMemo(() => { const value = typeof params.accessionNumber === 'string' ? params.accessionNumber : ''; return decodeURIComponent(value); }, [params.accessionNumber]); const [report, setReport] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const loadReport = useCallback(async () => { if (!accessionNumber) { setError('Invalid accession number.'); setReport(null); setLoading(false); return; } setLoading(true); setError(null); try { const response = await getCompanyAiReport(accessionNumber); setReport(response.report); } catch (err) { setReport(null); setError(err instanceof Error ? err.message : 'Unable to load AI summary'); } finally { setLoading(false); } }, [accessionNumber]); useEffect(() => { if (!isPending && isAuthenticated) { void loadReport(); } }, [isPending, isAuthenticated, loadReport]); if (isPending || !isAuthenticated) { return
Loading summary detail...
; } const resolvedTicker = report?.ticker ?? tickerFromRoute; return ( void loadReport()} disabled={loading}> Refresh )} >
Back to analysis Back to filings
{loading ? (

Loading AI summary...

) : null} {!loading && error ? (

{error}

) : null} {!loading && !error && report ? ( <>

Accession

{report.accessionNumber}

Provider

{report.provider}

Model

{report.model}

Full text view

{report.summary}

) : null}
); }