signal-desktop/ts/components/MyStoryButton.tsx

206 lines
6.4 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';
2022-07-01 00:52:03 +00:00
import classNames from 'classnames';
import type { ConversationType } from '../state/ducks/conversations';
import type { LocalizerType } from '../types/Util';
2022-11-16 22:10:11 +00:00
import type { MyStoryType, StoryViewType } from '../types/Stories';
import type { ShowToastAction } from '../state/ducks/toast';
2022-07-01 00:52:03 +00:00
import { Avatar, AvatarSize } from './Avatar';
2022-11-16 22:10:11 +00:00
import { HasStories, ResolvedSendStatus } from '../types/Stories';
2022-08-24 21:39:10 +00:00
import { MessageTimestamp } from './conversation/MessageTimestamp';
2022-08-19 18:36:47 +00:00
import { StoriesAddStoryButton } from './StoriesAddStoryButton';
2022-11-16 22:10:11 +00:00
import { StoryImage } from './StoryImage';
import { getAvatarColor } from '../types/Colors';
import { reduceStorySendStatus } from '../util/resolveStorySendStatus';
2022-08-19 18:36:47 +00:00
2022-07-01 00:52:03 +00:00
export type PropsType = {
i18n: LocalizerType;
maxAttachmentSizeInKb: number;
2022-07-01 00:52:03 +00:00
me: ConversationType;
2022-11-16 22:10:11 +00:00
myStories: Array<MyStoryType>;
onAddStory: () => unknown;
2022-07-01 00:52:03 +00:00
onClick: () => unknown;
2023-02-24 23:18:57 +00:00
onMediaPlaybackStart: () => void;
2022-07-01 00:52:03 +00:00
queueStoryDownload: (storyId: string) => unknown;
showToast: ShowToastAction;
2022-07-01 00:52:03 +00:00
};
2022-11-16 22:10:11 +00:00
function getNewestMyStory(story: MyStoryType): StoryViewType {
return story.stories[0];
}
2022-11-18 00:45:19 +00:00
export function MyStoryButton({
2022-07-01 00:52:03 +00:00
i18n,
maxAttachmentSizeInKb,
2022-07-01 00:52:03 +00:00
me,
2022-11-16 22:10:11 +00:00
myStories,
onAddStory,
2022-07-01 00:52:03 +00:00
onClick,
2023-02-24 23:18:57 +00:00
onMediaPlaybackStart,
2022-07-01 00:52:03 +00:00
queueStoryDownload,
2022-08-19 18:36:47 +00:00
showToast,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element {
const [active, setActive] = useState(false);
2022-11-16 22:10:11 +00:00
const newestStory = myStories.length
? getNewestMyStory(myStories[0])
: undefined;
2022-07-01 00:52:03 +00:00
const {
acceptedMessageRequest,
2024-07-11 19:44:09 +00:00
avatarUrl,
2022-07-01 00:52:03 +00:00
color,
isMe,
profileName,
sharedGroupNames,
title,
} = me;
2022-08-19 18:36:47 +00:00
if (!newestStory) {
return (
<StoriesAddStoryButton
i18n={i18n}
maxAttachmentSizeInKb={maxAttachmentSizeInKb}
2022-08-25 00:34:17 +00:00
moduleClassName="StoryListItem StoryListItem--active-opactiy"
2022-08-19 18:36:47 +00:00
onAddStory={onAddStory}
showToast={showToast}
>
<div className="MyStories__avatar-container">
<Avatar
acceptedMessageRequest={acceptedMessageRequest}
2024-07-11 19:44:09 +00:00
avatarUrl={avatarUrl}
badge={undefined}
color={getAvatarColor(color)}
conversationType="direct"
i18n={i18n}
isMe={Boolean(isMe)}
profileName={profileName}
sharedGroupNames={sharedGroupNames}
size={AvatarSize.FORTY_EIGHT}
title={title}
/>
<div className="MyStories__avatar__add-story" />
2022-07-01 00:52:03 +00:00
</div>
2022-08-19 18:36:47 +00:00
<div className="StoryListItem__info">
2022-11-18 00:45:19 +00:00
<div className="StoryListItem__info--title">
2023-03-30 00:03:25 +00:00
{i18n('icu:Stories__mine')}
2022-11-18 00:45:19 +00:00
</div>
<div className="StoryListItem__info--timestamp">
2023-03-30 00:03:25 +00:00
{i18n('icu:Stories__add')}
2022-11-18 00:45:19 +00:00
</div>
2022-08-19 18:36:47 +00:00
</div>
</StoriesAddStoryButton>
);
}
2022-11-16 22:10:11 +00:00
const hasMultiple = myStories.length
? myStories[0].stories.length > 1
: false;
const reducedSendStatus: ResolvedSendStatus = myStories.reduce(
(acc: ResolvedSendStatus, myStory) =>
reduceStorySendStatus(acc, myStory.reducedSendStatus),
ResolvedSendStatus.Sent
);
2022-08-19 18:36:47 +00:00
return (
<div
className={classNames(
'StoryListItem__button',
active && 'StoryListItem__button--active'
)}
>
2022-08-19 18:36:47 +00:00
<div className="MyStories__avatar-container">
<StoriesAddStoryButton
i18n={i18n}
maxAttachmentSizeInKb={maxAttachmentSizeInKb}
2022-08-25 00:34:17 +00:00
moduleClassName="StoryListItem--active-opacity"
2022-08-19 18:36:47 +00:00
onAddStory={onAddStory}
showToast={showToast}
onContextMenuShowingChanged={setActive}
2022-08-19 18:36:47 +00:00
>
<Avatar
acceptedMessageRequest={acceptedMessageRequest}
2024-07-11 19:44:09 +00:00
avatarUrl={avatarUrl}
2022-08-19 18:36:47 +00:00
badge={undefined}
color={getAvatarColor(color)}
conversationType="direct"
i18n={i18n}
isMe={Boolean(isMe)}
profileName={profileName}
sharedGroupNames={sharedGroupNames}
size={AvatarSize.FORTY_EIGHT}
2022-08-25 00:34:17 +00:00
storyRing={HasStories.Read}
2022-08-19 18:36:47 +00:00
title={title}
/>
<div className="MyStories__avatar__add-story" />
2022-08-19 18:36:47 +00:00
</StoriesAddStoryButton>
</div>
<div
2022-08-25 00:34:17 +00:00
className="StoryListItem__click-container StoryListItem--active-opacity"
2022-08-19 18:36:47 +00:00
onClick={onClick}
onKeyDown={ev => {
if (ev.key === 'Enter') {
onClick();
ev.stopPropagation();
ev.preventDefault();
}
}}
role="button"
tabIndex={0}
>
<div className="StoryListItem__info">
2022-08-25 00:34:17 +00:00
<div className="StoryListItem__info--title StoryListItem__chevron">
2023-03-30 00:03:25 +00:00
{i18n('icu:MyStories__list_item')}
2022-08-24 21:39:10 +00:00
</div>
2022-11-16 22:10:11 +00:00
{reducedSendStatus === ResolvedSendStatus.Sending && (
<span className="StoryListItem__info--sending">
2023-03-30 00:03:25 +00:00
{i18n('icu:Stories__list--sending')}
</span>
)}
2022-11-16 22:10:11 +00:00
{reducedSendStatus === ResolvedSendStatus.Failed && (
<span className="StoryListItem__info--send_failed">
2023-03-30 00:03:25 +00:00
{i18n('icu:Stories__list--send_failed')}
</span>
)}
2022-11-16 22:10:11 +00:00
{reducedSendStatus === ResolvedSendStatus.PartiallySent && (
<span className="StoryListItem__info--send_failed">
2023-03-30 00:03:25 +00:00
{i18n('icu:Stories__list--partially-sent')}
2022-11-16 22:10:11 +00:00
</span>
)}
{reducedSendStatus === ResolvedSendStatus.Sent && (
<MessageTimestamp
i18n={i18n}
isRelativeTime
module="StoryListItem__info--timestamp"
timestamp={newestStory.timestamp}
/>
)}
2022-08-19 18:36:47 +00:00
</div>
<div
2023-03-30 00:03:25 +00:00
aria-label={i18n('icu:StoryListItem__label')}
2022-08-19 18:36:47 +00:00
className={classNames('StoryListItem__previews', {
'StoryListItem__previews--multiple': hasMultiple,
})}
>
{hasMultiple && <div className="StoryListItem__previews--more" />}
<StoryImage
attachment={newestStory.attachment}
2023-03-30 00:03:25 +00:00
firstName={i18n('icu:you')}
2022-08-19 18:36:47 +00:00
i18n={i18n}
isMe
isThumbnail
label=""
moduleClassName="StoryListItem__previews--image"
queueStoryDownload={queueStoryDownload}
storyId={newestStory.messageId}
2023-02-24 23:18:57 +00:00
onMediaPlaybackStart={onMediaPlaybackStart}
2022-08-19 18:36:47 +00:00
/>
2022-07-01 00:52:03 +00:00
</div>
</div>
2022-07-01 00:52:03 +00:00
</div>
);
2022-11-18 00:45:19 +00:00
}