Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

spotlight-reveal.ts

32 lines1.1 KB

1import { useEffect, useRef } from "react";
2​
3import type { SpotlightTarget } from "./spotlight-targets";
4​
5/*
6 * Some spotlight targets can be hidden by app state the guide can't see:
7 * the macOS sidebar toggled off, the iPhone Folders screen's iCloud section
8 * collapsed. Whatever owns that state registers a "reveal" for the target,
9 * and the spotlight asks for it before it looks for the target.
10 */
11​
12const revealers = new Map<SpotlightTarget, Set<() => void>>();
13​
14/** Asks whoever can hide `target` to show it. */
15export function revealSpotlightTarget(target: SpotlightTarget) {
16 for (const reveal of revealers.get(target) ?? []) reveal();
18​
19/** Registers `reveal` (e.g. "open the sidebar") for `target` while mounted. */
20export function useRevealForSpotlight(target: SpotlightTarget, reveal: () => void) {
21 const revealRef = useRef(reveal);
22 revealRef.current = reveal;
23 useEffect(() => {
24 const run = () => revealRef.current();
25 const forTarget = revealers.get(target) ?? new Set();
26 revealers.set(target, forTarget);
27 forTarget.add(run);
28 return () => {
29 forTarget.delete(run);
30 };
31 }, [target]);