Refactor: replace Immutable.js methods with plain JavaScript equivalents - "action/compose"

remove-immutable-compose
danidfra 2024-12-18 17:20:41 -03:00
rodzic ed629e69fa
commit 2127bd52ce
1 zmienionych plików z 12 dodań i 12 usunięć

Wyświetl plik

@ -1,5 +1,4 @@
import { throttle } from 'es-toolkit'; import { throttle } from 'es-toolkit';
import { List as ImmutableList } from 'immutable';
import { defineMessages, IntlShape } from 'react-intl'; import { defineMessages, IntlShape } from 'react-intl';
import { HTTPError } from 'soapbox/api/HTTPError.ts'; import { HTTPError } from 'soapbox/api/HTTPError.ts';
@ -249,16 +248,16 @@ const handleComposeSubmit = (dispatch: AppDispatch, getState: () => RootState, c
}; };
const needsDescriptions = (state: RootState, composeId: string) => { const needsDescriptions = (state: RootState, composeId: string) => {
const media = state.compose.get(composeId)!.media_attachments; const media = state.compose[composeId].media_attachments;
const missingDescriptionModal = getSettings(state).get('missingDescriptionModal'); const missingDescriptionModal = getSettings(state).get('missingDescriptionModal');
const hasMissing = media.filter(item => !item.description).size > 0; const hasMissing = media.filter(item => !item.description).length > 0;
return missingDescriptionModal && hasMissing; return missingDescriptionModal && hasMissing;
}; };
const validateSchedule = (state: RootState, composeId: string) => { const validateSchedule = (state: RootState, composeId: string) => {
const schedule = state.compose.get(composeId)?.schedule; const schedule = state.compose[composeId]?.schedule;
if (!schedule) return true; if (!schedule) return true;
const fiveMinutesFromNow = new Date(new Date().getTime() + 300000); const fiveMinutesFromNow = new Date(new Date().getTime() + 300000);
@ -278,7 +277,7 @@ const submitCompose = (composeId: string, opts: SubmitComposeOpts = {}) =>
if (!isLoggedIn(getState)) return; if (!isLoggedIn(getState)) return;
const state = getState(); const state = getState();
const compose = state.compose.get(composeId)!; const compose = state.compose[composeId];
const status = compose.text; const status = compose.text;
const media = compose.media_attachments; const media = compose.media_attachments;
@ -290,7 +289,7 @@ const submitCompose = (composeId: string, opts: SubmitComposeOpts = {}) =>
return; return;
} }
if ((!status || !status.length) && media.size === 0) { if ((!status || !status.length) && media.length === 0) {
return; return;
} }
@ -307,7 +306,8 @@ const submitCompose = (composeId: string, opts: SubmitComposeOpts = {}) =>
const mentions: string[] | null = status.match(/(?:^|\s)@([^@\s]+(?:@[^@\s]+)?)/gi); const mentions: string[] | null = status.match(/(?:^|\s)@([^@\s]+(?:@[^@\s]+)?)/gi);
if (mentions) { if (mentions) {
to = to.union(mentions.map(mention => mention.trim().slice(1))); const trimmedMentions = mentions.map(mention => mention.trim().slice(1));
to = new Set([...to, ...trimmedMentions]);
} }
dispatch(submitComposeRequest(composeId)); dispatch(submitComposeRequest(composeId));
@ -366,9 +366,9 @@ const uploadCompose = (composeId: string, files: FileList, intl: IntlShape) =>
if (!isLoggedIn(getState)) return; if (!isLoggedIn(getState)) return;
const attachmentLimit = getState().instance.configuration.statuses.max_media_attachments; const attachmentLimit = getState().instance.configuration.statuses.max_media_attachments;
const media = getState().compose.get(composeId)?.media_attachments; const media = getState().compose[composeId]?.media_attachments;
const progress: number[] = new Array(files.length).fill(0); const progress: number[] = new Array(files.length).fill(0);
const mediaCount = media ? media.size : 0; const mediaCount = media ? media.length : 0;
if (files.length + mediaCount > attachmentLimit) { if (files.length + mediaCount > attachmentLimit) {
toast.error(messages.uploadErrorLimit); toast.error(messages.uploadErrorLimit);
@ -614,7 +614,7 @@ const selectComposeSuggestion = (composeId: string, position: number, token: str
dispatch(action); dispatch(action);
}; };
const updateSuggestionTags = (composeId: string, token: string, tags: ImmutableList<Tag>) => ({ const updateSuggestionTags = (composeId: string, token: string, tags: Tag[]) => ({
type: COMPOSE_SUGGESTION_TAGS_UPDATE, type: COMPOSE_SUGGESTION_TAGS_UPDATE,
id: composeId, id: composeId,
token, token,
@ -630,14 +630,14 @@ const updateTagHistory = (composeId: string, tags: string[]) => ({
const insertIntoTagHistory = (composeId: string, recognizedTags: APIEntity[], text: string) => const insertIntoTagHistory = (composeId: string, recognizedTags: APIEntity[], text: string) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
const state = getState(); const state = getState();
const oldHistory = state.compose.get(composeId)!.tagHistory; const oldHistory = state.compose[composeId]!.tagHistory;
const me = state.me; const me = state.me;
const names = recognizedTags const names = recognizedTags
.filter(tag => text.match(new RegExp(`#${tag.name}`, 'i'))) .filter(tag => text.match(new RegExp(`#${tag.name}`, 'i')))
.map(tag => tag.name); .map(tag => tag.name);
const intersectedOldHistory = oldHistory.filter(name => names.findIndex(newName => newName.toLowerCase() === name.toLowerCase()) === -1); const intersectedOldHistory = oldHistory.filter(name => names.findIndex(newName => newName.toLowerCase() === name.toLowerCase()) === -1);
names.push(...intersectedOldHistory.toJS()); names.push(...intersectedOldHistory);
const newHistory = names.slice(0, 1000); const newHistory = names.slice(0, 1000);