sforkowany z mirror/soapbox
Revert flattenPages order changes
rodzic
b3a0f785d8
commit
5de1c3a61d
|
@ -30,7 +30,7 @@ const reOrderChatListItems = () => {
|
||||||
chatA.last_message?.created_at as string,
|
chatA.last_message?.created_at as string,
|
||||||
chatB.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());
|
return Array.from(map.values());
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SortOrder = 'reverse' | 'default'
|
|
||||||
|
|
||||||
/** Flatten paginated results into a single array. */
|
/** Flatten paginated results into a single array. */
|
||||||
const flattenPages = <T>(
|
const flattenPages = <T>(queryData: InfiniteData<PaginatedResult<T>> | undefined) => {
|
||||||
queryData: InfiniteData<PaginatedResult<T>> | undefined,
|
|
||||||
order: SortOrder = 'reverse',
|
|
||||||
) => {
|
|
||||||
const data = queryData?.pages.reduce<T[]>(
|
const data = queryData?.pages.reduce<T[]>(
|
||||||
(prev: T[], curr) => {
|
(prev: T[], curr) => [...prev, ...curr.result],
|
||||||
if (order === 'reverse') {
|
|
||||||
return [...curr.result, ...prev];
|
|
||||||
} else {
|
|
||||||
return [...prev, ...curr.result];
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -101,15 +90,11 @@ const paginateQueryData = <T>(array: T[] | undefined) => {
|
||||||
}, []);
|
}, []);
|
||||||
};
|
};
|
||||||
|
|
||||||
const sortQueryData = <T>(
|
const sortQueryData = <T>(queryKey: QueryKey, comparator: (a: T, b: T) => number) => {
|
||||||
queryKey: QueryKey,
|
|
||||||
comparator: (a: T, b: T) => number,
|
|
||||||
order: SortOrder = 'reverse',
|
|
||||||
) => {
|
|
||||||
queryClient.setQueryData<InfiniteData<PaginatedResult<T>>>(queryKey, (prevResult) => {
|
queryClient.setQueryData<InfiniteData<PaginatedResult<T>>>(queryKey, (prevResult) => {
|
||||||
if (prevResult) {
|
if (prevResult) {
|
||||||
const nextResult = { ...prevResult };
|
const nextResult = { ...prevResult };
|
||||||
const flattenedQueryData = flattenPages(nextResult, order);
|
const flattenedQueryData = flattenPages(nextResult);
|
||||||
const sortedQueryData = flattenedQueryData?.sort(comparator);
|
const sortedQueryData = flattenedQueryData?.sort(comparator);
|
||||||
const paginatedPages = paginateQueryData(sortedQueryData);
|
const paginatedPages = paginateQueryData(sortedQueryData);
|
||||||
const newPages = paginatedPages.map((page: T, idx: number) => ({
|
const newPages = paginatedPages.map((page: T, idx: number) => ({
|
||||||
|
|
Ładowanie…
Reference in New Issue