SotiproAlpha2 / src /App.tsx
Ezmary's picture
Update src/App.tsx
bdec212 verified
raw
history blame
6.42 kB
// src/App.tsx
import React, { useEffect, useRef, useState } from "react";
import './App.scss';
import { AppProvider, useAppContext, PersonalityType, PersonalityInstructions } from "./contexts/AppContext";
import ControlTray from "./components/control-tray/ControlTray";
import { IOSModal } from "./components/ios-modal/IOSModal";
import { isIOS } from "./lib/platform";
import cn from "classnames";
import Logo from "./components/logo/Logo";
import { LiveConfig } from "./multimodal-live-types";
import { CustomModal } from "./components/CustomModal";
// منوی شخصیت‌ها (به صورت یک کامپوننت داخلی ساده)
const PersonalityMenu: React.FC<{ isOpen: boolean; onClose: () => void; onSelect: (p: PersonalityType) => void; }> = ({ isOpen, onClose, onSelect }) => {
const menuRef = useRef<HTMLDivElement>(null);
const { selectedPersonality } = useAppContext();
const personalityIcons: Record<PersonalityType, string> = { default: "person", teacher: "school", poetic: "auto_awesome", funny: "sentiment_satisfied", custom: "tune" };
const personalityLabels: Record<PersonalityType, string> = { default: 'دستیار پیش‌فرض', teacher: 'استاد زبان', poetic: 'حس خوب', funny: 'شوخ‌طبع', custom: 'شخصیت اختصاصی' };
useEffect(() => {
const handleClickOutside = (e: MouseEvent) => menuRef.current && !menuRef.current.contains(e.target as Node) && onClose();
if (isOpen) document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}, [isOpen, onClose]);
if (!isOpen) return null;
return (
<div ref={menuRef} className="personality-popover-wrapper open">
<div className="popover-content">
<ul>
{(Object.keys(personalityIcons) as PersonalityType[]).map(key => (
<li key={key} className={cn({ active: selectedPersonality === key })} onClick={() => onSelect(key)}>
<div><span className="material-symbols-outlined">{personalityIcons[key]}</span>{personalityLabels[key]}</div>
{selectedPersonality === key && <span className="material-symbols-outlined tick">done</span>}
</li>
))}
</ul>
</div>
</div>
);
};
// کامپوننت داخلی اصلی برنامه
const AppInternal: React.FC = () => {
const videoRef = useRef<HTMLVideoElement>(null);
const { volume, changePersonality, customUserName, customInstructions } = useAppContext();
const [isUserSpeaking, setIsUserSpeaking] = useState(false);
const [isMicActive, setIsMicActive] = useState(false);
const [isCamActive, setIsCamActive] = useState(false);
const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user');
const [isNotificationOpen, setIsNotificationOpen] = useState(false);
const [isPersonalityMenuOpen, setIsPersonalityMenuOpen] = useState(false);
const [isCustomModalOpen, setIsCustomModalOpen] = useState(false);
const handleSelectPersonality = (p: PersonalityType) => {
setIsPersonalityMenuOpen(false);
if (p === 'custom') setIsCustomModalOpen(true);
else changePersonality(p);
};
return (
<>
<div className="main-wrapper">
<div className="header-controls">
<button aria-label="انتخاب شخصیت" className="header-icon-button" onClick={() => setIsPersonalityMenuOpen(true)}>
<span className="material-symbols-outlined">psychology</span>
</button>
<button aria-label="اطلاعات" className="header-icon-button" onClick={() => setIsNotificationOpen(v => !v)}>
<span className="material-symbols-outlined">info</span>
</button>
</div>
{isNotificationOpen && <div className="notification-popover-wrapper open"><div className="popover-content">مدل‌های هوش مصنوعی می‌توانند اشتباه کنند.</div></div>}
<PersonalityMenu isOpen={isPersonalityMenuOpen} onClose={() => setIsPersonalityMenuOpen(false)} onSelect={handleSelectPersonality} />
<div className="media-area">
<video id="video-feed" ref={videoRef} autoPlay playsInline className={cn({ hidden: !isCamActive }, { "scale-x-[-1]": currentFacingMode === 'user' })} />
{isMicActive && !isCamActive && <div id="large-logo-container"><Logo isMini={false} isActive={true} isAi={false} speakingVolume={volume} isUserSpeaking={isUserSpeaking} /></div>}
</div>
<ControlTray videoRef={videoRef} onUserSpeakingChange={setIsUserSpeaking} isAppMicActive={isMicActive} onAppMicToggle={setIsMicActive} isAppCamActive={isCamActive} onAppCamToggle={setIsCamActive} currentFacingMode={currentFacingMode} onFacingModeChange={setCurrentFacingMode} />
</div>
<CustomModal isOpen={isCustomModalOpen} onClose={() => setIsCustomModalOpen(false)} onSave={(name, instructions) => changePersonality('custom', { name, instructions })} initialName={customUserName} initialInstructions={customInstructions} />
</>
);
};
// کامپوننت ریشه App
function App() {
const [showIOSModal, setShowIOSModal] = useState(false);
const [personalityInstructions, setPersonalityInstructions] = useState<PersonalityInstructions | null>(null);
const [loadingError, setLoadingError] = useState<string | null>(null);
useEffect(() => {
if (isIOS()) setShowIOSModal(true);
const fetchInstructions = async () => {
try {
const res = await fetch('/api/instructions');
if (!res.ok) throw new Error(`Network error: ${res.status}`);
setPersonalityInstructions(await res.json());
} catch (e) {
setLoadingError("امکان دریافت تنظیمات شخصیت‌ها وجود ندارد. لطفاً صفحه را رفرش کنید.");
}
};
fetchInstructions();
}, []);
if (loadingError) return <div className="loading-screen">{loadingError}</div>;
if (!personalityInstructions) return <div className="loading-screen">در حال بارگذاری...</div>;
const initialAppConfig: LiveConfig = { model: "models/gemini-2.0-flash-exp" };
return (
<AppProvider initialConfig={initialAppConfig} personalityInstructions={personalityInstructions}>
<AppInternal />
<IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} />
</AppProvider>
);
}
export default App;