mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-06 00:32:26 -08:00
fix: preserve across states
This commit is contained in:
1 parent
7664a94d79
commit
37d1e424b9
2 files changed
+32
-8
No files matched your search
@@ -93,6 +93,24 @@ describe('useStickyDisconnected (hysteresis)', () => {
|
||||
expect(result.current).toBe(SyncState.Reconnecting);
|
||||
});
|
||||
|
||||
it('preserves the show timer when the degraded variant changes', () => {
|
||||
const { result, rerender } = renderHook(({ state }) => useStickyDisconnected(state), {
|
||||
initialProps: { state: SyncState.Syncing as SyncState | null },
|
||||
});
|
||||
|
||||
rerender({ state: SyncState.Reconnecting });
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1_500);
|
||||
});
|
||||
|
||||
rerender({ state: SyncState.Error });
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(501);
|
||||
});
|
||||
|
||||
expect(result.current).toBe(SyncState.Error);
|
||||
});
|
||||
|
||||
it('stays shown when healthy for 1s then degraded again (flap regression)', () => {
|
||||
const { result, rerender } = renderHook(({ state }) => useStickyDisconnected(state), {
|
||||
initialProps: { state: SyncState.Syncing as SyncState | null },
|
||||
|
||||
@@ -34,28 +34,34 @@ export const shouldShowInlineSyncStatus = (hasCustomTitleBar: boolean): boolean
|
||||
!hasCustomTitleBar;
|
||||
|
||||
export const useStickyDisconnected = (syncCurrent: SyncState | null): SyncState | null => {
|
||||
const degraded =
|
||||
syncCurrent === SyncState.Reconnecting || syncCurrent === SyncState.Error ? syncCurrent : null;
|
||||
const showStartedAtRef = useRef<number | null>(null);
|
||||
const [stickyDisconnected, setStickyDisconnected] = useState<SyncState | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const degraded =
|
||||
syncCurrent === SyncState.Reconnecting || syncCurrent === SyncState.Error
|
||||
? syncCurrent
|
||||
: null;
|
||||
|
||||
if (degraded) {
|
||||
// Already visible: swap variant in place, don't re-arm the show timer.
|
||||
if (stickyDisconnected) {
|
||||
showStartedAtRef.current = null;
|
||||
if (stickyDisconnected !== degraded) setStickyDisconnected(degraded);
|
||||
return undefined;
|
||||
}
|
||||
const id = setTimeout(() => setStickyDisconnected(degraded), DISCONNECTED_SHOW_DELAY_MS);
|
||||
|
||||
const startedAt = showStartedAtRef.current ?? Date.now();
|
||||
showStartedAtRef.current = startedAt;
|
||||
const remaining = Math.max(0, DISCONNECTED_SHOW_DELAY_MS - (Date.now() - startedAt));
|
||||
const id = setTimeout(() => {
|
||||
showStartedAtRef.current = null;
|
||||
setStickyDisconnected(degraded);
|
||||
}, remaining);
|
||||
return () => clearTimeout(id);
|
||||
}
|
||||
|
||||
showStartedAtRef.current = null;
|
||||
if (!stickyDisconnected) return undefined;
|
||||
const id = setTimeout(() => setStickyDisconnected(null), DISCONNECTED_HIDE_DELAY_MS);
|
||||
return () => clearTimeout(id);
|
||||
}, [syncCurrent, stickyDisconnected]);
|
||||
}, [degraded, stickyDisconnected]);
|
||||
|
||||
return stickyDisconnected;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user