import { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom' import toast from 'react-hot-toast' import { ArrowLeft, Loader2, Plus, Trash2 } from 'lucide-react' import { readerAPI, type ReaderReplaceRule, type ReplaceRuleInput } from '../../api/reader' // 替换净化规则页(仿 legado ReplaceRuleActivity:列表 + 启停 + 编辑)。 const emptyInput: ReplaceRuleInput = { name: '', group: '', pattern: '', replacement: '', scope: '', scope_title: false, scope_content: true, exclude_scope: '', is_enabled: true, is_regex: true, timeout_millisecond: 3000, order: 0, } export default function ReaderReplacePage() { const navigate = useNavigate() const [rules, setRules] = useState(null) const [showForm, setShowForm] = useState(false) const [form, setForm] = useState(emptyInput) const [saving, setSaving] = useState(false) const load = () => { readerAPI .listReplaceRules() .then(setRules) .catch(() => toast.error('加载替换规则失败')) } // eslint-disable-next-line react-hooks/exhaustive-deps useEffect(load, []) const save = async () => { if (!form.pattern.trim()) { toast.error('替换规则不能为空') return } setSaving(true) try { await readerAPI.createReplaceRule(form) toast.success('已添加') setForm(emptyInput) setShowForm(false) load() } catch (e) { toast.error((e as { response?: { data?: { error?: string } } })?.response?.data?.error ?? '保存失败') } finally { setSaving(false) } } const toggle = async (r: ReaderReplaceRule) => { try { await readerAPI.updateReplaceRule(r.id, { name: r.name, group: r.group, pattern: r.pattern, replacement: r.replacement, scope: r.scope, scope_title: r.scope_title, scope_content: r.scope_content, exclude_scope: r.exclude_scope, is_enabled: !r.is_enabled, is_regex: r.is_regex, timeout_millisecond: 3000, order: r.order, }) setRules((prev) => prev?.map((x) => (x.id === r.id ? { ...x, is_enabled: !x.is_enabled } : x)) ?? null) } catch { toast.error('更新失败') } } const remove = async (r: ReaderReplaceRule) => { if (!window.confirm(`删除规则「${r.name || r.pattern}」?`)) return try { await readerAPI.deleteReplaceRule(r.id) setRules((prev) => prev?.filter((x) => x.id !== r.id) ?? null) } catch { toast.error('删除失败') } } return (

替换净化

{showForm && (
setForm({ ...form, name: e.target.value })} placeholder="规则名(可选)" className="rounded-xl border border-[var(--app-border)] bg-[var(--app-panel-soft)] px-3 py-2 text-xs text-[var(--app-text)] outline-none" /> setForm({ ...form, group: e.target.value })} placeholder="分组(可选)" className="rounded-xl border border-[var(--app-border)] bg-[var(--app-panel-soft)] px-3 py-2 text-xs text-[var(--app-text)] outline-none" />