signal-desktop/ts/components/Stories.tsx

163 lines
5 KiB
TypeScript
Raw Normal View History

2022-03-04 21:14:52 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2022-07-06 19:06:20 +00:00
import React, { useState } from 'react';
2022-03-04 21:14:52 +00:00
import classNames from 'classnames';
2022-07-01 00:52:03 +00:00
import type {
ConversationType,
ShowConversationType,
} from '../state/ducks/conversations';
import type {
ConversationStoryType,
MyStoryType,
StoryViewType,
} from '../types/Stories';
2022-03-04 21:14:52 +00:00
import type { LocalizerType } from '../types/Util';
import type { PreferredBadgeSelectorType } from '../state/selectors/badges';
import type { ShowToastAction } from '../state/ducks/toast';
2022-08-22 17:44:23 +00:00
import type {
AddStoryData,
2022-08-22 17:44:23 +00:00
ViewUserStoriesActionCreatorType,
ViewStoryActionCreatorType,
} from '../state/ducks/stories';
2022-07-01 00:52:03 +00:00
import { MyStories } from './MyStories';
2022-03-04 21:14:52 +00:00
import { StoriesPane } from './StoriesPane';
import { Theme, themeClassName } from '../util/theme';
import { getWidthFromPreferredWidth } from '../util/leftPaneWidth';
import { useEscapeHandling } from '../hooks/useEscapeHandling';
2022-03-04 21:14:52 +00:00
export type PropsType = {
addStoryData: AddStoryData;
2022-07-01 00:52:03 +00:00
deleteStoryForEveryone: (story: StoryViewType) => unknown;
getPreferredBadge: PreferredBadgeSelectorType;
2022-11-09 02:38:19 +00:00
hasViewReceiptSetting: boolean;
2022-03-04 21:14:52 +00:00
hiddenStories: Array<ConversationStoryType>;
i18n: LocalizerType;
2022-11-09 02:38:19 +00:00
isStoriesSettingsVisible: boolean;
isViewingStory: boolean;
maxAttachmentSizeInKb: number;
2022-07-01 00:52:03 +00:00
me: ConversationType;
myStories: Array<MyStoryType>;
onForwardStory: (storyId: string) => unknown;
onSaveStory: (story: StoryViewType) => unknown;
2023-02-24 23:18:57 +00:00
onMediaPlaybackStart: () => void;
2022-03-04 21:14:52 +00:00
preferredWidthFromStorage: number;
2022-03-29 01:10:08 +00:00
queueStoryDownload: (storyId: string) => unknown;
renderStoryCreator: () => JSX.Element;
retryMessageSend: (messageId: string) => unknown;
setAddStoryData: (data: AddStoryData) => unknown;
2022-06-16 19:12:50 +00:00
showConversation: ShowConversationType;
showStoriesSettings: () => unknown;
showToast: ShowToastAction;
2022-03-04 21:14:52 +00:00
stories: Array<ConversationStoryType>;
toggleHideStories: (conversationId: string) => unknown;
toggleStoriesView: () => unknown;
2022-07-06 19:06:20 +00:00
viewStory: ViewStoryActionCreatorType;
2022-11-09 02:38:19 +00:00
viewUserStories: ViewUserStoriesActionCreatorType;
2022-03-04 21:14:52 +00:00
};
export const STORIES_COLOR_THEME = Theme.Dark;
2022-11-18 00:45:19 +00:00
export function Stories({
addStoryData,
2022-07-01 00:52:03 +00:00
deleteStoryForEveryone,
getPreferredBadge,
2022-11-09 02:38:19 +00:00
hasViewReceiptSetting,
2022-03-04 21:14:52 +00:00
hiddenStories,
i18n,
2022-11-09 02:38:19 +00:00
isStoriesSettingsVisible,
isViewingStory,
maxAttachmentSizeInKb,
2022-07-01 00:52:03 +00:00
me,
myStories,
onForwardStory,
onSaveStory,
2023-02-24 23:18:57 +00:00
onMediaPlaybackStart,
2022-03-04 21:14:52 +00:00
preferredWidthFromStorage,
2022-03-29 01:10:08 +00:00
queueStoryDownload,
2022-06-17 00:48:57 +00:00
renderStoryCreator,
retryMessageSend,
setAddStoryData,
2022-06-16 19:12:50 +00:00
showConversation,
showStoriesSettings,
showToast,
2022-03-04 21:14:52 +00:00
stories,
toggleHideStories,
toggleStoriesView,
2022-07-06 19:06:20 +00:00
viewStory,
2022-11-09 02:38:19 +00:00
viewUserStories,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element {
2022-03-04 21:14:52 +00:00
const width = getWidthFromPreferredWidth(preferredWidthFromStorage, {
requiresFullWidth: true,
});
2022-07-01 00:52:03 +00:00
const [isMyStories, setIsMyStories] = useState(false);
2022-06-17 00:48:57 +00:00
// only handle ESC if not showing a child that handles their own ESC
useEscapeHandling(
(isMyStories && myStories.length) ||
isViewingStory ||
isStoriesSettingsVisible ||
addStoryData
? undefined
: toggleStoriesView
);
2022-03-04 21:14:52 +00:00
return (
<div className={classNames('Stories', themeClassName(STORIES_COLOR_THEME))}>
{addStoryData && renderStoryCreator()}
2022-08-04 19:23:24 +00:00
<div className="Stories__pane" style={{ width }}>
{isMyStories && myStories.length ? (
<MyStories
2022-11-16 22:10:11 +00:00
hasViewReceiptSetting={hasViewReceiptSetting}
2022-08-04 19:23:24 +00:00
i18n={i18n}
myStories={myStories}
onBack={() => setIsMyStories(false)}
onDelete={deleteStoryForEveryone}
onForward={onForwardStory}
onSave={onSaveStory}
2023-02-24 23:18:57 +00:00
onMediaPlaybackStart={onMediaPlaybackStart}
2022-08-04 19:23:24 +00:00
queueStoryDownload={queueStoryDownload}
retryMessageSend={retryMessageSend}
2022-08-04 19:23:24 +00:00
viewStory={viewStory}
/>
) : (
<StoriesPane
getPreferredBadge={getPreferredBadge}
hiddenStories={hiddenStories}
i18n={i18n}
maxAttachmentSizeInKb={maxAttachmentSizeInKb}
2022-08-04 19:23:24 +00:00
me={me}
myStories={myStories}
onAddStory={file =>
file
? setAddStoryData({ type: 'Media', file })
: setAddStoryData({ type: 'Text' })
}
onMyStoriesClicked={() => {
if (myStories.length) {
setIsMyStories(true);
} else {
setAddStoryData({ type: 'Text' });
}
}}
onStoriesSettings={showStoriesSettings}
2023-02-24 23:18:57 +00:00
onMediaPlaybackStart={onMediaPlaybackStart}
2022-08-04 19:23:24 +00:00
queueStoryDownload={queueStoryDownload}
showConversation={showConversation}
showToast={showToast}
2022-08-04 19:23:24 +00:00
stories={stories}
toggleHideStories={toggleHideStories}
toggleStoriesView={toggleStoriesView}
viewUserStories={viewUserStories}
/>
)}
</div>
2022-03-04 21:14:52 +00:00
<div className="Stories__placeholder">
<div className="Stories__placeholder__stories" />
2023-03-30 00:03:25 +00:00
{i18n('icu:Stories__placeholder--text')}
2022-03-04 21:14:52 +00:00
</div>
</div>
);
2022-11-18 00:45:19 +00:00
}