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

39 lines
1 KiB
TypeScript
Raw Normal View History

import { useEffect } from 'react';
import { useHistory } from 'react-router-dom';
2023-05-02 10:25:32 -07:00
import { Entities } from 'soapbox/entity-store/entities';
import { useEntity } from 'soapbox/entity-store/hooks';
import { useApi } from 'soapbox/hooks';
import { type Group, groupSchema } from 'soapbox/schemas';
import { useGroupRelationship } from './useGroupRelationship';
function useGroup(groupId: string, refetch = true) {
const api = useApi();
const history = useHistory();
2023-05-02 10:25:32 -07:00
const { entity: group, isUnauthorized, ...result } = useEntity<Group>(
2023-05-02 10:25:32 -07:00
[Entities.GROUPS, groupId],
() => api.get(`/api/v1/groups/${groupId}`),
2023-06-08 06:00:49 -07:00
{
schema: groupSchema,
refetch,
enabled: !!groupId,
},
2023-05-02 10:25:32 -07:00
);
const { groupRelationship: relationship } = useGroupRelationship(groupId);
2023-05-02 10:25:32 -07:00
useEffect(() => {
if (isUnauthorized) {
history.push('/login');
}
}, [isUnauthorized]);
2023-05-02 10:25:32 -07:00
return {
...result,
isUnauthorized,
2023-05-02 10:25:32 -07:00
group: group ? { ...group, relationship: relationship || null } : undefined,
};
}
export { useGroup };