signal-desktop/ts/components/MyStories.tsx

271 lines
8 KiB
TypeScript
Raw Normal View History

2022-07-01 00:52:03 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useState } from 'react';
import type { MyStoryType, StoryViewType } from '../types/Stories';
2022-11-16 22:10:11 +00:00
import {
ResolvedSendStatus,
StoryViewTargetType,
StoryViewModeType,
} from '../types/Stories';
2022-07-01 00:52:03 +00:00
import type { LocalizerType } from '../types/Util';
2022-07-06 19:06:20 +00:00
import type { ViewStoryActionCreatorType } from '../state/ducks/stories';
2022-07-01 00:52:03 +00:00
import { ConfirmationDialog } from './ConfirmationDialog';
import { ContextMenu } from './ContextMenu';
import { MessageTimestamp } from './conversation/MessageTimestamp';
import { StoryDistributionListName } from './StoryDistributionListName';
2022-07-01 00:52:03 +00:00
import { StoryImage } from './StoryImage';
import { Theme } from '../util/theme';
2022-11-16 22:10:11 +00:00
import { resolveStorySendStatus } from '../util/resolveStorySendStatus';
import { useRetryStorySend } from '../hooks/useRetryStorySend';
2022-07-01 00:52:03 +00:00
export type PropsType = {
i18n: LocalizerType;
myStories: Array<MyStoryType>;
onBack: () => unknown;
onDelete: (story: StoryViewType) => unknown;
onForward: (storyId: string) => unknown;
onSave: (story: StoryViewType) => unknown;
queueStoryDownload: (storyId: string) => unknown;
2022-11-16 22:10:11 +00:00
retrySend: (messageId: string) => unknown;
2022-07-06 19:06:20 +00:00
viewStory: ViewStoryActionCreatorType;
2022-10-25 22:18:42 +00:00
hasViewReceiptSetting: boolean;
2022-07-01 00:52:03 +00:00
};
2022-11-18 00:45:19 +00:00
export function MyStories({
2022-07-01 00:52:03 +00:00
i18n,
myStories,
onBack,
onDelete,
onForward,
onSave,
queueStoryDownload,
2022-11-16 22:10:11 +00:00
retrySend,
2022-07-06 19:06:20 +00:00
viewStory,
2022-10-25 22:18:42 +00:00
hasViewReceiptSetting,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element {
2022-07-01 00:52:03 +00:00
const [confirmDeleteStory, setConfirmDeleteStory] = useState<
StoryViewType | undefined
>();
return (
<>
{confirmDeleteStory && (
<ConfirmationDialog
2022-09-27 20:24:21 +00:00
dialogName="MyStories.delete"
2022-07-01 00:52:03 +00:00
actions={[
{
text: i18n('delete'),
action: () => onDelete(confirmDeleteStory),
style: 'negative',
},
]}
i18n={i18n}
onClose={() => setConfirmDeleteStory(undefined)}
>
{i18n('MyStories__delete')}
</ConfirmationDialog>
)}
<div className="Stories__pane__header Stories__pane__header--centered">
<button
aria-label={i18n('back')}
className="Stories__pane__header--back"
onClick={onBack}
type="button"
/>
<div className="Stories__pane__header--title">
{i18n('MyStories__title')}
</div>
</div>
<div className="Stories__pane__list">
{myStories.map(list => (
2022-08-05 01:13:26 +00:00
<div className="MyStories__distribution" key={list.id}>
2022-07-01 00:52:03 +00:00
<div className="MyStories__distribution__title">
<StoryDistributionListName
i18n={i18n}
2022-08-05 01:13:26 +00:00
id={list.id}
name={list.name}
/>
2022-07-01 00:52:03 +00:00
</div>
{list.stories.map(story => (
2022-11-16 22:10:11 +00:00
<StorySent
hasViewReceiptSetting={hasViewReceiptSetting}
i18n={i18n}
key={story.messageId}
onForward={onForward}
onSave={onSave}
queueStoryDownload={queueStoryDownload}
retrySend={retrySend}
setConfirmDeleteStory={setConfirmDeleteStory}
story={story}
viewStory={viewStory}
/>
2022-07-01 00:52:03 +00:00
))}
</div>
))}
</div>
{!myStories.length && (
<div className="Stories__pane__list--empty">
{i18n('Stories__list-empty')}
</div>
)}
</>
);
2022-11-18 00:45:19 +00:00
}
2022-11-16 22:10:11 +00:00
type StorySentPropsType = Pick<
PropsType,
| 'hasViewReceiptSetting'
| 'i18n'
| 'onForward'
| 'onSave'
| 'queueStoryDownload'
| 'retrySend'
| 'viewStory'
> & {
setConfirmDeleteStory: (_: StoryViewType | undefined) => unknown;
story: StoryViewType;
};
function StorySent({
hasViewReceiptSetting,
i18n,
onForward,
onSave,
queueStoryDownload,
retrySend,
setConfirmDeleteStory,
story,
viewStory,
}: StorySentPropsType): JSX.Element {
const sendStatus = resolveStorySendStatus(story.sendState ?? []);
const { renderAlert, setWasManuallyRetried, wasManuallyRetried } =
useRetryStorySend(i18n, sendStatus);
return (
<div className="MyStories__story" key={story.timestamp}>
{renderAlert()}
<button
aria-label={i18n('MyStories__story')}
className="StoryListItem__button MyStories__story-button"
onClick={() => {
if (
!wasManuallyRetried &&
(sendStatus === ResolvedSendStatus.Failed ||
sendStatus === ResolvedSendStatus.PartiallySent)
) {
setWasManuallyRetried(true);
retrySend(story.messageId);
return;
}
viewStory({
storyId: story.messageId,
storyViewMode: StoryViewModeType.MyStories,
});
}}
type="button"
>
<div className="StoryListItem__previews">
<StoryImage
attachment={story.attachment}
firstName={i18n('you')}
i18n={i18n}
isMe
isThumbnail
label={i18n('MyStories__story')}
moduleClassName="StoryListItem__previews--image"
queueStoryDownload={queueStoryDownload}
storyId={story.messageId}
/>
</div>
<div className="MyStories__story__details">
{sendStatus === ResolvedSendStatus.Sending &&
i18n('Stories__list--sending')}
{sendStatus === ResolvedSendStatus.Failed && (
<div className="MyStories__story__details__failed">
<div>
{i18n('Stories__list--send_failed')}
{!wasManuallyRetried && (
<div className="MyStories__story__details__failed__button">
{i18n('Stories__list--retry-send')}
</div>
)}
</div>
</div>
)}
{sendStatus === ResolvedSendStatus.PartiallySent && (
<div className="MyStories__story__details__failed">
<div>
{i18n('Stories__list--partially-sent')}
{!wasManuallyRetried && (
<div className="MyStories__story__details__failed__button">
{i18n('Stories__list--retry-send')}
</div>
)}
</div>
</div>
)}
{sendStatus === ResolvedSendStatus.Sent && (
<>
{hasViewReceiptSetting
? i18n('icu:MyStories__views', {
views: story.views ?? 0,
})
: i18n('icu:MyStories__views-off')}
<MessageTimestamp
i18n={i18n}
isRelativeTime
module="MyStories__story__timestamp"
timestamp={story.timestamp}
/>
</>
)}
</div>
</button>
{story.attachment && (story.attachment.path || story.attachment.data) && (
<button
aria-label={i18n('MyStories__download')}
className="MyStories__story__download"
onClick={() => {
onSave(story);
}}
type="button"
/>
)}
<ContextMenu
i18n={i18n}
menuOptions={[
{
icon: 'MyStories__icon--forward',
label: i18n('forward'),
onClick: () => {
onForward(story.messageId);
},
},
{
icon: 'StoryListItem__icon--info',
label: i18n('StoryListItem__info'),
onClick: () => {
viewStory({
storyId: story.messageId,
storyViewMode: StoryViewModeType.MyStories,
viewTarget: StoryViewTargetType.Details,
});
},
},
{
icon: 'MyStories__icon--delete',
label: i18n('delete'),
onClick: () => {
setConfirmDeleteStory(story);
},
},
]}
moduleClassName="MyStories__story__more"
theme={Theme.Dark}
/>
</div>
);
}