signal-desktop/ts/components/Stories.tsx

142 lines
4.4 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';
2022-06-17 00:48:57 +00:00
import type { PropsType as SmartStoryCreatorPropsType } from '../state/smart/StoryCreator';
import type { ShowToastActionCreatorType } from '../state/ducks/toast';
2022-08-22 17:44:23 +00:00
import type {
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';
export type PropsType = {
2022-07-01 00:52:03 +00:00
deleteStoryForEveryone: (story: StoryViewType) => unknown;
getPreferredBadge: PreferredBadgeSelectorType;
2022-03-04 21:14:52 +00:00
hiddenStories: Array<ConversationStoryType>;
i18n: LocalizerType;
2022-07-01 00:52:03 +00:00
me: ConversationType;
myStories: Array<MyStoryType>;
onForwardStory: (storyId: string) => unknown;
onSaveStory: (story: StoryViewType) => unknown;
2022-03-04 21:14:52 +00:00
preferredWidthFromStorage: number;
2022-03-29 01:10:08 +00:00
queueStoryDownload: (storyId: string) => unknown;
2022-06-17 00:48:57 +00:00
renderStoryCreator: (props: SmartStoryCreatorPropsType) => JSX.Element;
2022-06-16 19:12:50 +00:00
showConversation: ShowConversationType;
showStoriesSettings: () => unknown;
showToast: ShowToastActionCreatorType;
2022-03-04 21:14:52 +00:00
stories: Array<ConversationStoryType>;
toggleHideStories: (conversationId: string) => unknown;
toggleStoriesView: () => unknown;
2022-08-22 17:44:23 +00:00
viewUserStories: ViewUserStoriesActionCreatorType;
2022-07-06 19:06:20 +00:00
viewStory: ViewStoryActionCreatorType;
2022-03-04 21:14:52 +00:00
};
2022-08-04 19:23:24 +00:00
type AddStoryType =
| {
type: 'Media';
file: File;
}
| { type: 'Text' }
| undefined;
2022-03-04 21:14:52 +00:00
export const Stories = ({
2022-07-01 00:52:03 +00:00
deleteStoryForEveryone,
getPreferredBadge,
2022-03-04 21:14:52 +00:00
hiddenStories,
i18n,
2022-07-01 00:52:03 +00:00
me,
myStories,
onForwardStory,
onSaveStory,
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,
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
viewUserStories,
viewStory,
2022-03-04 21:14:52 +00:00
}: PropsType): JSX.Element => {
const width = getWidthFromPreferredWidth(preferredWidthFromStorage, {
requiresFullWidth: true,
});
2022-08-04 19:23:24 +00:00
const [addStoryData, setAddStoryData] = useState<AddStoryType>();
2022-07-01 00:52:03 +00:00
const [isMyStories, setIsMyStories] = useState(false);
2022-06-17 00:48:57 +00:00
2022-03-04 21:14:52 +00:00
return (
<div className={classNames('Stories', themeClassName(Theme.Dark))}>
2022-08-04 19:23:24 +00:00
{addStoryData &&
2022-06-17 00:48:57 +00:00
renderStoryCreator({
2022-08-04 19:23:24 +00:00
file: addStoryData.type === 'Media' ? addStoryData.file : undefined,
onClose: () => setAddStoryData(undefined),
2022-06-17 00:48:57 +00:00
})}
2022-08-04 19:23:24 +00:00
<div className="Stories__pane" style={{ width }}>
{isMyStories && myStories.length ? (
<MyStories
i18n={i18n}
myStories={myStories}
onBack={() => setIsMyStories(false)}
onDelete={deleteStoryForEveryone}
onForward={onForwardStory}
onSave={onSaveStory}
queueStoryDownload={queueStoryDownload}
viewStory={viewStory}
/>
) : (
<StoriesPane
getPreferredBadge={getPreferredBadge}
hiddenStories={hiddenStories}
i18n={i18n}
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}
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" />
{i18n('Stories__placeholder--text')}
</div>
</div>
);
};