Stop a bound dashboard setting rebuilding every tile

A setting driven by a flow re-renders whatever reads it on every message.
`LookProvider` and `PaletteProvider` built a fresh context value each time,
and `CanvasSurface` called its render prop again, so a value on `theme`,
`palette`, `look`, `background`, `touch` or `locked` reconciled the whole
widget tree and re-ran every chart's `setData`.

The palette is now read as roles once per set of colours, the tokens and
both context values are memoised on it, and the canvas holds the tiles it
built while the callback and the scale stay put. `WidgetBody` is memoised
too: its two props are stable while the document is, so a grid rendered for
its own reasons no longer rebuilds every chart with it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VfgHWr7AQDiLsTQVu7hpKB
This commit is contained in:
2026-09-14 14:38:59 +02:00
co-authored by Claude Opus 5
parent 265cea38d2
commit aa3e2e8b81
6 changed files with 95 additions and 23 deletions
+21 -3
View File
@@ -1,9 +1,9 @@
import { createContext, useContext } from "react"
import { createContext, useContext, useMemo } from "react"
import type { DashboardDef_Output, SettingDef } from "@/client"
import { NO_PALETTE } from "@/components/Common/UplotChart"
import { useLiveValue } from "@/components/Flow/liveStore"
import { isLight, parsePalette, rolesOf } from "./ui/core/theme"
import { isLight, parsePalette, type Roles, rolesOf } from "./ui/core/theme"
/**
* The dashboard's own settings channel.
@@ -109,6 +109,24 @@ export const useDashboardPalette = (
dashboard: DashboardDef_Output | undefined,
): string[] => parsePalette(useSetting(dashboard, "palette"))
/**
* The palette as roles, keeping its identity while the colours keep theirs.
*
* `parsePalette` and `rolesOf` build a fresh array and a fresh object on every
* render, and a bound setting re-renders whatever reads it on every message the
* flow sends — so without this the two contexts below hand every widget a new
* value several times a second. The colours joined are the whole key: roles are
* derived from the hexes and from nothing else (`ui/core/theme.check.ts`).
*/
export function useDashboardRoles(
dashboard: DashboardDef_Output | undefined,
): Roles | null {
const hexes = useDashboardPalette(dashboard)
const key = hexes.join(",")
// biome-ignore lint/correctness/useExhaustiveDependencies: the colours are the dependency; `hexes` is a fresh array every render.
return useMemo(() => rolesOf(hexes), [key])
}
/** An image drawn under the widgets, or `""` for the look's own ground. */
export function useDashboardBackground(
dashboard: DashboardDef_Output | undefined,
@@ -173,7 +191,7 @@ export function PaletteProvider({
dashboard: DashboardDef_Output | undefined
children: React.ReactNode
}) {
const palette = rolesOf(useDashboardPalette(dashboard))?.series ?? NO_PALETTE
const palette = useDashboardRoles(dashboard)?.series ?? NO_PALETTE
return (
<PaletteContext.Provider value={palette}>
{children}