import { useEffect, useState } from 'react' import { Sparkles, X } from 'lucide-react' import { ApiError } from '../lib/api' import modalStyles from './CategoryModal.module.css' import aiStyles from '../styles/ai.module.css' import styles from './CategoryAiPromptModal.module.css' export const DEFAULT_CATEGORY_AI_PROMPT = `Create a practical product category tree for my store. Include English names, Farsi names in Persian script, and short English descriptions. Use 3-5 main categories with relevant subcategories where it helps shoppers browse.` interface CategoryAiPromptModalProps { open: boolean onClose: () => void onRun: (prompt: string) => Promise isRunning: boolean } const ANIMATION_MS = 220 export function CategoryAiPromptModal({ open, onClose, onRun, isRunning, }: CategoryAiPromptModalProps) { const [mounted, setMounted] = useState(open) const [closing, setClosing] = useState(false) const [prompt, setPrompt] = useState(DEFAULT_CATEGORY_AI_PROMPT) const [error, setError] = useState('') useEffect(() => { if (open) { setMounted(true) setClosing(false) setPrompt(DEFAULT_CATEGORY_AI_PROMPT) setError('') } else if (mounted) { setClosing(true) const timer = setTimeout(() => { setMounted(false) setClosing(false) }, ANIMATION_MS) return () => clearTimeout(timer) } }, [open, mounted]) useEffect(() => { if (!mounted || closing) return const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape' && !isRunning) onClose() } document.addEventListener('keydown', onKey) return () => document.removeEventListener('keydown', onKey) }, [mounted, closing, isRunning, onClose]) async function handleSubmit(e: React.FormEvent) { e.preventDefault() const trimmed = prompt.trim() if (trimmed.length < 10) { setError('Prompt must be at least 10 characters.') return } setError('') try { await onRun(trimmed) } catch (err) { if (err instanceof ApiError) { setError(err.message) } else if (err instanceof Error) { setError(err.message) } else { setError('Unable to generate categories with AI.') } } } if (!mounted) return null return (
e.target === e.currentTarget && !isRunning && onClose()} role="presentation" >

Fill Categories with AI

Edit the prompt below, then run it to generate and save a category tree.

void handleSubmit(e)}>