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';
|
2023-08-10 22:16:51 +00:00
|
|
|
import type { LocalizerType, ThemeType } 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';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { StoryDistributionIdString } from '../types/StoryDistributionId';
|
2022-11-16 21:41:38 +00:00
|
|
|
import type { imageToBlurHash } from '../util/imageToBlurHash';
|
2022-11-28 18:52:16 +00:00
|
|
|
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';
|
2023-05-10 00:40:19 +00:00
|
|
|
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;
|
2022-11-02 00:36:16 +00:00
|
|
|
isSending: boolean;
|
2022-06-17 00:48:57 +00:00
|
|
|
linkPreview?: LinkPreviewType;
|
|
|
|
onClose: () => unknown;
|
2022-08-02 19:31:55 +00:00
|
|
|
onSend: (
|
2023-08-10 16:43:33 +00:00
|
|
|
listIds: Array<StoryDistributionIdString>,
|
2022-08-09 03:26:21 +00:00
|
|
|
conversationIds: Array<string>,
|
2023-05-10 00:40:19 +00:00
|
|
|
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;
|
2022-08-19 18:05:31 +00:00
|
|
|
sendStoryModalOpenStateChanged: (isOpen: boolean) => unknown;
|
2023-08-10 22:16:51 +00:00
|
|
|
theme: ThemeType;
|
2022-08-23 17:24:55 +00:00
|
|
|
} & Pick<StickerButtonProps, 'installedPacks' | 'recentStickers'> &
|
|
|
|
Pick<
|
|
|
|
SendStoryModalPropsType,
|
|
|
|
| 'candidateConversations'
|
|
|
|
| 'distributionLists'
|
|
|
|
| 'getPreferredBadge'
|
|
|
|
| 'groupConversations'
|
|
|
|
| 'groupStories'
|
|
|
|
| 'hasFirstStoryPostExperience'
|
|
|
|
| 'me'
|
2022-11-11 04:10:30 +00:00
|
|
|
| 'ourConversationId'
|
2022-08-30 19:13:32 +00:00
|
|
|
| 'onDeleteList'
|
2022-08-23 17:24:55 +00:00
|
|
|
| 'onDistributionListCreated'
|
|
|
|
| 'onHideMyStoriesFrom'
|
2022-11-11 04:10:30 +00:00
|
|
|
| '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'
|
2022-11-08 20:01:59 +00:00
|
|
|
| 'mostRecentActiveStoryTimestampByGroupOrDistributionList'
|
2022-08-23 17:24:55 +00:00
|
|
|
| 'toggleSignalConnectionsModal'
|
2023-02-24 23:18:57 +00:00
|
|
|
| 'onMediaPlaybackStart'
|
2022-11-28 18:52:16 +00:00
|
|
|
> &
|
|
|
|
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,
|
2022-11-02 00:36:16 +00:00
|
|
|
isSending,
|
2022-06-17 00:48:57 +00:00
|
|
|
linkPreview,
|
2022-08-02 19:31:55 +00:00
|
|
|
me,
|
2022-11-28 18:52:16 +00:00
|
|
|
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,
|
2022-11-11 04:10:30 +00:00
|
|
|
onRemoveMembers,
|
2022-08-30 19:13:32 +00:00
|
|
|
onRepliesNReactionsChanged,
|
2022-08-19 18:05:31 +00:00
|
|
|
onSelectedStoryList,
|
2022-08-02 19:31:55 +00:00
|
|
|
onSend,
|
2022-11-28 18:52:16 +00:00
|
|
|
onSetSkinTone,
|
|
|
|
onUseEmoji,
|
2022-08-23 17:24:55 +00:00
|
|
|
onViewersUpdated,
|
2023-02-24 23:18:57 +00:00
|
|
|
onMediaPlaybackStart,
|
2022-11-28 18:52:16 +00:00
|
|
|
ourConversationId,
|
2022-08-04 19:23:24 +00:00
|
|
|
processAttachment,
|
2022-11-28 18:52:16 +00:00
|
|
|
recentEmojis,
|
2022-08-04 19:23:24 +00:00
|
|
|
recentStickers,
|
2022-10-04 23:17:15 +00:00
|
|
|
renderCompositionTextArea,
|
2022-08-19 18:05:31 +00:00
|
|
|
sendStoryModalOpenStateChanged,
|
2022-08-23 17:24:55 +00:00
|
|
|
setMyStoriesToAllSignalConnections,
|
2022-08-02 19:31:55 +00:00
|
|
|
signalConnections,
|
2022-11-28 18:52:16 +00:00
|
|
|
skinTone,
|
2023-08-10 22:16:51 +00:00
|
|
|
theme,
|
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>();
|
2023-05-10 00:40:19 +00:00
|
|
|
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
|
|
|
|
2022-12-21 18:41:48 +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
|
|
|
|
2022-08-19 18:05:31 +00:00
|
|
|
useEffect(() => {
|
2022-11-16 21:41:38 +00:00
|
|
|
if (draftAttachment === undefined) {
|
|
|
|
sendStoryModalOpenStateChanged(false);
|
|
|
|
setIsReadyToSend(false);
|
|
|
|
} else {
|
|
|
|
sendStoryModalOpenStateChanged(true);
|
|
|
|
}
|
2022-08-19 18:05:31 +00:00
|
|
|
}, [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
|
|
|
|
}
|
2023-08-10 22:16:51 +00:00
|
|
|
theme={theme}
|
2023-08-09 00:53:06 +00:00
|
|
|
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
|
|
|
}
|