Files
vibe-kanban/frontend/src/contexts/SyncErrorContext.tsx
T
Anastasiia SolopandClaude Opus 4.6 caa003d368 Fix React hot reload (#2660)
* extract AppContent into a separate file

* fix(frontend): preserve React context identity across HMR via import.meta.hot.data

The previous fix (splitting AppContent into a separate file) didn't help
because the root cause is the context object itself being recreated when
ConfigProvider.tsx is re-executed during HMR. This happens whenever any
file in its import chain (lib/api.ts, i18n/config.ts, etc.) is edited.

The fix stores UserSystemContext in import.meta.hot.data so it survives
module re-execution. On first load, createContext() runs and the result
is stashed; on HMR re-executions, the stashed context is reused. This
keeps provider and consumer referencing the same object.

Also reverts the unnecessary AppContent.tsx file split.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(frontend): preserve all React context identities across HMR

Add createHmrContext utility that stashes context objects in
import.meta.hot.data so they survive module re-execution during Vite
HMR. Without this, hot-reloading a context module creates a new context
object while mounted providers still hold the old one, causing "must be
used within Provider" errors.

Migrate all 30 createContext calls that have throwing guards to use
createHmrContext, preventing intermittent HMR crashes across the entire
app — not just UserSystemContext but also WorkspaceContext,
TerminalContext, and every other guarded context.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* prettier

* lint

* remove unnecessary extensions in imports

* prettier

* remove AppContent after rebase

* update cargo lock

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 07:56:03 +01:00

147 lines
3.5 KiB
TypeScript

import {
useContext,
useState,
useCallback,
useMemo,
useEffect,
type ReactNode,
} from 'react';
import { createHmrContext } from '@/lib/hmrContext.ts';
import type { SyncError } from '@/lib/electric/types';
/**
* Represents an error from a specific shape stream.
*/
export interface StreamError {
streamId: string;
tableName: string;
error: SyncError;
retry: () => void;
}
/**
* Context value for managing sync errors across the application.
*/
export interface SyncErrorContextValue {
/** Array of all current stream errors */
errors: StreamError[];
/** Quick check if any errors exist */
hasErrors: boolean;
/** Register an error for a specific stream */
registerError: (
streamId: string,
tableName: string,
error: SyncError,
retry: () => void
) => void;
/** Clear error for a specific stream */
clearError: (streamId: string) => void;
/** Retry all failed streams */
retryAll: () => void;
}
const SyncErrorContext = createHmrContext<SyncErrorContextValue | null>(
'SyncErrorContext',
null
);
interface SyncErrorProviderProps {
children: ReactNode;
}
export function SyncErrorProvider({ children }: SyncErrorProviderProps) {
const [errorsMap, setErrorsMap] = useState<Map<string, StreamError>>(
() => new Map()
);
const registerError = useCallback(
(
streamId: string,
tableName: string,
error: SyncError,
retry: () => void
) => {
setErrorsMap((prev) => {
const next = new Map(prev);
next.set(streamId, { streamId, tableName, error, retry });
return next;
});
},
[]
);
const clearError = useCallback((streamId: string) => {
setErrorsMap((prev) => {
if (!prev.has(streamId)) return prev;
const next = new Map(prev);
next.delete(streamId);
return next;
});
}, []);
const errors = useMemo(() => Array.from(errorsMap.values()), [errorsMap]);
const hasErrors = errors.length > 0;
const retryAll = useCallback(() => {
for (const streamError of errors) {
streamError.retry();
}
}, [errors]);
// Auto-retry all failed streams when tab becomes visible
useEffect(() => {
const handleVisibilityChange = () => {
if (document.visibilityState === 'visible' && errorsMap.size > 0) {
for (const streamError of errorsMap.values()) {
streamError.retry();
}
}
};
document.addEventListener('visibilitychange', handleVisibilityChange);
return () => {
document.removeEventListener('visibilitychange', handleVisibilityChange);
};
}, [errorsMap]);
const value = useMemo<SyncErrorContextValue>(
() => ({
errors,
hasErrors,
registerError,
clearError,
retryAll,
}),
[errors, hasErrors, registerError, clearError, retryAll]
);
return (
<SyncErrorContext.Provider value={value}>
{children}
</SyncErrorContext.Provider>
);
}
/**
* Hook to access sync error context.
* Returns null if used outside of SyncErrorProvider (graceful fallback).
*/
export function useSyncErrorContext(): SyncErrorContextValue | null {
return useContext(SyncErrorContext);
}
/**
* Hook to access sync error context with required provider.
* Throws if used outside of SyncErrorProvider.
*/
export function useSyncErrorContextRequired(): SyncErrorContextValue {
const context = useContext(SyncErrorContext);
if (!context) {
throw new Error(
'useSyncErrorContextRequired must be used within a SyncErrorProvider'
);
}
return context;
}