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
53 changes: 34 additions & 19 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@ import * as React from "react";
import { Toaster } from "@/components/ui/sonner";
import { AdminPage } from "@/components/admin/admin-page";
import { PublicPage } from "@/components/public-page";
import { SetupPage } from "@/components/setup/setup-page";
import { useNavStore } from "@/lib/nav-store";

/**
* 应用路由:/ 公开页,登录以 /api/private 401 触发的弹窗承载(不整页跳转),
* #admin 后台(登录后可用)。
* /setup 数据库初始化/升级页,/#admin 后台(登录后可用)。
*
* pathname 优先(/setup 是独立页面,后端 SPA 回退已落到 index.html),
* 其余保持原有的 #admin 哈希路由。
*/
type Route = "public" | "admin" | "setup";

function readRoute(): Route {
if (window.location.pathname.replace(/\/+$/, "") === "/setup") return "setup";
return window.location.hash === "#admin" ? "admin" : "public";
}

export default function App() {
const [route, setRoute] = React.useState<"public" | "admin">("public");
const [route, setRoute] = React.useState<Route>(() => readRoute());
const { authenticated, loginPromptOpen, setLoginPrompt } = useNavStore();

React.useEffect(() => {
const onPop = () =>
setRoute(window.location.hash === "#admin" ? "admin" : "public");
window.addEventListener("hashchange", onPop);
onPop();
return () => window.removeEventListener("hashchange", onPop);
const sync = () => setRoute(readRoute());
window.addEventListener("hashchange", sync);
window.addEventListener("popstate", sync);
sync();
return () => {
window.removeEventListener("hashchange", sync);
window.removeEventListener("popstate", sync);
};
}, []);

// 站内跳转(不整页刷新):写历史再同步路由状态
const navigate = React.useCallback((path: string) => {
window.history.pushState({}, "", path);
setRoute(readRoute());
}, []);

// 未登录访问 #admin → 弹登录
Expand All @@ -29,20 +49,15 @@ export default function App() {

return (
<>
{route === "admin" && authenticated ? (
<AdminPage
onBack={() => {
window.location.hash = "";
setRoute("public");
}}
{route === "setup" ? (
<SetupPage
onGoHome={() => navigate("/")}
onGoAdmin={() => navigate("/#admin")}
/>
) : route === "admin" && authenticated ? (
<AdminPage onBack={() => navigate("/")} />
) : (
<PublicPage
onOpenAdmin={() => {
window.location.hash = "#admin";
setRoute("admin");
}}
/>
<PublicPage onOpenAdmin={() => navigate("/#admin")} />
)}
<Toaster />
</>
Expand Down
8 changes: 7 additions & 1 deletion web/src/components/admin/admin-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import {
SearchEnginePanel,
SettingsPanel,
} from "@/components/admin/settings-panels";
import { SystemPanel } from "@/components/admin/system-panel";
import { useNavStore } from "@/lib/nav-store";

/**
* 后台(/admin):分类 / 链接 / 搜索引擎 / 站点设置 CRUD + 导入导出,
* 后台(/admin):分类 / 链接 / 搜索引擎 / 站点设置 CRUD + 导入导出 + 系统管理,
* 列表用 dnd-kit 拖拽排序(含上移/下移键盘替代)。
* 后台数据由 store(useNavStore.adminData)承载,本页只负责加载与分发。
*/
Expand Down Expand Up @@ -58,6 +59,7 @@ export function AdminPage({ onBack }: { onBack: () => void }) {
<TabsTrigger value="engines">搜索引擎</TabsTrigger>
<TabsTrigger value="settings">站点设置</TabsTrigger>
<TabsTrigger value="io">导入导出</TabsTrigger>
<TabsTrigger value="system">系统管理</TabsTrigger>
</TabsList>

<TabsContent value="categories" className="mt-6 max-w-2xl">
Expand Down Expand Up @@ -98,6 +100,10 @@ export function AdminPage({ onBack }: { onBack: () => void }) {
onChanged={() => void loadAdmin()}
/>
</TabsContent>

<TabsContent value="system" className="mt-6 max-w-2xl">
<SystemPanel />
</TabsContent>
</Tabs>
) : (
<p className="text-sm text-muted-foreground">加载中…</p>
Expand Down
245 changes: 245 additions & 0 deletions web/src/components/admin/system-panel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
import * as React from "react";
import { Database, ExternalLink, Loader2 } from "lucide-react";
import { toast } from "sonner";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Label } from "@/components/ui/label";
import { Separator } from "@/components/ui/separator";
import { Switch } from "@/components/ui/switch";
import {
ApiError,
adminGetSystem,
adminRunSetup,
adminSetSetupPage,
} from "@/lib/api";
import { MigrationResults } from "@/components/setup/migration-results";
import type { AdminSystem, SetupResult } from "@/lib/types";

/**
* 后台「系统管理」选项卡(SETUP-PAGE-SPEC §5):
* - 数据库维护:生效 tablePrefix + 账本已记录迁移;「检查并升级数据库」走
* POST /api/admin/setup(免密钥,会话鉴权),逐条展示 applied/skipped;
* - setup 页面:Switch 绑定 setupPageEnabled,PUT 后回读确认;旁注说明开关只
* 门控页面、不门控密钥接口。
*/
export function SystemPanel() {
const [system, setSystem] = React.useState<AdminSystem | null>(null);
const [loadError, setLoadError] = React.useState<string | null>(null);
const [upgrading, setUpgrading] = React.useState(false);
const [saving, setSaving] = React.useState(false);
const [result, setResult] = React.useState<SetupResult | null>(null);
const [runError, setRunError] = React.useState<string | null>(null);

const load = React.useCallback(async () => {
try {
setSystem(await adminGetSystem());
setLoadError(null);
} catch (e) {
setLoadError(
e instanceof ApiError && e.status === 401
? "会话已失效,请重新登录。"
: "读取系统信息失败,请稍后重试。",
);
}
}, []);

React.useEffect(() => {
void load();
}, [load]);

const upgrade = async () => {
setUpgrading(true);
setResult(null);
setRunError(null);
try {
const res = await adminRunSetup();
setResult(res);
const applied = res.migrations.filter(
(m) => m.status === "applied",
).length;
const skipped = res.migrations.filter(
(m) => m.status === "skipped",
).length;
toast.success(
`数据库检查完成:${applied} 个迁移已应用,${skipped} 个已是最新`,
);
await load(); // 回读账本,账本状态以库为准
} catch (e) {
if (e instanceof ApiError && e.status === 409) {
// 前缀冲突:后端 detail 原文直接显示在区内,不吞错误
const detail = e.detail ?? e.message;
setRunError(detail);
toast.error(`表前缀冲突:${detail}`);
} else {
const message = "升级失败,请稍后重试。";
setRunError(message);
toast.error(message);
}
} finally {
setUpgrading(false);
}
};

const toggleSetupPage = async (enabled: boolean) => {
setSaving(true);
try {
const saved = await adminSetSetupPage(enabled);
toast.success(
saved.setupPageEnabled ? "已开启 setup 页面" : "已关闭 setup 页面",
);
} catch {
toast.error("保存失败,请稍后重试");
} finally {
// 无论成败都回读确认,开关状态以服务端为准
await load();
setSaving(false);
}
};

return (
<div className="flex flex-col gap-6">
<h2 className="font-heading text-lg font-semibold">系统管理</h2>

<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Database className="size-4" aria-hidden="true" />
数据库维护
</CardTitle>
<CardDescription>
只做前进式账本迁移:不建号、不改密码、不删任何表。
</CardDescription>
</CardHeader>
<CardContent className="grid gap-4">
{loadError ? (
<Alert variant="destructive">
<AlertTitle>读取失败</AlertTitle>
<AlertDescription>{loadError}</AlertDescription>
</Alert>
) : !system ? (
<p className="flex items-center gap-2 text-sm text-muted-foreground">
<Loader2 className="size-4 animate-spin" aria-hidden="true" />
正在读取系统信息…
</p>
) : (
<>
<div className="flex items-center gap-2 text-sm">
<span className="text-muted-foreground">生效表前缀</span>
<code className="rounded bg-muted px-1.5 py-0.5 font-mono text-xs">
{system.tablePrefix || "(无前缀)"}
</code>
</div>

<div className="grid gap-2">
<span className="text-sm text-muted-foreground">
账本已记录迁移
</span>
{system.ledger.length === 0 ? (
<p className="text-sm text-muted-foreground">
账本中暂无迁移记录。
</p>
) : (
<div className="flex flex-wrap gap-1.5">
{system.ledger.map((name) => (
<Badge
key={name}
variant="secondary"
className="font-mono"
>
{name}
</Badge>
))}
</div>
)}
</div>

<Separator />

<div className="flex flex-wrap items-center gap-2">
<Button onClick={() => void upgrade()} disabled={upgrading}>
{upgrading ? (
<Loader2
className="size-4 animate-spin"
aria-hidden="true"
/>
) : (
<Database className="size-4" aria-hidden="true" />
)}
{upgrading ? "正在检查并升级…" : "检查并升级数据库"}
</Button>
<span className="text-sm text-muted-foreground">
免密钥,使用当前登录会话执行。
</span>
</div>

{runError && (
<Alert variant="destructive">
<AlertTitle>升级失败</AlertTitle>
<AlertDescription className="break-words">
{runError}
</AlertDescription>
</Alert>
)}

{result && (
<div className="grid gap-2">
<span className="text-sm text-muted-foreground">
本次执行结果
</span>
<MigrationResults
tablePrefix={result.tablePrefix}
migrations={result.migrations}
/>
<p className="text-sm text-muted-foreground">
未创建或修改任何账号。
</p>
</div>
)}
</>
)}
</CardContent>
</Card>

<Card>
<CardHeader>
<CardTitle>setup 页面</CardTitle>
<CardDescription>
控制 /setup 页面是否对外开放(单人站点,初始化完成后默认关闭)。
</CardDescription>
</CardHeader>
<CardContent className="grid gap-4">
<div className="flex items-center justify-between gap-4">
<div className="grid gap-1">
<Label htmlFor="system-setup-page">允许访问 /setup 页面</Label>
<p className="text-sm text-muted-foreground">
关闭后 /setup
只显示已关闭提示;管理面升级按钮与密钥接口不受此开关影响。
</p>
</div>
<Switch
id="system-setup-page"
checked={system?.setupPageEnabled ?? false}
disabled={!system || saving}
onCheckedChange={(checked) => void toggleSetupPage(checked)}
/>
</div>

<Button variant="outline" size="sm" className="w-fit" asChild>
<a href="/setup" target="_blank" rel="noreferrer">
打开 setup 页面
<ExternalLink className="size-4" aria-hidden="true" />
</a>
</Button>
</CardContent>
</Card>
</div>
);
}
Loading
Loading