diff --git a/packages/pl-hooks/lib/hooks/statuses/use-status-history.ts b/packages/pl-hooks/lib/hooks/statuses/use-status-history.ts new file mode 100644 index 000000000..dffd96098 --- /dev/null +++ b/packages/pl-hooks/lib/hooks/statuses/use-status-history.ts @@ -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 }; diff --git a/packages/pl-hooks/lib/hooks/statuses/use-status-quotes.ts b/packages/pl-hooks/lib/hooks/statuses/use-status-quotes.ts new file mode 100644 index 000000000..e4cba2440 --- /dev/null +++ b/packages/pl-hooks/lib/hooks/statuses/use-status-quotes.ts @@ -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, + getNextPageParam: (page) => page.next ? page : undefined, + }, queryClient); + + const data = statusQuotesQuery.data?.pages.map(page => page.items).flat(); + + return { ...statusQuotesQuery, data }; +}; + +export { useStatusQuotes }; diff --git a/packages/pl-hooks/lib/main.ts b/packages/pl-hooks/lib/main.ts index 3c93843e3..3dfa63140 100644 --- a/packages/pl-hooks/lib/main.ts +++ b/packages/pl-hooks/lib/main.ts @@ -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'; diff --git a/packages/pl-hooks/lib/normalizers/status-edit.ts b/packages/pl-hooks/lib/normalizers/status-edit.ts new file mode 100644 index 000000000..67b43e282 --- /dev/null +++ b/packages/pl-hooks/lib/normalizers/status-edit.ts @@ -0,0 +1,8 @@ +import { StatusEdit } from 'pl-api'; + +const normalizeStatusEdit = ({ account, ...statusEdit }: StatusEdit) => ({ + account_id: account.id, + ...statusEdit, +}); + +export { normalizeStatusEdit }; diff --git a/packages/pl-hooks/lib/normalizers/status-list.ts b/packages/pl-hooks/lib/normalizers/status-list.ts new file mode 100644 index 000000000..40b4a94d9 --- /dev/null +++ b/packages/pl-hooks/lib/normalizers/status-list.ts @@ -0,0 +1,15 @@ +import { PaginatedResponse, Status } from 'pl-api'; + +import { importEntities } from 'pl-hooks/importer'; + +const minifyStatusList = ({ previous, next, items, ...response }: PaginatedResponse): PaginatedResponse => { + 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 };