2022-03-04 21:14:52 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-04-12 14:45:20 +00:00
|
|
|
import FocusTrap from 'focus-trap-react';
|
2022-04-20 23:38:38 +00:00
|
|
|
import React, { useCallback, useState } from 'react';
|
2022-03-04 21:14:52 +00:00
|
|
|
import classNames from 'classnames';
|
2022-04-15 00:08:46 +00:00
|
|
|
import type { ConversationStoryType } from './StoryListItem';
|
2022-03-04 21:14:52 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
|
|
|
import type { PropsType as SmartStoryViewerPropsType } from '../state/smart/StoryViewer';
|
|
|
|
import { StoriesPane } from './StoriesPane';
|
|
|
|
import { Theme, themeClassName } from '../util/theme';
|
|
|
|
import { getWidthFromPreferredWidth } from '../util/leftPaneWidth';
|
|
|
|
|
|
|
|
export type PropsType = {
|
|
|
|
hiddenStories: Array<ConversationStoryType>;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
preferredWidthFromStorage: number;
|
|
|
|
openConversationInternal: (_: { conversationId: string }) => unknown;
|
|
|
|
renderStoryViewer: (props: SmartStoryViewerPropsType) => JSX.Element;
|
2022-03-29 01:10:08 +00:00
|
|
|
queueStoryDownload: (storyId: string) => unknown;
|
2022-03-04 21:14:52 +00:00
|
|
|
stories: Array<ConversationStoryType>;
|
|
|
|
toggleHideStories: (conversationId: string) => unknown;
|
|
|
|
toggleStoriesView: () => unknown;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Stories = ({
|
|
|
|
hiddenStories,
|
|
|
|
i18n,
|
|
|
|
openConversationInternal,
|
|
|
|
preferredWidthFromStorage,
|
2022-03-29 01:10:08 +00:00
|
|
|
queueStoryDownload,
|
2022-03-04 21:14:52 +00:00
|
|
|
renderStoryViewer,
|
|
|
|
stories,
|
|
|
|
toggleHideStories,
|
|
|
|
toggleStoriesView,
|
|
|
|
}: PropsType): JSX.Element => {
|
2022-04-15 00:08:46 +00:00
|
|
|
const [conversationIdToView, setConversationIdToView] = useState<
|
|
|
|
undefined | string
|
2022-03-04 21:14:52 +00:00
|
|
|
>();
|
|
|
|
|
|
|
|
const width = getWidthFromPreferredWidth(preferredWidthFromStorage, {
|
|
|
|
requiresFullWidth: true,
|
|
|
|
});
|
|
|
|
|
2022-04-20 23:38:38 +00:00
|
|
|
const onNextUserStories = useCallback(() => {
|
|
|
|
const storyIndex = stories.findIndex(
|
|
|
|
x => x.conversationId === conversationIdToView
|
|
|
|
);
|
|
|
|
if (storyIndex >= stories.length - 1 || storyIndex === -1) {
|
|
|
|
setConversationIdToView(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const nextStory = stories[storyIndex + 1];
|
|
|
|
setConversationIdToView(nextStory.conversationId);
|
|
|
|
}, [conversationIdToView, stories]);
|
|
|
|
|
|
|
|
const onPrevUserStories = useCallback(() => {
|
|
|
|
const storyIndex = stories.findIndex(
|
|
|
|
x => x.conversationId === conversationIdToView
|
|
|
|
);
|
|
|
|
if (storyIndex <= 0) {
|
|
|
|
setConversationIdToView(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const prevStory = stories[storyIndex - 1];
|
|
|
|
setConversationIdToView(prevStory.conversationId);
|
|
|
|
}, [conversationIdToView, stories]);
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
return (
|
|
|
|
<div className={classNames('Stories', themeClassName(Theme.Dark))}>
|
2022-04-15 00:08:46 +00:00
|
|
|
{conversationIdToView &&
|
2022-03-04 21:14:52 +00:00
|
|
|
renderStoryViewer({
|
2022-04-15 00:08:46 +00:00
|
|
|
conversationId: conversationIdToView,
|
|
|
|
onClose: () => setConversationIdToView(undefined),
|
2022-04-20 23:38:38 +00:00
|
|
|
onNextUserStories,
|
|
|
|
onPrevUserStories,
|
2022-03-04 21:14:52 +00:00
|
|
|
})}
|
2022-04-15 00:08:46 +00:00
|
|
|
<FocusTrap focusTrapOptions={{ allowOutsideClick: true }}>
|
|
|
|
<div className="Stories__pane" style={{ width }}>
|
2022-04-12 14:45:20 +00:00
|
|
|
<StoriesPane
|
|
|
|
hiddenStories={hiddenStories}
|
|
|
|
i18n={i18n}
|
|
|
|
onBack={toggleStoriesView}
|
2022-04-15 22:31:18 +00:00
|
|
|
onStoryClicked={setConversationIdToView}
|
2022-04-12 14:45:20 +00:00
|
|
|
openConversationInternal={openConversationInternal}
|
|
|
|
queueStoryDownload={queueStoryDownload}
|
|
|
|
stories={stories}
|
|
|
|
toggleHideStories={toggleHideStories}
|
|
|
|
/>
|
2022-04-15 00:08:46 +00:00
|
|
|
</div>
|
|
|
|
</FocusTrap>
|
2022-03-04 21:14:52 +00:00
|
|
|
<div className="Stories__placeholder">
|
|
|
|
<div className="Stories__placeholder__stories" />
|
|
|
|
{i18n('Stories__placeholder--text')}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|