feat(web): 顶栏显示后端版本号

复用现有 /health 端点返回的 CARGO_PKG_VERSION,避免在 package.json 重复维护版本号;挂载时拉取一次,失败则不显示徽标。
This commit is contained in:
oudecheng 2026-08-07 14:52:11 +08:00
parent f26ec49182
commit 7de9a8a054
2 changed files with 26 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import { ConfigPage } from './components/Settings/ConfigPage';
import { ConnectionStatus } from './components/ConnectionStatus';
import { ChannelSelector } from './components/Header/ChannelSelector';
import { SessionSelector } from './components/Header/SessionSelector';
import { getVersion } from './api/config';
import { useWebSocket } from './hooks/useWebSocket';
import { useChat } from './hooks/useChat';
import type {
@ -47,6 +48,9 @@ function App() {
const wsUrl = buildWsUrl(gatewaySettings);
const lastAutoSwitchedTopicRef = useRef<string | null>(null);
// 后端版本号(来自 /health源自 Cargo.toml 的 CARGO_PKG_VERSION挂载时拉取一次失败则不显示。
const [appVersion, setAppVersion] = useState<string | null>(null);
const {
// 连接状态
isConnected,
@ -204,6 +208,11 @@ function App() {
localStorage.setItem('picobot-show-thinking', String(showThinking));
}, [showThinking]);
// 拉取后端版本号(/health 返回 CARGO_PKG_VERSION仅一次。
useEffect(() => {
getVersion().then(setAppVersion);
}, []);
// ---- WebSocket 初始化 ----
// Step 1: 连接建立后先请求通道列表
@ -616,6 +625,11 @@ function App() {
<span className="text-[var(--text-primary)]">Pico</span>
<span className="text-[var(--accent-cyan)] glow-text">Bot</span>
</h1>
{appVersion && (
<span className="text-xs font-mono text-[var(--text-muted)] select-none">
v{appVersion}
</span>
)}
</div>
<div className="h-4 w-px bg-[var(--divider-color)]" />
<ConnectionStatus status={status} />

View File

@ -37,3 +37,15 @@ export async function checkHealth(): Promise<boolean> {
return false;
}
}
/** 从 /health 端点获取后端版本号(源自 Cargo.toml 的 CARGO_PKG_VERSION。 */
export async function getVersion(): Promise<string | null> {
try {
const resp = await fetch(API.health);
if (!resp.ok) return null;
const data = (await resp.json()) as { status?: string; version?: string };
return data.version ?? null;
} catch {
return null;
}
}