Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 4x 19x 19x 19x 3x 19x 14x 4x 19x 22x 22x 2x 19x | import {
createContext,
useContext,
useCallback,
useEffect,
type ReactNode,
} from "react";
import type { Pro } from "./api";
import { useAuth } from "./auth-context";
import { useProMe } from "../hooks/queries/useProQueries";
import { prefetchDashboard } from "./route-loaders";
type ProContextType = {
pro: Pro | null;
proId: string | null;
proRole: string | null;
onboardingRequired: boolean;
pendingInvitationToken: string | null;
isLoading: boolean;
error: string | null;
refreshPro: () => Promise<void>;
};
const ProContext = createContext<ProContextType | null>(null);
export function ProProvider({ children }: { children: ReactNode }) {
const { isAuthenticated, isLoading: authLoading } = useAuth();
const { data, isLoading, error, refetch } = useProMe(isAuthenticated);
const refreshPro = useCallback(async () => {
await refetch();
}, [refetch]);
// Warm TanStack Query cache with dashboard data once proId is known
useEffect(() => {
if (data?.proId) {
prefetchDashboard();
}
}, [data?.proId]);
return (
<ProContext.Provider
value={{
pro: data?.pro || null,
proId: data?.proId || null,
proRole: data?.proRole || null,
onboardingRequired: data?.onboardingRequired ?? false,
pendingInvitationToken: data?.pendingInvitation?.token || null,
isLoading: authLoading || isLoading || (isAuthenticated && !data && !error),
error: error ? "Failed to load pro profile" : null,
refreshPro,
}}
>
{children}
</ProContext.Provider>
);
}
export function usePro() {
const context = useContext(ProContext);
if (!context) {
throw new Error("usePro must be used within a ProProvider");
}
return context;
}
|