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

guide-store.ts

27 lines782 Bytes

1import { useSyncExternalStore } from "react";
2​
3/**
4 * Which guide section is "active" (highlighted). Phase 4 sets this from app
5 * state; for now it can be set from the dev controls in the guide header.
6 */
7let activeSectionId: string | null = null;
8const listeners = new Set<() => void>();
9​
10export function setActiveSection(id: string | null) {
11 if (id === activeSectionId) return;
12 activeSectionId = id;
13 for (const listener of listeners) listener();
15​
16export function getActiveSection(): string | null {
17 return activeSectionId;
19​
20function subscribe(listener: () => void) {
21 listeners.add(listener);
22 return () => listeners.delete(listener);
24​
25export function useActiveSection(): string | null {
26 return useSyncExternalStore(subscribe, getActiveSection, () => null);