import { createContext, type Context } from "react"; export type UnsavedContextValue = { registerWarning: (id: string, message: string | null) => void; requestDiscard: (action: () => void) => void; }; /* * Vite Fast Refresh can re-evaluate a module while an existing route tree is * still mounted. Keep the context identity on the browser window so the old * provider and the refreshed consumers continue to refer to the same object. */ type ContextRegistry = Window & { __tallynoteUnsavedContext?: Context; }; const registry = typeof window === "undefined" ? null : window as ContextRegistry; export const UnsavedContext: Context = registry?.__tallynoteUnsavedContext ?? createContext(null); if (registry && !registry.__tallynoteUnsavedContext) registry.__tallynoteUnsavedContext = UnsavedContext;