feat(insights): warm assessments from the overview page

Wire the on-demand full assessment warm into the Insights overview. A new
useInsightsWarm hook POSTs /api/insights/pregenerate; the cards then fill
through their existing stale-while-revalidate reads with nothing to
invalidate locally.

Two entry points share the one mutation. A background warm fires once per
browser session (sessionStorage-gated) as soon as the overview confirms
the user has data, so a returning user lands on warm caches without
acting; it is silent and tolerates the 429 the server returns while a
warm is already in flight. A manual button ('Prepare assessments') runs
the same warm and shows a toast.

Add the insightsPregenerate mutationKey to the factory and the label +
toast strings across all six locales.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
(cherry picked from commit 1273a6253dc0d00c7c7ce774c5fbe352664de2e9)
This commit is contained in:
Marc-André Bombeck committed 2026-06-02 14:29:49 +02:00
1 parent bf5052bb01
commit 2472579059
9 files changed
+148 -1

No files matched your search

+26 -1
View File
@@ -1,9 +1,10 @@
"use client";
import { useEffect } from "react";
import { useQuery } from "@tanstack/react-query";
import dynamic from "next/dynamic";
import Link from "next/link";
import { TrendingUp } from "lucide-react";
import { TrendingUp, Sparkles } from "lucide-react";
import { useAuth } from "@/hooks/use-auth";
import { queryKeys } from "@/lib/query-keys";
@@ -14,6 +15,7 @@ import { Button } from "@/components/ui/button";
import { EmptyState } from "@/components/ui/empty-state";
import { HeroStrip } from "@/components/insights/hero-strip";
import { useInsightsAdvisorQuery } from "@/components/insights/use-insights-advisor";
import { useInsightsWarm } from "@/components/insights/use-insights-warm";
import { useCoachLaunch } from "@/lib/insights/coach-launch-context";
import { useAnalyticsQuery } from "@/lib/queries/use-analytics-query";
// v1.4.41 W-ORG — shared shape lives in `src/types/analytics.ts` as
@@ -150,6 +152,17 @@ export default function InsightsPage() {
const analyticsQuery = useAnalyticsQuery();
const analytics = analyticsQuery.data as AnalyticsData | undefined;
// v1.8.7.1 — on-demand full assessment warm. The auto-trigger fires once
// per session as soon as the overview confirms the user has data, so the
// status cards (and the iOS client, which reads the same routes) land on
// warm caches without the user waiting. The manual button below re-warms
// everything on demand.
const { warm, isWarming, autoWarmOnce } = useInsightsWarm();
const hasData = !!data && data.totalMeasurements > 0;
useEffect(() => {
autoWarmOnce(isAuthenticated && hasData);
}, [autoWarmOnce, isAuthenticated, hasData]);
// Empty-state shortcut — only paint once the comprehensive query has
// resolved AND reported zero measurements. While it's in-flight we
// fall through to the streamed shell so the user gets the hero +
@@ -210,6 +223,18 @@ export default function InsightsPage() {
healthScore={analytics?.healthScore ?? undefined}
/>
<div className="flex justify-end">
<Button
size="sm"
variant="outline"
onClick={warm}
disabled={isWarming}
>
<Sparkles className="size-4" />
{t("insights.warmAssessments")}
</Button>
</div>
{flags.briefing && (
<DailyBriefing
briefing={briefingPayload}