Add useAccountLookup hook, fix useRelationships
This commit is contained in:
@ -9,10 +9,11 @@ function useAccount(accountId?: string) {
|
||||
const api = useApi();
|
||||
|
||||
const { entity: account, ...result } = useEntity<Account>(
|
||||
[Entities.ACCOUNTS, accountId || ''],
|
||||
[Entities.ACCOUNTS, accountId!],
|
||||
() => api.get(`/api/v1/accounts/${accountId}`),
|
||||
{ schema: accountSchema, enabled: !!accountId },
|
||||
);
|
||||
|
||||
const {
|
||||
relationships,
|
||||
isLoading: isRelationshipLoading,
|
||||
|
||||
31
app/soapbox/api/hooks/accounts/useAccountLookup.ts
Normal file
31
app/soapbox/api/hooks/accounts/useAccountLookup.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { Entities } from 'soapbox/entity-store/entities';
|
||||
import { useEntityLookup } from 'soapbox/entity-store/hooks';
|
||||
import { useApi } from 'soapbox/hooks/useApi';
|
||||
import { type Account, accountSchema } from 'soapbox/schemas';
|
||||
|
||||
import { useRelationships } from './useRelationships';
|
||||
|
||||
function useAccountLookup(acct?: string) {
|
||||
const api = useApi();
|
||||
|
||||
const { entity: account, ...result } = useEntityLookup<Account>(
|
||||
Entities.ACCOUNTS,
|
||||
(account) => account.acct === acct,
|
||||
() => api.get(`/api/v1/accounts/lookup?acct=${acct}`),
|
||||
{ schema: accountSchema, enabled: !!acct },
|
||||
);
|
||||
|
||||
const {
|
||||
relationships,
|
||||
isLoading: isRelationshipLoading,
|
||||
} = useRelationships(account ? [account.id] : []);
|
||||
|
||||
return {
|
||||
...result,
|
||||
isLoading: result.isLoading,
|
||||
isRelationshipLoading,
|
||||
account: account ? { ...account, relationship: relationships[0] || null } : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export { useAccountLookup };
|
||||
@ -7,10 +7,11 @@ import { type Relationship, relationshipSchema } from 'soapbox/schemas';
|
||||
function useRelationships(ids: string[]) {
|
||||
const api = useApi();
|
||||
const { isLoggedIn } = useLoggedIn();
|
||||
const q = ids.map(id => `id[]=${id}`).join('&');
|
||||
|
||||
const { entities: relationships, ...result } = useEntities<Relationship>(
|
||||
[Entities.RELATIONSHIPS],
|
||||
() => api.get(`/api/v1/accounts/relationships?${ids.map(id => `id[]=${id}`).join('&')}`),
|
||||
[Entities.RELATIONSHIPS, q],
|
||||
() => api.get(`/api/v1/accounts/relationships?${q}`),
|
||||
{ schema: relationshipSchema, enabled: isLoggedIn && ids.filter(Boolean).length > 0 },
|
||||
);
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
|
||||
// Accounts
|
||||
export { useAccount } from './accounts/useAccount';
|
||||
export { useAccountLookup } from './accounts/useAccountLookup';
|
||||
export { useFollow } from './accounts/useFollow';
|
||||
export { useRelationships } from './accounts/useRelationships';
|
||||
export { usePatronUser } from './accounts/usePatronUser';
|
||||
|
||||
Reference in New Issue
Block a user