pl-hooks: Update basic hooks

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-10-23 23:27:36 +02:00
parent dc7939dea5
commit 39f975c793
4 changed files with 51 additions and 22 deletions

View file

@ -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 { usePlHooksApiClient } from 'pl-hooks/contexts/api-client';
import { queryClient, usePlHooksQueryClient } from 'pl-hooks/contexts/query-client'; import { queryClient, usePlHooksQueryClient } from 'pl-hooks/contexts/query-client';
import { importEntities } from 'pl-hooks/importer'; 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 { 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 { interface UseAccountOpts {
withRelationship?: boolean; withRelationship?: boolean;
@ -15,7 +20,9 @@ interface UseAccountOpts {
withMoveTarget?: boolean; withMoveTarget?: boolean;
} }
const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => { type UseAccountQueryResult = Omit<UseQueryResult<NormalizedAccount>, 'data'> & { data: Account | undefined };
const useAccount = (accountId?: string, opts: UseAccountOpts = {}): UseAccountQueryResult => {
const { client } = usePlHooksApiClient(); const { client } = usePlHooksApiClient();
const queryClient = usePlHooksQueryClient(); const queryClient = usePlHooksQueryClient();
@ -28,15 +35,15 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => {
const relationshipQuery = useAccountRelationship(opts.withRelationship ? accountId : undefined); const relationshipQuery = useAccountRelationship(opts.withRelationship ? accountId : undefined);
let data; let data: Account | undefined;
if (accountQuery.data) { if (accountQuery.data) {
data = { data = {
...accountQuery.data, ...accountQuery.data,
relationship: relationshipQuery.data, relationship: relationshipQuery.data || null,
moved: opts.withMoveTarget && queryClient.getQueryData(['accounts', 'entities', accountQuery.data?.moved_id]) as Account || null, moved: opts.withMoveTarget && queryClient.getQueryData(['accounts', 'entities', accountQuery.data?.moved_id]) as Account || null,
}; };
} else data = null; }
return { ...accountQuery, data }; 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 };

View file

@ -46,9 +46,9 @@ const useNotification = (notificationId: string) => {
let data: (NormalizedNotification & { let data: (NormalizedNotification & {
account: Account; account: Account;
accounts: Array<Account>; accounts: Array<Account>;
target: Account | null; target: Account | undefined;
status: Status | null; status: Status | undefined;
}) | null = null; }) | undefined;
if (notification) { if (notification) {
data = { data = {

View file

@ -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 { usePlHooksApiClient } from 'pl-hooks/contexts/api-client';
import { queryClient, usePlHooksQueryClient } from 'pl-hooks/contexts/query-client'; import { queryClient, usePlHooksQueryClient } from 'pl-hooks/contexts/query-client';
import { importEntities } from 'pl-hooks/importer'; import { importEntities } from 'pl-hooks/importer';
import { usePoll } from 'pl-hooks/main';
import { type Account, normalizeAccount } from 'pl-hooks/normalizers/account'; 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] => { // const toServerSideType = (columnType: string): Filter['context'][0] => {
// switch (columnType) { // switch (columnType) {
@ -77,14 +79,28 @@ import { normalizeStatus, type Status } from '../../normalizers/status';
// return result; // return result;
// }, [])), []); // }, [])), []);
const importStatus = (status: Status) => { const importStatus = (status: NormalizedStatus) => {
queryClient.setQueryData<Status>( queryClient.setQueryData<NormalizedStatus>(
['statuses', 'entities', status.id], ['statuses', 'entities', status.id],
status, status,
); );
}; };
const useStatus = (statusId?: string, opts: { language?: string } = {}) => { type Status = NormalizedStatus & {
account: Account;
accounts: Array<Account>;
poll?: Poll;
reblog?: Status;
};
interface UseStatusOpts {
language?: string;
withReblog?: boolean;
}
type UseStatusQueryResult = Omit<UseQueryResult<NormalizedStatus>, 'data'> & { data: Status | undefined };
const useStatus = (statusId?: string, opts: UseStatusOpts = { withReblog: true }): UseStatusQueryResult => {
const queryClient = usePlHooksQueryClient(); const queryClient = usePlHooksQueryClient();
const { client } = usePlHooksApiClient(); const { client } = usePlHooksApiClient();
@ -100,6 +116,13 @@ const useStatus = (statusId?: string, opts: { language?: string } = {}) => {
const status = statusQuery.data; 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({ const accountsQuery = useQueries({
queries: status?.account_ids.map(accountId => ({ queries: status?.account_ids.map(accountId => ({
queryKey: ['accounts', 'entities', accountId], queryKey: ['accounts', 'entities', accountId],
@ -109,16 +132,15 @@ const useStatus = (statusId?: string, opts: { language?: string } = {}) => {
})) || [], })) || [],
}, queryClient); }, queryClient);
let data: (Status & { let data: Status | undefined;
account: Account;
accounts: Array<Account>;
}) | null = null;
if (status) { if (status) {
data = { data = {
...status, ...status,
account: accountsQuery[0].data!, account: accountsQuery[0].data!,
accounts: accountsQuery.map(({ data }) => data!).filter(Boolean), accounts: accountsQuery.map(({ data }) => data!).filter(Boolean),
poll: pollQuery.data || undefined,
reblog: reblogQuery?.data || undefined,
// quote, // quote,
// reblog, // reblog,
// poll // poll
@ -128,4 +150,4 @@ const useStatus = (statusId?: string, opts: { language?: string } = {}) => {
return { ...statusQuery, data }; return { ...statusQuery, data };
}; };
export { useStatus, importStatus }; export { useStatus, importStatus, type Status as UseStatusData };

View file

@ -1,6 +1,6 @@
import type { Account as BaseAccount } from 'pl-api'; import type { Account as BaseAccount } from 'pl-api';
const normalizeAccount = ({ moved, ...account }: BaseAccount) => ({ const normalizeAccount = ({ moved, relationship, ...account }: BaseAccount) => ({
...account, ...account,
moved_id: moved?.id || null, moved_id: moved?.id || null,
}); });