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
73 changes: 66 additions & 7 deletions web/src/components/admin/admin-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ArrowDownUp,
ArrowLeft,
FolderTree,
Layers,
Link as LinkIcon,
LogOut,
Search,
Expand All @@ -11,6 +12,7 @@ import {
} from "lucide-react";
import * as React from "react";
import { CategoryPanel, LinkPanel } from "@/components/admin/crud-panels";
import { ScenePanel } from "@/components/admin/scene-panel";
import { SecurityPanel } from "@/components/admin/security-panel";
import {
ImportExportPanel,
Expand All @@ -19,11 +21,18 @@ import {
} from "@/components/admin/settings-panels";
import { SystemPanel } from "@/components/admin/system-panel";
import { FadeIn, GlassCard } from "@/components/glass";
import { SceneSwitcher } from "@/components/scene-switcher";
import { ThemeToggle } from "@/components/theme-toggle";
import { Button } from "@/components/ui/button";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { WallpaperBackground } from "@/components/wallpaper-background";
import { useNavStore } from "@/lib/nav-store";
import {
filterByScene,
resolveAdminScene,
useScenePreferenceStore,
} from "@/lib/scene-preference";
import type { AdminScene } from "@/lib/types";

/**
* 后台(/#admin):与公共页同一套设计语言 —— 壁纸背景 + 玻璃顶栏 + 毛玻璃左栏
Expand All @@ -42,6 +51,7 @@ import { useNavStore } from "@/lib/nav-store";
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 },
{ value: "io", label: "导入导出", icon: ArrowDownUp },
Expand Down Expand Up @@ -80,6 +90,30 @@ function PanelCard({ children }: { children: React.ReactNode }) {
);
}

/**
* 后台场景胶囊:「分类」「链接」两分区内容卡顶部,只有场景、没有「全部」
* (后台永远停在具体场景:新建分类/链接要带 sceneId)。与前台共用 `nav.scene`。
* 没有场景(旧后端 / 无场景概念)时不渲染。
*/
function AdminSceneBar({
scenes,
sceneId,
}: {
scenes: AdminScene[];
sceneId: string | null;
}) {
const setScenePreference = useScenePreferenceStore((s) => s.setRaw);
if (!sceneId || scenes.length === 0) return null;
return (
<SceneSwitcher
scenes={scenes}
value={sceneId}
onValueChange={setScenePreference}
ariaLabel="选择场景"
/>
);
}

export function AdminPage({ onBack }: { onBack: () => void }) {
const adminData = useNavStore((s) => s.adminData);
const loadAdmin = useNavStore((s) => s.loadAdmin);
Expand All @@ -92,9 +126,18 @@ export function AdminPage({ onBack }: { onBack: () => void }) {

const categories = adminData?.categories ?? [];
const links = adminData?.links ?? [];
const scenes = adminData?.scenes ?? [];
const engines = adminData?.searchEngines ?? [];
const settings = adminData?.settings ?? null;

// 场景偏好与前台共用同一个键;后台回落默认场景(不回写 localStorage)
const scenePreference = useScenePreferenceStore((s) => s.raw);
const activeScene = resolveAdminScene(scenePreference, scenes);
// 分类/链接面板只看当前场景;无场景概念时保持现状(全量)
const sceneCategories = activeScene
? filterByScene(categories, activeScene)
: categories;

return (
<div className="relative min-h-screen">
{/* 背景层(与公共页共用同一份实现:壁纸来源解析 + 罩层) */}
Expand Down Expand Up @@ -187,18 +230,34 @@ export function AdminPage({ onBack }: { onBack: () => void }) {
<div className="min-w-0 flex-1">
<TabsContent value="categories">
<PanelCard>
<CategoryPanel
categories={categories}
onChanged={() => void loadAdmin()}
/>
<div className="flex flex-col gap-5">
<AdminSceneBar scenes={scenes} sceneId={activeScene} />
<CategoryPanel
categories={sceneCategories}
sceneId={activeScene}
onChanged={() => void loadAdmin()}
/>
</div>
</PanelCard>
</TabsContent>

<TabsContent value="links">
<PanelCard>
<LinkPanel
categories={categories}
links={links}
<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
scenes={scenes}
onChanged={() => void loadAdmin()}
/>
</PanelCard>
Expand Down
69 changes: 52 additions & 17 deletions web/src/components/admin/crud-panels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ import {
import { useNavStore } from "@/lib/nav-store";
import type { AdminCategory, AdminLink, Visibility } from "@/lib/types";

/** 分类 CRUD + dnd-kit 拖拽排序 */
/** 分类 CRUD + dnd-kit 拖拽排序(分类列表由调用方按当前场景过滤后传入) */
export function CategoryPanel({
categories,
sceneId,
onChanged,
}: {
/** 当前场景的分类(已按 sceneId 过滤;无场景概念时为全量) */
categories: AdminCategory[];
/** 新建分类的归属场景;null/未传 = 无场景概念(旧后端),不提交该字段 */
sceneId?: string | null;
onChanged: () => void;
}) {
const [editing, setEditing] = React.useState<AdminCategory | null>(null);
Expand Down Expand Up @@ -98,12 +102,29 @@ export function CategoryPanel({
}
const previousIcon = editing?.icon ?? null;
const payload = { name: nextName, icon: icon || null, visibility };
if (editing) {
await adminUpdateCategory(editing.id, payload);
toast.success("分类已更新");
} else {
await adminCreateCategory(payload);
toast.success("分类已创建");
try {
if (editing) {
await adminUpdateCategory(editing.id, payload);
toast.success("分类已更新");
} else {
// 分类的 sort_order 是场景内排序:新建必须显式给「当前场景末尾」。
// 后端省略 sortOrder 会兜底 0(新分类插到场景首项之后,连建多个全堆在 0),
// 这里用面板拿到的(已按场景过滤的)分类取最大序号 + 1。
const nextSortOrder =
categories.reduce((max, c) => Math.max(max, c.sortOrder), -1) + 1;
// 新建分类默认归当前场景(契约:场景内排序从此刻开始)
await adminCreateCategory({
...payload,
sortOrder: nextSortOrder,
...(sceneId ? { sceneId } : {}),
});
toast.success("分类已创建");
}
} catch (error) {
// 例:另一个标签页刚删掉当前场景 → 后端 400「sceneId does not exist」。
// 失败时弹窗保持打开、错误原文透出,不能静默无反应。
toast.error(`保存失败:${(error as Error).message}`);
return;
}
draft.commit();
// 图标清旧由前端负责(后端只对 settings 的 background/logo 自动清旧)
Expand All @@ -121,6 +142,7 @@ export function CategoryPanel({

const reorder = async (ids: string[]) => {
// 乐观写入在 store 内同步完成(界面立刻定住),成功后不再整表重拉(见 nav-store 排序簿记)。
// 分类的 sort_order 是场景内排序:这里提交的就是当前场景的分类全集。
// 失败时 store 已回滚顺序并按服务端刷新,这里只负责提示。
try {
await useNavStore.getState().reorderCategories(ids);
Expand Down Expand Up @@ -206,21 +228,28 @@ export function CategoryPanel({
/** 「所有」筛选项的哨兵值:Radix Select 不允许 SelectItem 用空串(空串代表未选) */
const ALL_CATEGORIES = "__all__";

/** 链接 CRUD + 分类内排序(后台 links 为扁平列表,按 categoryId 过滤) */
/** 链接 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);
// 「所有」:不过滤分类,直接按全局 sortOrder 展示全部链接
// 链接没有场景字段:其场景由所属分类唯一决定,这里经分类 id 过滤(保持全局 sortOrder)
const sceneCategoryIds = new Set(categories.map((c) => c.id));
const sceneLinks = links.filter((l) => sceneCategoryIds.has(l.categoryId));
// 「所有」:不过滤分类,直接按全局 sortOrder 展示当前场景全部链接
const categoryLinks = (
category ? links.filter((l) => l.categoryId === category.id) : [...links]
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);
Expand Down Expand Up @@ -278,13 +307,19 @@ export function LinkPanel({
description: form.description || null,
visibility: form.visibility,
};
if (editingId) {
await adminUpdateLink(editingId, payload);
toast.success("链接已更新");
} else {
if (!category) return;
await adminCreateLink({ ...payload, categoryId: category.id });
toast.success("链接已创建");
try {
if (editingId) {
await adminUpdateLink(editingId, payload);
toast.success("链接已更新");
} else {
if (!category) return;
await adminCreateLink({ ...payload, categoryId: category.id });
toast.success("链接已创建");
}
} catch (error) {
// 与分类/场景面板同口径:保存失败弹错误原文,弹窗保持打开
toast.error(`保存失败:${(error as Error).message}`);
return;
}
draft.commit();
// 图标清旧由前端负责(后端只对 settings 的 background/logo 自动清旧)
Expand Down
Loading
Loading