import { useCallback, useEffect, useRef } from "react"; import { useActiveAccountId } from "./activeAccount"; // Re-read per-account persisted state when this tab's active account becomes known (or changes). The // provider tree above mounts before the account is pinned, so its useState initializers seed from // an unknown account (empty defaults); once pins it, `rehydrate` runs to load the real stored // value. Guarded so it never clobbers a value the user set in the brief pre-pin window: the returned // `markDirty` is called by the owning setter, and a dirty state skips the re-hydrate. // // Keyed on the account id ONLY — the user's own writes go through setAccountRaw, which does not change // the account id, so this effect can't loop on them. // // This is the localStorage-only counterpart of the `useEffect([me?.id])` server-prefs adopt pattern in // FeedFiltersProvider / FeedViewProvider / PrefsProvider (and App's shell prefs): those re-load from the // SERVER when the account query resolves; this re-loads from localStorage when the account is pinned. // A provider needs one or the other — not both — to survive a fresh tab's first paint. export function useAccountRehydrate(rehydrate: () => void): () => void { const accountId = useActiveAccountId(); const dirty = useRef(false); const prev = useRef(accountId); // Latest closure in a ref so the effect depends on the account id alone (a fresh `rehydrate` each // render must not re-fire it), while still calling the current logic. const fn = useRef(rehydrate); fn.current = rehydrate; useEffect(() => { if (accountId === prev.current) return; // mount, and renders with no real transition prev.current = accountId; if (!dirty.current) fn.current(); // Reset per account: `dirty` means "the user edited under THIS account". Today a real account // switch full-reloads (fresh instance), but clearing it here keeps the guard correct even if a // future switch stops reloading — the next account starts clean rather than inheriting dirtiness. dirty.current = false; }, [accountId]); return useCallback(() => { dirty.current = true; }, []); }