diff --git a/packages/pl-hooks/lib/hooks/accounts/use-account.ts b/packages/pl-hooks/lib/hooks/accounts/use-account.ts index fe0572a5c..2973823a6 100644 --- a/packages/pl-hooks/lib/hooks/accounts/use-account.ts +++ b/packages/pl-hooks/lib/hooks/accounts/use-account.ts @@ -1,13 +1,18 @@ -import { useQuery } from '@tanstack/react-query'; +import { useQuery, UseQueryResult } from '@tanstack/react-query'; import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client'; import { queryClient, usePlHooksQueryClient } from 'pl-hooks/contexts/query-client'; import { importEntities } from 'pl-hooks/importer'; -import { type Account, normalizeAccount } from 'pl-hooks/normalizers/account'; +import { normalizeAccount, type Account as NormalizedAccount } from 'pl-hooks/normalizers/account'; import { useAccountRelationship } from './use-account-relationship'; -import type { PlApiClient } from 'pl-api'; +import type { PlApiClient, Relationship } from 'pl-api'; + +interface Account extends NormalizedAccount { + relationship: Relationship | null; + moved: Account | null; +} interface UseAccountOpts { withRelationship?: boolean; @@ -15,7 +20,9 @@ interface UseAccountOpts { withMoveTarget?: boolean; } -const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => { +type UseAccountQueryResult = Omit, 'data'> & { data: Account | undefined }; + +const useAccount = (accountId?: string, opts: UseAccountOpts = {}): UseAccountQueryResult => { const { client } = usePlHooksApiClient(); const queryClient = usePlHooksQueryClient(); @@ -28,15 +35,15 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => { const relationshipQuery = useAccountRelationship(opts.withRelationship ? accountId : undefined); - let data; + let data: Account | undefined; if (accountQuery.data) { data = { ...accountQuery.data, - relationship: relationshipQuery.data, + relationship: relationshipQuery.data || null, moved: opts.withMoveTarget && queryClient.getQueryData(['accounts', 'entities', accountQuery.data?.moved_id]) as Account || null, }; - } else data = null; + } return { ...accountQuery, data }; }; @@ -52,4 +59,4 @@ const prefetchAccount = (client: PlApiClient, accountId: string) => }), }); -export { useAccount, prefetchAccount, type UseAccountOpts }; +export { useAccount, prefetchAccount, type UseAccountOpts, type Account as UseAccountData }; diff --git a/packages/pl-hooks/lib/hooks/notifications/use-notification.ts b/packages/pl-hooks/lib/hooks/notifications/use-notification.ts index a70c6db36..0d6d994fe 100644 --- a/packages/pl-hooks/lib/hooks/notifications/use-notification.ts +++ b/packages/pl-hooks/lib/hooks/notifications/use-notification.ts @@ -46,9 +46,9 @@ const useNotification = (notificationId: string) => { let data: (NormalizedNotification & { account: Account; accounts: Array; - target: Account | null; - status: Status | null; - }) | null = null; + target: Account | undefined; + status: Status | undefined; + }) | undefined; if (notification) { data = { diff --git a/packages/pl-hooks/lib/hooks/statuses/use-status.ts b/packages/pl-hooks/lib/hooks/statuses/use-status.ts index 6c1634e19..e8a6e1de3 100644 --- a/packages/pl-hooks/lib/hooks/statuses/use-status.ts +++ b/packages/pl-hooks/lib/hooks/statuses/use-status.ts @@ -1,11 +1,13 @@ -import { useQueries, useQuery } from '@tanstack/react-query'; +import { useQueries, useQuery, type UseQueryResult } from '@tanstack/react-query'; import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client'; import { queryClient, usePlHooksQueryClient } from 'pl-hooks/contexts/query-client'; import { importEntities } from 'pl-hooks/importer'; +import { usePoll } from 'pl-hooks/main'; import { type Account, normalizeAccount } from 'pl-hooks/normalizers/account'; +import { type Status as NormalizedStatus, normalizeStatus } from 'pl-hooks/normalizers/status'; -import { normalizeStatus, type Status } from '../../normalizers/status'; +import type { Poll } from 'pl-api'; // const toServerSideType = (columnType: string): Filter['context'][0] => { // switch (columnType) { @@ -77,14 +79,28 @@ import { normalizeStatus, type Status } from '../../normalizers/status'; // return result; // }, [])), []); -const importStatus = (status: Status) => { - queryClient.setQueryData( +const importStatus = (status: NormalizedStatus) => { + queryClient.setQueryData( ['statuses', 'entities', status.id], status, ); }; -const useStatus = (statusId?: string, opts: { language?: string } = {}) => { +type Status = NormalizedStatus & { + account: Account; + accounts: Array; + poll?: Poll; + reblog?: Status; +}; + +interface UseStatusOpts { + language?: string; + withReblog?: boolean; +} + +type UseStatusQueryResult = Omit, 'data'> & { data: Status | undefined }; + +const useStatus = (statusId?: string, opts: UseStatusOpts = { withReblog: true }): UseStatusQueryResult => { const queryClient = usePlHooksQueryClient(); const { client } = usePlHooksApiClient(); @@ -100,6 +116,13 @@ const useStatus = (statusId?: string, opts: { language?: string } = {}) => { const status = statusQuery.data; + const pollQuery = usePoll(status?.poll_id || undefined); + + let reblogQuery: UseStatusQueryResult | undefined; + if (opts.withReblog) { + reblogQuery = useStatus(status?.reblog_id || undefined, { ...opts, withReblog: false }); + } + const accountsQuery = useQueries({ queries: status?.account_ids.map(accountId => ({ queryKey: ['accounts', 'entities', accountId], @@ -109,16 +132,15 @@ const useStatus = (statusId?: string, opts: { language?: string } = {}) => { })) || [], }, queryClient); - let data: (Status & { - account: Account; - accounts: Array; - }) | null = null; + let data: Status | undefined; if (status) { data = { ...status, account: accountsQuery[0].data!, accounts: accountsQuery.map(({ data }) => data!).filter(Boolean), + poll: pollQuery.data || undefined, + reblog: reblogQuery?.data || undefined, // quote, // reblog, // poll @@ -128,4 +150,4 @@ const useStatus = (statusId?: string, opts: { language?: string } = {}) => { return { ...statusQuery, data }; }; -export { useStatus, importStatus }; +export { useStatus, importStatus, type Status as UseStatusData }; diff --git a/packages/pl-hooks/lib/normalizers/account.ts b/packages/pl-hooks/lib/normalizers/account.ts index 2fca21d28..7b2cf921e 100644 --- a/packages/pl-hooks/lib/normalizers/account.ts +++ b/packages/pl-hooks/lib/normalizers/account.ts @@ -1,6 +1,6 @@ import type { Account as BaseAccount } from 'pl-api'; -const normalizeAccount = ({ moved, ...account }: BaseAccount) => ({ +const normalizeAccount = ({ moved, relationship, ...account }: BaseAccount) => ({ ...account, moved_id: moved?.id || null, });