// Copyright 2022 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useEffect, useMemo, useState } from 'react'; import { SearchInput } from './SearchInput'; import { filterAndSortConversationsByRecent } from '../util/filterAndSortConversations'; import type { ConversationType } from '../state/ducks/conversations'; import type { LocalizerType } from '../types/Util'; import type { PreferredBadgeSelectorType } from '../state/selectors/badges'; import type { PropsType as StoriesSettingsModalPropsType } from './StoriesSettingsModal'; import type { StoryDistributionListDataType } from '../state/ducks/storyDistributionLists'; import type { UUIDStringType } from '../types/UUID'; import { Avatar, AvatarSize } from './Avatar'; import { Button, ButtonVariant } from './Button'; import { Checkbox } from './Checkbox'; import { ContextMenu } from './ContextMenu'; import { EditDistributionList, EditMyStoriesPrivacy, Page as StoriesSettingsPage, } from './StoriesSettingsModal'; import { MY_STORIES_ID, getStoryDistributionListName } from '../types/Stories'; import { Modal } from './Modal'; import { StoryDistributionListName } from './StoryDistributionListName'; import { Theme } from '../util/theme'; import { isNotNil } from '../util/isNotNil'; export type PropsType = { candidateConversations: Array; distributionLists: Array; getPreferredBadge: PreferredBadgeSelectorType; groupConversations: Array; groupStories: Array; hasFirstStoryPostExperience: boolean; i18n: LocalizerType; me: ConversationType; onClose: () => unknown; onDistributionListCreated: ( name: string, viewerUuids: Array ) => unknown; onSelectedStoryList: (memberUuids: Array) => unknown; onSend: ( listIds: Array, conversationIds: Array ) => unknown; signalConnections: Array; tagGroupsAsNewGroupStory: (cids: Array) => unknown; } & Pick< StoriesSettingsModalPropsType, | 'onHideMyStoriesFrom' | 'onViewersUpdated' | 'setMyStoriesToAllSignalConnections' | 'toggleSignalConnectionsModal' >; enum MyStoriesPrivacy { AllSignalConnections = 'AllSignalConnections', Exclude = 'Exclude', OnlyShareWith = 'OnlyShareWith', } enum SendStoryPage { ChooseGroups = 'ChooseGroups', SendStory = 'SendStory', SetMyStoriesPrivacy = 'SetMyStoriesPrivacy', } const Page = { ...SendStoryPage, ...StoriesSettingsPage, }; type PageType = SendStoryPage | StoriesSettingsPage; function getListMemberUuids( list: StoryDistributionListDataType, signalConnections: Array ): Array { if (list.id === MY_STORIES_ID && list.isBlockList) { const excludeUuids = new Set(list.memberUuids); return signalConnections .map(conversation => conversation.uuid) .filter(isNotNil) .filter(uuid => !excludeUuids.has(uuid)); } return list.memberUuids; } function getListViewers( list: StoryDistributionListDataType, i18n: LocalizerType, signalConnections: Array ): string { let memberCount = list.memberUuids.length; if (list.id === MY_STORIES_ID && list.isBlockList) { memberCount = list.isBlockList ? signalConnections.length - list.memberUuids.length : signalConnections.length; } return memberCount === 1 ? i18n('StoriesSettings__viewers--singular', ['1']) : i18n('StoriesSettings__viewers--plural', [String(memberCount)]); } export const SendStoryModal = ({ candidateConversations, distributionLists, getPreferredBadge, groupConversations, groupStories, hasFirstStoryPostExperience, i18n, me, onClose, onDistributionListCreated, onHideMyStoriesFrom, onSend, onSelectedStoryList, onViewersUpdated, setMyStoriesToAllSignalConnections, signalConnections, tagGroupsAsNewGroupStory, toggleSignalConnectionsModal, }: PropsType): JSX.Element => { const [page, setPage] = useState(Page.SendStory); const [selectedListIds, setSelectedListIds] = useState>( new Set() ); const [selectedGroupIds, setSelectedGroupIds] = useState>( new Set() ); const selectedStoryNames = useMemo( () => distributionLists .filter(list => selectedListIds.has(list.id)) .map(list => list.name) .concat( groupStories .filter(group => selectedGroupIds.has(group.id)) .map(group => group.title) ), [distributionLists, groupStories, selectedGroupIds, selectedListIds] ); const [searchTerm, setSearchTerm] = useState(''); const [filteredConversations, setFilteredConversations] = useState( filterAndSortConversationsByRecent( groupConversations, searchTerm, undefined ) ); const normalizedSearchTerm = searchTerm.trim(); useEffect(() => { const timeout = setTimeout(() => { setFilteredConversations( filterAndSortConversationsByRecent( groupConversations, normalizedSearchTerm, undefined ) ); }, 200); return () => { clearTimeout(timeout); }; }, [groupConversations, normalizedSearchTerm, setFilteredConversations]); const [chosenGroupIds, setChosenGroupIds] = useState>( new Set() ); const chosenGroupNames = useMemo( () => filteredConversations .filter(group => chosenGroupIds.has(group.id)) .map(group => group.title), [filteredConversations, chosenGroupIds] ); const [selectedContacts, setSelectedContacts] = useState< Array >([]); const [myStoriesPrivacy, setMyStoriesPrivacy] = useState( MyStoriesPrivacy.AllSignalConnections ); const [myStoriesPrivacyUuids, setMyStoriesPrivacyUuids] = useState< Set >(new Set()); const myStories = useMemo(() => { return { allowsReplies: true, id: MY_STORIES_ID, name: MY_STORIES_ID, isBlockList: myStoriesPrivacy !== MyStoriesPrivacy.OnlyShareWith, members: myStoriesPrivacy === MyStoriesPrivacy.AllSignalConnections ? [] : candidateConversations.filter( convo => convo.uuid && myStoriesPrivacyUuids.has(convo.uuid) ), }; }, [candidateConversations, myStoriesPrivacy, myStoriesPrivacyUuids]); let content: JSX.Element; if (page === Page.SetMyStoriesPrivacy) { content = ( { setMyStoriesPrivacy(MyStoriesPrivacy.Exclude); setMyStoriesPrivacyUuids(new Set()); setSelectedContacts([]); setPage(Page.HideStoryFrom); }} onClickOnlyShareWith={() => { setMyStoriesPrivacy(MyStoriesPrivacy.OnlyShareWith); setMyStoriesPrivacyUuids(new Set()); setSelectedContacts([]); setPage(Page.AddViewer); }} setSelectedContacts={setSelectedContacts} setMyStoriesToAllSignalConnections={() => { setMyStoriesPrivacy(MyStoriesPrivacy.AllSignalConnections); setMyStoriesPrivacyUuids(new Set()); setSelectedContacts([]); }} toggleSignalConnectionsModal={toggleSignalConnectionsModal} /> ); } else if ( page === Page.ChooseViewers || page === Page.NameStory || page === Page.AddViewer || page === Page.HideStoryFrom ) { content = ( { onDistributionListCreated(name, uuids); setPage(Page.SendStory); }} onViewersUpdated={uuids => { if (page === Page.ChooseViewers) { setPage(Page.NameStory); } else if (page === Page.HideStoryFrom || page === Page.AddViewer) { setMyStoriesPrivacyUuids(new Set(uuids)); setPage(Page.SetMyStoriesPrivacy); } else { setPage(Page.SendStory); } }} page={page} selectedContacts={selectedContacts} setSelectedContacts={setSelectedContacts} /> ); } else if (page === Page.ChooseGroups) { content = ( <> { setSearchTerm(event.target.value); }} value={searchTerm} /> {filteredConversations.length ? ( filteredConversations.map(group => ( { setChosenGroupIds(groupIds => { if (value) { groupIds.add(group.id); } else { groupIds.delete(group.id); } return new Set([...groupIds]); }); }} > {({ id, checkboxNode }) => ( <> {checkboxNode} )} )) ) : (
{i18n('noContactsFound')}
)} ); } else { content = ( <>
{i18n('stories')} setPage(Page.ChooseViewers), }, { label: i18n('SendStoryModal__new-group--title'), description: i18n('SendStoryModal__new-group--description'), icon: 'SendStoryModal__icon--group', onClick: () => setPage(Page.ChooseGroups), }, ]} moduleClassName="SendStoryModal__new-story" popperOptions={{ placement: 'bottom', strategy: 'absolute', }} theme={Theme.Dark} > {i18n('SendStoryModal__new')}
{distributionLists.map(list => ( { if ( list.id === MY_STORIES_ID && hasFirstStoryPostExperience && value ) { setPage(Page.SetMyStoriesPrivacy); return; } setSelectedListIds(listIds => { if (value) { listIds.add(list.id); } else { listIds.delete(list.id); } return new Set([...listIds]); }); if (value) { onSelectedStoryList( getListMemberUuids(list, signalConnections) ); } }} > {({ id, checkboxNode }) => ( <> {checkboxNode} )} ))} {groupStories.map(group => ( { if (!group.memberships) { return; } setSelectedGroupIds(groupIds => { if (value) { groupIds.add(group.id); } else { groupIds.delete(group.id); } return new Set([...groupIds]); }); if (value) { onSelectedStoryList(group.memberships.map(({ uuid }) => uuid)); } }} > {({ id, checkboxNode }) => ( <> {checkboxNode} )} ))} ); } let modalTitle: string; if (page === Page.SetMyStoriesPrivacy) { modalTitle = i18n('SendStoryModal__my-stories-privacy'); } else if (page === Page.HideStoryFrom) { modalTitle = i18n('StoriesSettings__hide-story'); } else if (page === Page.ChooseGroups) { modalTitle = i18n('SendStoryModal__choose-groups'); } else if (page === Page.NameStory) { modalTitle = i18n('StoriesSettings__name-story'); } else if (page === Page.ChooseViewers || page === Page.AddViewer) { modalTitle = i18n('StoriesSettings__choose-viewers'); } else { modalTitle = i18n('SendStoryModal__title'); } let selectedNames: string | undefined; if (page === Page.ChooseGroups) { selectedNames = chosenGroupNames.join(', '); } else { selectedNames = selectedStoryNames .map(listName => getStoryDistributionListName(i18n, listName, listName)) .join(', '); } const hasBackButton = page !== Page.SendStory; let modalFooter: JSX.Element | undefined; if ( page === Page.SendStory || page === Page.ChooseGroups || page === Page.SetMyStoriesPrivacy ) { modalFooter = ( {page !== Page.SetMyStoriesPrivacy && (
{selectedNames}
)} {page === Page.ChooseGroups && ( )}
); } return ( { if (page === Page.SetMyStoriesPrivacy) { setSelectedContacts([]); setMyStoriesPrivacyUuids(new Set()); setMyStoriesPrivacy(MyStoriesPrivacy.AllSignalConnections); setPage(Page.SendStory); } else if ( page === Page.HideStoryFrom || page === Page.AddViewer ) { setSelectedContacts([]); setMyStoriesPrivacyUuids(new Set()); setPage(Page.SetMyStoriesPrivacy); } else if (page === Page.ChooseGroups) { setChosenGroupIds(new Set()); setPage(Page.SendStory); } else if (page === Page.ChooseViewers) { setSelectedContacts([]); setPage(Page.SendStory); } else if (page === Page.NameStory) { setPage(Page.ChooseViewers); } } : undefined } onClose={onClose} title={modalTitle} theme={Theme.Dark} > {content} ); };