pl-hooks: Update basic hooks
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
dc7939dea5
commit
39f975c793
4 changed files with 51 additions and 22 deletions
|
@ -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<UseQueryResult<NormalizedAccount>, '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 };
|
||||
|
|
|
@ -46,9 +46,9 @@ const useNotification = (notificationId: string) => {
|
|||
let data: (NormalizedNotification & {
|
||||
account: Account;
|
||||
accounts: Array<Account>;
|
||||
target: Account | null;
|
||||
status: Status | null;
|
||||
}) | null = null;
|
||||
target: Account | undefined;
|
||||
status: Status | undefined;
|
||||
}) | undefined;
|
||||
|
||||
if (notification) {
|
||||
data = {
|
||||
|
|
|
@ -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<Status>(
|
||||
const importStatus = (status: NormalizedStatus) => {
|
||||
queryClient.setQueryData<NormalizedStatus>(
|
||||
['statuses', 'entities', status.id],
|
||||
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 { 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<Account>;
|
||||
}) | 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 };
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue