pleroma/app/soapbox/api/hooks/accounts/useAccount.ts
2023-06-25 15:57:38 -05:00

42 lines
No EOL
1.3 KiB
TypeScript

import { Entities } from 'soapbox/entity-store/entities';
import { useEntity } from 'soapbox/entity-store/hooks';
import { useFeatures, useLoggedIn } from 'soapbox/hooks';
import { useApi } from 'soapbox/hooks/useApi';
import { type Account, accountSchema } from 'soapbox/schemas';
import { useRelationship } from './useRelationship';
interface UseAccountOpts {
withRelationship?: boolean
}
function useAccount(accountId?: string, opts: UseAccountOpts = {}) {
const api = useApi();
const features = useFeatures();
const { me } = useLoggedIn();
const { withRelationship } = opts;
const { entity: account, ...result } = useEntity<Account>(
[Entities.ACCOUNTS, accountId!],
() => api.get(`/api/v1/accounts/${accountId}`),
{ schema: accountSchema, enabled: !!accountId },
);
const {
relationship,
isLoading: isRelationshipLoading,
} = useRelationship(accountId, { enabled: withRelationship });
const isBlocked = account?.relationship?.blocked_by === true;
const isUnavailable = (me === account?.id) ? false : (isBlocked && !features.blockersVisible);
return {
...result,
isLoading: result.isLoading,
isRelationshipLoading,
isUnavailable,
account: account ? { ...account, relationship } : undefined,
};
}
export { useAccount };