2022-03-04 21:14:52 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import Fuse from 'fuse.js';
|
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import classNames from 'classnames';
|
2022-06-17 00:48:57 +00:00
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
import type {
|
|
|
|
ConversationType,
|
|
|
|
ShowConversationType,
|
|
|
|
} from '../state/ducks/conversations';
|
2022-11-16 22:10:11 +00:00
|
|
|
import type { ConversationStoryType, MyStoryType } from '../types/Stories';
|
2022-03-04 21:14:52 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2022-07-29 00:16:30 +00:00
|
|
|
import type { PreferredBadgeSelectorType } from '../state/selectors/badges';
|
2022-08-12 23:44:10 +00:00
|
|
|
import type { ShowToastActionCreatorType } from '../state/ducks/toast';
|
2022-08-22 17:44:23 +00:00
|
|
|
import type { ViewUserStoriesActionCreatorType } from '../state/ducks/stories';
|
2022-07-21 00:07:09 +00:00
|
|
|
import { ContextMenu } from './ContextMenu';
|
2022-11-10 21:01:40 +00:00
|
|
|
import { MyStoryButton } from './MyStoryButton';
|
2022-03-04 21:14:52 +00:00
|
|
|
import { SearchInput } from './SearchInput';
|
2022-08-19 18:36:47 +00:00
|
|
|
import { StoriesAddStoryButton } from './StoriesAddStoryButton';
|
2022-03-04 21:14:52 +00:00
|
|
|
import { StoryListItem } from './StoryListItem';
|
2022-07-21 00:07:09 +00:00
|
|
|
import { Theme } from '../util/theme';
|
2022-06-17 00:48:57 +00:00
|
|
|
import { isNotNil } from '../util/isNotNil';
|
2022-10-13 16:09:21 +00:00
|
|
|
import { useRestoreFocus } from '../hooks/useRestoreFocus';
|
2022-03-04 21:14:52 +00:00
|
|
|
|
2022-04-05 00:38:22 +00:00
|
|
|
const FUSE_OPTIONS: Fuse.IFuseOptions<ConversationStoryType> = {
|
2022-07-06 19:06:20 +00:00
|
|
|
getFn: (story, path) => {
|
2022-10-19 22:56:55 +00:00
|
|
|
if (path[0] === 'searchNames' || path === 'searchNames') {
|
2022-07-06 19:06:20 +00:00
|
|
|
return [story.storyView.sender.title, story.storyView.sender.name].filter(
|
|
|
|
isNotNil
|
|
|
|
);
|
2022-03-04 21:14:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 19:06:20 +00:00
|
|
|
return story.group?.title ?? '';
|
2022-03-04 21:14:52 +00:00
|
|
|
},
|
|
|
|
keys: [
|
|
|
|
{
|
|
|
|
name: 'searchNames',
|
|
|
|
weight: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'group',
|
|
|
|
weight: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
threshold: 0.1,
|
|
|
|
};
|
|
|
|
|
|
|
|
function search(
|
|
|
|
stories: ReadonlyArray<ConversationStoryType>,
|
|
|
|
searchTerm: string
|
|
|
|
): Array<ConversationStoryType> {
|
2022-04-05 00:38:22 +00:00
|
|
|
return new Fuse<ConversationStoryType>(stories, FUSE_OPTIONS)
|
|
|
|
.search(searchTerm)
|
|
|
|
.map(result => result.item);
|
2022-03-04 21:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type PropsType = {
|
2022-07-29 00:16:30 +00:00
|
|
|
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>;
|
2022-08-04 19:23:24 +00:00
|
|
|
onAddStory: (file?: File) => unknown;
|
2022-07-01 00:52:03 +00:00
|
|
|
onMyStoriesClicked: () => unknown;
|
2022-07-21 00:07:09 +00:00
|
|
|
onStoriesSettings: () => unknown;
|
2022-03-29 01:10:08 +00:00
|
|
|
queueStoryDownload: (storyId: string) => unknown;
|
2022-06-16 19:12:50 +00:00
|
|
|
showConversation: ShowConversationType;
|
2022-08-12 23:44:10 +00:00
|
|
|
showToast: ShowToastActionCreatorType;
|
2022-03-04 21:14:52 +00:00
|
|
|
stories: Array<ConversationStoryType>;
|
|
|
|
toggleHideStories: (conversationId: string) => unknown;
|
2022-04-29 17:43:24 +00:00
|
|
|
toggleStoriesView: () => unknown;
|
2022-08-22 17:44:23 +00:00
|
|
|
viewUserStories: ViewUserStoriesActionCreatorType;
|
2022-03-04 21:14:52 +00:00
|
|
|
};
|
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
export function StoriesPane({
|
2022-07-29 00:16:30 +00:00
|
|
|
getPreferredBadge,
|
2022-04-15 22:31:18 +00:00
|
|
|
hiddenStories,
|
2022-03-04 21:14:52 +00:00
|
|
|
i18n,
|
2022-07-01 00:52:03 +00:00
|
|
|
me,
|
|
|
|
myStories,
|
2022-06-17 00:48:57 +00:00
|
|
|
onAddStory,
|
2022-07-01 00:52:03 +00:00
|
|
|
onMyStoriesClicked,
|
2022-07-21 00:07:09 +00:00
|
|
|
onStoriesSettings,
|
2022-03-29 01:10:08 +00:00
|
|
|
queueStoryDownload,
|
2022-06-16 19:12:50 +00:00
|
|
|
showConversation,
|
2022-08-12 23:44:10 +00:00
|
|
|
showToast,
|
2022-03-04 21:14:52 +00:00
|
|
|
stories,
|
|
|
|
toggleHideStories,
|
2022-04-29 17:43:24 +00:00
|
|
|
toggleStoriesView,
|
2022-07-25 18:55:44 +00:00
|
|
|
viewUserStories,
|
2022-11-18 00:45:19 +00:00
|
|
|
}: PropsType): JSX.Element {
|
2022-03-04 21:14:52 +00:00
|
|
|
const [searchTerm, setSearchTerm] = useState('');
|
2022-04-15 22:31:18 +00:00
|
|
|
const [isShowingHiddenStories, setIsShowingHiddenStories] = useState(false);
|
2022-03-04 21:14:52 +00:00
|
|
|
const [renderedStories, setRenderedStories] =
|
|
|
|
useState<Array<ConversationStoryType>>(stories);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (searchTerm) {
|
|
|
|
setRenderedStories(search(stories, searchTerm));
|
|
|
|
} else {
|
|
|
|
setRenderedStories(stories);
|
|
|
|
}
|
|
|
|
}, [searchTerm, stories]);
|
|
|
|
|
2022-10-13 16:09:21 +00:00
|
|
|
const [focusRef] = useRestoreFocus();
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
return (
|
2022-04-12 14:45:20 +00:00
|
|
|
<>
|
|
|
|
<div className="Stories__pane__header">
|
|
|
|
<button
|
2022-10-13 16:09:21 +00:00
|
|
|
ref={focusRef}
|
2022-04-12 14:45:20 +00:00
|
|
|
aria-label={i18n('back')}
|
|
|
|
className="Stories__pane__header--back"
|
2022-04-29 17:43:24 +00:00
|
|
|
onClick={toggleStoriesView}
|
2022-04-12 14:45:20 +00:00
|
|
|
tabIndex={0}
|
|
|
|
type="button"
|
2022-03-04 21:14:52 +00:00
|
|
|
/>
|
2022-04-12 14:45:20 +00:00
|
|
|
<div className="Stories__pane__header--title">
|
|
|
|
{i18n('Stories__title')}
|
2022-03-04 21:14:52 +00:00
|
|
|
</div>
|
2022-08-19 18:36:47 +00:00
|
|
|
<StoriesAddStoryButton
|
2022-08-04 19:23:24 +00:00
|
|
|
i18n={i18n}
|
|
|
|
moduleClassName="Stories__pane__add-story"
|
2022-08-19 18:36:47 +00:00
|
|
|
onAddStory={onAddStory}
|
|
|
|
showToast={showToast}
|
2022-06-17 00:48:57 +00:00
|
|
|
/>
|
2022-07-21 00:07:09 +00:00
|
|
|
<ContextMenu
|
|
|
|
i18n={i18n}
|
|
|
|
menuOptions={[
|
|
|
|
{
|
|
|
|
label: i18n('StoriesSettings__context-menu'),
|
2022-08-04 19:23:24 +00:00
|
|
|
onClick: () => onStoriesSettings(),
|
2022-07-21 00:07:09 +00:00
|
|
|
},
|
|
|
|
]}
|
2022-07-25 18:55:44 +00:00
|
|
|
moduleClassName="Stories__pane__settings"
|
2022-07-21 00:07:09 +00:00
|
|
|
popperOptions={{
|
|
|
|
placement: 'bottom',
|
|
|
|
strategy: 'absolute',
|
|
|
|
}}
|
|
|
|
theme={Theme.Dark}
|
|
|
|
/>
|
2022-03-04 21:14:52 +00:00
|
|
|
</div>
|
2022-04-12 14:45:20 +00:00
|
|
|
<SearchInput
|
|
|
|
i18n={i18n}
|
|
|
|
moduleClassName="Stories__search"
|
|
|
|
onChange={event => {
|
|
|
|
setSearchTerm(event.target.value);
|
|
|
|
}}
|
|
|
|
placeholder={i18n('search')}
|
|
|
|
value={searchTerm}
|
|
|
|
/>
|
2022-07-29 19:26:40 +00:00
|
|
|
<div className="Stories__pane__list">
|
2022-11-18 00:45:19 +00:00
|
|
|
<MyStoryButton
|
|
|
|
i18n={i18n}
|
|
|
|
me={me}
|
|
|
|
myStories={myStories}
|
|
|
|
onAddStory={onAddStory}
|
|
|
|
onClick={onMyStoriesClicked}
|
|
|
|
queueStoryDownload={queueStoryDownload}
|
|
|
|
showToast={showToast}
|
|
|
|
/>
|
|
|
|
{renderedStories.map(story => (
|
|
|
|
<StoryListItem
|
|
|
|
conversationId={story.conversationId}
|
|
|
|
getPreferredBadge={getPreferredBadge}
|
|
|
|
hasReplies={story.hasReplies}
|
|
|
|
hasRepliesFromSelf={story.hasRepliesFromSelf}
|
|
|
|
group={story.group}
|
2022-04-12 14:45:20 +00:00
|
|
|
i18n={i18n}
|
2022-11-18 00:45:19 +00:00
|
|
|
key={story.storyView.timestamp}
|
|
|
|
onGoToConversation={conversationId => {
|
|
|
|
showConversation({ conversationId });
|
|
|
|
toggleStoriesView();
|
|
|
|
}}
|
|
|
|
onHideStory={toggleHideStories}
|
2022-04-12 14:45:20 +00:00
|
|
|
queueStoryDownload={queueStoryDownload}
|
2022-11-18 00:45:19 +00:00
|
|
|
story={story.storyView}
|
|
|
|
viewUserStories={viewUserStories}
|
2022-04-12 14:45:20 +00:00
|
|
|
/>
|
2022-11-18 00:45:19 +00:00
|
|
|
))}
|
|
|
|
{Boolean(hiddenStories.length) && (
|
|
|
|
<>
|
|
|
|
<button
|
|
|
|
className={classNames('Stories__hidden-stories', {
|
|
|
|
'Stories__hidden-stories--expanded': isShowingHiddenStories,
|
|
|
|
})}
|
|
|
|
onClick={() => setIsShowingHiddenStories(!isShowingHiddenStories)}
|
|
|
|
type="button"
|
|
|
|
>
|
|
|
|
{i18n('Stories__hidden-stories')}
|
|
|
|
</button>
|
|
|
|
{isShowingHiddenStories &&
|
|
|
|
hiddenStories.map(story => (
|
|
|
|
<StoryListItem
|
|
|
|
conversationId={story.conversationId}
|
|
|
|
getPreferredBadge={getPreferredBadge}
|
|
|
|
group={story.group}
|
|
|
|
i18n={i18n}
|
|
|
|
isHidden
|
|
|
|
key={story.storyView.timestamp}
|
|
|
|
onGoToConversation={conversationId => {
|
|
|
|
showConversation({ conversationId });
|
|
|
|
toggleStoriesView();
|
|
|
|
}}
|
|
|
|
onHideStory={toggleHideStories}
|
|
|
|
queueStoryDownload={queueStoryDownload}
|
|
|
|
story={story.storyView}
|
|
|
|
viewUserStories={viewUserStories}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{!stories.length && (
|
|
|
|
<div className="Stories__pane__list--empty">
|
|
|
|
{i18n('Stories__list-empty')}
|
|
|
|
</div>
|
|
|
|
)}
|
2022-04-12 14:45:20 +00:00
|
|
|
</div>
|
|
|
|
</>
|
2022-03-04 21:14:52 +00:00
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|