| 1 | "use client" |
| 2 | |
| 3 | import { createContext, use, useCallback, useMemo, useState } from "react" |
| 4 | import { |
| 5 | type AreaVariant, |
| 6 | type ChartConfig, |
| 7 | type ChartType, |
| 8 | type Margins, |
| 9 | useRevision, |
| 10 | } from "./chart-context" |
| 11 | import type { CommonChart |
| 12 | import type { BloomInput |
| 13 | import type { Seed |
| 14 | import { seedOfColor } |
| 15 | import { type PieSlice, |
| 16 | import type { Dimensions |
| 17 | |
| 18 | type Row |
| 19 | |
| 20 | const ROOT_OF: Record |
| 21 | pie: "<PieChart />", |
| 22 | radar: "<RadarChart />", |
| 23 | } |
| 24 | |
| 25 | export type PolarChartContextValue |
| 26 | chartType: ChartType |
| 27 | config: ChartConfig |
| 28 | configKeys: string |
| 29 | data: Row |
| 30 | dataLength: number |
| 31 | ready: boolean |
| 32 | plot: { |
| 33 | margins: Margins |
| 34 | center: { |
| 35 | outerRadius: number |
| 36 | innerRadius: number |
| 37 | animate: boolean |
| 38 | animationDuration: number |
| 39 | revision: number |
| 40 | bloom: BloomInput |
| 41 | bloomOnHover: boolean |
| 42 | |
| 43 | seedOf: (key |
| 44 | variantOf: (key |
| 45 | registerVariant: (key |
| 46 | unregisterVariant: (key |
| 47 | |
| 48 | selectedDataKey: string |
| 49 | selectDataKey: (key |
| 50 | /** Legend-hover spotlight - dims every series but this one while set. */ |
| 51 | focusDataKey: string |
| 52 | setFocusDataKey: (key |
| 53 | hoverIndex: number |
| 54 | setHoverIndex: (i |
| 55 | setCursor: (px |
| 56 | isMouseInChart: boolean |
| 57 | setMouseInChart: (over |
| 58 | |
| 59 | pie: PieSlice |
| 60 | radar: { |
| 61 | |
| 62 | common: CommonChart |
| 63 | } |
| 64 | |
| 65 | const PolarChartContext = createContext |
| 66 | |
| 67 | export function usePolarChart() |
| 68 | const ctx = |
| 69 | if (!ctx) |
| 70 | throw new Error( |
| 71 | } |
| 72 | return ctx |
| 73 | } |
| 74 | |
| 75 | /** Boundary guard for polar parts (`<Pie>`, `<Radar>`). */ |
| 76 | export function usePolarPart(part |
| 77 | const ctx = |
| 78 | if (!ctx) |
| 79 | throw new Error( |
| 80 | } |
| 81 | if (ctx.chartType |
| 82 | throw new Error( |
| 83 | `<${part} /> is not valid inside ${ |
| 84 | ) |
| 85 | } |
| 86 | return ctx |
| 87 | } |
| 88 | |
| 89 | export { PolarChartContext } |
| 90 | |
| 91 | export function usePolarController({ |
| 92 | chartType, |
| 93 | data, |
| 94 | config, |
| 95 | dataKey, |
| 96 | nameKey, |
| 97 | innerRadiusRatio, |
| 98 | dimensions, |
| 99 | margins, |
| 100 | animate = true, |
| 101 | animationDuration = 900, |
| 102 | replayToken = 0, |
| 103 | bloom = "off" |
| 104 | bloomOnHover = false, |
| 105 | defaultSelectedDataKey = null, |
| 106 | onSelectionChange, |
| 107 | }: { |
| 108 | chartType: "pie |
| 109 | data: Row |
| 110 | config: ChartConfig |
| 111 | dataKey: string |
| 112 | nameKey: string |
| 113 | innerRadiusRatio: number |
| 114 | dimensions: Dimensions |
| 115 | margins: Margins |
| 116 | animate?: boolean |
| 117 | animationDuration?: number |
| 118 | replayToken?: number |
| 119 | bloom?: BloomInput |
| 120 | bloomOnHover?: boolean |
| 121 | defaultSelectedDataKey?: string |
| 122 | onSelectionChange?: ( |
| 123 | }): PolarChartContextValue { |
| 124 | // This object becomes the PolarChartContext value, so its identity - and the |
| 125 | // identity of every function/object it carries - must stay stable across |
| 126 | // renders that don't change the inputs; otherwise every consumer (legend, |
| 127 | // tooltip, slices, axes) re-renders on every parent render. The expensive |
| 128 | // derivations, exposed callbacks, and returned value are memoized below; |
| 129 | // cheap scalars (radii, ready) are left bare as plain recomputed reads. |
| 130 | |
| 131 | // Memoized: drives `pie`/`radar`/`common` - a fresh array would bust them. |
| 132 | const configKeys = |
| 133 | const revision = |
| 134 | |
| 135 | const [selectedDataKey, |
| 136 | defaultSelectedDataKey |
| 137 | ) |
| 138 | const [focusDataKey, |
| 139 | const [hoverIndex, |
| 140 | const [cursorX, |
| 141 | const [cursorY, |
| 142 | const [isMouseInChart, |
| 143 | // Stable (only wraps two useState setters) so the value keeps its identity. |
| 144 | const setCursor = |
| 145 | setCursorX(px) |
| 146 | setCursorY(py) |
| 147 | }, []) |
| 148 | const [variants, |
| 149 | |
| 150 | // useCallback for the same reason as registerSeries in chart-context.tsx: |
| 151 | // pie.tsx/radar.tsx list these as effect deps, so without stable identities |
| 152 | // the unregister/register effect re-fires and its setState pair loops. |
| 153 | const registerVariant = |
| 154 | setVariants((prev) |
| 155 | prev[key] === |
| 156 | ) |
| 157 | }, []) |
| 158 | const unregisterVariant = |
| 159 | setVariants((prev) |
| 160 | if (!(key |
| 161 | const next = |
| 162 | delete next[key] |
| 163 | return next |
| 164 | }) |
| 165 | }, []) |
| 166 | |
| 167 | // Stable so the value keeps its identity; re-created only on config change. |
| 168 | const selectDataKey = |
| 169 | (key: string |
| 170 | setSelectedDataKey(key) |
| 171 | onSelectionChange?.(key) |
| 172 | }, |
| 173 | [onSelectionChange] |
| 174 | ) |
| 175 | |
| 176 | // The root spreads margins fresh every render; pin a stable object off the |
| 177 | // four numbers so it doesn't, on its own, invalidate the value. |
| 178 | const { top: |
| 179 | const stableMargins = |
| 180 | () => ({ |
| 181 | [mTop, mRight, |
| 182 | ) |
| 183 | |
| 184 | const plotWidth = |
| 185 | const plotHeight = |
| 186 | const ready = |
| 187 | const pad = |
| 188 | const outerRadius = |
| 189 | const innerRadius = |
| 190 | const centerX = |
| 191 | const centerY = |
| 192 | |
| 193 | // Stable so `common` and the value stay stable; re-created only on config. |
| 194 | const seedOf = |
| 195 | (key: string |
| 196 | [config] |
| 197 | ) |
| 198 | // "*" is the pie-wide variant set by <Pie>; radar registers per series key. |
| 199 | const variantOf = |
| 200 | (key: string |
| 201 | [variants] |
| 202 | ) |
| 203 | |
| 204 | // Memoized: slice geometry - recomputing it on every hover/cursor tick would |
| 205 | // rebuild the pie layout needlessly. |
| 206 | const pie = |
| 207 | () => (chartType |
| 208 | [chartType, data, |
| 209 | ) |
| 210 | |
| 211 | // Memoized: walks every row × series for the axis max, then builds the axes. |
| 212 | const radar = |
| 213 | if (chartType !== |
| 214 | let max = |
| 215 | for (const row |
| 216 | for (const key |
| 217 | const v = |
| 218 | if (v > |
| 219 | } |
| 220 | } |
| 221 | return { axes: |
| 222 | }, [chartType, |
| 223 | |
| 224 | // Memoized: this is the value handed to CommonChartContext (Legend/Tooltip), |
| 225 | // so it needs its own stable identity independent of the parent value. |
| 226 | const common: |
| 227 | const tooltipLeft = |
| 228 | const tooltipTop = |
| 229 | const emphasis = |
| 230 | if (chartType === |
| 231 | const names = |
| 232 | return { |
| 233 | names, |
| 234 | tooltipTop, |
| 235 | labelOf: (n) |
| 236 | seedOf, |
| 237 | selectedDataKey, |
| 238 | selectDataKey, |
| 239 | focusDataKey, |
| 240 | setFocusDataKey, |
| 241 | hoverIndex, |
| 242 | ready, |
| 243 | tooltipLeft, |
| 244 | heading: (i) |
| 245 | itemsAt: (i) |
| 246 | const s = |
| 247 | if (!s) |
| 248 | return [ |
| 249 | { |
| 250 | name: s.name |
| 251 | label: config[s |
| 252 | value: s.value |
| 253 | seed: seedOf(s |
| 254 | dimmed: emphasis !== |
| 255 | }, |
| 256 | ] |
| 257 | }, |
| 258 | } |
| 259 | } |
| 260 | // radar |
| 261 | return { |
| 262 | names: configKeys, |
| 263 | tooltipTop, |
| 264 | labelOf: (n) |
| 265 | seedOf, |
| 266 | selectedDataKey, |
| 267 | selectDataKey, |
| 268 | focusDataKey, |
| 269 | setFocusDataKey, |
| 270 | hoverIndex, |
| 271 | ready, |
| 272 | tooltipLeft, |
| 273 | heading: (i) |
| 274 | itemsAt: (i) |
| 275 | configKeys.map((name |
| 276 | const raw = |
| 277 | return { |
| 278 | name, |
| 279 | label: config[name |
| 280 | value: typeof raw |
| 281 | seed: seedOf(name |
| 282 | dimmed: emphasis !== |
| 283 | } |
| 284 | }), |
| 285 | } |
| 286 | }, [ |
| 287 | chartType, |
| 288 | config, |
| 289 | configKeys, |
| 290 | data, |
| 291 | pie, |
| 292 | radar, |
| 293 | seedOf, |
| 294 | selectedDataKey, |
| 295 | selectDataKey, |
| 296 | focusDataKey, |
| 297 | setFocusDataKey, |
| 298 | hoverIndex, |
| 299 | ready, |
| 300 | plotWidth, |
| 301 | mLeft, |
| 302 | mTop, |
| 303 | cursorX, |
| 304 | cursorY, |
| 305 | ]) |
| 306 | |
| 307 | // Memoized: this is the PolarChartContext value. A fresh object here would |
| 308 | // re-render every consumer on every parent render - the reason the pieces |
| 309 | // above are stabilized. Rebuilds only when a listed input changes. The |
| 310 | // useState setters are listed but never change identity. |
| 311 | return useMemo<PolarChartContextValue |
| 312 | () => ({ |
| 313 | chartType, |
| 314 | config, |
| 315 | configKeys, |
| 316 | data, |
| 317 | dataLength: data.length, |
| 318 | ready, |
| 319 | plot: { width: plotWidth |
| 320 | margins: stableMargins, |
| 321 | center: { x: centerX |
| 322 | outerRadius, |
| 323 | innerRadius, |
| 324 | animate, |
| 325 | animationDuration, |
| 326 | revision, |
| 327 | bloom, |
| 328 | bloomOnHover, |
| 329 | seedOf, |
| 330 | variantOf, |
| 331 | registerVariant, |
| 332 | unregisterVariant, |
| 333 | selectedDataKey, |
| 334 | selectDataKey, |
| 335 | focusDataKey, |
| 336 | setFocusDataKey, |
| 337 | hoverIndex, |
| 338 | setHoverIndex, |
| 339 | setCursor, |
| 340 | isMouseInChart, |
| 341 | setMouseInChart, |
| 342 | pie, |
| 343 | radar, |
| 344 | common, |
| 345 | }), |
| 346 | [ |
| 347 | chartType, |
| 348 | config, |
| 349 | configKeys, |
| 350 | data, |
| 351 | ready, |
| 352 | plotWidth, |
| 353 | plotHeight, |
| 354 | stableMargins, |
| 355 | centerX, |
| 356 | centerY, |
| 357 | outerRadius, |
| 358 | innerRadius, |
| 359 | animate, |
| 360 | animationDuration, |
| 361 | revision, |
| 362 | bloom, |
| 363 | bloomOnHover, |
| 364 | seedOf, |
| 365 | variantOf, |
| 366 | registerVariant, |
| 367 | unregisterVariant, |
| 368 | selectedDataKey, |
| 369 | selectDataKey, |
| 370 | focusDataKey, |
| 371 | setFocusDataKey, |
| 372 | hoverIndex, |
| 373 | setHoverIndex, |
| 374 | setCursor, |
| 375 | isMouseInChart, |
| 376 | setMouseInChart, |
| 377 | pie, |
| 378 | radar, |
| 379 | common, |
| 380 | ] |
| 381 | ) |
| 382 | } |