"use client"
/**
* charts-core — ChartCard, ChartFigure, ChartDataTable shells + their minimal deps.
*
* This module deliberately carries ZERO gallery/demo code so that hub dashboards
* (library, data-view, placements, etc.) that only need the chart frame can import
* from here without pulling in the full gallery bundle.
*
* The gallery wiring lives in `charts-overview.tsx` which re-exports everything
* from this module for back-compat.
*/
import * as React from "react"
import {
Card, CardContent, CardDescription, CardHeader, CardTitle,
} from "@/components/ui/card"
import {
Tabs,
TabsList,
TabsTrigger,
TabsTriggerIcon,
TabsTriggerLabel,
TabsContent,
} from "@/components/ui/tabs"
import {
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
} from "@/components/ui/select"
import { AskLeoButton } from "@/components/ask-leo-button"
import { isEditableTarget } from "@/lib/editable-target"
import { cn } from "@/lib/utils"
import { metricTrendTone, type MetricTrendPolarity } from "@/components/key-metrics"
import {
type ChartLeoInsight,
type ChartLeoInsightAnchor,
type ChartLeoInsightKind,
} from "@/components/leo-insight-indicator"
import {
ChartLeoInsightOverlay,
ChartLeoPlotInsightOverlay,
ChartLeoPixelPlotInsightOverlay,
} from "@/components/chart-leo-spotting"
/* ── Re-export Leo types + components so callers can import from one place ── */
export type { ChartLeoInsight, ChartLeoInsightAnchor, ChartLeoInsightKind }
export { ChartLeoPlotInsightOverlay, ChartLeoInsightOverlay, ChartLeoPixelPlotInsightOverlay }
/* ════════════════════════════════════════════════════════════════════════════
Types
════════════════════════════════════════════════════════════════════════════ */
export type ChartCardVariant = "normal" | "tabs" | "selector" | "metrics-tabs" | "kpi-chart"
export type MiniMetric = {
label: string
value: string
trend?: "up" | "down" | "neutral"
/** Same semantics as `MetricItem.trendPolarity` on `KeyMetrics`. */
trendPolarity?: MetricTrendPolarity
}
/* ════════════════════════════════════════════════════════════════════════════
ChartDataTable — Screen-reader data fallback for charts
════════════════════════════════════════════════════════════════════════════ */
/** Screen-reader data fallback for charts — shared with list-page dashboards. */
export function ChartDataTable({
caption,
headers,
rows,
}: {
caption: string
headers: string[]
rows: (string | number)[][]
}) {
return (
{caption}
{headers.map((h) =>
{h}
)}
{rows.map((row, i) => (
{row.map((cell, j) =>
{cell}
)}
))}
)
}
/* ════════════════════════════════════════════════════════════════════════════
ChartFigure — Keyboard-focusable chart region
════════════════════════════════════════════════════════════════════════════ */
/**
* Keyboard-focusable chart region (arrow keys, Escape) + live announcement when a point is selected.
* Shared by the `/dashboard` gallery and **Data** view dashboards (Placements / Team / Compliance): same
* interaction model; visual differences come from `ChartCard` chrome and per-chart renderers (bar vs pie),
* not from a separate chart implementation.
*/
export function ChartFigure({
label,
summary,
dataLength,
leoInsight,
children,
}: {
label: string
summary: string
dataLength: number
/** Optional Ask-Leo insight context for chart bodies (same as `ChartCard`). */
leoInsight?: ChartLeoInsight | null
children: (activeIndex: number | null) => React.ReactNode
}) {
const [activeIndex, setActiveIndex] = React.useState(null)
const ref = React.useRef(null)
const prevActiveIndexRef = React.useRef(null)
React.useEffect(() => {
const prev = prevActiveIndexRef.current
prevActiveIndexRef.current = activeIndex
if (prev === null || activeIndex !== null) return
const wrapper = ref.current?.querySelector(".recharts-wrapper")
if (!wrapper) return
wrapper.dispatchEvent(
new MouseEvent("mouseleave", { bubbles: true, cancelable: true }),
)
}, [activeIndex])
const navigateKeys = React.useCallback(
(e: React.KeyboardEvent) => {
if (!dataLength) return
if (isEditableTarget(e.target)) return
switch (e.key) {
case "ArrowRight":
case "ArrowDown":
e.preventDefault()
e.stopPropagation()
setActiveIndex((i) => (i === null ? 0 : Math.min(i + 1, dataLength - 1)))
break
case "ArrowLeft":
case "ArrowUp":
e.preventDefault()
e.stopPropagation()
setActiveIndex((i) => (i === null ? dataLength - 1 : Math.max(i - 1, 0)))
break
case "Escape":
e.preventDefault()
e.stopPropagation()
setActiveIndex(null)
ref.current?.blur()
break
default:
break
}
},
[dataLength],
)
/** Clicks on Recharts SVG do not focus this node — focus so Arrow keys work without extra Tab stops. */
function handlePointerDownCapture(e: React.PointerEvent) {
if (!dataLength) return
const root = ref.current
if (!root?.contains(e.target as Node)) return
const el = e.target as HTMLElement | null
if (el?.closest?.("button, a, [role='tab'], [role='option'], input, select, textarea, [contenteditable='true']"))
return
queueMicrotask(() => root.focus())
}
return (
{/* A chart card is often the narrowest thing on a dashboard, so the row
starts shedding inactive labels to the overflow menu very early.
Using TabsTriggerIcon + TabsTriggerLabel instead of plain text gives
the row the glyph it needs to represent a tab without a visible label. */}
ChartTrend
{/* Caller-supplied labels of unknown length in a card that is
often the narrowest thing on a dashboard. */}
{tabOptions.map((tab) => (
{tab.label}
))}