From d572131f8529281c29170aa8b75f567516e20f7a Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 8 Oct 2025 12:33:23 +0200 Subject: [PATCH] Attachment Settings: Fix settings updating in drop area (#6296) --- src/components/common/Composer.tsx | 17 +++++++++++++++ .../middle/composer/AttachmentModal.tsx | 10 +++++++-- .../composer/hooks/useClipboardPaste.ts | 7 ++++++- src/global/actions/ui/misc.ts | 21 +++++++++++++++++++ src/global/initialState.ts | 1 + src/global/types/actions.ts | 4 ++++ src/global/types/globalState.ts | 2 ++ src/global/types/tabState.ts | 1 + src/types/index.ts | 2 ++ 9 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/components/common/Composer.tsx b/src/components/common/Composer.tsx index fd90ba128..efbee2119 100644 --- a/src/components/common/Composer.tsx +++ b/src/components/common/Composer.tsx @@ -466,6 +466,8 @@ const Composer: FC = ({ updateChatSilentPosting, updateInsertingPeerIdMention, updateDraftSuggestedPostInfo, + updateShouldSaveAttachmentsCompression, + applyDefaultAttachmentsCompression, } = getActions(); const oldLang = useOldLang(); @@ -551,6 +553,12 @@ const Composer: FC = ({ const hasAttachments = Boolean(attachments.length); const [nextText, setNextText] = useState(undefined); + useEffect(() => { + if (!attachments.length || !attachments) { + updateShouldSaveAttachmentsCompression({ shouldSave: false }); + } + }, [attachments]); + const { canSendStickers, canSendGifs, canAttachMedia, canAttachPolls, canAttachEmbedLinks, canAttachToDoLists, canSendVoices, canSendPlainText, canSendAudios, canSendVideos, canSendPhotos, canSendDocuments, @@ -1344,6 +1352,15 @@ const Composer: FC = ({ } }, [handleFileSelect, requestedDraftFiles, resetOpenChatWithDraft]); + useEffect(() => { + if (requestedDraftFiles?.length) { + updateShouldSaveAttachmentsCompression({ shouldSave: true }); + applyDefaultAttachmentsCompression(); + } else { + updateShouldSaveAttachmentsCompression({ shouldSave: false }); + } + }, [requestedDraftFiles, updateShouldSaveAttachmentsCompression, applyDefaultAttachmentsCompression]); + const handleCustomEmojiSelect = useLastCallback((emoji: ApiSticker, inInputId?: string) => { const emojiSetId = 'id' in emoji.stickerSetInfo && emoji.stickerSetInfo.id; if (!emoji.isFree && !isCurrentUserPremium && !isChatWithSelf && emojiSetId !== chatEmojiSetId) { diff --git a/src/components/middle/composer/AttachmentModal.tsx b/src/components/middle/composer/AttachmentModal.tsx index 6d0ed23dd..bcdd43918 100644 --- a/src/components/middle/composer/AttachmentModal.tsx +++ b/src/components/middle/composer/AttachmentModal.tsx @@ -21,7 +21,7 @@ import { } from '../../../config'; import { requestMutation } from '../../../lib/fasterdom/fasterdom'; import { getAttachmentMediaType } from '../../../global/helpers'; -import { selectChatFullInfo, selectIsChatWithSelf } from '../../../global/selectors'; +import { selectChatFullInfo, selectIsChatWithSelf, selectTabState } from '../../../global/selectors'; import { selectCurrentLimit } from '../../../global/selectors/limits'; import { selectSharedSettings } from '../../../global/selectors/sharedState'; import buildClassName from '../../../util/buildClassName'; @@ -106,6 +106,7 @@ type StateProps = { customEmojiForEmoji?: ApiSticker[]; captionLimit: number; attachmentSettings: GlobalState['attachmentSettings']; + shouldSaveAttachmentsCompression?: boolean; }; const ATTACHMENT_MODAL_INPUT_ID = 'caption-input-text'; @@ -133,6 +134,7 @@ const AttachmentModal: FC = ({ shouldSuggestCustomEmoji, customEmojiForEmoji, attachmentSettings, + shouldSaveAttachmentsCompression, shouldForceCompression, shouldForceAsFile, isForCurrentMessageList, @@ -305,7 +307,9 @@ const AttachmentModal: FC = ({ : isSilent ? onSendSilent : onSend; send(isSendingCompressed, shouldSendGrouped, isInvertedMedia); updateAttachmentSettings({ - shouldCompress: isSendingCompressed, + ...(shouldSaveAttachmentsCompression && { + defaultAttachmentCompression: attachmentSettings.shouldCompress ? 'compress' : 'original', + }), shouldSendGrouped, isInvertedMedia, shouldSendInHighQuality, @@ -784,6 +788,7 @@ export default memo(withGlobal( attachmentSettings, } = global; + const { shouldSaveAttachmentsCompression } = selectTabState(global); const chatFullInfo = selectChatFullInfo(global, chatId); const isChatWithSelf = selectIsChatWithSelf(global, chatId); const { shouldSuggestCustomEmoji } = global.settings.byKey; @@ -802,6 +807,7 @@ export default memo(withGlobal( customEmojiForEmoji: customEmojis.forEmoji.stickers, captionLimit: selectCurrentLimit(global, 'captionLength'), attachmentSettings, + shouldSaveAttachmentsCompression, }; }, )(AttachmentModal)); diff --git a/src/components/middle/composer/hooks/useClipboardPaste.ts b/src/components/middle/composer/hooks/useClipboardPaste.ts index b5dff20f4..810e30489 100644 --- a/src/components/middle/composer/hooks/useClipboardPaste.ts +++ b/src/components/middle/composer/hooks/useClipboardPaste.ts @@ -32,7 +32,10 @@ const useClipboardPaste = ( shouldStripCustomEmoji?: boolean, onCustomEmojiStripped?: VoidFunction, ) => { - const { showNotification } = getActions(); + const { + showNotification, + updateShouldSaveAttachmentsCompression, + applyDefaultAttachmentsCompression } = getActions(); const lang = useLang(); useEffect(() => { @@ -127,6 +130,8 @@ const useClipboardPaste = ( } if (shouldSetAttachments) { + updateShouldSaveAttachmentsCompression({ shouldSave: true }); + applyDefaultAttachmentsCompression(); setAttachments(editedMessage ? newAttachments : (attachments) => attachments.concat(newAttachments)); } diff --git a/src/global/actions/ui/misc.ts b/src/global/actions/ui/misc.ts index 8b194ca19..f13aeebaa 100644 --- a/src/global/actions/ui/misc.ts +++ b/src/global/actions/ui/misc.ts @@ -544,6 +544,27 @@ addActionHandler('updateAttachmentSettings', (global, actions, payload): ActionR }; }); +addActionHandler('updateShouldSaveAttachmentsCompression', (global, actions, payload): ActionReturnType => { + const { shouldSave, tabId = getCurrentTabId() } = payload; + + return updateTabState(global, { + shouldSaveAttachmentsCompression: shouldSave, + }, tabId); +}); + +addActionHandler('applyDefaultAttachmentsCompression', (global): ActionReturnType => { + const { defaultAttachmentCompression } = global.attachmentSettings; + const shouldCompress = defaultAttachmentCompression === 'compress'; + + return { + ...global, + attachmentSettings: { + ...global.attachmentSettings, + shouldCompress, + }, + }; +}); + addActionHandler('requestEffectInComposer', (global, actions, payload): ActionReturnType => { const { tabId = getCurrentTabId() } = payload; diff --git a/src/global/initialState.ts b/src/global/initialState.ts index 17f6b743c..3ef7f3b6c 100644 --- a/src/global/initialState.ts +++ b/src/global/initialState.ts @@ -180,6 +180,7 @@ export const INITIAL_GLOBAL_STATE: GlobalState = { attachmentSettings: { shouldCompress: true, + defaultAttachmentCompression: 'compress', shouldSendGrouped: true, isInvertedMedia: undefined, webPageMediaSize: undefined, diff --git a/src/global/types/actions.ts b/src/global/types/actions.ts index eafab1db1..85383411d 100644 --- a/src/global/types/actions.ts +++ b/src/global/types/actions.ts @@ -67,6 +67,7 @@ import type { ReducerAction } from '../../hooks/useReducer'; import type { P2pMessage } from '../../lib/secret-sauce'; import type { AccountSettings, + AttachmentCompression, AudioOrigin, CallSound, ChatListType, @@ -2280,12 +2281,15 @@ export interface ActionPayloads { } & WithTabId; updateAttachmentSettings: { + defaultAttachmentCompression?: AttachmentCompression; shouldCompress?: boolean; shouldSendGrouped?: boolean; isInvertedMedia?: true; webPageMediaSize?: WebPageMediaSize; shouldSendInHighQuality?: boolean; }; + updateShouldSaveAttachmentsCompression: { shouldSave: boolean } & WithTabId; + applyDefaultAttachmentsCompression: undefined; saveEffectInDraft: { chatId: string; diff --git a/src/global/types/globalState.ts b/src/global/types/globalState.ts index fbf9c742c..ca0e1725e 100644 --- a/src/global/types/globalState.ts +++ b/src/global/types/globalState.ts @@ -54,6 +54,7 @@ import type { } from '../../api/types'; import type { AccountSettings, + AttachmentCompression, BotAppPermissions, ChatListType, ChatTranslatedMessages, @@ -120,6 +121,7 @@ export type GlobalState = { attachmentSettings: { shouldCompress: boolean; + defaultAttachmentCompression: AttachmentCompression; shouldSendGrouped: boolean; isInvertedMedia?: true; webPageMediaSize?: WebPageMediaSize; diff --git a/src/global/types/tabState.ts b/src/global/types/tabState.ts index eabeeb980..868272cf5 100644 --- a/src/global/types/tabState.ts +++ b/src/global/types/tabState.ts @@ -888,4 +888,5 @@ export type TabState = { isWaitingForStarGiftUpgrade?: true; isWaitingForStarGiftTransfer?: true; insertingPeerIdMention?: string; + shouldSaveAttachmentsCompression?: boolean; }; diff --git a/src/types/index.ts b/src/types/index.ts index 7f9a8a5ce..6613499e5 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -667,6 +667,8 @@ export interface Point { export type WebPageMediaSize = 'large' | 'small'; +export type AttachmentCompression = 'compress' | 'original'; + export type StarGiftCategory = number | 'all' | 'limited' | 'stock' | 'resale'; export type CallSound = (