signal-desktop/ts/components/DialogNetworkStatus.tsx
2021-09-17 17:20:49 -05:00

92 lines
2 KiB
TypeScript

// Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useEffect } from 'react';
import { LeftPaneDialog } from './LeftPaneDialog';
import { Spinner } from './Spinner';
import { LocalizerType } from '../types/Util';
import { SocketStatus } from '../types/SocketStatus';
import { NetworkStateType } from '../state/ducks/network';
const FIVE_SECONDS = 5 * 1000;
export type PropsType = NetworkStateType & {
hasNetworkDialog: boolean;
i18n: LocalizerType;
manualReconnect: () => void;
};
export const DialogNetworkStatus = ({
hasNetworkDialog,
i18n,
isOnline,
socketStatus,
manualReconnect,
}: PropsType): JSX.Element | null => {
const [isConnecting, setIsConnecting] = React.useState<boolean>(
socketStatus === SocketStatus.CONNECTING
);
useEffect(() => {
if (!hasNetworkDialog) {
return () => null;
}
let timeout: NodeJS.Timeout;
if (isConnecting) {
timeout = setTimeout(() => {
setIsConnecting(false);
}, FIVE_SECONDS);
}
return () => {
if (timeout) {
clearTimeout(timeout);
}
};
}, [hasNetworkDialog, isConnecting, setIsConnecting]);
const reconnect = () => {
setIsConnecting(true);
manualReconnect();
};
if (!hasNetworkDialog) {
return null;
}
if (isConnecting) {
const spinner = (
<div className="LeftPaneDialog__spinner-container">
<Spinner
direction="on-avatar"
moduleClassName="LeftPaneDialog__spinner"
size="22px"
svgSize="small"
/>
</div>
);
return (
<LeftPaneDialog
type="warning"
icon={spinner}
title={i18n('connecting')}
subtitle={i18n('connectingHangOn')}
/>
);
}
return (
<LeftPaneDialog
type="warning"
icon="network"
title={isOnline ? i18n('disconnected') : i18n('offline')}
subtitle={i18n('checkNetworkConnection')}
hasAction
clickLabel={i18n('connect')}
onClick={reconnect}
/>
);
};