import { useState } from 'react' import { useTranslation } from 'react-i18next' import axios from 'axios' import { SparkleIcon, XIcon } from '@phosphor-icons/react' import { Alert, Button, TextArea } from '@/ui' import { useAiSuggestField } from '@/api/mutations' import { cn } from '@/lib/utils' /** * AI Schema Assist — per-field suggest panel. * *

Expanded inline (не modal) — admin вводит описание → preview JSON suggestion * → accept/reject. Accept callback получает {fieldName, schema} как plain JSON * fragment, caller интегрирует в свой schema state. * *

Graceful degradation: *

*/ type Props = { /** Current schema serialized as JSON object — будет передан как context */ existingSchema: Record /** Called когда юзер accepted suggestion */ onAccept: (suggestion: { fieldName: string; schema: Record }) => void /** Optional close handler — hide panel */ onClose?: () => void } export function AiFieldSuggestPanel({ existingSchema, onAccept, onClose }: Props) { const { t } = useTranslation() const [prompt, setPrompt] = useState('') const [suggestion, setSuggestion] = useState<{ fieldName: string schema: Record } | null>(null) const mutation = useAiSuggestField() const handleSuggest = () => { if (!prompt.trim()) return setSuggestion(null) mutation.mutate( { existingSchema, prompt }, { onSuccess: (data) => setSuggestion(data), }, ) } const handleAccept = () => { if (!suggestion) return onAccept(suggestion) setPrompt('') setSuggestion(null) } const handleRetry = () => { setSuggestion(null) handleSuggest() } const errorMessage = mutation.error ? formatError(mutation.error, t) : null return (
{t('aiSuggest.title', { defaultValue: 'AI: добавить поле' })} {onClose && ( )}