|
1 | | -import React from "react"; |
| 1 | +import { useEffect, useReducer } from "react"; |
2 | 2 | import { Badge } from "@/components/ui/badge"; |
3 | 3 | import { Spinner } from "@/components/ui/spinner"; |
4 | 4 | import { useEditorCache } from "@/components/ui/coaching-sessions/editor-cache-context"; |
@@ -34,13 +34,23 @@ interface ConnectionStatusConfig { |
34 | 34 | * - Offline: Provider lost connection or no collaboration provider available |
35 | 35 | * - Error: Provider initialization or connection error |
36 | 36 | */ |
37 | | -export const ConnectionStatus: React.FC = () => { |
| 37 | +export const ConnectionStatus = () => { |
38 | 38 | const { isReady, isLoading, error, collaborationProvider } = useEditorCache(); |
39 | | - // Force re-render mechanism to pick up provider.status changes (external non-React state) |
40 | | - const [, forceUpdate] = React.useReducer(x => x + 1, 0); |
| 39 | + |
| 40 | + /** |
| 41 | + * Force re-render mechanism for tracking external state changes. |
| 42 | + * |
| 43 | + * The collaboration provider's status property (provider.status) is not React state, |
| 44 | + * so changes to it don't automatically trigger re-renders. We use useReducer as a |
| 45 | + * "force update" function: calling forceUpdate() increments an internal counter, |
| 46 | + * which causes React to re-render this component and pick up the new provider.status value. |
| 47 | + * |
| 48 | + * This is a standard React pattern for syncing with external (non-React) state objects. |
| 49 | + */ |
| 50 | + const [, forceUpdate] = useReducer(x => x + 1, 0); |
41 | 51 |
|
42 | 52 | // Track provider status changes to trigger re-renders |
43 | | - React.useEffect(() => { |
| 53 | + useEffect(() => { |
44 | 54 | if (!collaborationProvider) { |
45 | 55 | return; |
46 | 56 | } |
|
0 commit comments