// Copyright 2022 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useEffect, useMemo, useState } from 'react'; import { noop, sortBy } from 'lodash'; import { SearchInput } from './SearchInput'; import { filterAndSortConversationsByRecent } from '../util/filterAndSortConversations'; import type { ConversationType } from '../state/ducks/conversations'; import type { ConversationWithStoriesType } from '../state/selectors/conversations'; import type { LocalizerType } from '../types/Util'; import { ThemeType } from '../types/Util'; import type { PreferredBadgeSelectorType } from '../state/selectors/badges'; import type { PropsType as StoriesSettingsModalPropsType } from './StoriesSettingsModal'; import { getI18nForMyStory, getListViewers, DistributionListSettingsModal, EditDistributionListModal, EditMyStoryPrivacy, Page as StoriesSettingsPage, } from './StoriesSettingsModal'; import type { StoryDistributionListWithMembersDataType } from '../types/Stories'; import type { StoryDistributionIdString } from '../types/StoryDistributionId'; import type { ServiceIdString } from '../types/ServiceId'; import { Alert } from './Alert'; import { Avatar, AvatarSize } from './Avatar'; import { Button, ButtonSize, ButtonVariant } from './Button'; import { Checkbox } from './Checkbox'; import { ConfirmationDialog } from './ConfirmationDialog'; import { ContextMenu } from './ContextMenu'; import { MY_STORY_ID, getStoryDistributionListName } from '../types/Stories'; import type { RenderModalPage, ModalPropsType } from './Modal'; import { PagedModal, ModalPage } from './Modal'; import { StoryDistributionListName } from './StoryDistributionListName'; import { isNotNil } from '../util/isNotNil'; import { StoryImage } from './StoryImage'; import type { AttachmentType } from '../types/Attachment'; import { useConfirmDiscard } from '../hooks/useConfirmDiscard'; import { getStoryBackground } from '../util/getStoryBackground'; import { makeObjectUrl, revokeObjectUrl } from '../types/VisualAttachment'; import { UserText } from './UserText'; import { Theme } from '../util/theme'; export type PropsType = { draftAttachment: AttachmentType; candidateConversations: Array; distributionLists: Array; getPreferredBadge: PreferredBadgeSelectorType; groupConversations: Array; groupStories: Array; hasFirstStoryPostExperience: boolean; ourConversationId: string | undefined; i18n: LocalizerType; me: ConversationType; onClose: () => unknown; onDeleteList: (listId: StoryDistributionIdString) => unknown; onDistributionListCreated: ( name: string, viewerServiceIds: Array ) => Promise; onSelectedStoryList: (options: { conversationId: string; distributionId: StoryDistributionIdString | undefined; serviceIds: Array; }) => unknown; onSend: ( listIds: Array, conversationIds: Array ) => unknown; signalConnections: Array; theme: ThemeType; toggleGroupsForStorySend: (cids: Array) => Promise; mostRecentActiveStoryTimestampByGroupOrDistributionList: Record< string, number >; onMediaPlaybackStart: () => void; } & Pick< StoriesSettingsModalPropsType, | 'onHideMyStoriesFrom' | 'onRemoveMembers' | 'onRepliesNReactionsChanged' | 'onViewersUpdated' | 'setMyStoriesToAllSignalConnections' | 'toggleSignalConnectionsModal' >; enum SendStoryPage { ChooseGroups = 'ChooseGroups', EditingDistributionList = 'EditingDistributionList', SendStory = 'SendStory', SetMyStoriesPrivacy = 'SetMyStoriesPrivacy', } const Page = { ...SendStoryPage, ...StoriesSettingsPage, }; type PageType = SendStoryPage | StoriesSettingsPage; function getListMemberServiceIds( list: StoryDistributionListWithMembersDataType, signalConnections: Array ): Array { const memberServiceIds = list.members .map(({ serviceId }) => serviceId) .filter(isNotNil); if (list.id === MY_STORY_ID && list.isBlockList) { const excludeServiceIds = new Set(memberServiceIds); return signalConnections .map(conversation => conversation.serviceId) .filter(isNotNil) .filter(serviceId => !excludeServiceIds.has(serviceId)); } return memberServiceIds; } export function SendStoryModal({ draftAttachment, candidateConversations, distributionLists, getPreferredBadge, groupConversations, groupStories, hasFirstStoryPostExperience, i18n, me, ourConversationId, onClose, onDeleteList, onDistributionListCreated, onHideMyStoriesFrom, onRemoveMembers, onRepliesNReactionsChanged, onSelectedStoryList, onSend, onViewersUpdated, setMyStoriesToAllSignalConnections, signalConnections, theme, toggleGroupsForStorySend, mostRecentActiveStoryTimestampByGroupOrDistributionList, toggleSignalConnectionsModal, onMediaPlaybackStart, }: PropsType): JSX.Element { const [page, setPage] = useState(Page.SendStory); const [confirmDiscardModal, confirmDiscardIf] = useConfirmDiscard(i18n); const [selectedListIds, setSelectedListIds] = useState< Set >(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 [hasAnnouncementsOnlyAlert, setHasAnnouncementsOnlyAlert] = useState(false); const [confirmRemoveGroupId, setConfirmRemoveGroupId] = useState< string | undefined >(); const [confirmDeleteList, setConfirmDeleteList] = useState< { id: StoryDistributionIdString; name: string } | undefined >(); const [listIdToEdit, setListIdToEdit] = useState(); useEffect(() => { if (listIdToEdit) { setPage(Page.EditingDistributionList); } else { setPage(Page.SendStory); } }, [listIdToEdit]); const listToEdit = useMemo(() => { if (!listIdToEdit) { return; } return distributionLists.find(list => list.id === listIdToEdit); }, [distributionLists, listIdToEdit]); // myStoriesPrivacy, myStoriesPrivacyServiceIds, and myStories are only used // during the first time posting to My Stories experience where we have // to select the privacy settings. const ogMyStories = useMemo( () => distributionLists.find(list => list.id === MY_STORY_ID), [distributionLists] ); const initialMyStories: StoryDistributionListWithMembersDataType = useMemo( () => ({ allowsReplies: true, id: MY_STORY_ID, name: i18n('icu:Stories__mine'), isBlockList: ogMyStories?.isBlockList ?? true, members: ogMyStories?.members || [], }), [i18n, ogMyStories] ); const [stagedMyStories, setStagedMyStories] = useState(initialMyStories); let selectedNames: string | undefined; if (page === Page.ChooseGroups) { selectedNames = chosenGroupNames.join(', '); } else { selectedNames = selectedStoryNames .map(listName => getStoryDistributionListName(i18n, undefined, listName)) .join(', '); } const [objectUrl, setObjectUrl] = useState(undefined); useEffect(() => { let url: undefined | string; if (draftAttachment.url) { setObjectUrl(draftAttachment.url); } else if (draftAttachment.data) { url = makeObjectUrl(draftAttachment.data, draftAttachment.contentType); setObjectUrl(url); } return () => { if (url) { revokeObjectUrl(url); } }; }, [setObjectUrl, draftAttachment]); const modalCommonProps: Pick = { hasXButton: true, i18n, }; let modal: RenderModalPage; if (page === Page.SetMyStoriesPrivacy) { const footer = ( <>
); modal = handleClose => ( { let nextSelectedContacts = stagedMyStories.members; if (!stagedMyStories.isBlockList) { setStagedMyStories(myStories => ({ ...myStories, isBlockList: true, members: [], })); nextSelectedContacts = []; } setSelectedContacts(nextSelectedContacts); setPage(Page.HideStoryFrom); }} onClickOnlyShareWith={() => { if (!stagedMyStories.isBlockList) { setSelectedContacts(stagedMyStories.members); } else { setStagedMyStories(myStories => ({ ...myStories, isBlockList: false, members: [], })); } setPage(Page.AddViewer); }} setSelectedContacts={setSelectedContacts} setMyStoriesToAllSignalConnections={() => { setStagedMyStories(myStories => ({ ...myStories, isBlockList: true, members: [], })); setSelectedContacts([]); }} toggleSignalConnectionsModal={toggleSignalConnectionsModal} /> ); } else if (page === Page.EditingDistributionList && listToEdit) { modal = handleClose => ( confirmDiscardIf(selectedContacts.length > 0, () => setListIdToEdit(undefined) ) } onClose={handleClose} /> ); } else if ( page === Page.ChooseViewers || page === Page.NameStory || page === Page.AddViewer || page === Page.HideStoryFrom ) { modal = handleClose => ( { const newDistributionListId = await onDistributionListCreated( name, serviceIds ); setSelectedContacts([]); setSelectedListIds( listIds => new Set([...listIds, newDistributionListId]) ); setPage(Page.SendStory); }} onViewersUpdated={serviceIds => { if (listIdToEdit && page === Page.AddViewer) { onViewersUpdated(listIdToEdit, serviceIds); setPage(Page.EditingDistributionList); } else if (page === Page.ChooseViewers) { setPage(Page.NameStory); } else if (listIdToEdit && page === Page.HideStoryFrom) { onHideMyStoriesFrom(serviceIds); setPage(Page.SendStory); } else if (page === Page.HideStoryFrom || page === Page.AddViewer) { const serviceIdSet = new Set(serviceIds); const members = candidateConversations.filter(convo => convo.serviceId ? serviceIdSet.has(convo.serviceId) : false ); setStagedMyStories(myStories => ({ ...myStories, members, })); setPage(Page.SetMyStoriesPrivacy); } else { setPage(Page.SendStory); } }} page={page} onClose={handleClose} onBackButtonClick={() => confirmDiscardIf(selectedContacts.length > 0, () => { if (listIdToEdit) { if ( page === Page.AddViewer || page === Page.HideStoryFrom || page === Page.ChooseViewers ) { setPage(Page.EditingDistributionList); } else { setListIdToEdit(undefined); } } else if (page === Page.HideStoryFrom || page === Page.AddViewer) { setSelectedContacts([]); setStagedMyStories(initialMyStories); setPage(Page.SetMyStoriesPrivacy); } else if (page === Page.ChooseViewers) { setSelectedContacts([]); setPage(Page.SendStory); } else if (page === Page.NameStory) { setPage(Page.ChooseViewers); } }) } selectedContacts={selectedContacts} setSelectedContacts={setSelectedContacts} theme={theme} /> ); } else if (page === Page.ChooseGroups) { const footer = ( <>
{selectedNames}
{selectedNames.length > 0 && ( {menuNode}
)} {fullList.map(listOrGroup => // only group has a type field 'type' in listOrGroup ? renderGroup(listOrGroup) : renderDistributionList(listOrGroup) )} ); } return ( <> {!confirmDiscardModal && ( confirmDiscardIf(selectedContacts.length > 0, onClose)} > {modal} )} {hasAnnouncementsOnlyAlert && ( setHasAnnouncementsOnlyAlert(false)} theme={theme === ThemeType.dark ? Theme.Dark : Theme.Light} /> )} {confirmRemoveGroupId && ( { void toggleGroupsForStorySend([confirmRemoveGroupId]); setConfirmRemoveGroupId(undefined); }, style: 'negative', text: i18n('icu:delete'), }, ]} i18n={i18n} onClose={() => { setConfirmRemoveGroupId(undefined); }} theme={theme === ThemeType.dark ? Theme.Dark : Theme.Light} > {i18n('icu:SendStoryModal__confirm-remove-group')} )} {confirmDeleteList && ( { onDeleteList(confirmDeleteList.id); setConfirmDeleteList(undefined); }, style: 'negative', text: i18n('icu:delete'), }, ]} i18n={i18n} onClose={() => { setConfirmDeleteList(undefined); }} theme={theme === ThemeType.dark ? Theme.Dark : Theme.Light} > {i18n('icu:StoriesSettings__delete-list--confirm', { name: confirmDeleteList.name, })} )} {confirmDiscardModal} ); }