Revert flattenPages order changes
This commit is contained in:
parent
b3a0f785d8
commit
5de1c3a61d
2 changed files with 5 additions and 20 deletions
|
@ -30,7 +30,7 @@ const reOrderChatListItems = () => {
|
|||
chatA.last_message?.created_at as string,
|
||||
chatB.last_message?.created_at as string,
|
||||
);
|
||||
}, 'default');
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -25,21 +25,10 @@ const deduplicateById = <T extends Entity>(entities: T[]): T[] => {
|
|||
return Array.from(map.values());
|
||||
};
|
||||
|
||||
export type SortOrder = 'reverse' | 'default'
|
||||
|
||||
/** Flatten paginated results into a single array. */
|
||||
const flattenPages = <T>(
|
||||
queryData: InfiniteData<PaginatedResult<T>> | undefined,
|
||||
order: SortOrder = 'reverse',
|
||||
) => {
|
||||
const flattenPages = <T>(queryData: InfiniteData<PaginatedResult<T>> | undefined) => {
|
||||
const data = queryData?.pages.reduce<T[]>(
|
||||
(prev: T[], curr) => {
|
||||
if (order === 'reverse') {
|
||||
return [...curr.result, ...prev];
|
||||
} else {
|
||||
return [...prev, ...curr.result];
|
||||
}
|
||||
},
|
||||
(prev: T[], curr) => [...prev, ...curr.result],
|
||||
[],
|
||||
);
|
||||
|
||||
|
@ -101,15 +90,11 @@ const paginateQueryData = <T>(array: T[] | undefined) => {
|
|||
}, []);
|
||||
};
|
||||
|
||||
const sortQueryData = <T>(
|
||||
queryKey: QueryKey,
|
||||
comparator: (a: T, b: T) => number,
|
||||
order: SortOrder = 'reverse',
|
||||
) => {
|
||||
const sortQueryData = <T>(queryKey: QueryKey, comparator: (a: T, b: T) => number) => {
|
||||
queryClient.setQueryData<InfiniteData<PaginatedResult<T>>>(queryKey, (prevResult) => {
|
||||
if (prevResult) {
|
||||
const nextResult = { ...prevResult };
|
||||
const flattenedQueryData = flattenPages(nextResult, order);
|
||||
const flattenedQueryData = flattenPages(nextResult);
|
||||
const sortedQueryData = flattenedQueryData?.sort(comparator);
|
||||
const paginatedPages = paginateQueryData(sortedQueryData);
|
||||
const newPages = paginatedPages.map((page: T, idx: number) => ({
|
||||
|
|
Loading…
Reference in a new issue