signal-desktop/ts/hooks/useRetryStorySend.tsx

53 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-11-16 22:10:11 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useState, useEffect } from 'react';
import type { LocalizerType } from '../types/Util';
import { Alert } from '../components/Alert';
import { ResolvedSendStatus } from '../types/Stories';
import { usePrevious } from './usePrevious';
export function useRetryStorySend(
i18n: LocalizerType,
sendStatus: ResolvedSendStatus | undefined
): {
renderAlert: () => JSX.Element | null;
setWasManuallyRetried: (value: boolean) => unknown;
wasManuallyRetried: boolean;
} {
const [hasSendFailedAlert, setHasSendFailedAlert] = useState(false);
const [wasManuallyRetried, setWasManuallyRetried] = useState(false);
const previousSendStatus = usePrevious(sendStatus, sendStatus);
useEffect(() => {
if (!wasManuallyRetried) {
return;
}
if (previousSendStatus === sendStatus) {
return;
}
if (
sendStatus === ResolvedSendStatus.Failed ||
sendStatus === ResolvedSendStatus.PartiallySent
) {
setHasSendFailedAlert(true);
}
}, [previousSendStatus, sendStatus, wasManuallyRetried]);
function renderAlert(): JSX.Element | null {
return hasSendFailedAlert ? (
<Alert
2023-03-30 00:03:25 +00:00
body={i18n('icu:Stories__failed-send')}
2022-11-16 22:10:11 +00:00
i18n={i18n}
onClose={() => setHasSendFailedAlert(false)}
/>
) : null;
}
return { renderAlert, setWasManuallyRetried, wasManuallyRetried };
}