"use client"; import { useQuery } from "@tanstack/react-query"; import { cockpitApi, type CockpitSummary } from "@/lib/api/cockpit"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Skeleton } from "@/components/ui/skeleton"; import { OfflineState } from "@/components/ui/offline-state"; // --------------------------------------------------------------------------- // Loading skeleton — three grouped skeleton blocks // --------------------------------------------------------------------------- function ScorecardSkeleton() { return ( {/* Group 1: Delivery + Spend */}
{/* Group 2: Spend */}
{/* Group 3: Speed + Objectives */}
); } // --------------------------------------------------------------------------- // Section header helper // --------------------------------------------------------------------------- function SectionLabel({ children }: { children: React.ReactNode }) { return (

{children}

); } // --------------------------------------------------------------------------- // Delivery section // --------------------------------------------------------------------------- interface DeliveryMetricProps { label: string; value: number; } function DeliveryMetric({ label, value }: DeliveryMetricProps) { return (
{value}
{label}
); } interface DeliverySectionProps { delivery: CockpitSummary["delivery"]; } function DeliverySection({ delivery }: DeliverySectionProps) { return (
Delivery
); } // --------------------------------------------------------------------------- // Spend section // --------------------------------------------------------------------------- interface SpendSectionProps { spend: CockpitSummary["spend"]; } function SpendSection({ spend }: SpendSectionProps) { const { monthly_budget_cap_usd, spend_30d_usd, projected_monthly_usd, over_budget } = spend; // Red/destructive only when cap is a non-null number AND over_budget is true const isOverBudget = monthly_budget_cap_usd !== null && over_budget; return (
Spend
30-day spend ${spend_30d_usd.toFixed(2)}
{projected_monthly_usd !== null && (
Projected monthly ${projected_monthly_usd.toFixed(2)}
)}
Monthly cap {monthly_budget_cap_usd === null ? ( No budget cap set ) : ( ${monthly_budget_cap_usd.toFixed(2)} {isOverBudget && ( (over budget) )} )}
); } // --------------------------------------------------------------------------- // Speed section // --------------------------------------------------------------------------- interface SpeedSectionProps { medianLeadTimeHours: number | null | undefined; } function SpeedSection({ medianLeadTimeHours }: SpeedSectionProps) { // Show 'No data yet' when null or undefined. Never render '0h'. const hasData = medianLeadTimeHours != null; return (
Speed
Median lead time {hasData ? ( {medianLeadTimeHours.toFixed(1)}h median — target: < 24h ) : ( No data yet )}
); } // --------------------------------------------------------------------------- // Stub objectives section // --------------------------------------------------------------------------- function StubObjectivesSection() { const stubs = [ { id: "obj-1", label: "Revenue growth" }, { id: "obj-2", label: "Customer retention" }, ]; return (
Objectives
{stubs.map((stub) => (
{stub.label} Not tracked yet
))}
); } // --------------------------------------------------------------------------- // Scorecard body — rendered when data is available // --------------------------------------------------------------------------- interface ScorecardBodyProps { data: CockpitSummary; } function ScorecardBody({ data }: ScorecardBodyProps) { return ( Company Scorecard Live performance against the charter ); } // --------------------------------------------------------------------------- // Public export // --------------------------------------------------------------------------- export function CompanyScorecardCard() { const { data, isLoading, isError, refetch } = useQuery({ queryKey: ["cockpit-summary"], queryFn: cockpitApi.summary, }); if (isLoading) return ; if (isError || !data) { return ( void refetch()} /> ); } return ; }