20 lines
896 B
TypeScript
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;
|