// sheet/ChartDialog.tsx — the Insert tab's CHART dialog (wave 54, W54-T11, ruling R5). // // ⭐⭐ R5, VERBATIM: "A chart reads BOTH. A cell range, exactly like Excel, is the primary path; a // database source is a second tab in the same dialog. Ship both." So this is ONE dialog with two // source tabs, and the cell range tab is the one that opens. // // ⛔⛔ HOW THE DATABASE TAB SURVIVES `SheetChart.tsx`'s RULING R9, WHICH SAYS THE OPPOSITE THING // AND IS STILL TRUE. That file's header reads: "A CHART IS A CAPABILITY, NOT A VIEW TYPE. It // occupies a rectangle of cells and READS those cells; it has no binding, no database and no query // of its own. If this file ever calls the sheet preview door it has become a second way to fetch a // number, and the sheet would then have two answers for one cell." // // The two reconcile in exactly one way, and it is the design below: THE DATABASE TAB DOES NOT GIVE // THE CHART A QUERY. It writes BOUND CELLS into the sheet and then anchors an ordinary cell range // chart over them. The chart still reads cells. The numbers still come from the one existing // binding path, evaluated server side for a bound cell exactly as a hand bound cell is. Nothing // here fetches a value, and `SheetChartSpec` gains no field: it is the same three keys it has // carried since W49-T08. // // ⭐ WHAT THE DATABASE TAB WRITES, in ONE call through the door, so it is ONE history step and ONE // save: a header row of period labels as typed literals, and beneath it one bound cell per period, // every one carrying the same database, aggregate and scope and its OWN period. Then the chart, // then a refresh, because a stale cell holds no value until the workbook refreshes. // // ⛔ THE BINDING VOCABULARY IS COPIED, NOT INVENTED. `BindingEditor.tsx` is the declaration of it // and the server fixture in `aios-web/api/verify_sheet.py` is the proof; this file reuses the same // names (`database`, `aggregate.fn`, `aggregate.field`, `scope.filter`, `period.unit`, // `period.start`, `period.locked`) and imports nothing from that panel, because W54-T11 may not // reach into lane E's files. A second spelling of the vocabulary would be a second dialect. // // ⛔ ESCAPE COMES FROM THE KIT AND FROM NOWHERE ELSE. `useOverlayLayer` registers this surface on // the shared stack and calls `ui/useEscapeClose.ts` itself, which is why there is no local keydown // listener anywhere in this file. DESIGN.md section 4: a local keydown listener is the defect, not // the shortcut. // // ⚠ MOUNTED CONDITIONALLY BY ITS CALLER, never rendered behind an `open` prop. `useOverlayLayer` // has no active flag: it claims Escape the moment it runs, and the stack hands the key to the LAST // entry pushed. Mounting late is what puts this dialog above anything already open, and popping it // on close hands Escape straight back. // // ⚠ DESIGN CONTRACT (DESIGN.md 2 through 5): centred modal on the base kit's `.lp-dialog` surface, // tokens only, sentence case, inline SVG in the house vocabulary and never an emoji, a skeleton // rather than a spinner while the database list is in flight, press feedback that does not change // layout, reduced motion honoured, and no em dash or en dash in any string that reaches a screen. import { createContext, Fragment, useContext, useEffect, useId, useMemo, useRef, useState } from "react"; import type { ReactNode } from "react"; import { useCellRefHighlight } from "./refHighlight"; import { AnchoredOverlay, BodyPortal, useOverlayLayer } from "../customer-grid/OverlaySurface"; // The document's own addresser, in SHEET coordinates. A second spelling of A1 in this file would // be a second answer to a question the document already answers. import { addressOf } from "./sheetState"; import { parseRange } from "./rules"; import type { Cell, SheetChartRange, SheetChartSpec, SheetTab, SheetChartPresentation, SheetChartColor, ChartAxisFormat } from "./types"; import { SHEET_CHART_STACKING_KINDS, withChartStacking } from "./types"; import { CATEGORICAL, chartPresentationDefaults as defaultChartPresentation, colorStyle, chartDataForRange, chartLegendEntries, DrawnChart } from "./SheetChart"; import type { ChartSeriesHue, SheetChartData } from "./SheetChart"; import "../styles/wave58/sheet.css"; import "../styles/wave59/chart-dialog.css"; export function ChartSeriesColorPicker({ slot, label, presentation, value, onChange }: { slot: number; label: string; presentation: SheetChartPresentation; value?: ChartSeriesHue; onChange: (color: ChartSeriesHue | undefined) => void; }) { const opener = useRef(null); const listId = useId(); const [open, setOpen] = useState(false); const [active, setActive] = useState(0); // A saved neutral override stays editable; new charts offer Theme and the eight hues. const choices: (ChartSeriesHue | undefined)[] = [undefined, ...CATEGORICAL, ...(value === "muted" ? [value] : [])]; const name = (color: ChartSeriesHue | undefined) => color ? color[0].toUpperCase()+color.slice(1) : "Theme"; const dot = (color: ChartSeriesHue | undefined) =>