Display "days ago" in loading screen
This commit is contained in:
parent
c02c8d9640
commit
3264c3d509
17 changed files with 316 additions and 66 deletions
96
ts/components/Inbox.stories.tsx
Normal file
96
ts/components/Inbox.stories.tsx
Normal file
|
@ -0,0 +1,96 @@
|
|||
// Copyright 2023 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import React, { useState, useEffect, useMemo } from 'react';
|
||||
import type { Meta, Story } from '@storybook/react';
|
||||
import { noop } from 'lodash';
|
||||
|
||||
import { Inbox } from './Inbox';
|
||||
import type { PropsType } from './Inbox';
|
||||
import { DAY, SECOND } from '../util/durations';
|
||||
|
||||
import { setupI18n } from '../util/setupI18n';
|
||||
import enMessages from '../../_locales/en/messages.json';
|
||||
|
||||
const i18n = setupI18n('en', enMessages);
|
||||
|
||||
export default {
|
||||
title: 'Components/Inbox',
|
||||
argTypes: {
|
||||
i18n: {
|
||||
defaultValue: i18n,
|
||||
},
|
||||
hasInitialLoadCompleted: {
|
||||
defaultValue: false,
|
||||
},
|
||||
daysAgo: {
|
||||
control: 'select',
|
||||
defaultValue: undefined,
|
||||
options: [undefined, 1, 2, 3, 7, 14, 21],
|
||||
},
|
||||
isCustomizingPreferredReactions: {
|
||||
defaultValue: false,
|
||||
},
|
||||
onConversationClosed: {
|
||||
action: true,
|
||||
},
|
||||
onConversationOpened: {
|
||||
action: true,
|
||||
},
|
||||
scrollToMessage: {
|
||||
action: true,
|
||||
},
|
||||
showConversation: {
|
||||
action: true,
|
||||
},
|
||||
showWhatsNewModal: {
|
||||
action: true,
|
||||
},
|
||||
},
|
||||
} as Meta;
|
||||
|
||||
// eslint-disable-next-line react/function-component-definition
|
||||
const Template: Story<PropsType & { daysAgo?: number }> = ({
|
||||
daysAgo,
|
||||
...args
|
||||
}) => {
|
||||
const now = useMemo(() => Date.now(), []);
|
||||
const [offset, setOffset] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
if (daysAgo === undefined) {
|
||||
setOffset(0);
|
||||
return noop;
|
||||
}
|
||||
|
||||
const interval = setInterval(() => {
|
||||
setOffset(prevValue => (prevValue + 1 / 4) % daysAgo);
|
||||
}, SECOND / 10);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [now, daysAgo]);
|
||||
|
||||
const firstEnvelopeTimestamp =
|
||||
daysAgo === undefined ? undefined : now - daysAgo * DAY;
|
||||
const envelopeTimestamp =
|
||||
firstEnvelopeTimestamp === undefined
|
||||
? undefined
|
||||
: firstEnvelopeTimestamp + offset * DAY;
|
||||
|
||||
return (
|
||||
<Inbox
|
||||
{...args}
|
||||
firstEnvelopeTimestamp={firstEnvelopeTimestamp}
|
||||
envelopeTimestamp={envelopeTimestamp}
|
||||
renderConversationView={() => <div />}
|
||||
renderCustomizingPreferredReactionsModal={() => <div />}
|
||||
renderLeftPane={() => <div />}
|
||||
renderMiniPlayer={() => <div />}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export const Default = Template.bind({});
|
||||
Default.story = {
|
||||
name: 'Default',
|
||||
};
|
|
@ -8,7 +8,7 @@ import type { ShowConversationType } from '../state/ducks/conversations';
|
|||
import type { LocalizerType } from '../types/Util';
|
||||
|
||||
import * as log from '../logging/log';
|
||||
import { SECOND } from '../util/durations';
|
||||
import { SECOND, DAY } from '../util/durations';
|
||||
import { ToastStickerPackInstallFailed } from './ToastStickerPackInstallFailed';
|
||||
import { WhatsNewLink } from './WhatsNewLink';
|
||||
import { showToast } from '../util/showToast';
|
||||
|
@ -17,6 +17,8 @@ import { TargetedMessageSource } from '../state/ducks/conversationsEnums';
|
|||
import { usePrevious } from '../hooks/usePrevious';
|
||||
|
||||
export type PropsType = {
|
||||
firstEnvelopeTimestamp: number | undefined;
|
||||
envelopeTimestamp: number | undefined;
|
||||
hasInitialLoadCompleted: boolean;
|
||||
i18n: LocalizerType;
|
||||
isCustomizingPreferredReactions: boolean;
|
||||
|
@ -35,6 +37,8 @@ export type PropsType = {
|
|||
};
|
||||
|
||||
export function Inbox({
|
||||
firstEnvelopeTimestamp,
|
||||
envelopeTimestamp,
|
||||
hasInitialLoadCompleted,
|
||||
i18n,
|
||||
isCustomizingPreferredReactions,
|
||||
|
@ -51,7 +55,6 @@ export function Inbox({
|
|||
showConversation,
|
||||
showWhatsNewModal,
|
||||
}: PropsType): JSX.Element {
|
||||
const [loadingMessageCount, setLoadingMessageCount] = useState(0);
|
||||
const [internalHasInitialLoadCompleted, setInternalHasInitialLoadCompleted] =
|
||||
useState(hasInitialLoadCompleted);
|
||||
|
||||
|
@ -123,13 +126,11 @@ export function Inbox({
|
|||
showToast(ToastStickerPackInstallFailed);
|
||||
}
|
||||
|
||||
window.Whisper.events.on('loadingProgress', setLoadingMessageCount);
|
||||
window.Whisper.events.on('pack-install-failed', packInstallFailed);
|
||||
window.Whisper.events.on('refreshConversation', refreshConversation);
|
||||
window.Whisper.events.on('setupAsNewDevice', unload);
|
||||
|
||||
return () => {
|
||||
window.Whisper.events.off('loadingProgress', setLoadingMessageCount);
|
||||
window.Whisper.events.off('pack-install-failed', packInstallFailed);
|
||||
window.Whisper.events.off('refreshConversation', refreshConversation);
|
||||
window.Whisper.events.off('setupAsNewDevice', unload);
|
||||
|
@ -175,26 +176,45 @@ export function Inbox({
|
|||
}, [hasInitialLoadCompleted]);
|
||||
|
||||
if (!internalHasInitialLoadCompleted) {
|
||||
const now = Date.now();
|
||||
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;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="app-loading-screen">
|
||||
<div className="module-title-bar-drag-area" />
|
||||
|
||||
<div className="content">
|
||||
<div className="module-splash-screen__logo module-img--150" />
|
||||
<div className="container">
|
||||
<span className="dot" />
|
||||
<span className="dot" />
|
||||
<span className="dot" />
|
||||
</div>
|
||||
<div className="message">
|
||||
{loadingMessageCount
|
||||
? i18n('loadingMessages', {
|
||||
count: String(loadingMessageCount),
|
||||
})
|
||||
: i18n('loading')}
|
||||
</div>
|
||||
<div id="toast" />
|
||||
<div className="module-splash-screen__logo module-img--150" />
|
||||
<div className="app-loading-screen__progress--container">
|
||||
<div
|
||||
className="app-loading-screen__progress--bar"
|
||||
style={{ transform: `translateX(${loadingProgress - 100}%)` }}
|
||||
/>
|
||||
</div>
|
||||
<div className="message">
|
||||
{envelopeTimestamp
|
||||
? i18n('icu:loadingMessages', {
|
||||
daysAgo: Math.max(
|
||||
1,
|
||||
Math.round((now - envelopeTimestamp) / DAY)
|
||||
),
|
||||
})
|
||||
: i18n('loading')}
|
||||
</div>
|
||||
<div id="toast" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue