Files
TallyNote/web-next/src/contexts/UnsavedChangesContext.ts
T
Qiufeng 12495fb6a4
TallyNote release / linux-x64 (push) Successful in 6m24s
release: 1.1.0
2026-08-31 20:11:34 +08:00

20 lines
896 B
TypeScript

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<UnsavedContextValue | null>;
};
const registry = typeof window === "undefined" ? null : window as ContextRegistry;
export const UnsavedContext: Context<UnsavedContextValue | null> = registry?.__tallynoteUnsavedContext ?? createContext<UnsavedContextValue | null>(null);
if (registry && !registry.__tallynoteUnsavedContext) registry.__tallynoteUnsavedContext = UnsavedContext;