Add useBatchedEntities hook for relationships

This commit is contained in:
Alex Gleason
2023-06-25 15:57:13 -05:00
parent a5e213eca0
commit 989d99f908
9 changed files with 231 additions and 119 deletions

View File

@ -1,5 +1,6 @@
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';
@ -11,6 +12,8 @@ interface UseAccountOpts {
function useAccount(accountId?: string, opts: UseAccountOpts = {}) {
const api = useApi();
const features = useFeatures();
const { me } = useLoggedIn();
const { withRelationship } = opts;
const { entity: account, ...result } = useEntity<Account>(
@ -24,10 +27,14 @@ function useAccount(accountId?: string, opts: UseAccountOpts = {}) {
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,
};
}

View File

@ -1,5 +1,6 @@
import { Entities } from 'soapbox/entity-store/entities';
import { useEntityLookup } from 'soapbox/entity-store/hooks';
import { useFeatures, useLoggedIn } from 'soapbox/hooks';
import { useApi } from 'soapbox/hooks/useApi';
import { type Account, accountSchema } from 'soapbox/schemas';
@ -11,6 +12,8 @@ interface UseAccountLookupOpts {
function useAccountLookup(acct: string | undefined, opts: UseAccountLookupOpts = {}) {
const api = useApi();
const features = useFeatures();
const { me } = useLoggedIn();
const { withRelationship } = opts;
const { entity: account, ...result } = useEntityLookup<Account>(
@ -25,10 +28,14 @@ function useAccountLookup(acct: string | undefined, opts: UseAccountLookupOpts =
isLoading: isRelationshipLoading,
} = useRelationship(account?.id, { 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,
};
}

View File

@ -0,0 +1,30 @@
import { Entities } from 'soapbox/entity-store/entities';
import { useEntities } from 'soapbox/entity-store/hooks';
import { useApi } from 'soapbox/hooks';
import { Account, accountSchema } from 'soapbox/schemas';
import { useRelationships } from './useRelationships';
function useFollowing(accountId: string | undefined) {
const api = useApi();
const { entities, ...rest } = useEntities(
[Entities.ACCOUNTS, accountId!, 'following'],
() => api.get(`/api/v1/accounts/${accountId}/following`),
{ schema: accountSchema, enabled: !!accountId },
);
const { relationships } = useRelationships(
[accountId!, 'following'],
entities.map(({ id }) => id),
);
const accounts: Account[] = entities.map((account) => ({
...account,
relationship: relationships[account.id],
}));
return { accounts, ...rest };
}
export { useFollowing };

View File

@ -1,24 +1,22 @@
import { Entities } from 'soapbox/entity-store/entities';
import { useEntities } from 'soapbox/entity-store/hooks';
import { useBatchedEntities } from 'soapbox/entity-store/hooks/useBatchedEntities';
import { useLoggedIn } from 'soapbox/hooks';
import { useApi } from 'soapbox/hooks/useApi';
import { type Relationship, relationshipSchema } from 'soapbox/schemas';
function useRelationships(ids: string[]) {
function useRelationships(listKey: string[], ids: string[]) {
const api = useApi();
const { isLoggedIn } = useLoggedIn();
const q = ids.map(id => `id[]=${id}`).join('&');
const { entities: relationships, ...result } = useEntities<Relationship>(
[Entities.RELATIONSHIPS, q],
const { entityMap: relationships, ...result } = useBatchedEntities<Relationship>(
[Entities.RELATIONSHIPS, ...listKey],
ids,
() => api.get(`/api/v1/accounts/relationships?${q}`),
{ schema: relationshipSchema, enabled: isLoggedIn && ids.filter(Boolean).length > 0 },
{ schema: relationshipSchema, enabled: isLoggedIn },
);
return {
...result,
relationships,
};
return { relationships, ...result };
}
export { useRelationships };