From 95a8c635917de88e405a3d806e0de1c1e223449f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Thu, 14 Nov 2024 12:03:17 +0100 Subject: [PATCH] pl-fe: more reducer migrations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- packages/pl-fe/src/actions/compose.ts | 14 +-- .../compose/components/compose-form.tsx | 2 +- .../compose/components/language-dropdown.tsx | 22 ++-- .../compose/components/polls/poll-form.tsx | 6 +- .../compose/components/reply-mentions.tsx | 2 +- .../compose/components/spoiler-input.tsx | 2 +- .../src/features/compose/editor/index.tsx | 4 +- packages/pl-fe/src/reducers/compose.ts | 103 ++++++++---------- 8 files changed, 76 insertions(+), 79 deletions(-) diff --git a/packages/pl-fe/src/actions/compose.ts b/packages/pl-fe/src/actions/compose.ts index c2b03bd29..051d621c5 100644 --- a/packages/pl-fe/src/actions/compose.ts +++ b/packages/pl-fe/src/actions/compose.ts @@ -375,7 +375,7 @@ const submitCompose = (composeId: string, opts: SubmitComposeOpts = {}) => const mentions: string[] | null = status.match(/(?:^|\s)@([a-z\d_-]+(?:@(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]+)?)/gi); if (mentions) { - to = to.union(mentions.map(mention => mention.replace(/ /g, '').trim().slice(1))); + to = [...new Set([...to, ...mentions.map(mention => mention.replace(/ /g, '').trim().slice(1))])]; } dispatch(submitComposeRequest(composeId)); @@ -399,26 +399,26 @@ const submitCompose = (composeId: string, opts: SubmitComposeOpts = {}) => content_type: contentType, scheduled_at: compose.schedule?.toISOString(), language: compose.language || compose.suggested_language || undefined, - to: to.size ? to.toArray() : undefined, + to: to.length ? to : undefined, local_only: !compose.federated, }; if (compose.poll) { params.poll = { - options: compose.poll.options.toArray(), + options: compose.poll.options, expires_in: compose.poll.expires_in, multiple: compose.poll.multiple, hide_totals: compose.poll.hide_totals, - options_map: compose.poll.options_map.toJS(), + options_map: compose.poll.options_map, }; } - if (compose.language && compose.textMap.size) { - params.status_map = compose.textMap.toJS(); + if (compose.language && Object.keys(compose.textMap).length) { + params.status_map = compose.textMap; params.status_map[compose.language] = status; if (params.spoiler_text) { - params.spoiler_text_map = compose.spoilerTextMap.toJS(); + params.spoiler_text_map = compose.spoilerTextMap; params.spoiler_text_map[compose.language] = compose.spoiler_text; } diff --git a/packages/pl-fe/src/features/compose/components/compose-form.tsx b/packages/pl-fe/src/features/compose/components/compose-form.tsx index b7f0ac521..9475c62d9 100644 --- a/packages/pl-fe/src/features/compose/components/compose-form.tsx +++ b/packages/pl-fe/src/features/compose/components/compose-form.tsx @@ -189,7 +189,7 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab ), [features, id, anyMedia]); - const showModifiers = !condensed && (compose.media_attachments.size || compose.is_uploading || compose.poll?.options.size || compose.schedule); + const showModifiers = !condensed && (compose.media_attachments.size || compose.is_uploading || compose.poll?.options.length || compose.schedule); const composeModifiers = showModifiers && ( diff --git a/packages/pl-fe/src/features/compose/components/language-dropdown.tsx b/packages/pl-fe/src/features/compose/components/language-dropdown.tsx index 0d4437fba..35b7ad056 100644 --- a/packages/pl-fe/src/features/compose/components/language-dropdown.tsx +++ b/packages/pl-fe/src/features/compose/components/language-dropdown.tsx @@ -53,11 +53,13 @@ const getLanguageDropdown = (composeId: string): React.FC => textMap, } = useCompose(composeId); + const hasMultipleLanguages = !!Object.keys(textMap).length; + const handleOptionClick: React.EventHandler = (e: MouseEvent | KeyboardEvent) => { const value = (e.currentTarget as HTMLElement)?.getAttribute('data-index') as Language; - if (textMap.size) { - if (!(textMap.has(value) || language === value)) return; + if (Object.keys(textMap).length) { + if (!(value in textMap || language === value)) return; dispatch(changeComposeModifiedLanguage(composeId, value)); } else { @@ -99,11 +101,11 @@ const getLanguageDropdown = (composeId: string): React.FC => return [...languages].sort((a, b) => { // Push current selection to the top of the list - if (textMap.has(a[0])) { + if (a[0] in textMap) { if (b[0] === language) return 1; return -1; } - if (textMap.has(b[0])) { + if (b[0] in textMap) { if (a[0] === language) return -1; return 1; } @@ -180,9 +182,9 @@ const getLanguageDropdown = (composeId: string): React.FC => 'flex w-full gap-2 p-2.5 text-left text-sm text-gray-700 dark:text-gray-400', { 'bg-gray-100 dark:bg-gray-800 black:bg-gray-900 cursor-pointer hover:bg-gray-200 dark:hover:bg-gray-700': modified, - 'cursor-pointer hover:bg-gray-100 black:hover:bg-gray-900 dark:hover:bg-gray-800': !textMap.size || textMap.has(code), + 'cursor-pointer hover:bg-gray-100 black:hover:bg-gray-900 dark:hover:bg-gray-800': !hasMultipleLanguages || code in textMap, 'cursor-pointer': active, - 'cursor-default': !active && !(!textMap.size || textMap.has(code)), + 'cursor-default': !active && !(!hasMultipleLanguages || code in textMap), }, )} aria-selected={active} @@ -196,7 +198,7 @@ const getLanguageDropdown = (composeId: string): React.FC => {name} {features.multiLanguage && !!language && !active && ( - textMap.has(code) ? ( + code in textMap ? ( @@ -228,11 +230,13 @@ const LanguageDropdownButton: React.FC = ({ composeId } textMap, } = useCompose(composeId); + const languagesCount = Object.keys(textMap).length; + let buttonLabel = intl.formatMessage(messages.languagePrompt); if (language) { const list: string[] = [languagesObject[modifiedLanguage || language]]; - if (textMap.size) list.push(intl.formatMessage(messages.multipleLanguages, { - count: textMap.size, + if (languagesCount) list.push(intl.formatMessage(messages.multipleLanguages, { + count: languagesCount, })); buttonLabel = intl.formatList(list); } else if (suggestedLanguage) buttonLabel = intl.formatMessage(messages.languageSuggestion, { diff --git a/packages/pl-fe/src/features/compose/components/polls/poll-form.tsx b/packages/pl-fe/src/features/compose/components/polls/poll-form.tsx index 37524b0f0..624050d5f 100644 --- a/packages/pl-fe/src/features/compose/components/polls/poll-form.tsx +++ b/packages/pl-fe/src/features/compose/components/polls/poll-form.tsx @@ -122,7 +122,7 @@ const PollForm: React.FC = ({ composeId }) => { const { poll, language, modified_language: modifiedLanguage } = useCompose(composeId); - const options = !modifiedLanguage || modifiedLanguage === language ? poll?.options : poll?.options_map.map((option, key) => option.get(modifiedLanguage, poll.options.get(key)!)); + const options = !modifiedLanguage || modifiedLanguage === language ? poll?.options : poll?.options_map.map((option, key) => option[modifiedLanguage] || poll.options[key]); const expiresIn = poll?.expires_in; const isMultiple = poll?.multiple; @@ -156,7 +156,7 @@ const PollForm: React.FC = ({ composeId }) => { onChange={onChangeOption} onRemove={onRemoveOption} maxChars={maxOptionChars} - numOptions={options.size} + numOptions={options.length} onRemovePoll={onRemovePoll} /> ))} @@ -164,7 +164,7 @@ const PollForm: React.FC = ({ composeId }) => {
- {options.size < maxOptions && ( + {options.length < maxOptions && (