import { useQuery } from '@tanstack/react-query'; import { useApi } from 'soapbox/hooks/useApi'; type Validation = { error: string message: string } const ValidationKeys = { validation: (name: string) => ['group', 'validation', name] as const, }; function useGroupValidation(name: string = '') { const api = useApi(); const getValidation = async() => { const { data } = await api.get('/api/v1/groups/validate', { params: { name }, }) .catch((error) => { if (error.response.status === 422) { return { data: error.response.data }; } throw error; }); return data; }; const queryInfo = useQuery(ValidationKeys.validation(name), getValidation, { enabled: !!name, }); return { ...queryInfo, data: { ...queryInfo.data, isValid: !queryInfo.data?.error, }, }; } export { useGroupValidation };