2023-06-29 13:10:45 -07:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
|
|
|
|
2023-04-12 13:51:30 -07:00
|
|
|
import { Entities } from 'soapbox/entity-store/entities';
|
|
|
|
import { useEntityLookup } from 'soapbox/entity-store/hooks';
|
|
|
|
import { useApi } from 'soapbox/hooks/useApi';
|
2023-06-29 13:10:45 -07:00
|
|
|
import { useFeatures } from 'soapbox/hooks/useFeatures';
|
2023-04-12 13:51:30 -07:00
|
|
|
import { groupSchema } from 'soapbox/schemas';
|
|
|
|
|
2023-05-11 11:41:31 -07:00
|
|
|
import { useGroupRelationship } from './useGroupRelationship';
|
|
|
|
|
2023-04-12 13:51:30 -07:00
|
|
|
function useGroupLookup(slug: string) {
|
|
|
|
const api = useApi();
|
2023-06-29 13:10:45 -07:00
|
|
|
const features = useFeatures();
|
|
|
|
const history = useHistory();
|
2023-04-12 13:51:30 -07:00
|
|
|
|
2023-06-29 13:10:45 -07:00
|
|
|
const { entity: group, isUnauthorized, ...result } = useEntityLookup(
|
2023-04-12 13:51:30 -07:00
|
|
|
Entities.GROUPS,
|
|
|
|
(group) => group.slug === slug,
|
|
|
|
() => api.get(`/api/v1/groups/lookup?name=${slug}`),
|
2023-06-29 13:10:45 -07:00
|
|
|
{ schema: groupSchema, enabled: features.groups && !!slug },
|
2023-04-12 13:51:30 -07:00
|
|
|
);
|
2023-05-11 11:41:31 -07:00
|
|
|
|
2023-06-26 10:09:30 -07:00
|
|
|
const { groupRelationship: relationship } = useGroupRelationship(group?.id);
|
2023-05-11 11:41:31 -07:00
|
|
|
|
2023-06-29 13:10:45 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (isUnauthorized) {
|
|
|
|
history.push('/login');
|
|
|
|
}
|
|
|
|
}, [isUnauthorized]);
|
|
|
|
|
2023-05-11 11:41:31 -07:00
|
|
|
return {
|
|
|
|
...result,
|
2023-06-29 13:10:45 -07:00
|
|
|
isUnauthorized,
|
2023-05-11 11:41:31 -07:00
|
|
|
entity: group ? { ...group, relationship: relationship || null } : undefined,
|
|
|
|
};
|
2023-04-12 13:51:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export { useGroupLookup };
|