AstrBot-Desktop/src/pages/SettingsPages.tsx
YokiiroBW e8d9dbc8f6 refactor(ui): share ipc error formatting
Tauri rejects `invoke` with the deserialized Rust error value, which for
`RuntimeCommandError { code, message }` is a plain object rather than an
`Error`. Nine call sites used `reason instanceof Error ? reason.message :
String(reason)`, so every send, reconnect, stop, and settings failure showed
the user `[object Object]`.

`ProfileManager` already had a correct implementation; this lifts it into
`src/lib/errorMessage.ts` and routes the other call sites through it. The
module also exports `isCancelledError`, which a later batch needs to tell a
deliberate stop apart from a real failure.

Refs P1-16 / F-08.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-26 22:31:16 +08:00

278 行
8.4 KiB
TypeScript

import { invoke } from "@tauri-apps/api/core";
import { useState } from "react";
import { errorMessage } from "../lib/errorMessage";
import type { BootstrapStatus } from "../types/bootstrap";
export type SettingsChatSlot = "companion" | "operations";
export function DesktopBehaviorPage({
bubbleTarget,
quickTarget,
onBubbleTargetChange,
onQuickTargetChange,
onOpenQuickChat,
}: {
bubbleTarget: "main" | "quick";
quickTarget: SettingsChatSlot;
onBubbleTargetChange: (value: "main" | "quick") => void;
onQuickTargetChange: (value: SettingsChatSlot) => void;
onOpenQuickChat: () => void;
}) {
const [quietHours, setQuietHours] = useState(
() => localStorage.getItem("astrbot.quietHours") ?? "off",
);
return (
<div className="page-stack">
<section className="settings-grid">
<SettingCard
title="快速聊天"
description="设置气泡或快捷入口打开哪个实例。"
>
<label>
<span>默认目标槽位</span>
<select
value={quickTarget}
onChange={(event) =>
onQuickTargetChange(event.target.value as SettingsChatSlot)
}
>
<option value="companion">陪伴实例</option>
<option value="operations">运维实例</option>
</select>
</label>
<button type="button" onClick={onOpenQuickChat}>
立即打开快速聊天
</button>
</SettingCard>
<SettingCard
title="状态气泡"
description="气泡显示连接状态,并根据这里的设置响应点击。"
>
<label>
<span>点击气泡时</span>
<select
value={bubbleTarget}
onChange={(event) =>
onBubbleTargetChange(event.target.value as "main" | "quick")
}
>
<option value="main">打开主界面</option>
<option value="quick">打开快速聊天</option>
</select>
</label>
<div className="status-preview">
<span className="connection-dot" />
气泡状态显示已预留
</div>
</SettingCard>
</section>
<section className="info-card">
<span className="section-kicker">Desktop companion</span>
<h2>后续桌面能力</h2>
<div className="feature-list">
<span>托盘与单实例</span>
<span>窗口置顶开关</span>
<span>多显示器策略</span>
<span>游戏模式靠边活动</span>
</div>
<p>
这些能力先保留为独立设置区,后续接入 Rust
本机能力层,不会与聊天状态混在一起。
</p>
</section>
<section className="settings-grid">
<SettingCard
title="主动消息静默时段"
description="静默时段只抑制气泡提醒,不会删除消息,也不会影响聊天未读数。"
>
<select
value={quietHours}
onChange={(event) => {
const next = event.target.value;
setQuietHours(next);
localStorage.setItem("astrbot.quietHours", next);
}}
>
<option value="off">关闭</option>
<option value="night">每天 22:00–07:00</option>
</select>
</SettingCard>
</section>
</div>
);
}
function DiagnosticsTools() {
const [result, setResult] = useState("");
const [busy, setBusy] = useState(false);
const [host, setHost] = useState("127.0.0.1");
const [port, setPort] = useState("80");
const [processId, setProcessId] = useState("0");
const run = async (command: string, request?: Record<string, unknown>) => {
setBusy(true);
try {
const value = await invoke<unknown>(
command,
request ? { request } : undefined,
);
setResult(JSON.stringify(value, null, 2));
} catch (reason) {
setResult(errorMessage(reason));
} finally {
setBusy(false);
}
};
return (
<>
<section className="settings-grid">
<SettingCard
title="前台窗口"
description="读取当前前台窗口的标题、类名和进程身份。"
>
<button
type="button"
disabled={busy}
onClick={() => void run("inspect_foreground_window")}
>
{busy ? "检查中…" : "检查前台窗口"}
</button>
</SettingCard>
<SettingCard
title="网络连通性"
description="只检查一个明确的主机和端口,不执行扫描。"
>
<label className="diagnostic-field">
<span>主机</span>
<input
value={host}
onChange={(event) => setHost(event.target.value)}
aria-label="主机"
/>
</label>
<label className="diagnostic-field">
<span>端口</span>
<input
value={port}
onChange={(event) => setPort(event.target.value)}
aria-label="端口"
inputMode="numeric"
/>
</label>
<button
type="button"
disabled={busy}
onClick={() =>
void run("check_network_target", {
host,
port: Number(port),
timeoutSeconds: 3,
})
}
>
{busy ? "检查中…" : "检查端口"}
</button>
</SettingCard>
<SettingCard
title="指定进程"
description="输入进程 ID 后读取有限的进程摘要,不枚举全部进程。"
>
<label className="diagnostic-field">
<span>进程 ID</span>
<input
value={processId}
onChange={(event) => setProcessId(event.target.value)}
aria-label="进程 ID"
inputMode="numeric"
/>
</label>
<button
type="button"
disabled={busy}
onClick={() =>
void run("inspect_process", { processId: Number(processId) })
}
>
{busy ? "检查中…" : "检查进程"}
</button>
</SettingCard>
</section>
{result ? (
<pre className="diagnostic-result" role="status" aria-live="polite">
{result}
</pre>
) : null}
</>
);
}
export function DiagnosticsPage({
status,
onRefresh,
}: {
status: BootstrapStatus;
onRefresh: () => void;
}) {
return (
<div className="page-stack">
<section className="status-grid" aria-label="bootstrap status">
<StatusCard label="Rust Core" value={status.rustCore} />
<StatusCard label="Platform" value={status.platform} />
<StatusCard label="App Version" value={status.appVersion} />
<StatusCard label="Protocol" value={status.protocolVersion} />
<StatusCard
label="Database Schema"
value={String(status.databaseSchemaVersion)}
/>
</section>
<DiagnosticsTools />
<section className="boundaries">
<span className="section-kicker">Security boundary</span>
<h2>本机能力保持可审计</h2>
<ul>
<li>凭据只在 Rust 与 Windows 安全存储之间流动。</li>
<li>两个 REST/SSE 槽位保持独立状态、会话、消息与未读数。</li>
<li>本机操作必须经过白名单、角色、路径和用户授权。</li>
<li>文件事务默认保留源文件,不静默覆盖目标。</li>
</ul>
</section>
<button type="button" onClick={onRefresh}>
刷新 IPC 状态
</button>
</div>
);
}
export function SettingCard({
title,
description,
children,
}: {
title: string;
description: string;
children: React.ReactNode;
}) {
return (
<section className="setting-card">
<div>
<span className="section-kicker">Settings</span>
<h2>{title}</h2>
<p>{description}</p>
</div>
<div className="setting-card-controls">{children}</div>
</section>
);
}
interface StatusCardProps {
label: string;
value: string;
}
function StatusCard({ label, value }: StatusCardProps) {
return (
<article className="status-card">
<span>{label}</span>
<strong>{value}</strong>
</article>
);
}