Add useRelationship hook, disable fetching relationship for account by default
This commit is contained in:
@ -3,10 +3,15 @@ import { useEntity } from 'soapbox/entity-store/hooks';
|
||||
import { useApi } from 'soapbox/hooks/useApi';
|
||||
import { type Account, accountSchema } from 'soapbox/schemas';
|
||||
|
||||
import { useRelationships } from './useRelationships';
|
||||
import { useRelationship } from './useRelationship';
|
||||
|
||||
function useAccount(accountId?: string) {
|
||||
interface UseAccountOpts {
|
||||
withRelationship?: boolean
|
||||
}
|
||||
|
||||
function useAccount(accountId?: string, opts: UseAccountOpts = {}) {
|
||||
const api = useApi();
|
||||
const { withRelationship } = opts;
|
||||
|
||||
const { entity: account, ...result } = useEntity<Account>(
|
||||
[Entities.ACCOUNTS, accountId!],
|
||||
@ -15,15 +20,15 @@ function useAccount(accountId?: string) {
|
||||
);
|
||||
|
||||
const {
|
||||
relationships,
|
||||
relationship,
|
||||
isLoading: isRelationshipLoading,
|
||||
} = useRelationships(accountId ? [accountId] : []);
|
||||
} = useRelationship(accountId, { enabled: withRelationship });
|
||||
|
||||
return {
|
||||
...result,
|
||||
isLoading: result.isLoading,
|
||||
isRelationshipLoading,
|
||||
account: account ? { ...account, relationship: relationships[0] || null } : undefined,
|
||||
account: account ? { ...account, relationship } : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -3,10 +3,15 @@ import { useEntityLookup } from 'soapbox/entity-store/hooks';
|
||||
import { useApi } from 'soapbox/hooks/useApi';
|
||||
import { type Account, accountSchema } from 'soapbox/schemas';
|
||||
|
||||
import { useRelationships } from './useRelationships';
|
||||
import { useRelationship } from './useRelationship';
|
||||
|
||||
function useAccountLookup(acct?: string) {
|
||||
interface UseAccountLookupOpts {
|
||||
withRelationship?: boolean
|
||||
}
|
||||
|
||||
function useAccountLookup(acct: string | undefined, opts: UseAccountLookupOpts = {}) {
|
||||
const api = useApi();
|
||||
const { withRelationship } = opts;
|
||||
|
||||
const { entity: account, ...result } = useEntityLookup<Account>(
|
||||
Entities.ACCOUNTS,
|
||||
@ -16,15 +21,15 @@ function useAccountLookup(acct?: string) {
|
||||
);
|
||||
|
||||
const {
|
||||
relationships,
|
||||
relationship,
|
||||
isLoading: isRelationshipLoading,
|
||||
} = useRelationships(account ? [account.id] : []);
|
||||
} = useRelationship(account?.id, { enabled: withRelationship });
|
||||
|
||||
return {
|
||||
...result,
|
||||
isLoading: result.isLoading,
|
||||
isRelationshipLoading,
|
||||
account: account ? { ...account, relationship: relationships[0] || null } : undefined,
|
||||
account: account ? { ...account, relationship } : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
28
app/soapbox/api/hooks/accounts/useRelationship.ts
Normal file
28
app/soapbox/api/hooks/accounts/useRelationship.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { Entities } from 'soapbox/entity-store/entities';
|
||||
import { useEntity } from 'soapbox/entity-store/hooks';
|
||||
import { useApi } from 'soapbox/hooks';
|
||||
import { type Relationship, relationshipSchema } from 'soapbox/schemas';
|
||||
|
||||
interface UseRelationshipOpts {
|
||||
enabled?: boolean
|
||||
}
|
||||
|
||||
function useRelationship(accountId: string | undefined, opts: UseRelationshipOpts = {}) {
|
||||
const api = useApi();
|
||||
const { enabled = false } = opts;
|
||||
|
||||
const { entity: relationship, ...result } = useEntity<Relationship>(
|
||||
[Entities.RELATIONSHIPS, accountId!],
|
||||
() => api.get(`/api/v1/accounts/relationships?id[]=${accountId}`),
|
||||
{
|
||||
enabled: enabled && !!accountId,
|
||||
schema: z.array(relationshipSchema).nonempty().transform(arr => arr[0]),
|
||||
},
|
||||
);
|
||||
|
||||
return { relationship, ...result };
|
||||
}
|
||||
|
||||
export { useRelationship };
|
||||
@ -16,7 +16,7 @@ function useGroupRelationship(groupId: string | undefined) {
|
||||
() => api.get(`/api/v1/groups/relationships?id[]=${groupId}`),
|
||||
{
|
||||
enabled: !!groupId,
|
||||
schema: z.array(groupRelationshipSchema).transform(arr => arr[0]),
|
||||
schema: z.array(groupRelationshipSchema).nonempty().transform(arr => arr[0]),
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user