import { MatrixClient, SyncState } from 'matrix-js-sdk'; import React, { useCallback, useState } from 'react'; import { useAtomValue } from 'jotai'; import { Box, config, Line, Text } from 'folds'; import { useSyncState } from '../../hooks/useSyncState'; import { ContainerColor } from '../../styles/ContainerColor.css'; import { serverStatusAtom } from '../../state/serverStatus'; import { serverConfirmedUpSince } from '../../utils/kumaStatus'; type StateData = { current: SyncState | null; previous: SyncState | null | undefined; }; type SyncStatusProps = { mx: MatrixClient; }; export function SyncStatus({ mx }: SyncStatusProps) { const serverStatus = useAtomValue(serverStatusAtom); const [stateData, setStateData] = useState({ current: null, previous: undefined, }); useSyncState( mx, useCallback((current, previous) => { setStateData((s) => { if (s.current === current && s.previous === previous) { return s; } return { current, previous }; }); }, []), ); if ( (stateData.current === SyncState.Prepared || stateData.current === SyncState.Syncing || stateData.current === SyncState.Catchup) && stateData.previous !== SyncState.Syncing ) { return ( Connecting... ); } // [Gitea #124] When Kuma confirms the homeserver is down, the status banner // says so ("Lotus Chat's server is down…") instead of "Connection Lost!". const serverDown = serverStatus.status?.homeserver === 'down'; // Only blame the user's connection when Kuma has checked the server since // the drop and it was fine (a stale "up" from before proves nothing). const serverUp = serverConfirmedUpSince(serverStatus.status, serverStatus.lostAt); if (stateData.current === SyncState.Reconnecting && !serverDown) { return ( {serverUp ? "Connection lost. Our status checks say Lotus Chat's server is up, so it may be your internet connection. Reconnecting…" : 'Connection Lost! Reconnecting...'} ); } if (stateData.current === SyncState.Error && !serverDown) { return ( {serverUp ? "Connection lost. Our status checks say Lotus Chat's server is up, so check your internet connection." : 'Connection Lost!'} ); } return null; }