// 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'; import type { ConversationType, ShowConversationType, } from '../state/ducks/conversations'; import type { ConversationStoryType, MyStoryType, StoryViewType, } from '../types/Stories'; import type { LocalizerType } from '../types/Util'; import type { PreferredBadgeSelectorType } from '../state/selectors/badges'; import type { ShowToastActionCreatorType } from '../state/ducks/toast'; import type { ViewUserStoriesActionCreatorType } from '../state/ducks/stories'; import { ContextMenu } from './ContextMenu'; import { MyStoryButton } from './MyStoryButton'; import { SearchInput } from './SearchInput'; import { StoriesAddStoryButton } from './StoriesAddStoryButton'; import { StoryListItem } from './StoryListItem'; import { Theme } from '../util/theme'; import { isNotNil } from '../util/isNotNil'; import { useRestoreFocus } from '../hooks/useRestoreFocus'; const FUSE_OPTIONS: Fuse.IFuseOptions = { getFn: (story, path) => { if (path[0] === 'searchNames' || path === 'searchNames') { return [story.storyView.sender.title, story.storyView.sender.name].filter( isNotNil ); } return story.group?.title ?? ''; }, keys: [ { name: 'searchNames', weight: 1, }, { name: 'group', weight: 1, }, ], threshold: 0.1, }; function search( stories: ReadonlyArray, searchTerm: string ): Array { return new Fuse(stories, FUSE_OPTIONS) .search(searchTerm) .map(result => result.item); } function getNewestMyStory(story: MyStoryType): StoryViewType { return story.stories[0]; } export type PropsType = { getPreferredBadge: PreferredBadgeSelectorType; hiddenStories: Array; i18n: LocalizerType; me: ConversationType; myStories: Array; onAddStory: (file?: File) => unknown; onMyStoriesClicked: () => unknown; onStoriesSettings: () => unknown; queueStoryDownload: (storyId: string) => unknown; showConversation: ShowConversationType; showToast: ShowToastActionCreatorType; stories: Array; toggleHideStories: (conversationId: string) => unknown; toggleStoriesView: () => unknown; viewUserStories: ViewUserStoriesActionCreatorType; }; export const StoriesPane = ({ getPreferredBadge, hiddenStories, i18n, me, myStories, onAddStory, onMyStoriesClicked, onStoriesSettings, queueStoryDownload, showConversation, showToast, stories, toggleHideStories, toggleStoriesView, viewUserStories, }: PropsType): JSX.Element => { const [searchTerm, setSearchTerm] = useState(''); const [isShowingHiddenStories, setIsShowingHiddenStories] = useState(false); const [renderedStories, setRenderedStories] = useState>(stories); useEffect(() => { if (searchTerm) { setRenderedStories(search(stories, searchTerm)); } else { setRenderedStories(stories); } }, [searchTerm, stories]); const [focusRef] = useRestoreFocus(); return ( <>
{ setSearchTerm(event.target.value); }} placeholder={i18n('search')} value={searchTerm} />
<> 1 : false } i18n={i18n} me={me} newestStory={ myStories.length ? getNewestMyStory(myStories[0]) : undefined } onAddStory={onAddStory} onClick={onMyStoriesClicked} queueStoryDownload={queueStoryDownload} showToast={showToast} /> {renderedStories.map(story => ( { showConversation({ conversationId }); toggleStoriesView(); }} onHideStory={toggleHideStories} queueStoryDownload={queueStoryDownload} story={story.storyView} viewUserStories={viewUserStories} /> ))} {Boolean(hiddenStories.length) && ( <> {isShowingHiddenStories && hiddenStories.map(story => ( { showConversation({ conversationId }); toggleStoriesView(); }} onHideStory={toggleHideStories} queueStoryDownload={queueStoryDownload} story={story.storyView} viewUserStories={viewUserStories} /> ))} )} {!stories.length && (
{i18n('Stories__list-empty')}
)}
); };