pleroma/app/soapbox/api/hooks/groups/useGroups.ts

93 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-04-10 10:06:06 -07:00
import { useEffect } from 'react';
import { z } from 'zod';
2023-04-10 10:06:06 -07:00
import { fetchGroupRelationshipsSuccess } from 'soapbox/actions/groups';
2023-03-14 07:19:34 -07:00
import { Entities } from 'soapbox/entity-store/entities';
import { useEntities, useEntity } from 'soapbox/entity-store/hooks';
2023-04-10 10:06:06 -07:00
import { useApi, useAppDispatch } from 'soapbox/hooks';
2023-05-01 11:58:40 -07:00
import { useFeatures } from 'soapbox/hooks/useFeatures';
import { groupSchema, Group } from 'soapbox/schemas/group';
import { groupRelationshipSchema, GroupRelationship } from 'soapbox/schemas/group-relationship';
2023-03-09 09:21:27 -08:00
2023-03-23 05:43:41 -07:00
function useGroups(q: string = '') {
const api = useApi();
const features = useFeatures();
2023-03-14 07:19:34 -07:00
const { entities, ...result } = useEntities<Group>(
2023-03-23 05:43:41 -07:00
[Entities.GROUPS, 'search', q],
() => api.get('/api/v1/groups', { params: { q } }),
{ enabled: features.groups, schema: groupSchema },
2023-03-14 07:19:34 -07:00
);
const { relationships } = useGroupRelationships(entities.map(entity => entity.id));
2023-03-09 09:21:27 -08:00
2023-03-14 07:19:34 -07:00
const groups = entities.map((group) => ({
...group,
relationship: relationships[group.id] || null,
}));
2023-03-09 09:21:27 -08:00
return {
...result,
groups,
2023-03-09 09:21:27 -08:00
};
}
function useGroup(groupId: string, refetch = true) {
const api = useApi();
2023-03-14 07:19:34 -07:00
const { entity: group, ...result } = useEntity<Group>(
[Entities.GROUPS, groupId],
() => api.get(`/api/v1/groups/${groupId}`),
2023-03-14 07:19:34 -07:00
{ schema: groupSchema, refetch },
);
const { entity: relationship } = useGroupRelationship(groupId);
return {
...result,
group: group ? { ...group, relationship: relationship || null } : undefined,
2023-03-09 10:36:20 -08:00
};
}
function useGroupRelationship(groupId: string) {
const api = useApi();
2023-04-10 10:06:06 -07:00
const dispatch = useAppDispatch();
2023-04-10 10:06:06 -07:00
const { entity: groupRelationship, ...result } = useEntity<GroupRelationship>(
2023-03-14 07:19:34 -07:00
[Entities.GROUP_RELATIONSHIPS, groupId],
() => api.get(`/api/v1/groups/relationships?id[]=${groupId}`),
{ schema: z.array(groupRelationshipSchema).transform(arr => arr[0]) },
2023-03-14 07:19:34 -07:00
);
2023-04-10 10:06:06 -07:00
useEffect(() => {
if (groupRelationship?.id) {
dispatch(fetchGroupRelationshipsSuccess([groupRelationship]));
}
}, [groupRelationship?.id]);
return {
entity: groupRelationship,
...result,
};
}
function useGroupRelationships(groupIds: string[]) {
const api = useApi();
const q = groupIds.map(id => `id[]=${id}`).join('&');
2023-03-14 07:19:34 -07:00
const { entities, ...result } = useEntities<GroupRelationship>(
[Entities.GROUP_RELATIONSHIPS, ...groupIds],
() => api.get(`/api/v1/groups/relationships?${q}`),
{ schema: groupRelationshipSchema, enabled: groupIds.length > 0 },
2023-03-14 07:19:34 -07:00
);
const relationships = entities.reduce<Record<string, GroupRelationship>>((map, relationship) => {
map[relationship.id] = relationship;
return map;
}, {});
return {
...result,
relationships,
};
}
export { useGroup, useGroups, useGroupRelationship, useGroupRelationships };