useGroupRelationships: switch to useBatchedEntities
This commit is contained in:
@ -1,27 +1,25 @@
|
||||
import { Entities } from 'soapbox/entity-store/entities';
|
||||
import { useEntities } from 'soapbox/entity-store/hooks';
|
||||
import { useApi } from 'soapbox/hooks';
|
||||
import { useBatchedEntities } from 'soapbox/entity-store/hooks/useBatchedEntities';
|
||||
import { useApi, useLoggedIn } from 'soapbox/hooks';
|
||||
import { type GroupRelationship, groupRelationshipSchema } from 'soapbox/schemas';
|
||||
|
||||
function useGroupRelationships(groupIds: string[]) {
|
||||
function useGroupRelationships(listKey: string[], ids: string[]) {
|
||||
const api = useApi();
|
||||
const q = groupIds.map(id => `id[]=${id}`).join('&');
|
||||
const { isLoggedIn } = useLoggedIn();
|
||||
|
||||
const { entities, ...result } = useEntities<GroupRelationship>(
|
||||
[Entities.GROUP_RELATIONSHIPS, ...groupIds],
|
||||
() => api.get(`/api/v1/groups/relationships?${q}`),
|
||||
{ schema: groupRelationshipSchema, enabled: groupIds.length > 0 },
|
||||
function fetchGroupRelationships(ids: string[]) {
|
||||
const q = ids.map((id) => `id[]=${id}`).join('&');
|
||||
return api.get(`/api/v1/groups/relationships?${q}`);
|
||||
}
|
||||
|
||||
const { entityMap: relationships, ...result } = useBatchedEntities<GroupRelationship>(
|
||||
[Entities.RELATIONSHIPS, ...listKey],
|
||||
ids,
|
||||
fetchGroupRelationships,
|
||||
{ schema: groupRelationshipSchema, enabled: isLoggedIn },
|
||||
);
|
||||
|
||||
const relationships = entities.reduce<Record<string, GroupRelationship>>((map, relationship) => {
|
||||
map[relationship.id] = relationship;
|
||||
return map;
|
||||
}, {});
|
||||
|
||||
return {
|
||||
...result,
|
||||
relationships,
|
||||
};
|
||||
return { relationships, ...result };
|
||||
}
|
||||
|
||||
export { useGroupRelationships };
|
||||
Reference in New Issue
Block a user