From 900a312c923a37ba75de98c91bb3d29b5def7a28 Mon Sep 17 00:00:00 2001 From: Shirone Date: Wed, 21 Jan 2026 00:09:35 +0100 Subject: [PATCH] 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. --- apps/ui/src/contexts/file-browser-context.tsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/apps/ui/src/contexts/file-browser-context.tsx b/apps/ui/src/contexts/file-browser-context.tsx index 959ba86b..74dc9200 100644 --- a/apps/ui/src/contexts/file-browser-context.tsx +++ b/apps/ui/src/contexts/file-browser-context.tsx @@ -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() { 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 (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'); } return context;