signal-desktop/ts/components/DialogNetworkStatus.tsx

102 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2021-09-17 22:20:49 +00:00
import React, { useEffect } from 'react';
2021-09-17 22:20:49 +00:00
import { LeftPaneDialog } from './LeftPaneDialog';
import { Spinner } from './Spinner';
import type { LocalizerType } from '../types/Util';
2021-06-09 22:28:54 +00:00
import { SocketStatus } from '../types/SocketStatus';
import type { NetworkStateType } from '../state/ducks/network';
import type { WidthBreakpoint } from './_util';
import { clearTimeoutIfNecessary } from '../util/clearTimeoutIfNecessary';
const FIVE_SECONDS = 5 * 1000;
export type PropsType = Pick<
NetworkStateType,
'isOnline' | 'isOutage' | 'socketStatus'
> & {
containerWidthBreakpoint: WidthBreakpoint;
i18n: LocalizerType;
manualReconnect: () => void;
};
2022-11-18 00:45:19 +00:00
export function DialogNetworkStatus({
containerWidthBreakpoint,
i18n,
isOnline,
isOutage,
socketStatus,
manualReconnect,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element | null {
2021-09-17 22:20:49 +00:00
const [isConnecting, setIsConnecting] = React.useState<boolean>(
socketStatus === SocketStatus.CONNECTING
);
useEffect(() => {
2020-09-12 00:46:52 +00:00
let timeout: NodeJS.Timeout;
if (isConnecting) {
timeout = setTimeout(() => {
setIsConnecting(false);
}, FIVE_SECONDS);
}
return () => {
clearTimeoutIfNecessary(timeout);
};
2023-01-18 23:31:10 +00:00
}, [isConnecting, setIsConnecting]);
2020-09-12 00:46:52 +00:00
const reconnect = () => {
setIsConnecting(true);
manualReconnect();
};
if (isOutage) {
return (
<LeftPaneDialog
containerWidthBreakpoint={containerWidthBreakpoint}
type="warning"
icon="error"
subtitle={i18n('icu:DialogNetworkStatus__outage')}
/>
);
}
2021-09-17 22:20:49 +00:00
if (isConnecting) {
const spinner = (
<div className="LeftPaneDialog__spinner-container">
<Spinner
direction="on-avatar"
moduleClassName="LeftPaneDialog__spinner"
size="22px"
svgSize="small"
/>
</div>
);
return (
<LeftPaneDialog
containerWidthBreakpoint={containerWidthBreakpoint}
2021-09-17 22:20:49 +00:00
type="warning"
icon={spinner}
2023-03-30 00:03:25 +00:00
title={i18n('icu:connecting')}
subtitle={i18n('icu:connectingHangOn')}
2021-09-17 22:20:49 +00:00
/>
);
}
2021-09-17 22:20:49 +00:00
return (
<LeftPaneDialog
containerWidthBreakpoint={containerWidthBreakpoint}
2021-09-17 22:20:49 +00:00
type="warning"
icon="network"
2023-03-30 00:03:25 +00:00
title={isOnline ? i18n('icu:disconnected') : i18n('icu:offline')}
subtitle={i18n('icu:checkNetworkConnection')}
2021-09-17 22:20:49 +00:00
hasAction
2023-03-30 00:03:25 +00:00
clickLabel={i18n('icu:connect')}
2021-09-17 22:20:49 +00:00
onClick={reconnect}
/>
);
2022-11-18 00:45:19 +00:00
}