Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

1.6 KB
Raw Blame
import { useEffect, useRef } from 'react';
import { rollPalette } from '../lib/canvasRoll.js';

// Resolves the piano-roll canvas palette (theme-following accent + roll bg)
// once, then re-resolves on theme switch and triggers a redraw. Canvas
// `fillStyle` can't read CSS custom properties, so the imperative roll draws
// read the resolved strings from the returned ref inside draw() instead of
// hardcoding hex. Mirrors `useChartColors`'s `data-port-theme` MutationObserver
// (set by `useTheme` on <html>), but hands back a ref — not React state — so a
// theme switch repaints the canvas without forcing a component re-render.
//
// `resolve` is the token reader — `rollPalette` for the piano rolls; any other
// imperative canvas (the Core Assembly avatar) passes its own so the theme
// observer lifecycle lives in one place.
export default function useCanvasRollPalette(drawRef, resolve = rollPalette) {
  // Lazy one-time init — `useRef(rollPalette())` would re-run the getComputedStyle
  // read on every render (hover repaints re-render this component), defeating the
  // point of caching the palette.
  const paletteRef = useRef(null);
  if (paletteRef.current === null) paletteRef.current = resolve();
  useEffect(() => {
    const apply = () => { paletteRef.current = resolve(); drawRef.current?.(); };
    apply();
    if (typeof MutationObserver !== 'function') return undefined;
    const observer = new MutationObserver(apply);
    observer.observe(document.documentElement, { attributes: true, attributeFilter: ['data-port-theme'] });
    return () => observer.disconnect();
  }, [drawRef, resolve]);
  return paletteRef;
}