diff --git a/web/src/components/admin/admin-page.tsx b/web/src/components/admin/admin-page.tsx index a55371d..895917a 100644 --- a/web/src/components/admin/admin-page.tsx +++ b/web/src/components/admin/admin-page.tsx @@ -4,7 +4,6 @@ import { FolderTree, Info, Layers, - Link as LinkIcon, LogOut, Search, Settings as SettingsIcon, @@ -13,7 +12,7 @@ import { } from "lucide-react"; import * as React from "react"; import { AboutPanel } from "@/components/admin/about-panel"; -import { CategoryPanel, LinkPanel } from "@/components/admin/crud-panels"; +import { CategoryPanel } from "@/components/admin/crud-panels"; import { ScenePanel } from "@/components/admin/scene-panel"; import { SecurityPanel } from "@/components/admin/security-panel"; import { @@ -41,8 +40,9 @@ import type { AdminScene } from "@/lib/types"; * + 玻璃内容卡。分区菜单用 Radix Tabs 单一根:宽屏是左栏竖排,窄屏变内容区顶部的 * 横向滚动菜单条(同一个 TabsList,只换 orientation 与响应式类,不做抽屉)。 * - * 分区内容(分类 / 链接 / 场景管理 / 搜索引擎 / 站点设置 / 导入导出 / + * 分区内容(分类 / 场景管理 / 搜索引擎 / 站点设置 / 导入导出 / * 安全·两步验证 / 系统管理 / 关于)的容器换成了玻璃卡,面板组件本身的表单与业务逻辑不动。 + * 链接不在这里管:首页编辑模式(`home-edit-board.tsx`)已能增删改与拖动排序。 * * 后台数据由 store(useNavStore.adminData)承载,本页只负责加载与分发; * 安全区的数据(2FA 状态、Passkey 凭据)属会话期敏感数据,由 SecurityPanel 自行拉取, @@ -52,7 +52,6 @@ import type { AdminScene } from "@/lib/types"; /** 左栏菜单项(图标用 Lucide,与前台同一视觉家族)。 */ const ADMIN_TABS = [ { value: "categories", label: "分类", icon: FolderTree }, - { value: "links", label: "链接", icon: LinkIcon }, { value: "scenes", label: "场景管理", icon: Layers }, { value: "engines", label: "搜索引擎", icon: Search }, { value: "settings", label: "站点设置", icon: SettingsIcon }, @@ -94,8 +93,8 @@ function PanelCard({ children }: { children: React.ReactNode }) { } /** - * 后台场景胶囊:「分类」「链接」两分区内容卡顶部,只有场景、没有「全部」 - * (后台永远停在具体场景:新建分类/链接要带 sceneId)。与前台共用 `nav.scene`。 + * 后台场景胶囊:「分类」分区内容卡顶部,只有场景、没有「全部」 + * (后台永远停在具体场景:新建分类要带 sceneId)。与前台共用 `nav.scene`。 * 没有场景(旧后端 / 无场景概念)时不渲染。 */ function AdminSceneBar({ @@ -248,19 +247,6 @@ export function AdminPage({ onBack }: { onBack: () => void }) { - - -
- - void loadAdmin()} - /> -
-
-
- ); } - -/** 「所有」筛选项的哨兵值:Radix Select 不允许 SelectItem 用空串(空串代表未选) */ -const ALL_CATEGORIES = "__all__"; - -/** 链接 CRUD + 分类内排序(后台 links 为扁平列表;按当前场景的分类 id 过滤) */ -export function LinkPanel({ - categories, - links, - onChanged, -}: { - /** 当前场景的分类(已过滤):分类选择器只列这些,新建链接只归当前场景 */ - categories: AdminCategory[]; - /** 全量链接:展示按当前场景过滤,排序重建仍用全局 ids(链接 sort_order 语义不变) */ - links: AdminLink[]; - onChanged: () => void; -}) { - const [categoryId, setCategoryId] = React.useState(ALL_CATEGORIES); - const category = categories.find((c) => c.id === categoryId); - // 链接没有场景字段:其场景由所属分类唯一决定,这里经分类 id 过滤(保持全局 sortOrder) - const sceneCategoryIds = new Set(categories.map((c) => c.id)); - const sceneLinks = links.filter((l) => sceneCategoryIds.has(l.categoryId)); - // 「所有」:不过滤分类,直接按全局 sortOrder 展示当前场景全部链接 - const categoryLinks = ( - category - ? sceneLinks.filter((l) => l.categoryId === category.id) - : sceneLinks - ).sort((a, b) => a.sortOrder - b.sortOrder); - const [editingId, setEditingId] = React.useState(null); - const [creating, setCreating] = React.useState(false); - - const openCreate = () => { - if (!category) { - toast.error("请先选择一个分类"); - return; - } - setEditingId(null); - setCreating(true); - }; - - const openEdit = (id: string) => { - // 「所有」视图下 categoryLinks 就是当前场景的全部链接,因此编辑照样可用, - // 预选值由弹窗自己按链接实际所属分类填写。 - setEditingId(id); - setCreating(false); - }; - - const closeDialog = () => { - setCreating(false); - setEditingId(null); - }; - - const dialogLink = editingId - ? (links.find((l) => l.id === editingId) ?? null) - : null; - - const remove = async (id: string) => { - await adminDeleteLink(id); - toast.success("链接已删除"); - onChanged(); - }; - - const reorder = async (orderedIds: string[]) => { - // 契约 reorder 为全局 ids 列表(sort_order 全局);此处重建全量顺序, - // 仅替换当前分类链接的相对位置,其余分类链接保持原有相对顺序。 - const selectedSet = new Set(orderedIds); - const sorted = [...links].sort((a, b) => a.sortOrder - b.sortOrder); - const firstIdx = sorted.findIndex((l) => selectedSet.has(l.id)); - const before: string[] = []; - const after: string[] = []; - sorted.forEach((l, i) => { - if (selectedSet.has(l.id)) return; - if (i < firstIdx) before.push(l.id); - else after.push(l.id); - }); - try { - await useNavStore - .getState() - .reorderLinks([...before, ...orderedIds, ...after]); - } catch { - toast.error("排序失败,已恢复原顺序"); - } - }; - - return ( -
-
-

链接

-
- - -
-
- - {categories.length > 0 ? ( - ({ - id: l.id, - label: l.title, - meta: ( - - - - {l.visibility === "private" ? "私有" : "公开"} - - - ), - }))} - onReorder={reorder} - onEdit={openEdit} - onRemove={remove} - confirmRemove - removeDescription="删除链接后无法恢复,请确认。" - /> - ) : ( -

- 暂无分类,请先创建分类。 -

- )} - - { - closeDialog(); - onChanged(); - }} - /> -
- ); -} diff --git a/web/src/components/admin/link-edit-dialog.tsx b/web/src/components/admin/link-edit-dialog.tsx index eb03b0d..b67f255 100644 --- a/web/src/components/admin/link-edit-dialog.tsx +++ b/web/src/components/admin/link-edit-dialog.tsx @@ -23,7 +23,7 @@ import { SelectValue, } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; -import { adminCreateLink, adminUpdateLink } from "@/lib/api"; +import { adminUpdateLink, type CreateLinkInput } from "@/lib/api"; import { useNavStore } from "@/lib/nav-store"; import type { AdminCategory, AdminLink, Visibility } from "@/lib/types"; @@ -36,13 +36,16 @@ import type { AdminCategory, AdminLink, Visibility } from "@/lib/types"; * 置全局 MAX+1);所以换分类前先 `settleLinkOrder()`,保证这次 PUT 是最后一次写; * - 保存失败弹错误原文、弹窗保持打开;成功清理被替换的 R2 图标。 * - * `link === null` 表示新建(后台用);首页编辑模式只用编辑态。 + * `link === null` 是新建(首页编辑模式分类末尾的「+」砖)。新建交给调用方的 + * `onCreateLink`:置位/排序与读回完整 DTO 是调用方的事(契约 POST 只回 `{id}`); + * 两条路径成功后都走 `onSaved`,并入 store 只有一条路径。 */ export function LinkEditDialog({ open, link, categories, defaultCategoryId, + onCreateLink, onClose, onSaved, }: { @@ -51,10 +54,12 @@ export function LinkEditDialog({ link: AdminLink | null; /** 当前场景的分类(分类下拉只列这些) */ categories: AdminCategory[]; - /** 新建时预选的分类(后台必须先在筛选器里选一个) */ + /** 新建时预选的分类(首页编辑模式选中那块「+」砖所属的分类) */ defaultCategoryId?: string; + /** 新建提交:落库后回传完整 DTO(排序号由调用方定,POST 只回 {id}) */ + onCreateLink: (input: CreateLinkInput) => Promise; onClose: () => void; - /** 保存成功:回传服务端返回的最新 DTO,由调用方决定就地替换还是整表刷新 */ + /** 保存成功:回传服务端的最新 DTO,由调用方就地并入 store */ onSaved: (link: AdminLink) => void; }) { const [form, setForm] = React.useState({ @@ -136,7 +141,7 @@ export function LinkEditDialog({ : "链接已更新", ); } else { - saved = await adminCreateLink(payload); + saved = await onCreateLink(payload); toast.success("链接已创建"); } draft.commit(); diff --git a/web/src/components/home-edit-board.tsx b/web/src/components/home-edit-board.tsx index f34e9e5..ac1bcda 100644 --- a/web/src/components/home-edit-board.tsx +++ b/web/src/components/home-edit-board.tsx @@ -21,7 +21,7 @@ import { useSortable, } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; -import { X } from "lucide-react"; +import { Plus, X } from "lucide-react"; import { FadeIn } from "@/components/glass"; import { CategoryHeading, @@ -39,7 +39,11 @@ import type { LinkStyle, SiteCategory, SiteLink } from "@/lib/types"; * (`rectSortingStrategy`,砖是自动换行的网格)。投放目标三种: * - 某块砖:落到它的下标位置(同分类换位、跨分类插入都走这条); * - 分类标题行(`end:`):落到该分类**末尾**; - * - 空分类占位(`cat:`):虚线框、无文字、与同档砖同高,落到唯一位置。 + * - 空分类末尾的「+」砖(`cat:`):与同档砖同高,落到唯一位置。 + * + * 每个分类网格末尾恒有一块「+」砖(`AddLinkTile`):点它在该分类新建链接。 + * 分类为空时这块砖就是上面那个落点(把最后一块拖走后还能拖回来);分类非空时 + * 它不注册 droppable,不抢命中。 * * 拖动中的预览顺序放在组件本地草稿里(只是拖拽期间的临时 UI 状态),松手才写 store: * `onDragOver` 做跨容器搬运,`onDragEnd` 再补同容器换位,然后把「目标分类 + 前驱 id」 @@ -59,7 +63,7 @@ import type { LinkStyle, SiteCategory, SiteLink } from "@/lib/types"; /** 分类标题行投放目标(落到该分类末尾)的 id 前缀 */ const END_PREFIX = "end:"; -/** 空分类占位投放目标的 id 前缀 */ +/** 空分类落点(也就是那块「+」砖)投放目标的 id 前缀 */ const CAT_PREFIX = "cat:"; /** @@ -77,9 +81,9 @@ const collisionDetection: CollisionDetection = (args) => { }; /** - * 空分类占位的高度:与同档一块砖一致(虚线框内不写字)——实测值,砖壳撑满格子后 - * 三档分别落在 130 / 36 / 62px(classic 图标 44 + 名称与网址各一行;compact 小图标 - * 单行;wide 图标 36 一行名称 + 一行域名),占位框随之对齐。 + * 空分类落点 / 分类末尾「+」砖的高度:与同档一块砖一致(框内只有一个加号)—— + * 实测值,砖壳撑满格子后三档分别落在 130 / 36 / 62px(classic 图标 44 + 名称与网址 + * 各一行;compact 小图标单行;wide 图标 36 一行名称 + 一行域名),「+」砖随之对齐。 */ const PLACEHOLDER_CLASS: Record = { classic: "min-h-[130px]", @@ -256,27 +260,47 @@ function EndDropTarget({ ); } -/** 空分类的空投放位:与砖同尺寸的虚线框,不写字。 */ -function EmptyDropZone({ - categoryId, +/** 空分类的落点 + 末尾新增入口:与同档砖同尺寸的虚线「+」砖。 */ +function AddLinkTile({ + category, linkStyle, + empty, + onAdd, }: { - categoryId: string; + category: SiteCategory; linkStyle: LinkStyle; + /** 分类里还没有链接:此时它同时是该分类唯一的投放目标 */ + empty: boolean; + onAdd: (categoryId: string) => void; }) { const { setNodeRef, isOver } = useDroppable({ - id: `${CAT_PREFIX}${categoryId}`, + id: `${CAT_PREFIX}${category.id}`, + // 非空分类里不注册落点:否则会抢 `closestCorners` 的命中,把「拖到标题行 + // 落到末尾」变成命中这块砖(标题行只有 ~30px,本来就容易被下方砖抢走) + disabled: !empty, }); return ( -