2024-10-04 15:13:47 -07:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
|
2024-10-05 14:12:51 -07:00
|
|
|
import { useRelationship } from 'pl-fe/api/hooks/accounts/useRelationship';
|
2024-10-07 14:53:05 -07:00
|
|
|
import { useClient } from 'pl-fe/hooks';
|
|
|
|
import { type MinifiedAccount, minifyAccount } from 'pl-fe/pl-hooks/minifiers/minifyAccount';
|
|
|
|
import { normalizeAccount } from 'pl-fe/pl-hooks/normalizers/normalizeAccount';
|
|
|
|
import { queryClient } from 'pl-fe/queries/client';
|
2024-10-04 15:13:47 -07:00
|
|
|
|
|
|
|
interface UseAccountOpts {
|
|
|
|
withRelationship?: boolean;
|
|
|
|
withScrobble?: boolean;
|
|
|
|
withMoveTarget?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => {
|
|
|
|
const client = useClient();
|
|
|
|
|
|
|
|
const accountQuery = useQuery({
|
|
|
|
queryKey: ['accounts', 'entities', accountId],
|
|
|
|
queryFn: () => client.accounts.getAccount(accountId!)
|
|
|
|
.then(normalizeAccount)
|
|
|
|
.then(minifyAccount),
|
|
|
|
enabled: !!accountId,
|
|
|
|
});
|
|
|
|
|
|
|
|
const relationshipQuery = useRelationship(accountId, {
|
|
|
|
enabled: opts.withRelationship,
|
|
|
|
});
|
|
|
|
|
2024-10-07 14:53:05 -07:00
|
|
|
let data;
|
|
|
|
if (accountQuery.data) {
|
|
|
|
data = {
|
|
|
|
...accountQuery.data,
|
2024-10-04 15:13:47 -07:00
|
|
|
relationship: relationshipQuery.relationship,
|
2024-10-07 14:53:05 -07:00
|
|
|
moved: opts.withMoveTarget && queryClient.getQueryData(['accounts', 'entities', accountQuery.data?.moved_id]) as MinifiedAccount || null,
|
2024-10-04 15:13:47 -07:00
|
|
|
};
|
2024-10-07 14:53:05 -07:00
|
|
|
} else data = null;
|
2024-10-04 15:13:47 -07:00
|
|
|
|
|
|
|
return { ...accountQuery, data };
|
|
|
|
};
|
2024-10-05 14:12:51 -07:00
|
|
|
|
|
|
|
export { useAccount };
|