import { useState } from 'react' import { Plus, Play, Pencil, Trash2, X, Layers } from 'lucide-react' import { motion, AnimatePresence } from 'framer-motion' import type { ScheduleTask, SearchConditions } from '../types' import { useStore, conditionsText, uid, nowStr } from '../lib/store' import { PrimaryButton, GhostButton } from './ui-ext/AiButton' import { cn } from '@/lib/utils' interface Props { currentConditions: SearchConditions /** 任务列表弹窗是否打开(列表不占首页版面) */ open: boolean onClose: () => void } const emptyTaskForm = (conds: SearchConditions, n: number) => ({ name: `方案${n}`, conditions: { ...conds }, }) const inputCls = 'rounded-ctrl border border-line-soft bg-white px-3 py-2 text-sm text-ink-900 outline-none transition-shadow placeholder:text-ink-400 focus:border-brand-500/60 focus:ring-2 focus:ring-brand-500/10' export function ScheduleTasks({ currentConditions, open, onClose }: Props) { const { tasks, saveTask, deleteTask, runTaskNow } = useStore() const [editing, setEditing] = useState(null) const [showModal, setShowModal] = useState(false) const [form, setForm] = useState(() => emptyTaskForm(currentConditions, tasks.length + 1)) const [confirmDelete, setConfirmDelete] = useState(null) const openCreate = () => { setEditing(null) setForm(emptyTaskForm(currentConditions, tasks.length + 1)) setShowModal(true) } const openEdit = (t: ScheduleTask) => { setEditing(t) setForm({ name: t.name, conditions: { ...t.conditions } }) setShowModal(true) } const submit = () => { if (!form.name.trim()) return if (editing) { saveTask({ ...editing, name: form.name.trim(), conditions: form.conditions }) } else { // 检索方案不再包含定时设置;保留字段默认值仅作数据兼容 saveTask({ id: uid('task'), name: form.name.trim(), frequency: 'daily', time: '09:15', conditions: form.conditions, createdAt: nowStr(), runs: [], }) } setShowModal(false) } return ( <> {/* 检索方案列表:以弹窗承载,不在首页占位;点检索区「检索方案」打开 */} {open && ( { if (e.target === e.currentTarget) onClose() }}>
检索方案 {tasks.length} 个已保存的方案 新建方案
{tasks.length === 0 ? (
还没有检索方案,点右上角「新建方案」,把常用的检索条件存下来一键复用
) : (
    {tasks.map(t => { const last = t.runs[0] return (
  • {t.name} {conditionsText(t.conditions)} {last ? ( 最近执行 {last.time} ·{' '} {last.status === 'success' ? {last.count} 条结果 : 失败:{last.error ?? '未知原因'}} ) : ( 尚未执行 )}
  • ) })}
)}
)}
{/* 创建 / 编辑检索方案窗口 */} {showModal && ( { if (e.target === e.currentTarget) setShowModal(false) }}>

{editing ? '编辑检索方案' : '新建检索方案'}

setForm(f => ({ ...f, name: e.target.value }))} className={cn(inputCls, 'mt-1 w-full')} />
检索条件
setForm(f => ({ ...f, conditions: { ...f.conditions, keyword: e.target.value } }))} className={cn(inputCls, '!py-1.5 text-[13px]')} /> setForm(f => ({ ...f, conditions: { ...f.conditions, region: e.target.value } }))} className={cn(inputCls, '!py-1.5 text-[13px]')} /> setForm(f => ({ ...f, conditions: { ...f.conditions, budgetMin: e.target.value } }))} className={cn(inputCls, '!py-1.5 font-num text-[13px]')} /> setForm(f => ({ ...f, conditions: { ...f.conditions, budgetMax: e.target.value } }))} className={cn(inputCls, '!py-1.5 font-num text-[13px]')} />
setShowModal(false)}>取消 {editing ? '保存修改' : '保存方案'}
)}
{/* 删除确认 */} {confirmDelete && ( { if (e.target === e.currentTarget) setConfirmDelete(null) }}>
!

删除这个检索方案?

删除后不可恢复,历史执行记录将一并删除。

setConfirmDelete(null)}>取消 { deleteTask(confirmDelete); setConfirmDelete(null) }} className="!bg-bad-500 hover:!bg-bad-500/90 !shadow-[0_2px_8px_rgba(244,63,94,.25)]">删除
)}
) }