ProfileHoverCard: use useAccount hook, add usePatronUser hook

This commit is contained in:
Alex Gleason
2023-06-21 16:20:07 -05:00
parent 25d3925b76
commit e01ee84ee9
7 changed files with 56 additions and 18 deletions

View File

@@ -3,22 +3,25 @@ import { useEntity } from 'soapbox/entity-store/hooks';
import { useApi } from 'soapbox/hooks/useApi';
import { type Account, accountSchema } from 'soapbox/schemas';
import { useRelationships } from './useRelationships';
function useAccount(id: string) {
function useAccount(accountId?: string) {
const api = useApi();
const { entity: account, ...result } = useEntity<Account>(
[Entities.ACCOUNTS, id],
() => api.get(`/api/v1/accounts/${id}`),
{ schema: accountSchema },
[Entities.ACCOUNTS, accountId || ''],
() => api.get(`/api/v1/accounts/${accountId}`),
{ schema: accountSchema, enabled: !!accountId },
);
const { relationships, isLoading } = useRelationships([account?.id as string]);
const {
relationships,
isLoading: isRelationshipLoading,
} = useRelationships(accountId ? [accountId] : []);
return {
...result,
isLoading: result.isLoading || isLoading,
isLoading: result.isLoading,
isRelationshipLoading,
account: account ? { ...account, relationship: relationships[0] || null } : undefined,
};
}

View File

@@ -0,0 +1,18 @@
import { Entities } from 'soapbox/entity-store/entities';
import { useEntity } from 'soapbox/entity-store/hooks';
import { useApi } from 'soapbox/hooks/useApi';
import { type PatronUser, patronUserSchema } from 'soapbox/schemas';
function usePatronUser(url?: string) {
const api = useApi();
const { entity: patronUser, ...result } = useEntity<PatronUser>(
[Entities.PATRON_USERS, url || ''],
() => api.get(`/api/patron/v1/accounts/${encodeURIComponent(url!)}`),
{ schema: patronUserSchema, enabled: !!url },
);
return { patronUser, ...result };
}
export { usePatronUser };

View File

@@ -3,6 +3,7 @@
* Accounts
*/
export { useAccount } from './accounts/useAccount';
export { usePatronUser } from './accounts/usePatronUser';
/**
* Groups