Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions web/src/components/admin/admin-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
FolderTree,
Info,
Layers,
Link as LinkIcon,
LogOut,
Search,
Settings as SettingsIcon,
Expand All @@ -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 {
Expand Down Expand Up @@ -41,8 +40,9 @@ import type { AdminScene } from "@/lib/types";
* + 玻璃内容卡。分区菜单用 Radix Tabs 单一根:宽屏是左栏竖排,窄屏变内容区顶部的
* 横向滚动菜单条(同一个 TabsList,只换 orientation 与响应式类,不做抽屉)。
*
* 分区内容(分类 / 链接 / 场景管理 / 搜索引擎 / 站点设置 / 导入导出 /
* 分区内容(分类 / 场景管理 / 搜索引擎 / 站点设置 / 导入导出 /
* 安全·两步验证 / 系统管理 / 关于)的容器换成了玻璃卡,面板组件本身的表单与业务逻辑不动。
* 链接不在这里管:首页编辑模式(`home-edit-board.tsx`)已能增删改与拖动排序。
*
* 后台数据由 store(useNavStore.adminData)承载,本页只负责加载与分发;
* 安全区的数据(2FA 状态、Passkey 凭据)属会话期敏感数据,由 SecurityPanel 自行拉取,
Expand All @@ -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 },
Expand Down Expand Up @@ -94,8 +93,8 @@ function PanelCard({ children }: { children: React.ReactNode }) {
}

/**
* 后台场景胶囊:「分类」「链接」两分区内容卡顶部,只有场景、没有「全部」
* (后台永远停在具体场景:新建分类/链接要带 sceneId)。与前台共用 `nav.scene`。
* 后台场景胶囊:「分类」分区内容卡顶部,只有场景、没有「全部」
* (后台永远停在具体场景:新建分类要带 sceneId)。与前台共用 `nav.scene`。
* 没有场景(旧后端 / 无场景概念)时不渲染。
*/
function AdminSceneBar({
Expand Down Expand Up @@ -248,19 +247,6 @@ export function AdminPage({ onBack }: { onBack: () => void }) {
</PanelCard>
</TabsContent>

<TabsContent value="links">
<PanelCard>
<div className="flex flex-col gap-5">
<AdminSceneBar scenes={scenes} sceneId={activeScene} />
<LinkPanel
categories={sceneCategories}
links={links}
onChanged={() => void loadAdmin()}
/>
</div>
</PanelCard>
</TabsContent>

<TabsContent value="scenes">
<PanelCard>
<ScenePanel
Expand Down
167 changes: 1 addition & 166 deletions web/src/components/admin/crud-panels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ import {
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Switch } from "@/components/ui/switch";
import { Badge } from "@/components/ui/badge";
import {
Expand All @@ -30,16 +23,13 @@ import {
deleteReplacedAsset,
useAssetDraftCleanup,
} from "@/components/admin/asset-upload";
import { LinkEditDialog } from "@/components/admin/link-edit-dialog";
import { SiteIcon } from "@/components/site-icon";
import {
adminCreateCategory,
adminDeleteCategory,
adminDeleteLink,
adminUpdateCategory,
} from "@/lib/api";
import { useNavStore } from "@/lib/nav-store";
import type { AdminCategory, AdminLink, Visibility } from "@/lib/types";
import type { AdminCategory, Visibility } from "@/lib/types";

/**
* 分类 CRUD + dnd-kit 拖拽排序(分类列表由调用方按当前场景过滤后传入)。
Expand Down Expand Up @@ -237,158 +227,3 @@ export function CategoryPanel({
</div>
);
}

/** 「所有」筛选项的哨兵值: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<string>(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<string | null>(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 (
<div className="flex flex-col gap-4">
<div className="flex items-center justify-between gap-2">
<h2 className="font-heading text-lg font-semibold">链接</h2>
<div className="flex items-center gap-2">
<Select
value={category ? categoryId : ALL_CATEGORIES}
onValueChange={setCategoryId}
>
<SelectTrigger className="w-40" aria-label="选择分类">
<SelectValue placeholder="选择分类" />
</SelectTrigger>
<SelectContent>
<SelectItem value={ALL_CATEGORIES}>所有</SelectItem>
{categories.map((c) => (
<SelectItem key={c.id} value={c.id}>
{c.name}
</SelectItem>
))}
</SelectContent>
</Select>
<Button onClick={openCreate}>新建链接</Button>
</div>
</div>

{categories.length > 0 ? (
<SortableList
entries={categoryLinks.map((l) => ({
id: l.id,
label: l.title,
meta: (
<span className="flex items-center gap-2">
<SiteIcon
title={l.title}
url={l.url}
iconUrl={l.iconUrl}
size={20}
/>
<Badge
variant={l.visibility === "private" ? "default" : "secondary"}
>
{l.visibility === "private" ? "私有" : "公开"}
</Badge>
</span>
),
}))}
onReorder={reorder}
onEdit={openEdit}
onRemove={remove}
confirmRemove
removeDescription="删除链接后无法恢复,请确认。"
/>
) : (
<p className="text-sm text-muted-foreground">
暂无分类,请先创建分类。
</p>
)}

<LinkEditDialog
open={creating || !!dialogLink}
link={dialogLink}
categories={categories}
defaultCategoryId={category?.id}
onClose={closeDialog}
onSaved={() => {
closeDialog();
onChanged();
}}
/>
</div>
);
}
15 changes: 10 additions & 5 deletions web/src/components/admin/link-edit-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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,
}: {
Expand All @@ -51,10 +54,12 @@ export function LinkEditDialog({
link: AdminLink | null;
/** 当前场景的分类(分类下拉只列这些) */
categories: AdminCategory[];
/** 新建时预选的分类(后台必须先在筛选器里选一个) */
/** 新建时预选的分类(首页编辑模式选中那块「+」砖所属的分类) */
defaultCategoryId?: string;
/** 新建提交:落库后回传完整 DTO(排序号由调用方定,POST 只回 {id}) */
onCreateLink: (input: CreateLinkInput) => Promise<AdminLink>;
onClose: () => void;
/** 保存成功:回传服务端返回的最新 DTO,由调用方决定就地替换还是整表刷新 */
/** 保存成功:回传服务端的最新 DTO,由调用方就地并入 store */
onSaved: (link: AdminLink) => void;
}) {
const [form, setForm] = React.useState({
Expand Down Expand Up @@ -136,7 +141,7 @@ export function LinkEditDialog({
: "链接已更新",
);
} else {
saved = await adminCreateLink(payload);
saved = await onCreateLink(payload);
toast.success("链接已创建");
}
draft.commit();
Expand Down
Loading
Loading