pl-hooks: Add user directory hook

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-10-28 23:46:35 +01:00
parent ad24c03084
commit 81ffbc1e31
3 changed files with 128 additions and 0 deletions

View file

@ -0,0 +1,31 @@
import { useInfiniteQuery } from '@tanstack/react-query';
import { importEntities, usePlHooksApiClient, usePlHooksQueryClient } from 'pl-hooks/main';
const useDirectory = (order: 'active' | 'new', local: boolean = false) => {
const { client } = usePlHooksApiClient();
const queryClient = usePlHooksQueryClient();
const directoryQuery = useInfiniteQuery({
queryKey: ['accountsLists', 'directory', order, local],
queryFn: ({ pageParam }) => client.instance.profileDirectory({
order,
local,
offset: pageParam ? data?.length : 0,
}).then((accounts) => {
importEntities({ accounts });
return accounts.map(({ id }) => id);
}),
initialPageParam: [''],
getNextPageParam: (page) => page.length ? page : undefined,
}, queryClient);
const data: Array<string> | undefined = directoryQuery.data?.pages.flat();
return {
...directoryQuery,
data,
};
};
export { useDirectory };

View file

@ -0,0 +1,95 @@
import { useInfiniteQuery } from '@tanstack/react-query';
import { importEntities, usePlHooksApiClient, usePlHooksQueryClient } from 'pl-hooks/main';
import type { SearchParams, Tag } from 'pl-api';
import type { PaginationParams } from 'pl-api/dist/params/common';
const useSearchAccounts = (
query: string,
params?: Omit<SearchParams, keyof PaginationParams | 'type' | 'offset'>,
) => {
const queryClient = usePlHooksQueryClient();
const { client } = usePlHooksApiClient();
const searchQuery = useInfiniteQuery({
queryKey: ['search', 'accounts', query, params],
queryFn: ({ pageParam }) => client.search.search(query!, {
...params,
offset: pageParam ? data?.length : 0,
type: 'accounts',
}).then(({ accounts }) => {
importEntities({ accounts });
return accounts.map(({ id }) => id);
}),
enabled: !!query?.trim(),
initialPageParam: [''],
getNextPageParam: (page) => page.length ? page : undefined,
}, queryClient);
const data: Array<string> | undefined = searchQuery.data?.pages.flat();
return {
...searchQuery,
data,
};
};
const useSearchStatuses = (
query: string,
params?: Omit<SearchParams, keyof PaginationParams | 'type' | 'offset'>,
) => {
const queryClient = usePlHooksQueryClient();
const { client } = usePlHooksApiClient();
const searchQuery = useInfiniteQuery({
queryKey: ['search', 'statuses', query, params],
queryFn: ({ pageParam }) => client.search.search(query, {
...params,
offset: pageParam ? data?.length : 0,
type: 'statuses',
}).then(({ statuses }) => {
importEntities({ statuses });
return statuses.map(({ id }) => id);
}),
enabled: !!query?.trim(),
initialPageParam: [''],
getNextPageParam: (page) => page.length ? page : undefined,
}, queryClient);
const data: Array<string> | undefined = searchQuery.data?.pages.flat();
return {
...searchQuery,
data,
};
};
const useSearchHashtags = (
query: string,
params?: Omit<SearchParams, keyof PaginationParams | 'type' | 'offset'>,
) => {
const queryClient = usePlHooksQueryClient();
const { client } = usePlHooksApiClient();
const searchQuery = useInfiniteQuery({
queryKey: ['search', 'hashtags', query, params],
queryFn: ({ pageParam }) => client.search.search(query, {
...params,
offset: pageParam ? data?.length : 0,
type: 'hashtags',
}).then(({ hashtags }) => hashtags as Array<Tag>),
enabled: !!query?.trim(),
initialPageParam: [{}],
getNextPageParam: (page) => page.length ? page : undefined,
}, queryClient);
const data: Array<Tag> | undefined = searchQuery.data?.pages.flat();
return {
...searchQuery,
data,
};
};
export { useSearchAccounts, useSearchStatuses, useSearchHashtags };

View file

@ -4,6 +4,7 @@ export * from './contexts/query-client';
export * from './hooks/accounts/use-account';
export * from './hooks/accounts/use-account-lookup';
export * from './hooks/accounts/use-account-relationship';
export * from './hooks/accounts/use-directory';
export * from './hooks/instance/use-instance';
export * from './hooks/instance/use-translation-languages';
export * from './hooks/markers/use-markers';
@ -11,6 +12,7 @@ export * from './hooks/markers/use-update-marker-mutation';
export * from './hooks/notifications/use-notification';
export * from './hooks/notifications/use-notification-list';
export * from './hooks/polls/use-poll';
export * from './hooks/search/use-search';
export * from './hooks/statuses/use-status';
export * from './hooks/statuses/use-status-history';
export * from './hooks/statuses/use-status-translation';