pl-hooks: Add new queries
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
df430d948f
commit
ad24c03084
5 changed files with 70 additions and 0 deletions
20
packages/pl-hooks/lib/hooks/statuses/use-status-history.ts
Normal file
20
packages/pl-hooks/lib/hooks/statuses/use-status-history.ts
Normal 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 };
|
25
packages/pl-hooks/lib/hooks/statuses/use-status-quotes.ts
Normal file
25
packages/pl-hooks/lib/hooks/statuses/use-status-quotes.ts
Normal 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 };
|
|
@ -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';
|
||||
|
||||
|
|
8
packages/pl-hooks/lib/normalizers/status-edit.ts
Normal file
8
packages/pl-hooks/lib/normalizers/status-edit.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
import { StatusEdit } from 'pl-api';
|
||||
|
||||
const normalizeStatusEdit = ({ account, ...statusEdit }: StatusEdit) => ({
|
||||
account_id: account.id,
|
||||
...statusEdit,
|
||||
});
|
||||
|
||||
export { normalizeStatusEdit };
|
15
packages/pl-hooks/lib/normalizers/status-list.ts
Normal file
15
packages/pl-hooks/lib/normalizers/status-list.ts
Normal 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 };
|
Loading…
Reference in a new issue