import { useQuery, type QueryClient } from "@tanstack/react-query"; import { qk } from "./queryKeys"; import { api, type Me } from "./api"; /** Write a change the SERVER already has (or is about to) into the `me` cache — and cancel a `me` * fetch still in flight. That fetch may have left before the change was saved (BootGate's 60 s poll, * a refetch on window focus); answering after the write, it would put the old value back and hold it * until the next poll. Cancelling reverts the query to its latest state, which is this write: * react-query takes a manual write as the state to revert to. */ export function updateMeCache(qc: QueryClient, update: (me: Me) => Me): void { qc.setQueryData(qk.me(), (me) => (me ? update(me) : me)); void qc.cancelQueries({ queryKey: qk.me() }); } // The signed-in account, read from the qk.me() query cache. BootGate owns the FETCHING query (gated on // setup state) and App renders modules only once it has resolved to a real user — so anywhere inside the // authenticated module tree the cache is always populated. `enabled:false` makes this a passive // observer (no second fetch, which could 503 in setup mode) that still re-renders when the cache // updates (login, the 60s refetch, a Google-link round-trip). Lets modules read `me` from a hook // instead of having it prop-drilled through App. export function useMe(): Me { const { data } = useQuery({ queryKey: qk.me(), queryFn: api.me, enabled: false }); return data as Me; }