import { useEffect, useRef, useState } from 'react' import { FileUp, X } from 'lucide-react' import { api } from '../lib/api' import { inputClass, message } from '../lib/day2' import { PrototypeDialog } from './PrototypeDialog' import { GhostButton, PrimaryButton } from './ui-ext/AiButton' /** 原型手动添加入口:附件真正上传到同源后端,项目与关注原子保存,失败保留输入。 */ export function AddProjectDialog({ onClose, onAdded }: { onClose: () => void; onAdded: () => Promise }) { const [form, setForm] = useState({ url: '', title: '', purchaser: '', region: '', budget: '' }) const [file, setFile] = useState<{ id: string; filename: string } | null>(null) const [saved, setSaved] = useState(false) const [limit, setLimit] = useState(20 * 1024 * 1024) const [busy, setBusy] = useState(false), [error, setError] = useState('') const pending = useRef<{ body: string; commandId: string } | null>(null) useEffect(() => { let active = true void api<{ maxFileBytes: number }>('/system/limits').then(value => { if (active) setLimit(value.maxFileBytes) }).catch(e => { if (active) setError(message(e)) }) return () => { active = false } }, []) const upload = async (value?: File) => { if (!value || busy) return if (value.size > limit) { setError(`文件不能超过 ${Math.floor(limit / 1024 / 1024)} MB`); return } setBusy(true); setError('') try { // 替换附件先清理旧临时原件;只有上传成功后才展示新文件名。 if (file) { await api(`/files/${file.id}`, { method: 'DELETE' }); setFile(null) } const body = new FormData(); body.set('purpose', 'BID_PROJECT'); body.set('file', value) const uploaded = await api<{ id: string; filename: string }>('/files', { method: 'POST', body }) setFile(uploaded) // 与原型一致,仅用文件名预填尚未填写的项目名称,不虚构缺失的采购方、预算等资料。 setForm(current => current.title ? current : { ...current, title: uploaded.filename.replace(/\.[^.]+$/, '') }) } catch (e) { setError(message(e)) } finally { setBusy(false) } } const close = async () => { if (busy) return setBusy(true); setError('') // 已尝试提交但响应未知时,文件可能已关联成功;关闭只保留到期清理,不能误删已保存项目的原件。 try { if (file && !pending.current) await api(`/files/${file.id}`, { method: 'DELETE' }); onClose() } catch (e) { setError(message(e)) } finally { setBusy(false) } } const submit = async () => { if (busy) return if (!form.url.trim() && !file) { setError('请填写公告网址或上传公告文件'); return } if (form.url.trim() && !/^https?:\/\//i.test(form.url.trim())) { setError('网址需以 http:// 或 https:// 开头'); return } setBusy(true); setError('') try { if (saved) { await onAdded(); onClose(); return } const data = { title: form.title.trim(), url: form.url.trim(), purchaser: form.purchaser.trim(), region: form.region.trim(), budgetWan: form.budget === '' ? null : Number(form.budget), fileId: file?.id ?? null } const body = JSON.stringify(data) if (pending.current?.body !== body) pending.current = { body, commandId: crypto.randomUUID() } await api('/watch/manual', { method: 'POST', body: JSON.stringify({ ...data, commandId: pending.current.commandId }) }) // 保存后即使刷新列表失败,也不能在关闭弹窗时删除已绑定的附件。 setFile(null); pending.current = null; setSaved(true) await onAdded(); onClose() } catch (e) { setError(message(e)) } finally { setBusy(false) } } return void close()} title="添加项目" className="w-[420px]">

粘贴公告网址,或直接上传公告文件(PDF / Word / 图片)。

{ e.preventDefault(); void submit() }} className="mt-3 space-y-2.5">
{file ?
{file.filename}
: }
{error &&

{error}

}
void close()}>取消{saved ? '重试' : '添加并关注'}
}