import { useEffect, useState } from "react"; import { useOrganizationOwnerService } from "../services/OrganizationOwnerService"; /** * Does this practice still need an owner? * * Until somebody accepts ownership the owner gate refuses a practice's staff reads, which the dashboard would * otherwise report as a communication system error. Read only when `enabled`, and answered from the server's own * `needsOwner`, never re-derived from `status`. The answer is kept with the practice it was read for, so another * practice never sees it. Undefined while unknown or when the read fails, so a caller falls back to what it did before. */ export const usePracticeNeedsOwner = (organizationId: string | undefined, enabled: boolean): boolean | undefined => { const { fetchOwner } = useOrganizationOwnerService(); const [answer, setAnswer] = useState<{ organizationId: string; needsOwner: boolean } | undefined>(undefined); useEffect(() => { if (!enabled || !organizationId) return; let live = true; fetchOwner(organizationId) .then((state) => { if (live) setAnswer({ organizationId, needsOwner: state.needsOwner }); }) .catch(() => { if (live) setAnswer(undefined); }); return () => { live = false; }; }, [organizationId, enabled, fetchOwner]); return enabled && answer && answer.organizationId === organizationId ? answer.needsOwner : undefined; };