2023-03-14 07:19:34 -07:00
|
|
|
import { Entities } from 'soapbox/entity-store/entities';
|
2023-05-02 10:25:32 -07:00
|
|
|
import { useEntities } from 'soapbox/entity-store/hooks';
|
|
|
|
import { useApi } from 'soapbox/hooks';
|
2023-05-01 11:58:40 -07:00
|
|
|
import { useFeatures } from 'soapbox/hooks/useFeatures';
|
2023-05-02 10:25:32 -07:00
|
|
|
import { groupSchema, type Group } from 'soapbox/schemas/group';
|
|
|
|
|
|
|
|
import { useGroupRelationships } from './useGroupRelationships';
|
2023-03-09 09:21:27 -08:00
|
|
|
|
2023-03-23 05:43:41 -07:00
|
|
|
function useGroups(q: string = '') {
|
2023-03-23 17:22:26 -07:00
|
|
|
const api = useApi();
|
2023-03-19 12:32:53 -07:00
|
|
|
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 } }),
|
2023-03-19 12:32:53 -07:00
|
|
|
{ enabled: features.groups, schema: groupSchema },
|
2023-03-14 07:19:34 -07:00
|
|
|
);
|
2023-03-09 13:05:54 -08: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:47:24 -08:00
|
|
|
|
2023-03-09 09:21:27 -08:00
|
|
|
return {
|
|
|
|
...result,
|
2023-03-09 09:47:24 -08:00
|
|
|
groups,
|
2023-03-09 09:21:27 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-02 10:25:32 -07:00
|
|
|
export { useGroups };
|