@@ -3,6 +3,7 @@ import { Link } from 'react-router';
33import { useDashboardStats , useDailyStats } from '@/hooks/useAnalytics' ;
44import { useSessions } from '@/hooks/useSessions' ;
55import { useInsights } from '@/hooks/useInsights' ;
6+ import { useAnalyzedSessionIds } from '@/hooks/useAnalyzedSessionIds' ;
67import { useProjects } from '@/hooks/useProjects' ;
78import { StatsHero } from '@/components/dashboard/StatsHero' ;
89import { DashboardActivityChart } from '@/components/dashboard/DashboardActivityChart' ;
@@ -34,19 +35,24 @@ export default function DashboardPage() {
3435 const { data : dailyStats = [ ] , isLoading : dailyLoading , isError : dailyError , refetch : refetchDaily } = useDailyStats ( range , effectiveHomeId ) ;
3536 const { data : sessions = [ ] , isLoading : sessionsLoading , isError : sessionsError , refetch : refetchSessions } = useSessions ( { limit : 500 , ...( homeId !== 'all' && { homeId } ) } ) ;
3637 const { data : insights = [ ] , isLoading : insightsLoading } = useInsights ( ) ;
38+ const { data : analyzedSessionIds , isLoading : analyzedIdsLoading } = useAnalyzedSessionIds ( ) ;
3739 const { data : projects = [ ] } = useProjects ( ) ;
3840
39- const loading = statsLoading || sessionsLoading || insightsLoading || dailyLoading ;
41+ const loading = statsLoading || sessionsLoading || insightsLoading || dailyLoading || analyzedIdsLoading ;
4042 const hasError = statsError || sessionsError || dailyError ;
4143
4244 const todayLabel = new Date ( ) . toLocaleDateString ( undefined , {
4345 month : 'long' ,
4446 day : 'numeric' ,
4547 } ) ;
4648
47- // Sessions not yet analyzed
48- const analyzedSessionIds = new Set ( insights . map ( ( i ) => i . session_id ) ) ;
49- const unanalyzedSessions = sessions . filter ( ( s ) => ! analyzedSessionIds . has ( s . id ) ) ;
49+ // Sessions not yet analyzed. Sourced from analysis_usage (via useAnalyzedSessionIds),
50+ // not the insights list — insights has no safe row cap to rely on at scale (a single
51+ // session's analysis produces 5-10+ insight rows), so a capped insights query would
52+ // silently misclassify already-analyzed sessions as unanalyzed on large histories.
53+ const unanalyzedSessions = analyzedSessionIds
54+ ? sessions . filter ( ( s ) => ! analyzedSessionIds . has ( s . id ) )
55+ : [ ] ;
5056
5157 // Compute stats for hero — all from dashStats (range-filtered)
5258 const totalTokens = dashStats
0 commit comments