// Copyright 2022 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import type { FuseOptions } from 'fuse.js'; import Fuse from 'fuse.js'; import React, { useEffect, useState } from 'react'; import classNames from 'classnames'; import type { ConversationStoryType, StoryViewType } from './StoryListItem'; import type { LocalizerType } from '../types/Util'; import { SearchInput } from './SearchInput'; import { StoryListItem } from './StoryListItem'; const FUSE_OPTIONS: FuseOptions = { getFn: (obj, path) => { if (path === 'searchNames') { return obj.stories.flatMap((story: StoryViewType) => [ story.sender.title, story.sender.name, ]); } return obj.group?.title; }, keys: [ { name: 'searchNames', weight: 1, }, { name: 'group', weight: 1, }, ], threshold: 0.1, tokenize: true, }; function search( stories: ReadonlyArray, searchTerm: string ): Array { return new Fuse(stories, FUSE_OPTIONS).search( searchTerm ); } export type PropsType = { hiddenStories: Array; i18n: LocalizerType; onBack: () => unknown; onStoryClicked: (conversationId: string) => unknown; openConversationInternal: (_: { conversationId: string }) => unknown; stories: Array; toggleHideStories: (conversationId: string) => unknown; }; export const StoriesPane = ({ i18n, onBack, onStoryClicked, openConversationInternal, stories, toggleHideStories, }: PropsType): JSX.Element => { const [searchTerm, setSearchTerm] = useState(''); const [renderedStories, setRenderedStories] = useState>(stories); useEffect(() => { if (searchTerm) { setRenderedStories(search(stories, searchTerm)); } else { setRenderedStories(stories); } }, [searchTerm, stories]); return ( <>
{ setSearchTerm(event.target.value); }} placeholder={i18n('search')} value={searchTerm} />
{renderedStories.map(story => ( { onStoryClicked(story.conversationId); }} onHideStory={() => { toggleHideStories(story.stories[0].sender.id); }} onGoToConversation={conversationId => { openConversationInternal({ conversationId }); }} story={story.stories[0]} /> ))} {!stories.length && i18n('Stories__list-empty')}
); };