2023-03-09 09:21:27 -08:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
2023-03-09 09:23:14 -08:00
|
|
|
import { useEntities, useEntity } from 'soapbox/entity-store/hooks';
|
2023-03-09 09:21:27 -08:00
|
|
|
import { normalizeGroup } from 'soapbox/normalizers';
|
|
|
|
|
|
|
|
import type { Group } from 'soapbox/types/entities';
|
|
|
|
|
|
|
|
function useGroups() {
|
|
|
|
const result = useEntities<Group>(['Group', ''], '/api/v1/groups');
|
|
|
|
const { entities, isLoading, fetchEntities } = result;
|
|
|
|
|
2023-03-09 09:23:14 -08:00
|
|
|
// Note: we have to fetch them in the hook right now because I haven't implemented
|
|
|
|
// max-age or cache expiry in the entity store yet. It's planned.
|
2023-03-09 09:21:27 -08:00
|
|
|
useEffect(() => {
|
|
|
|
if (!isLoading) {
|
|
|
|
fetchEntities();
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...result,
|
|
|
|
groups: entities.map(normalizeGroup),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-09 09:23:14 -08:00
|
|
|
function useGroup(groupId: string) {
|
|
|
|
const result = useEntity<Group>(['Group', groupId], `/api/v1/groups/${groupId}`);
|
|
|
|
const { entity, isLoading, fetchEntity } = result;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!isLoading) {
|
|
|
|
fetchEntity();
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...result,
|
|
|
|
group: entity ? normalizeGroup(entity) : undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useGroup, useGroups };
|