import React, { useState, useCallback } from "react"; import { Flex, Button, Text, Stack, Card } from "@sanity/ui"; import { SparklesIcon } from "@sanity/icons"; import { useFormValue } from "sanity"; import { getPluginConfig } from "../config"; import { generateSEOContent, AIField } from "../utils/aiGenerate"; import { resolveBodyContent, getBodyFieldPaths } from "../utils/resolveBodyContent"; interface Props { field: AIField; focusKeyword?: string; onGenerate: (value: string) => void; } export default function AIGenerateButton({ field, focusKeyword = "", onGenerate }: Props) { const config = getPluginConfig(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const docValue = useFormValue([]); const bodyFieldPaths = getBodyFieldPaths(config.bodyFields, config.bodyField); const bodyText = resolveBodyContent(docValue, bodyFieldPaths); const handleGenerate = useCallback(async () => { if (!config.aiFeature) return; setLoading(true); setError(null); try { const result = await generateSEOContent(field, bodyText, focusKeyword, config.aiFeature); onGenerate(result); } catch (err: unknown) { setError(err instanceof Error ? err.message : "AI generation failed. Check your API key."); } finally { setLoading(false); } }, [config.aiFeature, field, bodyText, focusKeyword, onGenerate]); if (!config.aiFeature) return null; return (