File size: 3,208 Bytes
658edb3
 
 
 
 
 
 
 
bbfcaa6
 
 
 
 
 
658edb3
 
 
bbfcaa6
658edb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbfcaa6
 
 
 
 
 
 
 
 
 
 
 
 
 
658edb3
 
 
bbfcaa6
 
658edb3
bbfcaa6
 
 
 
 
 
 
658edb3
 
bbfcaa6
658edb3
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Hotfix 2026-09-24, owner: "when a user ANYWHERE in the app must reference a cell, the cell must
// be highlighted automatically ... This is the immutable rule everywhere a cell must be referenced."
//
// ONE store, so the rule is a property of the door rather than of each panel. Any surface that
// asks a person for a cell reference calls `useCellRefHighlight(texts)` while it is mounted; the
// grid reads `useHighlightedCellRefs()` and paints those cells through the SAME outline path a
// formula's references use (`SheetGrid.tsx::formulaRefCells`), so there is no second border
// mechanism to keep in step. Unmounting a panel takes its outlines off with it.
//
// ⛔ OUTLINE ON OPEN, SCROLL ONLY ON A CHANGE. A panel opened over a saved binding names every cell
// it reads, including a far-away helper (Daily Sales F10000). Scrolling to the last one registered
// would move the sheet the moment a cell is opened, so the grid scrolls only to a reference that
// APPEARED after the surface mounted: the one the person just typed.
import { useEffect, useRef, useSyncExternalStore } from "react";

const owners = new Map<number, readonly string[]>();
const listeners = new Set<() => void>();
const focusListeners = new Set<(text: string) => void>();
let snapshot: readonly string[] = [];
let nextOwner = 1;

const publish = () => {
  const seen = new Set<string>();
  for (const texts of owners.values()) for (const text of texts) seen.add(text);
  snapshot = [...seen];
  for (const listener of listeners) listener();
};

/** Every cell reference some mounted surface is asking about, as typed (bare or sheet-qualified). */
export const useHighlightedCellRefs = (): readonly string[] => useSyncExternalStore(
  (listener) => { listeners.add(listener); return () => { listeners.delete(listener); }; },
  () => snapshot,
  () => snapshot,
);

/** The grid's subscription to "a reference was just typed": it scrolls there. */
export const useCellRefFocus = (onFocus: (text: string) => void): void => {
  const latest = useRef(onFocus);
  latest.current = onFocus;
  useEffect(() => {
    const listener = (text: string) => latest.current(text);
    focusListeners.add(listener);
    return () => { focusListeners.delete(listener); };
  }, []);
};

const clean = (texts: readonly string[]) =>
  texts.map((text) => String(text ?? "").trim()).filter(Boolean);

/** Register the cell references a surface is showing. Blank or partial text is simply ignored
 *  by the grid, which only outlines what parses as a reference. */
export const useCellRefHighlight = (texts: readonly string[]): void => {
  const key = clean(texts).join("\u0000");
  const opened = useRef<Set<string> | null>(null);
  useEffect(() => {
    const now = key ? key.split("\u0000") : [];
    if (opened.current === null) opened.current = new Set(now);
    else {
      const typed = now.filter((text) => !opened.current!.has(text));
      opened.current = new Set(now);
      if (typed.length) for (const listener of focusListeners) listener(typed[typed.length - 1]);
    }
    if (!key) return;
    const id = nextOwner++;
    owners.set(id, now);
    publish();
    return () => { owners.delete(id); publish(); };
  }, [key]);
};