import { useMutation, useQueryClient } from '@tanstack/react-query' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { Button } from '../../../shared/ui/Button' import { TextField } from '../../../shared/ui/TextField' import { publish } from '../api' const MAX_DESCRIPTION = 500 export function PublishForm({ slot, onCancel, onPublished, }: { slot: number onCancel: () => void onPublished: () => void }) { const { t } = useTranslation('configs') const client = useQueryClient() const [title, setTitle] = useState('') const [description, setDescription] = useState('') const [touched, setTouched] = useState(false) const mutation = useMutation({ mutationFn: () => publish(slot, title.trim(), description), onSuccess: () => { client.invalidateQueries({ queryKey: ['configs'] }) onPublished() }, }) const invalid = touched && title.trim().length === 0 function submit() { setTouched(true) if (title.trim().length === 0 || title.trim().length > 64 || description.length > MAX_DESCRIPTION) return mutation.mutate() } return (