pl-hooks: Add new queries

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-10-28 14:58:32 +01:00
parent df430d948f
commit ad24c03084
5 changed files with 70 additions and 0 deletions

View file

@ -0,0 +1,20 @@
import { useQuery } from '@tanstack/react-query';
import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client';
import { usePlHooksQueryClient } from 'pl-hooks/contexts/query-client';
import { importEntities } from 'pl-hooks/importer';
import { normalizeStatusEdit } from 'pl-hooks/normalizers/status-edit';
const useStatusHistory = (statusId: string) => {
const queryClient = usePlHooksQueryClient();
const { client } = usePlHooksApiClient();
return useQuery({
queryKey: ['statuses', 'history', statusId],
queryFn: () => client.statuses.getStatusHistory(statusId)
.then(history => (importEntities({ accounts: history.map(({ account }) => account) }), history))
.then(history => history.map(normalizeStatusEdit)),
}, queryClient);
};
export { useStatusHistory };

View file

@ -0,0 +1,25 @@
import { useInfiniteQuery } from '@tanstack/react-query';
import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client';
import { usePlHooksQueryClient } from 'pl-hooks/contexts/query-client';
import { minifyStatusList } from 'pl-hooks/normalizers/status-list';
import type { PaginatedResponse } from 'pl-api';
const useStatusQuotes = (statusId: string) => {
const queryClient = usePlHooksQueryClient();
const { client } = usePlHooksApiClient();
const statusQuotesQuery = 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,
}, queryClient);
const data = statusQuotesQuery.data?.pages.map(page => page.items).flat();
return { ...statusQuotesQuery, data };
};
export { useStatusQuotes };

View file

@ -12,7 +12,9 @@ export * from './hooks/notifications/use-notification';
export * from './hooks/notifications/use-notification-list';
export * from './hooks/polls/use-poll';
export * from './hooks/statuses/use-status';
export * from './hooks/statuses/use-status-history';
export * from './hooks/statuses/use-status-translation';
export * from './hooks/statuses/use-status-quotes';
export * from './importer';

View file

@ -0,0 +1,8 @@
import { StatusEdit } from 'pl-api';
const normalizeStatusEdit = ({ account, ...statusEdit }: StatusEdit) => ({
account_id: account.id,
...statusEdit,
});
export { normalizeStatusEdit };

View file

@ -0,0 +1,15 @@
import { PaginatedResponse, Status } from 'pl-api';
import { importEntities } from 'pl-hooks/importer';
const minifyStatusList = ({ previous, next, items, ...response }: PaginatedResponse<Status>): PaginatedResponse<string> => {
importEntities({ statuses: items });
return {
...response,
previous: previous ? () => previous().then(minifyStatusList) : null,
next: next ? () => next().then(minifyStatusList) : null,
items: items.map(status => status.id),
};
};
export { minifyStatusList };