2026-06-12 17:46:25 +08:00
|
|
|
|
// ============================================================
|
2026-06-13 16:16:37 +08:00
|
|
|
|
// CamTalk — 主应用组件(Web 端双栏布局)
|
2026-06-12 17:46:25 +08:00
|
|
|
|
// ============================================================
|
|
|
|
|
|
|
2026-06-14 14:34:30 +08:00
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2026-06-12 17:46:25 +08:00
|
|
|
|
import { useVisionSession } from "./hooks/useVisionSession";
|
|
|
|
|
|
import { VideoPreview } from "./components/VideoPreview";
|
|
|
|
|
|
import { ChatPanel } from "./components/ChatPanel";
|
2026-06-13 14:19:47 +08:00
|
|
|
|
import { ConfigPanel } from "./components/ConfigPanel";
|
2026-06-13 11:43:16 +08:00
|
|
|
|
import { ToastContainer } from "./components/Toast";
|
2026-06-14 14:34:30 +08:00
|
|
|
|
import { loadConfig, loadTheme, saveTheme } from "./lib/storage";
|
|
|
|
|
|
import { I18nContext, parseLocale, t } from "./lib/i18n";
|
|
|
|
|
|
import type { Locale } from "./lib/i18n";
|
2026-06-13 17:08:56 +08:00
|
|
|
|
import type { Theme } from "./types";
|
2026-06-12 17:46:25 +08:00
|
|
|
|
import "./App.css";
|
|
|
|
|
|
|
2026-06-14 14:34:30 +08:00
|
|
|
|
/** 内部组件,确保在 I18nContext.Provider 内部使用 useVisionSession */
|
|
|
|
|
|
function AppContent() {
|
2026-06-13 14:19:47 +08:00
|
|
|
|
const [showConfig, setShowConfig] = useState(false);
|
2026-06-13 17:08:56 +08:00
|
|
|
|
const [theme, setTheme] = useState<Theme>(loadTheme);
|
2026-06-13 19:01:33 +08:00
|
|
|
|
const [elapsed, setElapsed] = useState(0);
|
|
|
|
|
|
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
2026-06-13 17:08:56 +08:00
|
|
|
|
|
|
|
|
|
|
// 切换主题时更新 <html> 的 data-theme 属性
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
document.documentElement.setAttribute("data-theme", theme);
|
|
|
|
|
|
}, [theme]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleThemeChange = useCallback((t: Theme) => {
|
|
|
|
|
|
setTheme(t);
|
|
|
|
|
|
saveTheme(t);
|
|
|
|
|
|
}, []);
|
2026-06-13 14:19:47 +08:00
|
|
|
|
|
2026-06-13 19:01:33 +08:00
|
|
|
|
const formatTime = (s: number) => {
|
|
|
|
|
|
const m = Math.floor(s / 60);
|
|
|
|
|
|
const sec = s % 60;
|
|
|
|
|
|
return `${m.toString().padStart(2, "0")}:${sec.toString().padStart(2, "0")}`;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-06-12 17:46:25 +08:00
|
|
|
|
const {
|
|
|
|
|
|
messages,
|
|
|
|
|
|
currentReply,
|
|
|
|
|
|
isProcessing,
|
2026-06-13 13:57:36 +08:00
|
|
|
|
isAudioPlaying,
|
2026-06-12 17:46:25 +08:00
|
|
|
|
isSpeaking,
|
2026-06-13 11:10:29 +08:00
|
|
|
|
isVADReady,
|
|
|
|
|
|
vadError,
|
2026-06-12 17:46:25 +08:00
|
|
|
|
connectionStatus,
|
|
|
|
|
|
videoRef,
|
|
|
|
|
|
stream,
|
2026-06-13 14:19:47 +08:00
|
|
|
|
config,
|
|
|
|
|
|
updateConfig,
|
2026-06-13 14:38:15 +08:00
|
|
|
|
stats,
|
2026-06-13 14:46:42 +08:00
|
|
|
|
mode,
|
|
|
|
|
|
isObserving,
|
|
|
|
|
|
toggleMode,
|
2026-06-12 17:46:25 +08:00
|
|
|
|
startSession,
|
|
|
|
|
|
stopSession,
|
|
|
|
|
|
interrupt,
|
2026-06-13 20:01:37 +08:00
|
|
|
|
isCameraOn,
|
|
|
|
|
|
isMicOn,
|
|
|
|
|
|
toggleCamera,
|
|
|
|
|
|
toggleMic,
|
2026-06-14 12:52:39 +08:00
|
|
|
|
sendTextMessage,
|
2026-06-12 17:46:25 +08:00
|
|
|
|
} = useVisionSession();
|
|
|
|
|
|
|
2026-06-13 11:10:29 +08:00
|
|
|
|
const isConnected = connectionStatus === "connected";
|
|
|
|
|
|
|
2026-06-13 19:01:33 +08:00
|
|
|
|
// 连接计时器
|
|
|
|
|
|
const elapsedRef = useRef(0);
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (isConnected) {
|
|
|
|
|
|
elapsedRef.current = 0;
|
|
|
|
|
|
setElapsed(0); // eslint-disable-line react-hooks/set-state-in-effect -- 连接时重置计时器
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [isConnected]);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (!isConnected) {
|
|
|
|
|
|
if (timerRef.current) {
|
|
|
|
|
|
clearInterval(timerRef.current);
|
|
|
|
|
|
timerRef.current = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const id = setInterval(() => {
|
|
|
|
|
|
elapsedRef.current += 1;
|
|
|
|
|
|
setElapsed(elapsedRef.current);
|
|
|
|
|
|
}, 1000);
|
|
|
|
|
|
timerRef.current = id;
|
|
|
|
|
|
return () => clearInterval(id);
|
|
|
|
|
|
}, [isConnected]);
|
|
|
|
|
|
|
2026-06-14 14:34:30 +08:00
|
|
|
|
const { t: tr } = useMemo(() => ({ t: (key: string) => t(key, parseLocale(config.language)) }), [config.language]);
|
|
|
|
|
|
|
2026-06-12 17:46:25 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<div className="app">
|
2026-06-13 16:16:37 +08:00
|
|
|
|
{/* ---- 顶部导航栏 ---- */}
|
|
|
|
|
|
<header className="header">
|
|
|
|
|
|
<div className="header__left">
|
|
|
|
|
|
<h1 className="header__title">CamTalk</h1>
|
2026-06-14 14:34:30 +08:00
|
|
|
|
<span className="header__subtitle">{tr("app.title")}</span>
|
2026-06-13 16:16:37 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div className="header__right">
|
|
|
|
|
|
{isConnected && (stats.queryCount > 0 || stats.totalTokens > 0) && (
|
|
|
|
|
|
<span className="header__stats">
|
2026-06-14 14:34:30 +08:00
|
|
|
|
{stats.queryCount} {tr("app.stats.requests")} · {stats.totalTokens} tokens
|
2026-06-13 16:16:37 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
<span className={`badge badge--${connectionStatus}`}>
|
2026-06-14 14:34:30 +08:00
|
|
|
|
{isConnected ? tr("status.connected") : connectionStatus === "connecting" ? tr("status.connecting") : tr("status.disconnected")}
|
2026-06-13 14:19:47 +08:00
|
|
|
|
</span>
|
2026-06-13 16:54:57 +08:00
|
|
|
|
<button
|
|
|
|
|
|
className="btn-icon"
|
|
|
|
|
|
onClick={() => setShowConfig((v) => !v)}
|
2026-06-14 14:34:30 +08:00
|
|
|
|
title={tr("settings.title")}
|
|
|
|
|
|
aria-label={tr("settings.title")}
|
2026-06-13 16:54:57 +08:00
|
|
|
|
>
|
2026-06-14 10:18:36 +08:00
|
|
|
|
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
|
|
|
|
|
|
<circle cx="12" cy="12" r="3" />
|
|
|
|
|
|
<path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z" />
|
|
|
|
|
|
</svg>
|
2026-06-13 16:54:57 +08:00
|
|
|
|
</button>
|
2026-06-13 14:19:47 +08:00
|
|
|
|
</div>
|
2026-06-12 17:46:25 +08:00
|
|
|
|
</header>
|
|
|
|
|
|
|
2026-06-13 14:19:47 +08:00
|
|
|
|
{showConfig && (
|
|
|
|
|
|
<ConfigPanel
|
|
|
|
|
|
config={config}
|
2026-06-13 17:08:56 +08:00
|
|
|
|
theme={theme}
|
2026-06-13 14:19:47 +08:00
|
|
|
|
onUpdate={updateConfig}
|
2026-06-13 17:08:56 +08:00
|
|
|
|
onThemeChange={handleThemeChange}
|
2026-06-13 14:19:47 +08:00
|
|
|
|
onClose={() => setShowConfig(false)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2026-06-13 16:16:37 +08:00
|
|
|
|
{/* ---- 主体:左侧视频 + 右侧聊天 ---- */}
|
|
|
|
|
|
<div className="workspace">
|
|
|
|
|
|
{/* 左侧:视频预览 + 控制栏 */}
|
|
|
|
|
|
<div className="video-panel">
|
|
|
|
|
|
<div className="video-container">
|
|
|
|
|
|
<VideoPreview ref={videoRef} isStreaming={!!stream} />
|
2026-06-13 19:01:33 +08:00
|
|
|
|
{isConnected && (
|
|
|
|
|
|
<div className="live-badge">
|
|
|
|
|
|
<span className="live-badge__dot" />
|
|
|
|
|
|
LIVE {formatTime(elapsed)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-06-13 16:16:37 +08:00
|
|
|
|
{config.detailLevel === "high" && (
|
|
|
|
|
|
<div className="detail-badge">HD</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{isObserving && (
|
2026-06-14 14:34:30 +08:00
|
|
|
|
<div className="observation-badge">{tr("video.observing")}</div>
|
2026-06-13 16:16:37 +08:00
|
|
|
|
)}
|
|
|
|
|
|
{isSpeaking && (
|
2026-06-14 14:34:30 +08:00
|
|
|
|
<div className="video-indicator">{tr("video.listening")}</div>
|
2026-06-13 16:16:37 +08:00
|
|
|
|
)}
|
|
|
|
|
|
{isAudioPlaying && config.ttsEnabled && (
|
2026-06-14 14:34:30 +08:00
|
|
|
|
<div className="video-indicator video-indicator--audio">{tr("video.playing")}</div>
|
2026-06-13 16:16:37 +08:00
|
|
|
|
)}
|
|
|
|
|
|
{isConnected && !isVADReady && !vadError && (
|
2026-06-14 14:34:30 +08:00
|
|
|
|
<div className="video-indicator video-indicator--loading">{tr("video.initVad")}</div>
|
2026-06-13 16:16:37 +08:00
|
|
|
|
)}
|
|
|
|
|
|
{vadError && (
|
|
|
|
|
|
<div className="video-indicator video-indicator--error">⚠️ {vadError}</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{isProcessing && (
|
|
|
|
|
|
<div className="video-overlay">
|
|
|
|
|
|
<div className="video-overlay__spinner" />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{!isConnected && !stream && (
|
|
|
|
|
|
<div className="video-placeholder">
|
2026-06-14 10:18:36 +08:00
|
|
|
|
<svg className="video-placeholder__icon" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" strokeLinejoin="round">
|
|
|
|
|
|
<path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z" />
|
|
|
|
|
|
<circle cx="12" cy="13" r="4" />
|
|
|
|
|
|
</svg>
|
2026-06-14 14:34:30 +08:00
|
|
|
|
<span>{tr("video.clickToStart")}</span>
|
2026-06-13 16:16:37 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-06-14 13:13:29 +08:00
|
|
|
|
{isConnected && !isCameraOn && (
|
|
|
|
|
|
<div className="video-placeholder">
|
|
|
|
|
|
<svg className="video-placeholder__icon" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" strokeLinejoin="round">
|
|
|
|
|
|
<path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z" />
|
|
|
|
|
|
<circle cx="12" cy="13" r="4" />
|
|
|
|
|
|
</svg>
|
2026-06-14 14:34:30 +08:00
|
|
|
|
<span>{tr("video.cameraOff")}</span>
|
|
|
|
|
|
<span className="video-placeholder__hint">{tr("video.cameraOff.hint")}</span>
|
2026-06-14 13:13:29 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-06-13 16:16:37 +08:00
|
|
|
|
</div>
|
2026-06-12 17:46:25 +08:00
|
|
|
|
|
2026-06-13 16:16:37 +08:00
|
|
|
|
{/* 视频下方控制栏 */}
|
|
|
|
|
|
<div className="video-controls">
|
|
|
|
|
|
{!isConnected ? (
|
|
|
|
|
|
<button className="btn btn--primary btn--lg" onClick={startSession}>
|
2026-06-14 14:34:30 +08:00
|
|
|
|
{connectionStatus === "connecting" ? tr("controls.connecting") : tr("controls.start")}
|
2026-06-13 16:16:37 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="video-controls__row">
|
2026-06-13 20:01:37 +08:00
|
|
|
|
<button
|
|
|
|
|
|
className={`btn btn--ctrl ${isCameraOn ? "btn--ctrl-on" : "btn--ctrl-off"}`}
|
|
|
|
|
|
onClick={toggleCamera}
|
2026-06-14 14:34:30 +08:00
|
|
|
|
title={isCameraOn ? tr("controls.cameraOff") : tr("controls.cameraOn")}
|
2026-06-13 20:01:37 +08:00
|
|
|
|
>
|
|
|
|
|
|
📷
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
className={`btn btn--ctrl ${isMicOn ? "btn--ctrl-on" : "btn--ctrl-off"} ${isSpeaking ? "btn--speaking" : ""}`}
|
|
|
|
|
|
onClick={toggleMic}
|
2026-06-14 14:34:30 +08:00
|
|
|
|
title={isMicOn ? tr("controls.micOff") : tr("controls.micOn")}
|
2026-06-13 20:01:37 +08:00
|
|
|
|
>
|
|
|
|
|
|
🎤
|
|
|
|
|
|
</button>
|
2026-06-13 16:16:37 +08:00
|
|
|
|
<button
|
|
|
|
|
|
className={`btn ${mode === "observation" ? "btn--active" : "btn--secondary"}`}
|
|
|
|
|
|
onClick={toggleMode}
|
|
|
|
|
|
>
|
2026-06-14 14:34:30 +08:00
|
|
|
|
{mode === "observation" ? tr("controls.observing") : tr("controls.observation")}
|
2026-06-13 16:16:37 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
{isProcessing && (
|
|
|
|
|
|
<button className="btn btn--warning" onClick={interrupt}>
|
2026-06-14 14:34:30 +08:00
|
|
|
|
{tr("controls.interrupt")}
|
2026-06-13 16:16:37 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
<button className="btn btn--danger" onClick={stopSession}>
|
2026-06-14 14:34:30 +08:00
|
|
|
|
{tr("controls.stop")}
|
2026-06-13 16:16:37 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2026-06-13 14:38:15 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-06-13 16:16:37 +08:00
|
|
|
|
{/* 右侧:聊天面板 */}
|
|
|
|
|
|
<div className="chat-panel-wrapper">
|
|
|
|
|
|
<div className="chat-panel-header">
|
2026-06-14 14:34:30 +08:00
|
|
|
|
<span>{tr("chat.title")}</span>
|
2026-06-13 16:16:37 +08:00
|
|
|
|
{isConnected && mode === "observation" && (
|
2026-06-14 14:34:30 +08:00
|
|
|
|
<span className="chat-panel-header__mode">{tr("chat.mode.observation")}</span>
|
2026-06-12 17:46:25 +08:00
|
|
|
|
)}
|
2026-06-13 16:16:37 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div className="chat-panel-body">
|
|
|
|
|
|
{connectionStatus === "disconnected" && messages.length > 0 && (
|
|
|
|
|
|
<div className="system-message system-message--warning">
|
2026-06-14 14:34:30 +08:00
|
|
|
|
{tr("chat.reconnecting")}
|
2026-06-13 16:16:37 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
<ChatPanel
|
|
|
|
|
|
messages={messages}
|
|
|
|
|
|
currentReply={currentReply}
|
|
|
|
|
|
connectionStatus={connectionStatus}
|
2026-06-14 12:52:39 +08:00
|
|
|
|
onSendText={sendTextMessage}
|
2026-06-13 16:16:37 +08:00
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-06-13 11:43:16 +08:00
|
|
|
|
|
|
|
|
|
|
<ToastContainer />
|
2026-06-12 17:46:25 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-14 14:34:30 +08:00
|
|
|
|
function App() {
|
|
|
|
|
|
const [locale, setLocale] = useState<Locale>(() => parseLocale(loadConfig().language));
|
|
|
|
|
|
|
|
|
|
|
|
const i18nValue = useMemo(() => ({
|
|
|
|
|
|
locale,
|
|
|
|
|
|
t: (key: string) => t(key, locale),
|
|
|
|
|
|
}), [locale]);
|
|
|
|
|
|
|
|
|
|
|
|
// 监听配置变化以更新 locale
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const handler = () => {
|
|
|
|
|
|
const cfg = loadConfig();
|
|
|
|
|
|
setLocale(parseLocale(cfg.language));
|
|
|
|
|
|
};
|
|
|
|
|
|
// 自定义事件,由 saveConfig 触发
|
|
|
|
|
|
window.addEventListener("camtalk-config-changed", handler);
|
|
|
|
|
|
return () => window.removeEventListener("camtalk-config-changed", handler);
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<I18nContext.Provider value={i18nValue}>
|
|
|
|
|
|
<AppContent />
|
|
|
|
|
|
</I18nContext.Provider>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-12 17:46:25 +08:00
|
|
|
|
export default App;
|