signal-desktop/ts/components/Inbox.tsx

175 lines
4.9 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactNode } from 'react';
2023-03-29 16:47:06 +00:00
import React, { useEffect, useState, useMemo } from 'react';
import type { LocalizerType } from '../types/Util';
2022-06-16 19:12:50 +00:00
import * as log from '../logging/log';
2023-03-28 20:31:24 +00:00
import { SECOND, DAY } from '../util/durations';
2023-08-09 00:53:06 +00:00
import type { SmartNavTabsProps } from '../state/smart/NavTabs';
2021-06-14 19:01:00 +00:00
export type PropsType = {
2023-03-28 20:31:24 +00:00
firstEnvelopeTimestamp: number | undefined;
envelopeTimestamp: number | undefined;
2021-06-14 19:01:00 +00:00
hasInitialLoadCompleted: boolean;
i18n: LocalizerType;
isCustomizingPreferredReactions: boolean;
2023-08-09 00:53:06 +00:00
navTabsCollapsed: boolean;
onToggleNavTabsCollapse: (navTabsCollapsed: boolean) => unknown;
renderCallsTab: () => JSX.Element;
renderChatsTab: () => JSX.Element;
renderCustomizingPreferredReactionsModal: () => JSX.Element;
2023-08-09 00:53:06 +00:00
renderNavTabs: (props: SmartNavTabsProps) => JSX.Element;
renderStoriesTab: () => JSX.Element;
2021-06-14 19:01:00 +00:00
};
2022-11-18 00:45:19 +00:00
export function Inbox({
2023-03-28 20:31:24 +00:00
firstEnvelopeTimestamp,
envelopeTimestamp,
hasInitialLoadCompleted,
i18n,
isCustomizingPreferredReactions,
2023-08-09 00:53:06 +00:00
navTabsCollapsed,
onToggleNavTabsCollapse,
renderCallsTab,
renderChatsTab,
renderCustomizingPreferredReactionsModal,
2023-08-09 00:53:06 +00:00
renderNavTabs,
renderStoriesTab,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element {
2022-06-16 19:12:50 +00:00
const [internalHasInitialLoadCompleted, setInternalHasInitialLoadCompleted] =
useState(hasInitialLoadCompleted);
2023-03-29 16:47:06 +00:00
const now = useMemo(() => Date.now(), []);
const midnight = useMemo(() => {
const date = new Date(now);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
return date.getTime();
}, [now]);
2021-06-14 19:01:00 +00:00
useEffect(() => {
2022-06-16 19:12:50 +00:00
if (internalHasInitialLoadCompleted) {
return;
2021-06-14 19:01:00 +00:00
}
2022-06-16 19:12:50 +00:00
const interval = setInterval(() => {
const status = window.getSocketStatus();
switch (status) {
case 'CONNECTING':
break;
case 'OPEN':
// if we've connected, we can wait for real empty event
clearInterval(interval);
break;
case 'CLOSING':
case 'CLOSED':
clearInterval(interval);
// if we failed to connect, we pretend we loaded
setInternalHasInitialLoadCompleted(true);
break;
default:
log.warn(
`startConnectionListener: Found unexpected socket status ${status}; setting load to done manually.`
);
setInternalHasInitialLoadCompleted(true);
break;
}
}, SECOND);
return () => {
clearInterval(interval);
};
}, [internalHasInitialLoadCompleted]);
useEffect(() => {
setInternalHasInitialLoadCompleted(hasInitialLoadCompleted);
}, [hasInitialLoadCompleted]);
if (!internalHasInitialLoadCompleted) {
2023-03-28 20:31:24 +00:00
let loadingProgress = 0;
if (
firstEnvelopeTimestamp !== undefined &&
envelopeTimestamp !== undefined
) {
loadingProgress =
Math.max(
0,
Math.min(
1,
Math.max(0, envelopeTimestamp - firstEnvelopeTimestamp) /
Math.max(1e-23, now - firstEnvelopeTimestamp)
)
) * 100;
}
let message: string | undefined;
2023-03-29 16:47:06 +00:00
if (envelopeTimestamp !== undefined) {
const daysBeforeMidnight = Math.ceil(
(midnight - envelopeTimestamp) / DAY
);
if (daysBeforeMidnight <= 0) {
message = i18n('icu:loadingMessages--today');
} else if (daysBeforeMidnight === 1) {
message = i18n('icu:loadingMessages--yesterday');
} else {
message = i18n('icu:loadingMessages--other', {
daysAgo: daysBeforeMidnight,
});
}
}
2022-06-16 19:12:50 +00:00
return (
<div className="app-loading-screen">
<div className="module-title-bar-drag-area" />
2023-03-28 20:31:24 +00:00
<div className="module-splash-screen__logo module-img--150" />
{envelopeTimestamp === undefined ? (
<div className="container">
<span className="dot" />
<span className="dot" />
<span className="dot" />
</div>
) : (
<div className="app-loading-screen__progress--container">
<div
className="app-loading-screen__progress--bar"
style={{ transform: `translateX(${loadingProgress - 100}%)` }}
/>
</div>
)}
{message === undefined ? (
<div className="message-placeholder" />
) : (
<div className="message">{message}</div>
)}
2023-03-28 20:31:24 +00:00
<div id="toast" />
2022-06-16 19:12:50 +00:00
</div>
);
}
2021-06-14 19:01:00 +00:00
let activeModal: ReactNode;
if (isCustomizingPreferredReactions) {
activeModal = renderCustomizingPreferredReactionsModal();
}
return (
<>
2022-06-16 19:12:50 +00:00
<div className="Inbox">
<div className="module-title-bar-drag-area" />
2023-08-09 00:53:06 +00:00
{renderNavTabs({
navTabsCollapsed,
onToggleNavTabsCollapse,
renderChatsTab,
renderCallsTab,
renderStoriesTab,
})}
2022-06-16 19:12:50 +00:00
</div>
{activeModal}
</>
);
2022-11-18 00:45:19 +00:00
}