259 lines
9.7 KiB
TypeScript
259 lines
9.7 KiB
TypeScript
// ============================================================
|
||
// CamTalk — 主应用组件(Web 端双栏布局)
|
||
// ============================================================
|
||
|
||
import { useCallback, useEffect, useRef, useState } from "react";
|
||
import { useVisionSession } from "./hooks/useVisionSession";
|
||
import { VideoPreview } from "./components/VideoPreview";
|
||
import { ChatPanel } from "./components/ChatPanel";
|
||
import { ConfigPanel } from "./components/ConfigPanel";
|
||
import { ToastContainer } from "./components/Toast";
|
||
import { loadTheme, saveTheme } from "./lib/storage";
|
||
import type { Theme } from "./types";
|
||
import "./App.css";
|
||
|
||
function App() {
|
||
const [showConfig, setShowConfig] = useState(false);
|
||
const [theme, setTheme] = useState<Theme>(loadTheme);
|
||
const [elapsed, setElapsed] = useState(0);
|
||
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||
|
||
// 切换主题时更新 <html> 的 data-theme 属性
|
||
useEffect(() => {
|
||
document.documentElement.setAttribute("data-theme", theme);
|
||
}, [theme]);
|
||
|
||
const handleThemeChange = useCallback((t: Theme) => {
|
||
setTheme(t);
|
||
saveTheme(t);
|
||
}, []);
|
||
|
||
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")}`;
|
||
};
|
||
|
||
const {
|
||
messages,
|
||
currentReply,
|
||
isProcessing,
|
||
isAudioPlaying,
|
||
isSpeaking,
|
||
isVADReady,
|
||
vadError,
|
||
connectionStatus,
|
||
videoRef,
|
||
stream,
|
||
config,
|
||
updateConfig,
|
||
stats,
|
||
mode,
|
||
isObserving,
|
||
toggleMode,
|
||
startSession,
|
||
stopSession,
|
||
interrupt,
|
||
isCameraOn,
|
||
isMicOn,
|
||
toggleCamera,
|
||
toggleMic,
|
||
sendTextMessage,
|
||
} = useVisionSession();
|
||
|
||
const isConnected = connectionStatus === "connected";
|
||
|
||
// 连接计时器
|
||
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]);
|
||
|
||
return (
|
||
<div className="app">
|
||
{/* ---- 顶部导航栏 ---- */}
|
||
<header className="header">
|
||
<div className="header__left">
|
||
<h1 className="header__title">CamTalk</h1>
|
||
<span className="header__subtitle">AI 视觉对话助手</span>
|
||
</div>
|
||
<div className="header__right">
|
||
{isConnected && (stats.queryCount > 0 || stats.totalTokens > 0) && (
|
||
<span className="header__stats">
|
||
{stats.queryCount} 次请求 · {stats.totalTokens} tokens
|
||
</span>
|
||
)}
|
||
<span className={`badge badge--${connectionStatus}`}>
|
||
{isConnected ? "已连接" : connectionStatus === "connecting" ? "连接中..." : "未连接"}
|
||
</span>
|
||
<button
|
||
className="btn-icon"
|
||
onClick={() => setShowConfig((v) => !v)}
|
||
title="设置"
|
||
aria-label="设置"
|
||
>
|
||
<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>
|
||
</button>
|
||
</div>
|
||
</header>
|
||
|
||
{showConfig && (
|
||
<ConfigPanel
|
||
config={config}
|
||
theme={theme}
|
||
onUpdate={updateConfig}
|
||
onThemeChange={handleThemeChange}
|
||
onClose={() => setShowConfig(false)}
|
||
/>
|
||
)}
|
||
|
||
{/* ---- 主体:左侧视频 + 右侧聊天 ---- */}
|
||
<div className="workspace">
|
||
{/* 左侧:视频预览 + 控制栏 */}
|
||
<div className="video-panel">
|
||
<div className="video-container">
|
||
<VideoPreview ref={videoRef} isStreaming={!!stream} />
|
||
{isConnected && (
|
||
<div className="live-badge">
|
||
<span className="live-badge__dot" />
|
||
LIVE {formatTime(elapsed)}
|
||
</div>
|
||
)}
|
||
{config.detailLevel === "high" && (
|
||
<div className="detail-badge">HD</div>
|
||
)}
|
||
{isObserving && (
|
||
<div className="observation-badge">👁️ 观察中</div>
|
||
)}
|
||
{isSpeaking && (
|
||
<div className="video-indicator">🎤 正在聆听...</div>
|
||
)}
|
||
{isAudioPlaying && config.ttsEnabled && (
|
||
<div className="video-indicator video-indicator--audio">🔊 正在播放...</div>
|
||
)}
|
||
{isConnected && !isVADReady && !vadError && (
|
||
<div className="video-indicator video-indicator--loading">正在初始化语音检测...</div>
|
||
)}
|
||
{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">
|
||
<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>
|
||
<span>点击下方按钮开始对话</span>
|
||
</div>
|
||
)}
|
||
{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>
|
||
<span>摄像头未开启</span>
|
||
<span className="video-placeholder__hint">可在右侧聊天框打字对话</span>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{/* 视频下方控制栏 */}
|
||
<div className="video-controls">
|
||
{!isConnected ? (
|
||
<button className="btn btn--primary btn--lg" onClick={startSession}>
|
||
{connectionStatus === "connecting" ? "连接中..." : "🎙️ 开始对话"}
|
||
</button>
|
||
) : (
|
||
<div className="video-controls__row">
|
||
<button
|
||
className={`btn btn--ctrl ${isCameraOn ? "btn--ctrl-on" : "btn--ctrl-off"}`}
|
||
onClick={toggleCamera}
|
||
title={isCameraOn ? "关闭摄像头" : "开启摄像头"}
|
||
>
|
||
📷
|
||
</button>
|
||
<button
|
||
className={`btn btn--ctrl ${isMicOn ? "btn--ctrl-on" : "btn--ctrl-off"} ${isSpeaking ? "btn--speaking" : ""}`}
|
||
onClick={toggleMic}
|
||
title={isMicOn ? "关闭麦克风" : "开启麦克风"}
|
||
>
|
||
🎤
|
||
</button>
|
||
<button
|
||
className={`btn ${mode === "observation" ? "btn--active" : "btn--secondary"}`}
|
||
onClick={toggleMode}
|
||
>
|
||
{mode === "observation" ? "👁️ 观察中" : "👁️ 观察模式"}
|
||
</button>
|
||
{isProcessing && (
|
||
<button className="btn btn--warning" onClick={interrupt}>
|
||
⏹ 打断
|
||
</button>
|
||
)}
|
||
<button className="btn btn--danger" onClick={stopSession}>
|
||
结束对话
|
||
</button>
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
|
||
{/* 右侧:聊天面板 */}
|
||
<div className="chat-panel-wrapper">
|
||
<div className="chat-panel-header">
|
||
<span>对话</span>
|
||
{isConnected && mode === "observation" && (
|
||
<span className="chat-panel-header__mode">观察模式</span>
|
||
)}
|
||
</div>
|
||
<div className="chat-panel-body">
|
||
{connectionStatus === "disconnected" && messages.length > 0 && (
|
||
<div className="system-message system-message--warning">
|
||
连接已断开,正在重连...
|
||
</div>
|
||
)}
|
||
<ChatPanel
|
||
messages={messages}
|
||
currentReply={currentReply}
|
||
connectionStatus={connectionStatus}
|
||
onSendText={sendTextMessage}
|
||
/>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<ToastContainer />
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export default App;
|