2023-04-04 05:11:03 -07:00
|
|
|
import { Entities } from 'soapbox/entity-store/entities';
|
|
|
|
import { useEntity } from 'soapbox/entity-store/hooks';
|
2023-05-01 11:58:40 -07:00
|
|
|
import { useApi } from 'soapbox/hooks/useApi';
|
2023-04-04 05:11:03 -07:00
|
|
|
import { type Account, accountSchema } from 'soapbox/schemas';
|
|
|
|
|
|
|
|
import { useRelationships } from './useRelationships';
|
|
|
|
|
2023-06-21 14:20:07 -07:00
|
|
|
function useAccount(accountId?: string) {
|
2023-04-04 05:11:03 -07:00
|
|
|
const api = useApi();
|
|
|
|
|
|
|
|
const { entity: account, ...result } = useEntity<Account>(
|
2023-06-21 14:20:07 -07:00
|
|
|
[Entities.ACCOUNTS, accountId || ''],
|
|
|
|
() => api.get(`/api/v1/accounts/${accountId}`),
|
|
|
|
{ schema: accountSchema, enabled: !!accountId },
|
2023-04-04 05:11:03 -07:00
|
|
|
);
|
2023-06-21 14:20:07 -07:00
|
|
|
const {
|
|
|
|
relationships,
|
|
|
|
isLoading: isRelationshipLoading,
|
|
|
|
} = useRelationships(accountId ? [accountId] : []);
|
2023-04-04 05:11:03 -07:00
|
|
|
|
|
|
|
return {
|
|
|
|
...result,
|
2023-06-21 14:20:07 -07:00
|
|
|
isLoading: result.isLoading,
|
|
|
|
isRelationshipLoading,
|
2023-04-04 05:11:03 -07:00
|
|
|
account: account ? { ...account, relationship: relationships[0] || null } : undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useAccount };
|