From 1315d3cfe0fcba4bec8f77b8bd55b38edb18a00d Mon Sep 17 00:00:00 2001 From: Josh Perez <60019601+josh-signal@users.noreply.github.com> Date: Mon, 7 Aug 2023 12:36:59 -0400 Subject: [PATCH] Allow link previews to be added to an edit if original msg had no links --- ts/state/ducks/composer.ts | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/ts/state/ducks/composer.ts b/ts/state/ducks/composer.ts index 9e3778ced..e18435cad 100644 --- a/ts/state/ducks/composer.ts +++ b/ts/state/ducks/composer.ts @@ -20,13 +20,17 @@ import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions'; import type { DraftBodyRanges } from '../../types/BodyRange'; import { BodyRange } from '../../types/BodyRange'; import type { LinkPreviewType } from '../../types/message/LinkPreviews'; -import type { MessageAttributesType } from '../../model-types.d'; +import type { + DraftEditMessageType, + MessageAttributesType, +} from '../../model-types.d'; import type { NoopActionType } from './noop'; import type { ShowToastActionType } from './toast'; import type { StateType as RootStateType } from '../reducer'; import type { UUIDStringType } from '../../types/UUID'; import * as log from '../../logging/log'; import * as Errors from '../../types/errors'; +import * as LinkPreview from '../../types/LinkPreview'; import { ADD_PREVIEW as ADD_LINK_PREVIEW, REMOVE_PREVIEW as REMOVE_LINK_PREVIEW, @@ -252,6 +256,24 @@ export const actions = { setQuotedMessage, }; +function hadSameLinkPreviewDismissed( + messageText: string, + draftEditMessage: DraftEditMessageType | undefined +): boolean { + if (!draftEditMessage) { + return false; + } + + const currentLink = LinkPreview.findLinks(messageText).find( + LinkPreview.shouldPreviewHref + ); + const prevLink = LinkPreview.findLinks(draftEditMessage.body).find( + LinkPreview.shouldPreviewHref + ); + + return currentLink === prevLink && !draftEditMessage.preview; +} + function incrementSendCounter(conversationId: string): IncrementSendActionType { return { type: INCREMENT_SEND_COUNTER, @@ -994,9 +1016,10 @@ function onEditorStateChange({ includePending: true, }) || Boolean(conversation.attributes.draftEditMessage?.attachmentThumbnail) || - // If we already didn't have a preview attached, don't fetch another one - (conversation.attributes.draftEditMessage && - !conversation.attributes.draftEditMessage.preview) + hadSameLinkPreviewDismissed( + messageText, + conversation.attributes.draftEditMessage + ) ) { return; }