[Glitch] Reduce account overfetching when displaying collection lists
Port 0caf334891e0a5f4e995f1d667eb83d3aad7e41e to glitch-soc Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
6032a17cc2
commit
45628d7e6e
@ -3,6 +3,7 @@ import { createAction } from '@reduxjs/toolkit';
|
||||
import {
|
||||
apiRemoveAccountFromFollowers,
|
||||
apiGetEndorsedAccounts,
|
||||
apiGetAccounts,
|
||||
} from 'flavours/glitch/api/accounts';
|
||||
import type { ApiRelationshipJSON } from 'flavours/glitch/api_types/relationships';
|
||||
import { createDataLoadingThunk } from 'flavours/glitch/store/typed_functions';
|
||||
@ -113,3 +114,12 @@ export const fetchEndorsedAccounts = createDataLoadingThunk(
|
||||
return data;
|
||||
},
|
||||
);
|
||||
|
||||
export const fetchAccounts = createDataLoadingThunk(
|
||||
'accounts/multi_accounts',
|
||||
({ accountIds }: { accountIds: string[] }) => apiGetAccounts(accountIds),
|
||||
(data, { dispatch }) => {
|
||||
dispatch(importFetchedAccounts(data));
|
||||
return data;
|
||||
},
|
||||
);
|
||||
|
||||
@ -19,6 +19,11 @@ import type {
|
||||
ApiProfileUpdateParams,
|
||||
} from '../api_types/profile';
|
||||
|
||||
export const apiGetAccounts = (ids: string[]) =>
|
||||
apiRequestGet<ApiAccountJSON[]>('v1/accounts', {
|
||||
id: ids,
|
||||
});
|
||||
|
||||
export const apiSubmitAccountNote = (id: string, value: string) =>
|
||||
apiRequestPost<ApiRelationshipJSON>(`v1/accounts/${id}/note`, {
|
||||
comment: value,
|
||||
|
||||
@ -15,7 +15,7 @@ export const apiUpdate = (list: Partial<ApiListJSON>) =>
|
||||
|
||||
export const apiGetLists = () => apiRequestGet<ApiListJSON[]>('v1/lists');
|
||||
|
||||
export const apiGetAccounts = (listId: string) =>
|
||||
export const apiGetListAccounts = (listId: string) =>
|
||||
apiRequestGet<ApiAccountJSON[]>(`v1/lists/${listId}/accounts`, {
|
||||
limit: 0,
|
||||
});
|
||||
|
||||
@ -15,7 +15,7 @@ import { fetchList } from 'flavours/glitch/actions/lists';
|
||||
import { openModal } from 'flavours/glitch/actions/modal';
|
||||
import { apiFollowAccount } from 'flavours/glitch/api/accounts';
|
||||
import {
|
||||
apiGetAccounts,
|
||||
apiGetListAccounts,
|
||||
apiAddAccountToList,
|
||||
apiRemoveAccountFromList,
|
||||
} from 'flavours/glitch/api/lists';
|
||||
@ -184,7 +184,7 @@ const ListMembers: React.FC<{
|
||||
if (id) {
|
||||
dispatch(fetchList(id));
|
||||
|
||||
void apiGetAccounts(id)
|
||||
void apiGetListAccounts(id)
|
||||
.then((data) => {
|
||||
dispatch(importFetchedAccounts(data));
|
||||
setAccountIds(data.map((a) => a.id));
|
||||
|
||||
@ -12,7 +12,7 @@ import ChevronRightIcon from '@/material-icons/400-24px/chevron_right.svg?react'
|
||||
import ListAltIcon from '@/material-icons/400-24px/list_alt.svg?react';
|
||||
import { fetchList } from 'flavours/glitch/actions/lists';
|
||||
import { createList, updateList } from 'flavours/glitch/actions/lists_typed';
|
||||
import { apiGetAccounts } from 'flavours/glitch/api/lists';
|
||||
import { apiGetListAccounts } from 'flavours/glitch/api/lists';
|
||||
import type { ApiAccountJSON } from 'flavours/glitch/api_types/accounts';
|
||||
import type { RepliesPolicyType } from 'flavours/glitch/api_types/lists';
|
||||
import { Avatar } from 'flavours/glitch/components/avatar';
|
||||
@ -44,7 +44,7 @@ const MembersLink: React.FC<{
|
||||
const [avatarAccounts, setAvatarAccounts] = useState<ApiAccountJSON[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
void apiGetAccounts(id)
|
||||
void apiGetListAccounts(id)
|
||||
.then((data) => {
|
||||
setAvatarCount(data.length);
|
||||
setAvatarAccounts(data.slice(0, 3));
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import type { PayloadAction } from '@reduxjs/toolkit';
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
import { fetchAccounts } from '@/flavours/glitch/actions/accounts_typed';
|
||||
import { importFetchedAccounts } from '@/flavours/glitch/actions/importer';
|
||||
import {
|
||||
apiCreateCollection,
|
||||
@ -20,6 +21,7 @@ import type {
|
||||
CollectionAccountItem,
|
||||
} from '@/flavours/glitch/api_types/collections';
|
||||
import { initialState, me } from '@/flavours/glitch/initial_state';
|
||||
import type { AppDispatch } from '@/flavours/glitch/store';
|
||||
import {
|
||||
createAppAsyncThunk,
|
||||
createAppSelector,
|
||||
@ -322,16 +324,42 @@ const collectionSlice = createSlice({
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Prefetch accounts whose avatars will be displayed in the collection list
|
||||
*/
|
||||
async function importAccountsForPreviewCard(
|
||||
collections: ApiCollectionJSON[],
|
||||
dispatch: AppDispatch,
|
||||
) {
|
||||
const previewAccountIds = collections
|
||||
.flatMap((collection) =>
|
||||
collection.items.slice(0, 3).map((item) => item.account_id),
|
||||
)
|
||||
.filter((id): id is string => !!id);
|
||||
|
||||
await dispatch(
|
||||
fetchAccounts({
|
||||
accountIds: previewAccountIds,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export const fetchCollectionsCreatedByAccount = createDataLoadingThunk(
|
||||
`${collectionSlice.name}/fetchCollectionsCreatedByAccount`,
|
||||
({ accountId }: { accountId: string }) =>
|
||||
apiGetCollectionsCreatedByAccount(accountId),
|
||||
async ({ collections }, { dispatch }) => {
|
||||
await importAccountsForPreviewCard(collections, dispatch);
|
||||
},
|
||||
);
|
||||
|
||||
export const fetchCollectionsFeaturingAccount = createDataLoadingThunk(
|
||||
`${collectionSlice.name}/fetchCollectionsFeaturingAccount`,
|
||||
({ accountId }: { accountId: string }) =>
|
||||
apiGetCollectionsFeaturingAccount(accountId),
|
||||
async ({ collections }, { dispatch }) => {
|
||||
await importAccountsForPreviewCard(collections, dispatch);
|
||||
},
|
||||
);
|
||||
|
||||
export const fetchCollection = createDataLoadingThunk(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user