pl-hooks: simplify queries
Signed-off-by: mkljczk <git@mkljczk.pl>
This commit is contained in:
parent
9f8b9a783c
commit
154a71142e
3 changed files with 32 additions and 57 deletions
|
@ -14,26 +14,22 @@ const useSearchAccounts = (
|
|||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
const searchQuery = useInfiniteQuery({
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['search', 'accounts', query, params],
|
||||
queryFn: ({ pageParam, signal }) => client.search.search(query!, {
|
||||
queryFn: ({ pageParam: offset, signal }) => client.search.search(query!, {
|
||||
with_relationships: true,
|
||||
...params,
|
||||
offset: pageParam ? data?.length : 0,
|
||||
offset,
|
||||
type: 'accounts',
|
||||
}, { signal }).then(({ accounts }) => {
|
||||
dispatch(importEntities({ accounts }));
|
||||
return accounts.map(({ id }) => id);
|
||||
}),
|
||||
enabled: !!query?.trim(),
|
||||
initialPageParam: [''],
|
||||
getNextPageParam: (page) => page.length ? page : undefined,
|
||||
select: (data => data.pages.flat()),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data) => data.pages.flat(),
|
||||
});
|
||||
|
||||
const data: Array<string> | undefined = searchQuery.data;
|
||||
|
||||
return searchQuery;
|
||||
};
|
||||
|
||||
const useSearchStatuses = (
|
||||
|
@ -57,7 +53,7 @@ const useSearchStatuses = (
|
|||
enabled: !!query?.trim(),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data => data.pages.flat()),
|
||||
select: (data) => data.pages.flat(),
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -77,7 +73,7 @@ const useSearchHashtags = (
|
|||
enabled: !!query?.trim(),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data => data.pages.flat()),
|
||||
select: (data) => data.pages.flat(),
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -101,7 +97,7 @@ const useSearchGroups = (
|
|||
enabled: !!query?.trim(),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data => data.pages.flat()),
|
||||
select: (data) => data.pages.flat(),
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -14,27 +14,21 @@ const useSearchAccounts = (
|
|||
const queryClient = usePlHooksQueryClient();
|
||||
const { client } = usePlHooksApiClient();
|
||||
|
||||
const searchQuery = useInfiniteQuery({
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['search', 'accounts', query, params],
|
||||
queryFn: ({ pageParam }) => client.search.search(query!, {
|
||||
queryFn: ({ pageParam: offset, signal }) => client.search.search(query!, {
|
||||
...params,
|
||||
offset: pageParam ? data?.length : 0,
|
||||
offset,
|
||||
type: 'accounts',
|
||||
}).then(({ accounts }) => {
|
||||
}, { signal }).then(({ accounts }) => {
|
||||
importEntities({ accounts });
|
||||
return accounts.map(({ id }) => id);
|
||||
}),
|
||||
enabled: !!query?.trim(),
|
||||
initialPageParam: [''],
|
||||
getNextPageParam: (page) => page.length ? page : undefined,
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data) => data.pages.flat(),
|
||||
}, queryClient);
|
||||
|
||||
const data: Array<string> | undefined = searchQuery.data?.pages.flat();
|
||||
|
||||
return {
|
||||
...searchQuery,
|
||||
data,
|
||||
};
|
||||
};
|
||||
|
||||
const useSearchStatuses = (
|
||||
|
@ -44,27 +38,21 @@ const useSearchStatuses = (
|
|||
const queryClient = usePlHooksQueryClient();
|
||||
const { client } = usePlHooksApiClient();
|
||||
|
||||
const searchQuery = useInfiniteQuery({
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['search', 'statuses', query, params],
|
||||
queryFn: ({ pageParam }) => client.search.search(query, {
|
||||
queryFn: ({ pageParam: offset, signal }) => client.search.search(query, {
|
||||
...params,
|
||||
offset: pageParam ? data?.length : 0,
|
||||
offset,
|
||||
type: 'statuses',
|
||||
}).then(({ statuses }) => {
|
||||
}, { signal }).then(({ statuses }) => {
|
||||
importEntities({ statuses });
|
||||
return statuses.map(({ id }) => id);
|
||||
}),
|
||||
enabled: !!query?.trim(),
|
||||
initialPageParam: [''],
|
||||
getNextPageParam: (page) => page.length ? page : undefined,
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data) => data.pages.flat(),
|
||||
}, queryClient);
|
||||
|
||||
const data: Array<string> | undefined = searchQuery.data?.pages.flat();
|
||||
|
||||
return {
|
||||
...searchQuery,
|
||||
data,
|
||||
};
|
||||
};
|
||||
|
||||
const useSearchHashtags = (
|
||||
|
@ -74,24 +62,18 @@ const useSearchHashtags = (
|
|||
const queryClient = usePlHooksQueryClient();
|
||||
const { client } = usePlHooksApiClient();
|
||||
|
||||
const searchQuery = useInfiniteQuery({
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['search', 'hashtags', query, params],
|
||||
queryFn: ({ pageParam }) => client.search.search(query, {
|
||||
queryFn: ({ pageParam: offset, signal }) => client.search.search(query, {
|
||||
...params,
|
||||
offset: pageParam ? data?.length : 0,
|
||||
offset,
|
||||
type: 'hashtags',
|
||||
}).then(({ hashtags }) => hashtags as Array<Tag>),
|
||||
}, { signal }).then(({ hashtags }) => hashtags as Array<Tag>),
|
||||
enabled: !!query?.trim(),
|
||||
initialPageParam: [{}],
|
||||
getNextPageParam: (page) => page.length ? page : undefined,
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||
select: (data) => data.pages.flat(),
|
||||
}, queryClient);
|
||||
|
||||
const data: Array<Tag> | undefined = searchQuery.data?.pages.flat();
|
||||
|
||||
return {
|
||||
...searchQuery,
|
||||
data,
|
||||
};
|
||||
};
|
||||
|
||||
export { useSearchAccounts, useSearchStatuses, useSearchHashtags };
|
||||
|
|
|
@ -10,16 +10,13 @@ const useStatusQuotes = (statusId: string) => {
|
|||
const queryClient = usePlHooksQueryClient();
|
||||
const { client } = usePlHooksApiClient();
|
||||
|
||||
const statusQuotesQuery = useInfiniteQuery({
|
||||
return useInfiniteQuery({
|
||||
queryKey: ['statusesLists', 'quotes', statusId],
|
||||
queryFn: ({ pageParam }) => pageParam.next?.() || client.statuses.getStatusQuotes(statusId).then(minifyStatusList),
|
||||
initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse<string>,
|
||||
getNextPageParam: (page) => page.next ? page : undefined,
|
||||
select: (data) => data.pages.map(page => page.items).flat(),
|
||||
}, queryClient);
|
||||
|
||||
const data = statusQuotesQuery.data?.pages.map(page => page.items).flat();
|
||||
|
||||
return { ...statusQuotesQuery, data };
|
||||
};
|
||||
|
||||
export { useStatusQuotes };
|
||||
|
|
Loading…
Reference in a new issue