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.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { motion } from "motion/react"
|
||||
import { useEffect, useRef, useState } from "react"
|
||||
import { useEffect, useMemo, useRef, useState } from "react"
|
||||
import type { DashboardDef_Output, Placement, WidgetDef } from "@/client"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
@@ -188,6 +188,15 @@ export function CanvasSurface({
|
||||
const scale = Math.min(box.width / width, box.height / height)
|
||||
const root = useCanvasRoot(dashboard, touch)
|
||||
const area = areaOf(dashboard, Boolean(rail))
|
||||
// The arrangement, held across the renders that are not about it. This
|
||||
// component reads the dashboard's own settings, so a bound one wakes it on
|
||||
// every message the flow sends — and calling a render prop again rebuilds
|
||||
// every tile. With the callback and the scale both unmoved React skips the
|
||||
// whole subtree instead.
|
||||
const content = useMemo(
|
||||
() => (scale > 0 ? children(scale) : null),
|
||||
[children, scale],
|
||||
)
|
||||
|
||||
return (
|
||||
<div ref={ref} className="relative size-full overflow-hidden">
|
||||
@@ -238,7 +247,7 @@ export function CanvasSurface({
|
||||
{dots ? (
|
||||
<div className="dot-canvas pointer-events-none absolute inset-0" />
|
||||
) : null}
|
||||
{children(scale)}
|
||||
{content}
|
||||
</div>
|
||||
</div>
|
||||
</LookProvider>
|
||||
|
||||
Reference in New Issue
Block a user