"use client"; import dynamic from "next/dynamic"; import { Activity, Sparkles } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader } from "@/components/ui/card"; import { TileHeader } from "@/components/insights/tile-header"; import { EmptyState } from "@/components/ui/empty-state"; import { Skeleton } from "@/components/ui/skeleton"; import { useTranslations } from "@/lib/i18n/context"; import { cn } from "@/lib/utils"; import type { CorrelationResult } from "@/lib/insights/correlations"; import type { CoachScopeSource } from "@/lib/ai/coach/types"; import { CONFIDENCE_BADGE_CLASS } from "./confidence-badge"; import { AskCoachAction } from "./ask-coach-action"; import { PatternDismissButton, PatternDismissedNotice, usePatternDismissalOverrides, } from "./pattern-dismiss-action"; /** * v1.4.20 phase B3 — single Correlation card. * * Each card surfaces ONE pre-defined hypothesis (BP × compliance, * mood × pulse, weight × weekday). Below-threshold results render an * EmptyState rather than a misleading near-zero scatter. * * Recharts is dynamic-imported behind the existing * `` wrapper so each card pays the bundle * cost only when it actually paints a chart. */ const ScatterCorrelationChart = dynamic( () => import("@/components/charts/chart-runtime").then((mod) => ({ default: mod.ScatterCorrelationChart, })), { ssr: false, loading: () => ( // v1.4.43 W11 — mirror the loaded chart's responsive aspect- // ratio classes (`scatter-correlation-chart.tsx:100`) so the // skeleton reserves the same space across breakpoints. The // legacy `h-[180px]` was ~60 px shorter than the painted chart // at `sm+` and caused a visible CLS shift on insights mount. ), }, ); interface CorrelationCardProps { /** Correlation runner output. `kind` drives title + subtitle copy. */ result: CorrelationResult; } const TONE_BAR_CLASSNAME: Record = { "bp-compliance": "bg-brand-pink", "mood-pulse": "bg-info", "weight-weekday": "bg-primary", }; const TITLE_KEY: Record = { "bp-compliance": "insights.correlationRow.card.bpComplianceTitle", "mood-pulse": "insights.correlationRow.card.moodPulseTitle", "weight-weekday": "insights.correlationRow.card.weightWeekdayTitle", }; const SUBTITLE_KEY: Record = { "bp-compliance": "insights.correlationRow.card.bpComplianceSubtitle", "mood-pulse": "insights.correlationRow.card.moodPulseSubtitle", "weight-weekday": "insights.correlationRow.card.weightWeekdaySubtitle", }; const CONFIDENCE_LABEL_KEY: Record<"low" | "moderate" | "high", string> = { high: "insights.correlationRow.confidenceHigh", moderate: "insights.correlationRow.confidenceModerate", low: "insights.correlationRow.confidenceLow", }; // v1.21.0 (C4 H2) — the two sources each correlation pair spans, so the // Coach hand-off narrows its snapshot to exactly the relationship the // card describes. const COACH_SOURCES_BY_KIND: Record< CorrelationResult["kind"], CoachScopeSource[] > = { "bp-compliance": ["bp", "compliance"], "mood-pulse": ["mood", "pulse"], "weight-weekday": ["weight"], }; // v1.22 (W6) — i18n keys (resolved at the call site), not hardcoded English. const COACH_QUESTION_BY_KIND: Record = { "bp-compliance": "insights.coach.seed.correlationBpCompliance", "mood-pulse": "insights.coach.seed.correlationMoodPulse", "weight-weekday": "insights.coach.seed.correlationWeightWeekday", }; export function CorrelationCard({ result }: CorrelationCardProps) { const { t } = useTranslations(); const dismissal = usePatternDismissalOverrides(); const patternId = result.status === "ok" ? result.patternId : undefined; const dismissed = dismissal.isDismissed( patternId, result.status === "ok" && result.dismissed === true, ); const title = t(TITLE_KEY[result.kind]); const subtitle = t(SUBTITLE_KEY[result.kind]); const coachSources = COACH_SOURCES_BY_KIND[result.kind]; const coachScope = coachSources.length > 0 ? { metric: coachSources[0], also: coachSources.slice(1) } : undefined; return ( ); } function tonesByKind(kind: CorrelationResult["kind"]): string { // Keep in step with TONE_BAR_CLASSNAME above — same token per kind. switch (kind) { case "bp-compliance": return "var(--brand-pink)"; case "mood-pulse": return "var(--info)"; case "weight-weekday": return "var(--primary)"; } }