Skip to content

Commit fecd4e2

Browse files
committed
refactor(app-shell): read the docs portal's connection state through an external-store subscription
Claude-Session: https://claude.ai/code/session_01QSeKYo1Rb7aYhQiJdWshkL Co-authored-by: Claude <noreply@anthropic.com>
1 parent e78f292 commit fecd4e2

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

‎ui/packages/app-shell/src/console/docs/DocsPortalShell.tsx‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* @module
3939
*/
4040

41-
import React, { useEffect, useMemo, useState } from 'react';
41+
import React, { useCallback, useEffect, useMemo, useState, useSyncExternalStore } from 'react';
4242
import type { ConnectionState } from '@object-ui/data-objectstack';
4343
import { useNavigate } from 'react-router-dom';
4444
import { AppShell } from '@object-ui/layout';
@@ -82,14 +82,12 @@ function useAdapterConnectionState(dataSource: unknown): ConnectionState | undef
8282
getConnectionState?: () => ConnectionState;
8383
onConnectionStateChange?: (listener: (event: { state: ConnectionState }) => void) => () => void;
8484
} | null;
85-
const [state, setState] = useState<ConnectionState | undefined>(() => adapter?.getConnectionState?.());
86-
useEffect(() => {
87-
if (!adapter?.onConnectionStateChange) return undefined;
88-
const unsubscribe = adapter.onConnectionStateChange((event) => setState(event.state));
89-
setState(adapter.getConnectionState?.());
90-
return unsubscribe;
91-
}, [adapter]);
92-
return state;
85+
const subscribe = useCallback(
86+
(onChange: () => void) => adapter?.onConnectionStateChange?.(() => onChange()) ?? (() => {}),
87+
[adapter],
88+
);
89+
const read = useCallback(() => adapter?.getConnectionState?.(), [adapter]);
90+
return useSyncExternalStore(subscribe, read, read);
9391
}
9492

9593
/** Inside the sidebar provider: the window's width decides the sidebar's start state, as in an app. */

0 commit comments

Comments
 (0)