signal-desktop/ts/components/StoryCreator.tsx

292 lines
9.1 KiB
TypeScript
Raw Normal View History

2022-06-17 00:48:57 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2022-08-04 19:23:24 +00:00
import React, { useEffect, useState } from 'react';
2022-06-17 00:48:57 +00:00
import { get, has } from 'lodash';
2023-08-09 00:53:06 +00:00
import { createPortal } from 'react-dom';
2022-08-04 19:23:24 +00:00
import type {
AttachmentType,
InMemoryAttachmentDraftType,
} from '../types/Attachment';
import type { LinkPreviewSourceType } from '../types/LinkPreview';
2022-06-17 00:48:57 +00:00
import type { LinkPreviewType } from '../types/message/LinkPreviews';
import type { LocalizerType } from '../types/Util';
2022-08-04 19:23:24 +00:00
import type { Props as StickerButtonProps } from './stickers/StickerButton';
2022-08-23 17:24:55 +00:00
import type { PropsType as SendStoryModalPropsType } from './SendStoryModal';
import type { StoryDistributionIdString } from '../types/StoryDistributionId';
2022-11-16 21:41:38 +00:00
import type { imageToBlurHash } from '../util/imageToBlurHash';
import type { PropsType as TextStoryCreatorPropsType } from './TextStoryCreator';
2022-06-17 00:48:57 +00:00
2022-11-16 21:41:38 +00:00
import { TEXT_ATTACHMENT } from '../types/MIME';
2022-08-04 19:23:24 +00:00
import { isVideoAttachment } from '../types/Attachment';
2022-08-02 19:31:55 +00:00
import { SendStoryModal } from './SendStoryModal';
2022-08-04 19:23:24 +00:00
import { MediaEditor } from './MediaEditor';
import { TextStoryCreator } from './TextStoryCreator';
2022-10-04 23:17:15 +00:00
import type { SmartCompositionTextAreaProps } from '../state/smart/CompositionTextArea';
import type { DraftBodyRanges } from '../types/BodyRange';
2022-06-17 00:48:57 +00:00
2023-08-09 00:53:06 +00:00
function usePortalElement(testid: string): HTMLDivElement | null {
const [element, setElement] = useState<HTMLDivElement | null>(null);
useEffect(() => {
const div = document.createElement('div');
div.dataset.testid = testid;
document.body.appendChild(div);
setElement(div);
return () => {
document.body.removeChild(div);
};
}, [testid]);
return element;
}
2022-06-17 00:48:57 +00:00
export type PropsType = {
debouncedMaybeGrabLinkPreview: (
message: string,
source: LinkPreviewSourceType
) => unknown;
2022-08-04 19:23:24 +00:00
file?: File;
2022-06-17 00:48:57 +00:00
i18n: LocalizerType;
isSending: boolean;
2022-06-17 00:48:57 +00:00
linkPreview?: LinkPreviewType;
onClose: () => unknown;
2022-08-02 19:31:55 +00:00
onSend: (
listIds: Array<StoryDistributionIdString>,
2022-08-09 03:26:21 +00:00
conversationIds: Array<string>,
attachment: AttachmentType,
bodyRanges: DraftBodyRanges | undefined
2022-08-02 19:31:55 +00:00
) => unknown;
2022-11-16 21:41:38 +00:00
imageToBlurHash: typeof imageToBlurHash;
2022-08-04 19:23:24 +00:00
processAttachment: (
file: File
) => Promise<void | InMemoryAttachmentDraftType>;
2022-10-04 23:17:15 +00:00
renderCompositionTextArea: (
props: SmartCompositionTextAreaProps
) => JSX.Element;
sendStoryModalOpenStateChanged: (isOpen: boolean) => unknown;
2022-08-23 17:24:55 +00:00
} & Pick<StickerButtonProps, 'installedPacks' | 'recentStickers'> &
Pick<
SendStoryModalPropsType,
| 'candidateConversations'
| 'distributionLists'
| 'getPreferredBadge'
| 'groupConversations'
| 'groupStories'
| 'hasFirstStoryPostExperience'
| 'me'
| 'ourConversationId'
2022-08-30 19:13:32 +00:00
| 'onDeleteList'
2022-08-23 17:24:55 +00:00
| 'onDistributionListCreated'
| 'onHideMyStoriesFrom'
| 'onRemoveMembers'
2022-08-30 19:13:32 +00:00
| 'onRepliesNReactionsChanged'
| 'onSelectedStoryList'
2022-08-23 17:24:55 +00:00
| 'onViewersUpdated'
| 'setMyStoriesToAllSignalConnections'
| 'signalConnections'
2022-08-30 19:13:32 +00:00
| 'toggleGroupsForStorySend'
| 'mostRecentActiveStoryTimestampByGroupOrDistributionList'
2022-08-23 17:24:55 +00:00
| 'toggleSignalConnectionsModal'
2023-02-24 23:18:57 +00:00
| 'onMediaPlaybackStart'
> &
Pick<
TextStoryCreatorPropsType,
'onUseEmoji' | 'skinTone' | 'onSetSkinTone' | 'recentEmojis'
2022-08-23 17:24:55 +00:00
>;
2022-06-17 00:48:57 +00:00
2022-11-18 00:45:19 +00:00
export function StoryCreator({
2022-08-10 18:37:19 +00:00
candidateConversations,
2022-06-17 00:48:57 +00:00
debouncedMaybeGrabLinkPreview,
2022-08-02 19:31:55 +00:00
distributionLists,
2022-08-04 19:23:24 +00:00
file,
2022-08-10 18:37:19 +00:00
getPreferredBadge,
groupConversations,
groupStories,
2022-08-23 17:24:55 +00:00
hasFirstStoryPostExperience,
2022-06-17 00:48:57 +00:00
i18n,
2022-11-16 21:41:38 +00:00
imageToBlurHash,
2022-08-04 19:23:24 +00:00
installedPacks,
isSending,
2022-06-17 00:48:57 +00:00
linkPreview,
2022-08-02 19:31:55 +00:00
me,
mostRecentActiveStoryTimestampByGroupOrDistributionList,
2022-06-17 00:48:57 +00:00
onClose,
2022-08-30 19:13:32 +00:00
onDeleteList,
2022-08-10 18:37:19 +00:00
onDistributionListCreated,
2022-08-23 17:24:55 +00:00
onHideMyStoriesFrom,
onRemoveMembers,
2022-08-30 19:13:32 +00:00
onRepliesNReactionsChanged,
onSelectedStoryList,
2022-08-02 19:31:55 +00:00
onSend,
onSetSkinTone,
onUseEmoji,
2022-08-23 17:24:55 +00:00
onViewersUpdated,
2023-02-24 23:18:57 +00:00
onMediaPlaybackStart,
ourConversationId,
2022-08-04 19:23:24 +00:00
processAttachment,
recentEmojis,
2022-08-04 19:23:24 +00:00
recentStickers,
2022-10-04 23:17:15 +00:00
renderCompositionTextArea,
sendStoryModalOpenStateChanged,
2022-08-23 17:24:55 +00:00
setMyStoriesToAllSignalConnections,
2022-08-02 19:31:55 +00:00
signalConnections,
skinTone,
2022-08-30 19:13:32 +00:00
toggleGroupsForStorySend,
2022-08-23 17:24:55 +00:00
toggleSignalConnectionsModal,
2023-08-09 00:53:06 +00:00
}: PropsType): JSX.Element | null {
const portalElement = usePortalElement('StoryCreatorPortal');
2022-08-04 19:23:24 +00:00
const [draftAttachment, setDraftAttachment] = useState<
AttachmentType | undefined
>();
2022-11-16 21:41:38 +00:00
const [isReadyToSend, setIsReadyToSend] = useState(false);
2022-08-04 19:23:24 +00:00
const [attachmentUrl, setAttachmentUrl] = useState<string | undefined>();
const [bodyRanges, setBodyRanges] = useState<DraftBodyRanges | undefined>();
2022-06-17 00:48:57 +00:00
useEffect(() => {
2022-08-04 19:23:24 +00:00
let url: string | undefined;
let unmounted = false;
2022-06-17 00:48:57 +00:00
2022-08-04 19:23:24 +00:00
async function loadAttachment(): Promise<void> {
if (!file || unmounted) {
return;
}
2022-06-17 00:48:57 +00:00
2022-08-04 19:23:24 +00:00
const attachment = await processAttachment(file);
if (!attachment || unmounted) {
return;
2022-06-17 00:48:57 +00:00
}
2022-08-04 19:23:24 +00:00
2022-11-16 21:41:38 +00:00
setDraftAttachment(attachment);
2022-08-04 19:23:24 +00:00
if (isVideoAttachment(attachment)) {
2022-11-16 21:41:38 +00:00
setAttachmentUrl(undefined);
setIsReadyToSend(true);
2022-08-04 19:23:24 +00:00
} else if (attachment && has(attachment, 'data')) {
url = URL.createObjectURL(new Blob([get(attachment, 'data')]));
setAttachmentUrl(url);
2022-11-16 21:41:38 +00:00
// Needs editing in MediaEditor
setIsReadyToSend(false);
2022-06-17 00:48:57 +00:00
}
2022-08-04 19:23:24 +00:00
}
2022-06-17 00:48:57 +00:00
void loadAttachment();
2022-06-17 00:48:57 +00:00
return () => {
2022-08-04 19:23:24 +00:00
unmounted = true;
if (url) {
URL.revokeObjectURL(url);
}
2022-06-17 00:48:57 +00:00
};
2022-08-04 19:23:24 +00:00
}, [file, processAttachment]);
2022-08-02 19:31:55 +00:00
useEffect(() => {
2022-11-16 21:41:38 +00:00
if (draftAttachment === undefined) {
sendStoryModalOpenStateChanged(false);
setIsReadyToSend(false);
} else {
sendStoryModalOpenStateChanged(true);
}
}, [draftAttachment, sendStoryModalOpenStateChanged]);
2023-08-09 00:53:06 +00:00
return portalElement != null
? createPortal(
<>
{draftAttachment && isReadyToSend && (
<SendStoryModal
draftAttachment={draftAttachment}
candidateConversations={candidateConversations}
distributionLists={distributionLists}
getPreferredBadge={getPreferredBadge}
groupConversations={groupConversations}
groupStories={groupStories}
hasFirstStoryPostExperience={hasFirstStoryPostExperience}
ourConversationId={ourConversationId}
i18n={i18n}
me={me}
onClose={() => setDraftAttachment(undefined)}
onDeleteList={onDeleteList}
onDistributionListCreated={onDistributionListCreated}
onHideMyStoriesFrom={onHideMyStoriesFrom}
onRemoveMembers={onRemoveMembers}
onRepliesNReactionsChanged={onRepliesNReactionsChanged}
onSelectedStoryList={onSelectedStoryList}
onSend={(listIds, groupIds) => {
onSend(listIds, groupIds, draftAttachment, bodyRanges);
setDraftAttachment(undefined);
}}
onViewersUpdated={onViewersUpdated}
onMediaPlaybackStart={onMediaPlaybackStart}
setMyStoriesToAllSignalConnections={
setMyStoriesToAllSignalConnections
}
signalConnections={signalConnections}
toggleGroupsForStorySend={toggleGroupsForStorySend}
mostRecentActiveStoryTimestampByGroupOrDistributionList={
mostRecentActiveStoryTimestampByGroupOrDistributionList
}
toggleSignalConnectionsModal={toggleSignalConnectionsModal}
/>
)}
{draftAttachment && !isReadyToSend && attachmentUrl && (
<MediaEditor
doneButtonLabel={i18n('icu:next2')}
i18n={i18n}
imageSrc={attachmentUrl}
installedPacks={installedPacks}
isSending={isSending}
onClose={onClose}
supportsCaption
renderCompositionTextArea={renderCompositionTextArea}
imageToBlurHash={imageToBlurHash}
onDone={({
contentType,
data,
blurHash,
caption,
captionBodyRanges,
}) => {
setDraftAttachment({
...draftAttachment,
contentType,
data,
size: data.byteLength,
blurHash,
caption,
});
setBodyRanges(captionBodyRanges);
setIsReadyToSend(true);
}}
recentStickers={recentStickers}
/>
)}
{!file && (
<TextStoryCreator
debouncedMaybeGrabLinkPreview={debouncedMaybeGrabLinkPreview}
i18n={i18n}
isSending={isSending}
linkPreview={linkPreview}
onClose={onClose}
onDone={textAttachment => {
setDraftAttachment({
contentType: TEXT_ATTACHMENT,
textAttachment,
size: textAttachment.text?.length || 0,
});
setIsReadyToSend(true);
}}
onUseEmoji={onUseEmoji}
onSetSkinTone={onSetSkinTone}
recentEmojis={recentEmojis}
skinTone={skinTone}
/>
)}
</>,
portalElement
)
: null;
2022-11-18 00:45:19 +00:00
}