#5 规则 DELETE + id:暴露 RuleView.id(PK);新增 DELETE /projects/{id}/rules/{rule_id} (项目/规则不存在→404,成功→204,按 (id,project_id) 限定);rule_repo 加 list_for_project/delete;RulesPage 每条加删除(乐观删+回滚+toast)。assemble 侧 RuleView(缓存前缀)不动,列表另立 RuleListItemView。 #7 codex 角色 relations:写侧本已持久化、读端点 _existing_characters 硬编码 []。 加 _relations_from_jsonb 解析 {name,kind,note},CodexPage 渲染关系 chip。 #8 角色入库幂等:SqlCharacterWriteRepo.create 改 (project_id,name) app 层 upsert—— 重复入库改更新而非插入;不加 UNIQUE/迁移(线上已有重复行会让约束迁移失败)。 #1 大纲卷过滤:GET /outline 支持可选 ?volume(无参=全部,向后兼容);OutlineEditor 加「查看:全部/卷N」筛选,与生成目标卷解耦。 H3/#9 文风回炉锚点:StyleDriftSegment 加 text(逐字命中段),style.md 指示审稿输出; 前端按内容锚点定位回炉目标(idx 仅排序),命中失败 → 提示「无法定位该段」而非 静默 no-op。style golden fixture 已重生成。 契约变更已 pnpm gen:api(RuleView.id / DELETE rules / outline ?volume)。无迁移 (alembic 无漂移)。门禁绿:ruff/mypy(210)/alembic/pytest 760 · 前端 tsc/lint/vitest 329。
119 lines
4.1 KiB
TypeScript
119 lines
4.1 KiB
TypeScript
"use client";
|
||
|
||
import { useMemo, useState } from "react";
|
||
|
||
import { AppShell } from "@/components/AppShell";
|
||
import type { ProjectResponse, RuleView } from "@/lib/api/types";
|
||
import {
|
||
RULE_LEVELS,
|
||
RULE_LEVEL_LABELS,
|
||
groupByLevel,
|
||
type RuleLevel,
|
||
} from "@/lib/rules/rules";
|
||
import { useRules } from "@/lib/rules/useRules";
|
||
|
||
interface RulesPageProps {
|
||
project: ProjectResponse;
|
||
initialRules: RuleView[];
|
||
}
|
||
|
||
// 规则页(UX §7):四级规则列表 + 新增(乐观 + 回滚)。
|
||
export function RulesPage({ project, initialRules }: RulesPageProps) {
|
||
const { items, busy, add, remove } = useRules(initialRules);
|
||
const [level, setLevel] = useState<RuleLevel>("project");
|
||
const [content, setContent] = useState("");
|
||
const groups = useMemo(() => groupByLevel(items), [items]);
|
||
|
||
const onSubmit = async (e: React.FormEvent): Promise<void> => {
|
||
e.preventDefault();
|
||
const ok = await add(project.id, level, content);
|
||
if (ok) setContent("");
|
||
};
|
||
|
||
return (
|
||
<AppShell
|
||
title={`〈${project.title}〉`}
|
||
subtitle="规则"
|
||
projectId={project.id}
|
||
activeNav="rules"
|
||
>
|
||
<div className="mx-auto flex max-w-3xl flex-col gap-6 p-6">
|
||
<form
|
||
onSubmit={onSubmit}
|
||
className="rounded border border-line bg-panel p-4"
|
||
>
|
||
<h1 className="mb-3 font-serif text-lg text-ink">新增规则</h1>
|
||
<div className="mb-3 flex items-end gap-3">
|
||
<label className="text-sm text-ink-soft">
|
||
级别
|
||
<select
|
||
value={level}
|
||
onChange={(e) => setLevel(e.target.value as RuleLevel)}
|
||
className="mt-1 block rounded border border-line bg-bg px-2 py-1.5 text-sm text-ink"
|
||
>
|
||
{RULE_LEVELS.map((lv) => (
|
||
<option key={lv} value={lv}>
|
||
{RULE_LEVEL_LABELS[lv]}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</label>
|
||
</div>
|
||
<textarea
|
||
value={content}
|
||
onChange={(e) => setContent(e.target.value)}
|
||
placeholder="如:本作禁用现代科技词汇;称呼一律用「道友」"
|
||
rows={3}
|
||
className="w-full resize-y rounded border border-line bg-bg px-3 py-2 text-sm text-ink"
|
||
/>
|
||
<button
|
||
type="submit"
|
||
disabled={busy || content.trim().length === 0}
|
||
className="mt-3 rounded bg-cinnabar px-4 py-2 text-sm text-white disabled:opacity-50"
|
||
>
|
||
{busy ? "保存中…" : "新增规则"}
|
||
</button>
|
||
</form>
|
||
|
||
<div className="flex flex-col gap-4">
|
||
{RULE_LEVELS.map((lv) => (
|
||
<section key={lv}>
|
||
<h2 className="mb-2 font-serif text-sm text-ink">
|
||
{RULE_LEVEL_LABELS[lv]}
|
||
<span className="ml-2 text-xs text-ink-soft">
|
||
{groups[lv].length}
|
||
</span>
|
||
</h2>
|
||
{groups[lv].length === 0 ? (
|
||
<p className="text-xs text-ink-soft">(暂无)</p>
|
||
) : (
|
||
<ul className="flex flex-col gap-2">
|
||
{groups[lv].map((rule) => (
|
||
<li
|
||
key={rule.id}
|
||
className="flex items-start justify-between gap-3 rounded border border-line bg-panel px-3 py-2 text-sm text-ink"
|
||
>
|
||
<span className="min-w-0 flex-1 break-words">
|
||
{rule.content}
|
||
</span>
|
||
<button
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => void remove(project.id, rule.id)}
|
||
aria-label="删除规则"
|
||
className="shrink-0 text-xs text-ink-soft hover:text-cinnabar disabled:opacity-50"
|
||
>
|
||
删除
|
||
</button>
|
||
</li>
|
||
))}
|
||
</ul>
|
||
)}
|
||
</section>
|
||
))}
|
||
</div>
|
||
</div>
|
||
</AppShell>
|
||
);
|
||
}
|