From 7241b48df9c2b62bb882745eb2fd7c04b59c7dc5 Mon Sep 17 00:00:00 2001 From: Todos Date: Sun, 8 Nov 2026 18:17:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(public):=20=E9=A6=96=E9=A1=B5=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E6=A8=A1=E5=BC=8F=E6=94=AF=E6=8C=81=E6=8B=96=E6=8B=BD?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E5=88=86=E7=BB=84=E3=80=81=E8=A7=92=E6=A0=87?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=8E=E7=82=B9=E5=87=BB=E7=BC=96=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - dnd-kit 多容器拖拽:分类内换位、跨分类落点/标题行末尾/空分类,乐观写入 + 失败整体回滚 - 编辑模式坞入口(铅笔 ⇄ 对勾)、Esc/场景/搜索自动退出;✕ 延迟 5 秒落库并带撤销 - 链接编辑弹窗抽成共享组件,首页与后台复用;privateData 由 adminData 同步投影,不整表重拉 - 补充 store/纯函数/组件测试与 390px 底部留白 Checkpoint: 5AD6rIaHM66eWgl0E4t4C --- web/src/components/admin/crud-panels.tsx | 184 +------ web/src/components/admin/link-edit-dialog.tsx | 239 +++++++++ web/src/components/home-edit-board.tsx | 420 ++++++++++++++++ web/src/components/link-grid.tsx | 166 +++++-- web/src/components/public-dock.tsx | 30 +- web/src/components/public-page.tsx | 140 +++++- web/src/components/search-bar.tsx | 8 +- web/src/lib/api.ts | 19 + web/src/lib/link-order.ts | 53 ++ web/src/lib/mockDb.ts | 27 ++ web/src/lib/nav-store.ts | 373 +++++++++++++- web/test/api-link-move.test.ts | 67 +++ web/test/link-order.test.ts | 92 ++++ web/test/nav-store-link-edits.test.ts | 459 ++++++++++++++++++ web/test/public-dock.test.tsx | 39 ++ web/test/public-edit-mode.test.tsx | 394 +++++++++++++++ web/test/public-page.test.tsx | 4 +- 17 files changed, 2471 insertions(+), 243 deletions(-) create mode 100644 web/src/components/admin/link-edit-dialog.tsx create mode 100644 web/src/components/home-edit-board.tsx create mode 100644 web/src/lib/link-order.ts create mode 100644 web/test/api-link-move.test.ts create mode 100644 web/test/link-order.test.ts create mode 100644 web/test/nav-store-link-edits.test.ts create mode 100644 web/test/public-edit-mode.test.tsx diff --git a/web/src/components/admin/crud-panels.tsx b/web/src/components/admin/crud-panels.tsx index 74cfd72..8730d79 100644 --- a/web/src/components/admin/crud-panels.tsx +++ b/web/src/components/admin/crud-panels.tsx @@ -30,15 +30,13 @@ import { deleteReplacedAsset, useAssetDraftCleanup, } from "@/components/admin/asset-upload"; -import { IconPickerField } from "@/components/admin/icon-picker"; +import { LinkEditDialog } from "@/components/admin/link-edit-dialog"; import { SiteIcon } from "@/components/site-icon"; import { adminCreateCategory, - adminCreateLink, adminDeleteCategory, adminDeleteLink, adminUpdateCategory, - adminUpdateLink, } from "@/lib/api"; import { useNavStore } from "@/lib/nav-store"; import type { AdminCategory, AdminLink, Visibility } from "@/lib/types"; @@ -268,107 +266,31 @@ export function LinkPanel({ ).sort((a, b) => a.sortOrder - b.sortOrder); const [editingId, setEditingId] = React.useState(null); const [creating, setCreating] = React.useState(false); - const [form, setForm] = React.useState({ - categoryId: "", - title: "", - url: "", - iconUrl: "", - description: "", - visibility: "public" as Visibility, - }); - const draft = useAssetDraftCleanup(); const openCreate = () => { if (!category) { toast.error("请先选择一个分类"); return; } - setForm({ - categoryId: category.id, - title: "", - url: "", - iconUrl: "", - description: "", - visibility: "public", - }); setEditingId(null); setCreating(true); }; const openEdit = (id: string) => { // 「所有」视图下 categoryLinks 就是当前场景的全部链接,因此编辑照样可用, - // 且预选值取链接实际所属的分类(不是筛选器当前选中的分类)。 - const l = categoryLinks.find((x) => x.id === id); - if (!l) return; - setForm({ - categoryId: l.categoryId, - title: l.title, - url: l.url, - iconUrl: l.iconUrl ?? "", - description: l.description ?? "", - visibility: l.visibility, - }); + // 预选值由弹窗自己按链接实际所属分类填写。 setEditingId(id); setCreating(false); }; - const close = () => { - void draft.discard(); + const closeDialog = () => { setCreating(false); setEditingId(null); }; - const save = async () => { - // 所属分类是必填项(「所有」视图下 openCreate 已拦一次,这里再兜一道) - if (!form.categoryId) { - toast.error("请先选择一个分类"); - return; - } - const previousIcon = links.find((l) => l.id === editingId)?.iconUrl ?? null; - // 分类变了 = 把链接搬到目标分类末尾(服务端在 categoryId 变更且未给 sortOrder - // 时置全局 MAX+1,沿 #17 分类搬家口径);未变时服务端不扰动顺序。 - const moved = - !!editingId && - links.find((l) => l.id === editingId)?.categoryId !== form.categoryId; - const targetCategory = categories.find((c) => c.id === form.categoryId); - const payload = { - categoryId: form.categoryId, - title: form.title, - url: form.url, - iconUrl: form.iconUrl || null, - description: form.description || null, - visibility: form.visibility, - }; - try { - if (editingId) { - if (moved) { - // 换分类要把「最后一次写」留给这次 PUT:先等在飞的 /links/reorder 落库, - // 同时作废本地顺序簿记(旧顺序若在 PUT 之后落库,会把这个链接改回旧槽位, - // 成功后还会把旧顺序写回本地、让界面继续按旧槽位排)。 - await useNavStore.getState().settleLinkOrder(); - } - await adminUpdateLink(editingId, payload); - toast.success( - moved && targetCategory - ? `链接已移到「${targetCategory.name}」末尾` - : "链接已更新", - ); - } else { - await adminCreateLink(payload); - toast.success("链接已创建"); - } - } catch (error) { - // 与分类/场景面板同口径:保存失败弹错误原文,弹窗保持打开 - toast.error(`保存失败:${(error as Error).message}`); - return; - } - draft.commit(); - // 图标清旧由前端负责(后端只对 settings 的 background/logo 自动清旧) - await deleteReplacedAsset(previousIcon, payload.iconUrl); - setCreating(false); - setEditingId(null); - onChanged(); - }; + const dialogLink = editingId + ? (links.find((l) => l.id === editingId) ?? null) + : null; const remove = async (id: string) => { await adminDeleteLink(id); @@ -456,91 +378,17 @@ export function LinkPanel({