sforkowany z mirror/soapbox
Chats: deduplicate items
rodzic
4a48e9d510
commit
cb7f0aeab8
|
@ -136,9 +136,9 @@ const useChats = (search?: string) => {
|
|||
const nextPageLink = pageParam?.link;
|
||||
const uri = nextPageLink || endpoint;
|
||||
const response = await api.get<IChat[]>(uri, {
|
||||
params: {
|
||||
params: search ? {
|
||||
search,
|
||||
},
|
||||
} : undefined,
|
||||
});
|
||||
const { data } = response;
|
||||
|
||||
|
|
|
@ -8,12 +8,27 @@ export interface PaginatedResult<T> {
|
|||
link?: string,
|
||||
}
|
||||
|
||||
/** Deduplicate an array of entities by their ID. */
|
||||
const deduplicate = <T>(entities: T[]): T[] => {
|
||||
const map = entities.reduce<Map<string, T>>((result, entity) => {
|
||||
// @ts-expect-error Entity might not have an ID... but it probably does.
|
||||
return result.set(entity.id, entity);
|
||||
}, new Map());
|
||||
|
||||
return Array.from(map.values());
|
||||
};
|
||||
|
||||
/** Flatten paginated results into a single array. */
|
||||
const flattenPages = <T>(queryData: InfiniteData<PaginatedResult<T>> | undefined) => {
|
||||
return queryData?.pages.reduce<T[]>(
|
||||
const data = queryData?.pages.reduce<T[]>(
|
||||
// FIXME: Pleroma wants these to be reversed for Chats.
|
||||
(prev: T[], curr) => [...curr.result, ...prev],
|
||||
[],
|
||||
);
|
||||
|
||||
if (data) {
|
||||
return deduplicate<T>(data);
|
||||
}
|
||||
};
|
||||
|
||||
/** Traverse pages and update the item inside if found. */
|
||||
|
|
Ładowanie…
Reference in New Issue