pl-fe: move profile directory to tanstack query

Signed-off-by: mkljczk <git@mkljczk.pl>
This commit is contained in:
mkljczk
2024-11-30 20:27:23 +01:00
parent 2053998732
commit ae77b678b4
4 changed files with 40 additions and 133 deletions

View File

@ -0,0 +1,27 @@
import { useInfiniteQuery } from '@tanstack/react-query';
import { importEntities } from 'pl-fe/actions/importer';
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
import { useClient } from 'pl-fe/hooks/use-client';
const useDirectory = (order: 'active' | 'new', local: boolean = false) => {
const client = useClient();
const dispatch = useAppDispatch();
return useInfiniteQuery({
queryKey: ['accountsLists', 'directory', order, local],
queryFn: ({ pageParam: offset }) => client.instance.profileDirectory({
order,
local,
offset,
}).then((accounts) => {
dispatch(importEntities({ accounts }));
return accounts.map(({ id }) => id);
}),
initialPageParam: 0,
getNextPageParam: (_, allPages) => allPages.at(-1)?.length === 0 ? undefined : allPages.flat().length,
select: (data) => data?.pages.flat(),
});
};
export { useDirectory };