fix(ui): add HMR fallback for FileBrowserContext to prevent crashes during module reloads

- Implemented a no-op fallback for useFileBrowser to handle cases where the context is temporarily unavailable during Hot Module Replacement (HMR).
- Added warnings to notify when the context is not available, ensuring a smoother development experience without crashing the app.
This commit is contained in:
Shirone
2026-01-21 00:09:35 +01:00
parent 69ff8df7c1
commit 900a312c92

View File

@@ -67,9 +67,25 @@ export function FileBrowserProvider({ children }: { children: ReactNode }) {
); );
} }
// No-op fallback for HMR transitions when context temporarily becomes unavailable
const hmrFallback: FileBrowserContextValue = {
openFileBrowser: async () => {
console.warn('[HMR] FileBrowserContext not available, returning null');
return null;
},
};
export function useFileBrowser() { export function useFileBrowser() {
const context = useContext(FileBrowserContext); const context = useContext(FileBrowserContext);
// During HMR, the context can temporarily be null as modules reload.
// Instead of crashing the app, return a safe no-op fallback that will
// be replaced once the provider re-mounts.
if (!context) { if (!context) {
if (import.meta.hot) {
// In development with HMR active, gracefully degrade
return hmrFallback;
}
// In production, this indicates a real bug - throw to help debug
throw new Error('useFileBrowser must be used within FileBrowserProvider'); throw new Error('useFileBrowser must be used within FileBrowserProvider');
} }
return context; return context;