import { useState } from "react"; import { useAsyncData } from "../../lib/useAsyncData"; import { adminListAutomations, adminCreateAutomation, adminRunAutomation, adminDeleteAutomation, isNotImplemented, } from "../../lib/api"; import type { AdminAutomationRecord } from "../../lib/types"; import { useAuditStore } from "../audit"; import { Notice, PageHeader } from "../components"; import { Button, Surface, Switch, Tag } from "../../components/primitives"; import { Icon } from "../../components/Icon"; const sample: AdminAutomationRecord[] = [ { id: "r1", name: "睡前总结", kind: "reminder", cron: "0 22 * * *", enabled: true }, { id: "r2", name: "晨间问候", kind: "cron", cron: "0 8 * * *", enabled: true }, { id: "r3", name: "设备待机提醒", kind: "queue", cron: "—", enabled: false }, ]; export function AutomationsModule(): React.ReactNode { const { data, error, reload } = useAsyncData(adminListAutomations, []); const log = useAuditStore((s) => s.log); const backend = !isNotImplemented(error); const rows = backend && data && data.length > 0 ? data : sample; const [name, setName] = useState(""); const [kind, setKind] = useState("cron"); const [expr, setExpr] = useState(""); const [busy, setBusy] = useState(false); const [msg, setMsg] = useState(null); const create = () => { setBusy(true); setMsg(null); adminCreateAutomation({ name, kind, expression: expr }) .then((r) => { log("automation.create", "ok"); setMsg(`已创建 ${r.name}(${r.kind})`); setName(""); setExpr(""); reload(); }) .catch((e: unknown) => { log("automation.create", "error"); setMsg(e instanceof Error ? e.message : "创建失败"); }) .finally(() => setBusy(false)); }; const run = (id: string, label: string) => { setBusy(true); setMsg(null); adminRunAutomation(id) .then((r) => { log("automation.run", "ok"); setMsg(`已触发 ${label}:${JSON.stringify(r.output)}`); }) .catch((e: unknown) => { log("automation.run", "error"); setMsg(e instanceof Error ? e.message : "触发失败"); }) .finally(() => setBusy(false)); }; const del = (id: string, label: string) => { setBusy(true); setMsg(null); adminDeleteAutomation(id) .then(() => { log("automation.delete", "ok"); setMsg(`已删除 ${label}`); reload(); }) .catch((e: unknown) => { log("automation.delete", "error"); setMsg(e instanceof Error ? e.message : "删除失败"); }) .finally(() => setBusy(false)); }; return (
新增自动化 } /> {msg ? {msg} : null} {backend && !error ? 后端已返回自动化配置(数据以接口为准)。 : null} {error && !backend ? 后端未提供 /api/v1/admin/automations,以下为示例壳(M10 提供真实数据)。 : null} {error && backend ? {error} : null}

提醒 / CRON / 队列事件

{rows.map((r) => (
{r.name} {r.kind} {r.cron || "—"} {}} label="启用" />
))}

后台任务 / 运行历史

{["消息队列事件", "后台任务(含终端任务)", "运行日志"].map((x) => (
{x} 占位 —— 后端 M10+ 提供
))}
); } const inputCls = "w-full rounded-[var(--mu-radius)] bg-[var(--mu-surface-2)] px-3 py-2 text-sm outline-none focus:ring-1 focus:ring-[var(--mu-accent)]";