signal-desktop/ts/components/StoriesPane.tsx

198 lines
5.6 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
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';
import type {
ConversationStoryType,
MyStoryType,
StoryViewType,
} from '../types/Stories';
2022-03-04 21:14:52 +00:00
import type { LocalizerType } from '../types/Util';
2022-07-01 00:52:03 +00:00
import { MyStoriesButton } from './MyStoriesButton';
2022-03-04 21:14:52 +00:00
import { SearchInput } from './SearchInput';
import { StoryListItem } from './StoryListItem';
2022-06-17 00:48:57 +00:00
import { isNotNil } from '../util/isNotNil';
2022-03-04 21:14:52 +00:00
const FUSE_OPTIONS: Fuse.IFuseOptions<ConversationStoryType> = {
2022-07-06 19:06:20 +00:00
getFn: (story, path) => {
2022-03-04 21:14:52 +00:00
if (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> {
return new Fuse<ConversationStoryType>(stories, FUSE_OPTIONS)
.search(searchTerm)
.map(result => result.item);
2022-03-04 21:14:52 +00:00
}
2022-07-06 19:06:20 +00:00
function getNewestMyStory(story: MyStoryType): StoryViewType {
2022-03-29 01:10:08 +00:00
return story.stories[story.stories.length - 1];
}
2022-03-04 21:14:52 +00:00
export type PropsType = {
hiddenStories: Array<ConversationStoryType>;
i18n: LocalizerType;
2022-07-01 00:52:03 +00:00
me: ConversationType;
myStories: Array<MyStoryType>;
2022-06-17 00:48:57 +00:00
onAddStory: () => unknown;
2022-07-01 00:52:03 +00:00
onMyStoriesClicked: () => unknown;
2022-03-04 21:14:52 +00:00
onStoryClicked: (conversationId: string) => unknown;
2022-03-29 01:10:08 +00:00
queueStoryDownload: (storyId: string) => unknown;
2022-06-16 19:12:50 +00:00
showConversation: ShowConversationType;
2022-03-04 21:14:52 +00:00
stories: Array<ConversationStoryType>;
toggleHideStories: (conversationId: string) => unknown;
toggleStoriesView: () => unknown;
2022-03-04 21:14:52 +00:00
};
export const StoriesPane = ({
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-03-04 21:14:52 +00:00
onStoryClicked,
2022-03-29 01:10:08 +00:00
queueStoryDownload,
2022-06-16 19:12:50 +00:00
showConversation,
2022-03-04 21:14:52 +00:00
stories,
toggleHideStories,
toggleStoriesView,
2022-03-04 21:14:52 +00:00
}: PropsType): JSX.Element => {
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]);
return (
<>
<div className="Stories__pane__header">
<button
aria-label={i18n('back')}
className="Stories__pane__header--back"
onClick={toggleStoriesView}
tabIndex={0}
type="button"
2022-03-04 21:14:52 +00:00
/>
<div className="Stories__pane__header--title">
{i18n('Stories__title')}
2022-03-04 21:14:52 +00:00
</div>
2022-06-17 00:48:57 +00:00
<button
aria-label={i18n('Stories__add')}
className="Stories__pane__header--camera"
onClick={onAddStory}
type="button"
/>
2022-03-04 21:14:52 +00:00
</div>
<SearchInput
i18n={i18n}
moduleClassName="Stories__search"
onChange={event => {
setSearchTerm(event.target.value);
}}
placeholder={i18n('search')}
value={searchTerm}
/>
2022-07-01 00:52:03 +00:00
<MyStoriesButton
hasMultiple={myStories.length ? myStories[0].stories.length > 1 : false}
i18n={i18n}
me={me}
newestStory={
2022-07-06 19:06:20 +00:00
myStories.length ? getNewestMyStory(myStories[0]) : undefined
2022-07-01 00:52:03 +00:00
}
onClick={onMyStoriesClicked}
queueStoryDownload={queueStoryDownload}
/>
<div
className={classNames('Stories__pane__list', {
'Stories__pane__list--empty': !stories.length,
})}
>
{renderedStories.map(story => (
<StoryListItem
2022-04-15 00:08:46 +00:00
group={story.group}
i18n={i18n}
2022-07-06 19:06:20 +00:00
key={story.storyView.timestamp}
onClick={() => {
onStoryClicked(story.conversationId);
}}
onHideStory={toggleHideStories}
onGoToConversation={conversationId => {
2022-06-16 19:12:50 +00:00
showConversation({ conversationId });
toggleStoriesView();
}}
queueStoryDownload={queueStoryDownload}
2022-07-06 19:06:20 +00:00
story={story.storyView}
/>
))}
2022-04-15 22:31:18 +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
2022-07-06 19:06:20 +00:00
key={story.storyView.timestamp}
2022-04-15 22:31:18 +00:00
i18n={i18n}
isHidden
onClick={() => {
onStoryClicked(story.conversationId);
}}
onHideStory={toggleHideStories}
2022-04-15 22:31:18 +00:00
onGoToConversation={conversationId => {
2022-06-16 19:12:50 +00:00
showConversation({ conversationId });
toggleStoriesView();
2022-04-15 22:31:18 +00:00
}}
queueStoryDownload={queueStoryDownload}
2022-07-06 19:06:20 +00:00
story={story.storyView}
2022-04-15 22:31:18 +00:00
/>
))}
</>
)}
{!stories.length && i18n('Stories__list-empty')}
</div>
</>
2022-03-04 21:14:52 +00:00
);
};