import { useEffect, useRef } from "react"; // An agency practice refuses every staff read until its owner accepts. Each read keeps its own error, so // the acceptance is announced once and every read that failed before it asks again. const PRACTICE_UNLOCKED = "akashic:practice-unlocked"; export const announcePracticeUnlocked = (): void => { window.dispatchEvent(new Event(PRACTICE_UNLOCKED)); }; /** Runs `reread` when the practice is unlocked; the caller decides whether it has anything to reread. */ export const usePracticeUnlocked = (reread: () => void): void => { const latest = useRef(reread); latest.current = reread; useEffect(() => { const onUnlocked = () => latest.current(); window.addEventListener(PRACTICE_UNLOCKED, onUnlocked); return () => window.removeEventListener(PRACTICE_UNLOCKED, onUnlocked); }, []); };