import { createContext, use } from "react"; import type YProvider from "y-partyserver/provider"; import type { ActivePagePatch, ActivePageState } from "@/client/lib/active-page-model"; export interface ActivePageSyncValue { syncProvider: YProvider | null; setSyncProvider: (provider: YProvider | null) => void; } export interface ActivePageActionsValue { patchPage: (updates: ActivePagePatch) => void; } const ActivePageStateCtx = createContext(null); const ActivePageSyncCtx = createContext(null); const ActivePageActionsCtx = createContext(null); export function useActivePageState(): ActivePageState { const ctx = use(ActivePageStateCtx); if (!ctx) throw new Error("useActivePageState must be used inside ActivePageProvider"); return ctx; } export function useActivePageSync(): ActivePageSyncValue { const ctx = use(ActivePageSyncCtx); if (!ctx) throw new Error("useActivePageSync must be used inside ActivePageProvider"); return ctx; } export function useActivePageActions(): ActivePageActionsValue { const ctx = use(ActivePageActionsCtx); if (!ctx) throw new Error("useActivePageActions must be used inside ActivePageProvider"); return ctx; } export const ActivePageContexts = { State: ActivePageStateCtx, Sync: ActivePageSyncCtx, Actions: ActivePageActionsCtx, };