Merge branch 'rm-lodash' into 'main'

Replace some usages of lodash

See merge request soapbox-pub/soapbox!3268
This commit is contained in:
Alex Gleason 2024-11-19 04:43:56 +00:00
commit 17440c4dbc
9 changed files with 24 additions and 30 deletions

View File

@ -3,9 +3,6 @@
* @module soapbox/build-config
*/
// eslint-disable-next-line import/extensions
import trimEnd from 'lodash/trimEnd.js';
const {
NODE_ENV,
BACKEND_URL,
@ -13,9 +10,9 @@ const {
SENTRY_DSN,
} = process.env;
const sanitizeURL = (url: string | undefined = ''): string => {
const sanitizeURL = (url: string = ''): string => {
try {
return trimEnd(new URL(url).toString(), '/');
return new URL(url).href;
} catch {
return '';
}

View File

@ -1,9 +1,8 @@
import pick from 'lodash/pick';
import { describe, expect, it } from 'vitest';
import search, { addCustomToPool } from './search.ts';
const trimEmojis = (emoji: any) => pick(emoji, ['id', 'unified', 'native', 'custom']);
const trimEmojis = ({ id, unified, native, custom }: any) => ({ id, unified, native, custom });
describe('emoji_index', () => {
it('should give same result for emoji_index_light and emoji-mart', () => {

View File

@ -1,5 +1,4 @@
import clsx from 'clsx';
import noop from 'lodash/noop';
import { Suspense } from 'react';
import { toggleStatusReport } from 'soapbox/actions/reports.ts';
@ -66,7 +65,7 @@ const StatusCheckBox: React.FC<IStatusCheckBox> = ({ id, disabled }) => {
media={status.media_attachments}
sensitive={status.sensitive}
height={110}
onOpenMedia={noop}
onOpenMedia={() => {}}
/>
);
}

View File

@ -1,5 +1,3 @@
import noop from 'lodash/noop';
import PollOption from 'soapbox/components/polls/poll-option.tsx';
import Stack from 'soapbox/components/ui/stack.tsx';
import { useAppSelector } from 'soapbox/hooks/useAppSelector.ts';
@ -26,7 +24,7 @@ const PollPreview: React.FC<IPollPreview> = ({ pollId }) => {
index={i}
showResults={false}
active={false}
onToggle={noop}
onToggle={() => {}}
/>
))}
</Stack>

View File

@ -4,7 +4,6 @@ import {
Record as ImmutableRecord,
fromJS,
} from 'immutable';
import trimStart from 'lodash/trimStart';
import { normalizeUsername } from 'soapbox/utils/input.ts';
import { toTailwind } from 'soapbox/utils/tailwind.ts';
@ -123,7 +122,7 @@ type SoapboxConfigMap = ImmutableMap<string, any>;
const normalizeCryptoAddress = (address: unknown): CryptoAddress => {
return CryptoAddressRecord(ImmutableMap(fromJS(address))).update('ticker', ticker => {
return trimStart(ticker, '$').toLowerCase();
return ticker.replace(/^\$/, '').toLowerCase();
});
};

View File

@ -1,5 +1,4 @@
import { Map as ImmutableMap } from 'immutable';
import sumBy from 'lodash/sumBy';
import { useEffect } from 'react';
import { beforeEach, describe, expect, it } from 'vitest';
@ -79,8 +78,9 @@ describe('isLastMessage', () => {
],
pageParams: [undefined],
};
const initialFlattenedData = flattenPages(initialQueryData);
expect(sumBy(initialFlattenedData, (chat: IChat) => chat.unread)).toBe(0);
const initialFlattenedData = flattenPages<IChat>(initialQueryData);
const count = initialFlattenedData!.reduce((n, chat) => n + chat.unread, 0);
expect(count).toBe(0);
queryClient.setQueryData(ChatKeys.chatSearch(), initialQueryData);
@ -99,7 +99,8 @@ describe('isLastMessage', () => {
pageParams: [undefined],
};
const initialFlattenedData = flattenPages(initialQueryData);
expect(sumBy(initialFlattenedData, (chat: IChat) => chat.unread)).toBe(0);
const count = initialFlattenedData!.reduce((n, chat) => n + chat.unread, 0);
expect(count).toBe(0);
queryClient.setQueryData(ChatKeys.chatSearch(), initialQueryData);
@ -297,7 +298,9 @@ describe('useChatActions', () => {
pageParams: [undefined],
};
const initialFlattenedData = flattenPages(initialQueryData);
expect(sumBy(initialFlattenedData, (chat: IChat) => chat.unread)).toBe(0);
const count = initialFlattenedData!.reduce((n, chat) => n + chat.unread, 0);
expect(count).toBe(0);
queryClient.setQueryData(ChatKeys.chatSearch(), initialQueryData);
@ -308,8 +311,10 @@ describe('useChatActions', () => {
});
const nextQueryData = queryClient.getQueryData(ChatKeys.chatSearch());
const nextFlattenedData = flattenPages(nextQueryData as any);
expect(sumBy(nextFlattenedData as any, (chat: IChat) => chat.unread)).toBe(nextUnreadCount);
const nextFlattenedData = flattenPages<IChat>(nextQueryData as any);
const nextCount = nextFlattenedData!.reduce((n, chat) => n + chat.unread, 0);
expect(nextCount).toBe(nextUnreadCount);
});
});

View File

@ -1,5 +1,4 @@
import { InfiniteData, keepPreviousData, useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query';
import sumBy from 'lodash/sumBy';
import { importFetchedAccount, importFetchedAccounts } from 'soapbox/actions/importer/index.ts';
import { ChatWidgetScreens, useChatContext } from 'soapbox/contexts/chat-context.tsx';
@ -146,7 +145,7 @@ const useChats = (search?: string) => {
const { next } = response.pagination();
const hasMore = !!next;
setUnreadChatsCount(Number(response.headers.get('x-unread-messages-count')) || sumBy(data, (chat) => chat.unread));
setUnreadChatsCount(Number(response.headers.get('x-unread-messages-count')) || data.reduce((n, chat) => n + chat.unread, 0));
// Set the relationships to these users in the redux store.
fetchRelationships.mutate({ accountIds: data.map((item) => item.account.id) });
@ -225,17 +224,17 @@ const useChatActions = (chatId: string) => {
.then(async (response) => {
const data = await response.json();
updatePageItem(ChatKeys.chatSearch(), data, (o, n) => o.id === n.id);
const queryData = queryClient.getQueryData<InfiniteData<PaginatedResult<unknown>>>(ChatKeys.chatSearch());
const queryData = queryClient.getQueryData<InfiniteData<PaginatedResult<IChat>>>(ChatKeys.chatSearch());
if (queryData) {
const flattenedQueryData: any = flattenPages(queryData)?.map((chat: any) => {
const flattenedQueryData = flattenPages<IChat>(queryData)?.map((chat: any) => {
if (chat.id === data.id) {
return data;
} else {
return chat;
}
});
setUnreadChatsCount(sumBy(flattenedQueryData, (chat: IChat) => chat.unread));
setUnreadChatsCount(flattenedQueryData?.reduce((n, chat) => n + chat.unread, 0));
}
return data;

View File

@ -1,5 +1,4 @@
import { InfiniteData } from '@tanstack/react-query';
import sumBy from 'lodash/sumBy';
import { normalizeChatMessage } from 'soapbox/normalizers/index.ts';
import { ChatKeys } from 'soapbox/queries/chats.ts';
@ -83,7 +82,7 @@ const getUnreadChatsCount = (): number => {
queryClient.getQueryData<InfiniteData<PaginatedResult<Chat>>>(ChatKeys.chatSearch()),
);
return sumBy(chats, chat => chat.unread);
return chats?.reduce((acc, chat) => acc + chat.unread, 0) ?? 0;
};
/** Update the query cache for an individual Chat Message */

View File

@ -4,7 +4,6 @@ import {
Set as ImmutableSet,
fromJS,
} from 'immutable';
import trimStart from 'lodash/trimStart';
import { type MRFSimple, mrfSimpleSchema } from 'soapbox/schemas/pleroma.ts';
@ -27,7 +26,7 @@ const toSimplePolicy = (configs: ImmutableList<Config>): MRFSimple => {
const reducer = (acc: ImmutableMap<string, any>, curr: ImmutableMap<string, any>) => {
const key = curr.getIn(['tuple', 0]) as string;
const hosts = curr.getIn(['tuple', 1]) as ImmutableList<string>;
return acc.set(trimStart(key, ':'), ImmutableSet(hosts));
return acc.set(key.replace(/^:/, ''), ImmutableSet(hosts));
};
if (config?.get) {