diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 9b0205c09..ae0fdfa20 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -318,6 +318,11 @@ 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 new file mode 100644 index 000000000..4164e8b5a --- /dev/null +++ b/src/app/hooks/useResumeSyncRetry.ts @@ -0,0 +1,29 @@ +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 65b9b2cd9..954215769 100644 --- a/src/app/pages/client/SyncStatus.tsx +++ b/src/app/pages/client/SyncStatus.tsx @@ -4,6 +4,7 @@ import { useCallback, 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'; type StateData = { @@ -33,6 +34,7 @@ export function SyncStatus({ mx }: SyncStatusProps) { previous: undefined, showConnecting: false, }); + useResumeSyncRetry(mx); useSyncState( mx,