import { useCallback, useEffect, useRef } from "react"; import type { RequestAdmission } from "../services/HttpService"; export const NO_LONGER_PERMITTED = "You can no longer do this here, so it was not sent."; /** * An `admit` for one write (see `HttpService`'s `FetchOptions.admit`). It reads the latest answer each time it is * asked before dispatch, so a permission withdrawn during token/proof waits stops the write. An acknowledged * answer keeps its outcome; operation ownership is checked separately before publication. */ export const useAdmission = (permitted: boolean) => { const latest = useRef(permitted); useEffect(() => { latest.current = permitted; }, [permitted]); return useCallback((phase) => { if (phase !== 'answer' && !latest.current) throw new Error(NO_LONGER_PERMITTED); }, []); };