2023-06-29 13:10:45 -07:00
|
|
|
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();
|
2023-06-29 13:10:45 -07:00
|
|
|
const history = useHistory();
|
2023-05-02 10:25:32 -07:00
|
|
|
|
2023-06-29 13:10:45 -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
|
|
|
);
|
2023-06-26 10:09:30 -07:00
|
|
|
const { groupRelationship: relationship } = useGroupRelationship(groupId);
|
2023-05-02 10:25:32 -07:00
|
|
|
|
2023-06-29 13:10:45 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (isUnauthorized) {
|
|
|
|
history.push('/login');
|
|
|
|
}
|
|
|
|
}, [isUnauthorized]);
|
|
|
|
|
2023-05-02 10:25:32 -07:00
|
|
|
return {
|
|
|
|
...result,
|
2023-06-29 13:10:45 -07:00
|
|
|
isUnauthorized,
|
2023-05-02 10:25:32 -07:00
|
|
|
group: group ? { ...group, relationship: relationship || null } : undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useGroup };
|