import { useState, useEffect } from 'react' import { useTranslation } from 'react-i18next' import { FolderOpen, Sparkles } from 'lucide-react' import { useProjectStore } from '../../stores/project-store' import { ipc } from '../../services/ipc-client' import { Dialog, DialogContent, DialogHeader, DialogFooter, DialogTitle, DialogDescription, } from '../ui/Dialog' import { Button } from '../ui/Button' import { Input } from '../ui/Input' import { Label } from '../ui/Label' interface NewProjectDialogProps { open: boolean onClose: () => void } /** 新建项目对话框 */ export default function NewProjectDialog({ open, onClose }: NewProjectDialogProps) { const { t } = useTranslation('dialogs') const createProject = useProjectStore((s) => s.createProject) const [name, setName] = useState('') const [path, setPath] = useState('') const [creating, setCreating] = useState(false) /** 对话框每次打开时重置名称 */ useEffect(() => { if (!open) return let mounted = true Promise.resolve().then(() => { if (mounted) setName('') }) return () => { mounted = false } }, [open]) /** 选择文件夹 */ const handleSelectFolder = async () => { const selected = await ipc.invoke('dialog:select-folder') if (selected) setPath(selected) } /** 创建项目(类型/受众留空,在小说配置页面填写) */ const handleCreate = async () => { if (!name.trim() || !path.trim()) return setCreating(true) const success = await createProject({ name: name.trim(), path: path.trim(), genre: '', targetAudience: '', }) setCreating(false) if (success) { onClose() } } return ( !v && onClose()}> {t('newProject.title')} {t('newProject.description')} {/* 表单 */}
{/* 项目名称 */}
setName(e.target.value)} placeholder={t('newProject.namePlaceholder')} autoFocus onKeyDown={(e) => e.key === 'Enter' && handleCreate()} />
{/* 保存路径 */}
setPath(e.target.value)} placeholder={t('newProject.pathPlaceholder')} className="flex-1" />
) }