bigbuffet-rw/app/soapbox/hooks/useGroups.ts

42 lines
1 KiB
TypeScript
Raw Normal View History

2023-03-09 09:21:27 -08:00
import { useEffect } from 'react';
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;
// 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),
};
}
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 };