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 client = useClient();
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
const searchQuery = useInfiniteQuery({
|
return useInfiniteQuery({
|
||||||
queryKey: ['search', 'accounts', query, params],
|
queryKey: ['search', 'accounts', query, params],
|
||||||
queryFn: ({ pageParam, signal }) => client.search.search(query!, {
|
queryFn: ({ pageParam: offset, signal }) => client.search.search(query!, {
|
||||||
with_relationships: true,
|
with_relationships: true,
|
||||||
...params,
|
...params,
|
||||||
offset: pageParam ? data?.length : 0,
|
offset,
|
||||||
type: 'accounts',
|
type: 'accounts',
|
||||||
}, { signal }).then(({ accounts }) => {
|
}, { signal }).then(({ accounts }) => {
|
||||||
dispatch(importEntities({ accounts }));
|
dispatch(importEntities({ accounts }));
|
||||||
return accounts.map(({ id }) => id);
|
return accounts.map(({ id }) => id);
|
||||||
}),
|
}),
|
||||||
enabled: !!query?.trim(),
|
enabled: !!query?.trim(),
|
||||||
initialPageParam: [''],
|
initialPageParam: 0,
|
||||||
getNextPageParam: (page) => page.length ? page : undefined,
|
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||||
select: (data => data.pages.flat()),
|
select: (data) => data.pages.flat(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const data: Array<string> | undefined = searchQuery.data;
|
|
||||||
|
|
||||||
return searchQuery;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const useSearchStatuses = (
|
const useSearchStatuses = (
|
||||||
|
@ -57,7 +53,7 @@ const useSearchStatuses = (
|
||||||
enabled: !!query?.trim(),
|
enabled: !!query?.trim(),
|
||||||
initialPageParam: 0,
|
initialPageParam: 0,
|
||||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||||
select: (data => data.pages.flat()),
|
select: (data) => data.pages.flat(),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -77,7 +73,7 @@ const useSearchHashtags = (
|
||||||
enabled: !!query?.trim(),
|
enabled: !!query?.trim(),
|
||||||
initialPageParam: 0,
|
initialPageParam: 0,
|
||||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||||
select: (data => data.pages.flat()),
|
select: (data) => data.pages.flat(),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -101,7 +97,7 @@ const useSearchGroups = (
|
||||||
enabled: !!query?.trim(),
|
enabled: !!query?.trim(),
|
||||||
initialPageParam: 0,
|
initialPageParam: 0,
|
||||||
getNextPageParam: (_, allPages) => allPages.flat().length,
|
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 queryClient = usePlHooksQueryClient();
|
||||||
const { client } = usePlHooksApiClient();
|
const { client } = usePlHooksApiClient();
|
||||||
|
|
||||||
const searchQuery = useInfiniteQuery({
|
return useInfiniteQuery({
|
||||||
queryKey: ['search', 'accounts', query, params],
|
queryKey: ['search', 'accounts', query, params],
|
||||||
queryFn: ({ pageParam }) => client.search.search(query!, {
|
queryFn: ({ pageParam: offset, signal }) => client.search.search(query!, {
|
||||||
...params,
|
...params,
|
||||||
offset: pageParam ? data?.length : 0,
|
offset,
|
||||||
type: 'accounts',
|
type: 'accounts',
|
||||||
}).then(({ accounts }) => {
|
}, { signal }).then(({ accounts }) => {
|
||||||
importEntities({ accounts });
|
importEntities({ accounts });
|
||||||
return accounts.map(({ id }) => id);
|
return accounts.map(({ id }) => id);
|
||||||
}),
|
}),
|
||||||
enabled: !!query?.trim(),
|
enabled: !!query?.trim(),
|
||||||
initialPageParam: [''],
|
initialPageParam: 0,
|
||||||
getNextPageParam: (page) => page.length ? page : undefined,
|
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||||
|
select: (data) => data.pages.flat(),
|
||||||
}, queryClient);
|
}, queryClient);
|
||||||
|
|
||||||
const data: Array<string> | undefined = searchQuery.data?.pages.flat();
|
|
||||||
|
|
||||||
return {
|
|
||||||
...searchQuery,
|
|
||||||
data,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const useSearchStatuses = (
|
const useSearchStatuses = (
|
||||||
|
@ -44,27 +38,21 @@ const useSearchStatuses = (
|
||||||
const queryClient = usePlHooksQueryClient();
|
const queryClient = usePlHooksQueryClient();
|
||||||
const { client } = usePlHooksApiClient();
|
const { client } = usePlHooksApiClient();
|
||||||
|
|
||||||
const searchQuery = useInfiniteQuery({
|
return useInfiniteQuery({
|
||||||
queryKey: ['search', 'statuses', query, params],
|
queryKey: ['search', 'statuses', query, params],
|
||||||
queryFn: ({ pageParam }) => client.search.search(query, {
|
queryFn: ({ pageParam: offset, signal }) => client.search.search(query, {
|
||||||
...params,
|
...params,
|
||||||
offset: pageParam ? data?.length : 0,
|
offset,
|
||||||
type: 'statuses',
|
type: 'statuses',
|
||||||
}).then(({ statuses }) => {
|
}, { signal }).then(({ statuses }) => {
|
||||||
importEntities({ statuses });
|
importEntities({ statuses });
|
||||||
return statuses.map(({ id }) => id);
|
return statuses.map(({ id }) => id);
|
||||||
}),
|
}),
|
||||||
enabled: !!query?.trim(),
|
enabled: !!query?.trim(),
|
||||||
initialPageParam: [''],
|
initialPageParam: 0,
|
||||||
getNextPageParam: (page) => page.length ? page : undefined,
|
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||||
|
select: (data) => data.pages.flat(),
|
||||||
}, queryClient);
|
}, queryClient);
|
||||||
|
|
||||||
const data: Array<string> | undefined = searchQuery.data?.pages.flat();
|
|
||||||
|
|
||||||
return {
|
|
||||||
...searchQuery,
|
|
||||||
data,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const useSearchHashtags = (
|
const useSearchHashtags = (
|
||||||
|
@ -74,24 +62,18 @@ const useSearchHashtags = (
|
||||||
const queryClient = usePlHooksQueryClient();
|
const queryClient = usePlHooksQueryClient();
|
||||||
const { client } = usePlHooksApiClient();
|
const { client } = usePlHooksApiClient();
|
||||||
|
|
||||||
const searchQuery = useInfiniteQuery({
|
return useInfiniteQuery({
|
||||||
queryKey: ['search', 'hashtags', query, params],
|
queryKey: ['search', 'hashtags', query, params],
|
||||||
queryFn: ({ pageParam }) => client.search.search(query, {
|
queryFn: ({ pageParam: offset, signal }) => client.search.search(query, {
|
||||||
...params,
|
...params,
|
||||||
offset: pageParam ? data?.length : 0,
|
offset,
|
||||||
type: 'hashtags',
|
type: 'hashtags',
|
||||||
}).then(({ hashtags }) => hashtags as Array<Tag>),
|
}, { signal }).then(({ hashtags }) => hashtags as Array<Tag>),
|
||||||
enabled: !!query?.trim(),
|
enabled: !!query?.trim(),
|
||||||
initialPageParam: [{}],
|
initialPageParam: 0,
|
||||||
getNextPageParam: (page) => page.length ? page : undefined,
|
getNextPageParam: (_, allPages) => allPages.flat().length,
|
||||||
|
select: (data) => data.pages.flat(),
|
||||||
}, queryClient);
|
}, queryClient);
|
||||||
|
|
||||||
const data: Array<Tag> | undefined = searchQuery.data?.pages.flat();
|
|
||||||
|
|
||||||
return {
|
|
||||||
...searchQuery,
|
|
||||||
data,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { useSearchAccounts, useSearchStatuses, useSearchHashtags };
|
export { useSearchAccounts, useSearchStatuses, useSearchHashtags };
|
||||||
|
|
|
@ -10,16 +10,13 @@ const useStatusQuotes = (statusId: string) => {
|
||||||
const queryClient = usePlHooksQueryClient();
|
const queryClient = usePlHooksQueryClient();
|
||||||
const { client } = usePlHooksApiClient();
|
const { client } = usePlHooksApiClient();
|
||||||
|
|
||||||
const statusQuotesQuery = useInfiniteQuery({
|
return useInfiniteQuery({
|
||||||
queryKey: ['statusesLists', 'quotes', statusId],
|
queryKey: ['statusesLists', 'quotes', statusId],
|
||||||
queryFn: ({ pageParam }) => pageParam.next?.() || client.statuses.getStatusQuotes(statusId).then(minifyStatusList),
|
queryFn: ({ pageParam }) => pageParam.next?.() || client.statuses.getStatusQuotes(statusId).then(minifyStatusList),
|
||||||
initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse<string>,
|
initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse<string>,
|
||||||
getNextPageParam: (page) => page.next ? page : undefined,
|
getNextPageParam: (page) => page.next ? page : undefined,
|
||||||
|
select: (data) => data.pages.map(page => page.items).flat(),
|
||||||
}, queryClient);
|
}, queryClient);
|
||||||
|
|
||||||
const data = statusQuotesQuery.data?.pages.map(page => page.items).flat();
|
|
||||||
|
|
||||||
return { ...statusQuotesQuery, data };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { useStatusQuotes };
|
export { useStatusQuotes };
|
||||||
|
|
Loading…
Reference in a new issue