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

39 lines
1.1 KiB
TypeScript
Raw Normal View History

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';
import { useFeatures } from 'soapbox/hooks/useFeatures';
2023-04-12 13:51:30 -07:00
import { groupSchema } from 'soapbox/schemas';
import { useGroupRelationship } from './useGroupRelationship';
2023-04-12 13:51:30 -07:00
function useGroupLookup(slug: string) {
const api = useApi();
const features = useFeatures();
const history = useHistory();
2023-04-12 13:51:30 -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}`),
{ schema: groupSchema, enabled: features.groups && !!slug },
2023-04-12 13:51:30 -07:00
);
const { groupRelationship: relationship } = useGroupRelationship(group?.id);
useEffect(() => {
if (isUnauthorized) {
history.push('/login');
}
}, [isUnauthorized]);
return {
...result,
isUnauthorized,
entity: group ? { ...group, relationship: relationship || null } : undefined,
};
2023-04-12 13:51:30 -07:00
}
export { useGroupLookup };