diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index c9fa034cf..9014ad62d 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -320,11 +320,6 @@ pub fn run() { }; app.run(|app, event| { - if matches!(event, tauri::RunEvent::Resumed) { - use tauri::Emitter; - let _ = app.emit("app-resumed", ()); - } - #[cfg(desktop)] desktop::tray::handle_run_event(app, event); diff --git a/src/app/hooks/useResumeSyncRetry.ts b/src/app/hooks/useResumeSyncRetry.ts deleted file mode 100644 index 4164e8b5a..000000000 --- a/src/app/hooks/useResumeSyncRetry.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { useEffect } from 'react'; -import { isTauri } from '@tauri-apps/api/core'; -import { listen } from '@tauri-apps/api/event'; -import type { MatrixClient } from '$types/matrix-sdk'; -import { SyncState } from '$types/matrix-sdk'; - -export const useResumeSyncRetry = (mx: MatrixClient | undefined): void => { - useEffect(() => { - if (!mx || !isTauri()) return undefined; - - let disposed = false; - let unlisten: (() => void) | undefined; - - listen('app-resumed', () => { - const state = mx.getSyncState(); - if (state === SyncState.Error || state === SyncState.Reconnecting) { - mx.retryImmediately(); - } - }).then((fn) => { - if (disposed) fn(); - else unlisten = fn; - }); - - return () => { - disposed = true; - unlisten?.(); - }; - }, [mx]); -}; diff --git a/src/app/pages/client/SyncStatus.tsx b/src/app/pages/client/SyncStatus.tsx index 954215769..b616e0a1f 100644 --- a/src/app/pages/client/SyncStatus.tsx +++ b/src/app/pages/client/SyncStatus.tsx @@ -1,12 +1,13 @@ import type { MatrixClient } from '$types/matrix-sdk'; import { SyncState } from '$types/matrix-sdk'; -import { useCallback, useRef, useState } from 'react'; +import { useCallback, useEffect, useRef, useState } from 'react'; import { Box, config, Line, Text } from 'folds'; import * as Sentry from '@sentry/react'; import { useSyncState } from '$hooks/useSyncState'; -import { useResumeSyncRetry } from '$hooks/useResumeSyncRetry'; import { ContainerColor } from '$styles/ContainerColor.css'; +const DISCONNECTED_GRACE_MS = 2000; + type StateData = { current: SyncState | null; previous: SyncState | null | undefined; @@ -34,7 +35,19 @@ export function SyncStatus({ mx }: SyncStatusProps) { previous: undefined, showConnecting: false, }); - useResumeSyncRetry(mx); + const [showDisconnected, setShowDisconnected] = useState(false); + + const isDisconnected = + stateData.current === SyncState.Reconnecting || stateData.current === SyncState.Error; + + useEffect(() => { + if (!isDisconnected) { + setShowDisconnected(false); + return undefined; + } + const timeoutId = setTimeout(() => setShowDisconnected(true), DISCONNECTED_GRACE_MS); + return () => clearTimeout(timeoutId); + }, [isDisconnected]); useSyncState( mx, @@ -83,7 +96,7 @@ export function SyncStatus({ mx }: SyncStatusProps) { ); } - if (stateData.current === SyncState.Reconnecting) { + if (showDisconnected && stateData.current === SyncState.Reconnecting) { return (