Ezmary commited on
Commit
142b779
·
verified ·
1 Parent(s): fba4fcd

Update src/components/control-tray/ControlTray.tsx

Browse files
src/components/control-tray/ControlTray.tsx CHANGED
@@ -1,24 +1,15 @@
1
- /**
2
- Copyright 2024 Google LLC
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
- http://www.apache.org/licenses/LICENSE-2.0
7
- Unless required by applicable law or agreed to in writing, software
8
- distributed under the License is distributed on an "AS IS" BASIS,
9
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
- See the License for the specific language governing permissions and
11
- limitations under the License.
12
- */
13
  import cn from "classnames";
14
- import React, { memo, ReactNode, RefObject, useEffect, useState, useCallback } from "react"; // useCallback اضافه شد
15
  import { useLiveAPIContext } from "../../contexts/LiveAPIContext";
16
  import { AudioRecorder } from "../../lib/audio-recorder";
17
- import LogoAnimation from '../logo-animation/LogoAnimation';
 
 
 
18
 
19
- const SvgPauseIcon = () => <svg width="37" height="37" viewBox="0 0 37 37" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M15.9872 29.6198V8.28342C15.9872 6.25781 15.132 5.44757 12.9713 5.44757H7.52469C5.36404 5.44757 4.50879 6.25781 4.50879 8.28342V29.6198C4.50879 31.6454 5.36404 32.4556 7.52469 32.4556H12.9713C15.132 32.4556 15.9872 31.6454 15.9872 29.6198Z" fill="#BE123C"/><path opacity="0.4" d="M31.5175 29.6198V8.28342C31.5175 6.25781 30.6622 5.44757 28.5016 5.44757H23.055C20.9093 5.44757 20.0391 6.25781 20.0391 8.28342V29.6198C20.0391 31.6454 20.8943 32.4556 23.055 32.4556H28.5016C30.6622 32.4556 31.5175 31.6454 31.5175 29.6198Z" fill="#BE123C"/></svg>;
20
- const SvgCameraIcon = () => <svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg"><path opacity="0.4" d="M21.8118 5.52235H11.9265C6.29183 5.52235 4.10059 7.7136 4.10059 13.3482V26.5286C4.10059 30.318 6.16002 34.3545 11.9265 34.3545H21.8118C27.4464 34.3545 29.6376 32.1633 29.6376 26.5286V13.3482C29.6376 7.7136 27.4464 5.52235 21.8118 5.52235Z" fill="#2252A0"/><path d="M19.3406 18.9169C21.0512 18.9169 22.438 17.5302 22.438 15.8195C22.438 14.1089 21.0512 12.7222 19.3406 12.7222C17.6299 12.7222 16.2432 14.1089 16.2432 15.8195C16.2432 17.5302 17.6299 18.9169 19.3406 18.9169Z" fill="#2252A0"/><path d="M36.0629 10.3332C35.3874 9.98721 33.9705 9.5918 32.0429 10.9428L29.6045 12.6562C29.621 12.8869 29.6374 13.1011 29.6374 13.3482V26.5286C29.6374 26.7758 29.6045 26.9899 29.6045 27.2206L32.0429 28.934C33.0643 29.659 33.954 29.8896 34.6625 29.8896C35.2721 29.8896 35.7499 29.7249 36.0629 29.5601C36.7384 29.2141 37.8752 28.275 37.8752 25.919V13.9743C37.8752 11.6183 36.7384 10.6792 36.0629 10.3332Z" fill="#2252A0"/></svg>;
21
- const SvgStopCamIcon = () => <svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg"><path opacity="0.4" d="M29.0785 10.8076L6.91966 32.9665C4.61316 31.5002 3.70703 28.8807 3.70703 26.36V13.18C3.70703 7.54555 5.89821 5.35437 11.5327 5.35437H21.4177C26.1789 5.35437 28.4854 6.9195 29.0785 10.8076Z" fill="#2252A0"/><path opacity="0.4" d="M29.2427 15.2394V26.36C29.2427 26.4918 29.2262 26.5907 29.2262 26.706C29.2262 26.8213 29.2097 26.9366 29.2097 27.052H29.2262C29.045 32.1757 26.8208 34.1856 21.417 34.1856H11.532C11.1202 34.1856 10.7412 34.1692 10.3623 34.1197L29.2427 15.2394Z" fill="#2252A0"/><path opacity="0.4" d="M29.21 27.052C29.21 26.9366 29.2264 26.8213 29.2264 26.706C29.2429 26.8213 29.2429 26.9366 29.2264 27.052H29.21Z" fill="#2252A0"/><path opacity="0.4" d="M29.2264 12.4716C29.2429 12.5869 29.2429 12.7187 29.2264 12.834C29.2264 12.7187 29.21 12.6034 29.21 12.488L29.2264 12.4716Z" fill="#2252A0"/><path d="M37.4804 13.8061V25.734C37.4804 28.0899 36.3436 29.029 35.6682 29.3749C35.3551 29.5397 34.8774 29.7209 34.2678 29.7209C33.5594 29.7209 32.6697 29.4903 31.6483 28.7654L29.2264 27.052H29.21C29.21 26.9366 29.2264 26.8213 29.2264 26.706C29.2264 26.5907 29.2429 26.4918 29.2429 26.36V15.2394L34.6302 9.85205C35.0751 9.885 35.421 10.0168 35.6682 10.1651C36.3436 10.5111 37.4804 11.4501 37.4804 13.8061Z" fill="#2252A0"/><path d="M35.8666 3.67393C35.3723 3.17968 34.565 3.17968 34.0708 3.67393L3.6744 34.0868C3.18015 34.581 3.18015 35.3883 3.6744 35.8826C3.92152 36.1132 4.23455 36.245 4.56405 36.245C4.89355 36.245 5.20657 36.1132 5.4537 35.8661L35.8666 5.45323C36.3773 4.95898 36.3773 4.16818 35.8666 3.67393Z" fill="#2252A0"/></svg>;
22
  const SvgSwitchCameraIcon = () => <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="w-[22px] h-[22px]"><path d="M11 19H4a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h5"/><path d="M13 5h7a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2h-5"/><path d="m17 12-3-3 3-3"/><path d="m7 12 3 3-3 3"/></svg>;
23
 
24
  export type ControlTrayProps = {
@@ -29,9 +20,9 @@ export type ControlTrayProps = {
29
  onAppMicToggle: (active: boolean) => void;
30
  isAppCamActive: boolean;
31
  onAppCamToggle: (active: boolean) => void;
32
- ReferenceMicrophoneIcon: () => JSX.Element;
33
- currentFacingMode: 'user' | 'environment'; // <<-- PROP از والد
34
- onFacingModeChange: (mode: 'user' | 'environment') => void; // <<-- PROP از والد
35
  };
36
 
37
  const ControlTray: React.FC<ControlTrayProps> = ({
@@ -42,19 +33,16 @@ const ControlTray: React.FC<ControlTrayProps> = ({
42
  onAppMicToggle,
43
  isAppCamActive,
44
  onAppCamToggle,
45
- ReferenceMicrophoneIcon,
46
- currentFacingMode, // <<-- دریافت PROP
47
- onFacingModeChange, // <<-- دریافت PROP
48
  }) => {
49
  const { client, connected, connect } = useLiveAPIContext();
50
  const [audioRecorder] = useState(() => new AudioRecorder());
51
  const [activeLocalVideoStream, setActiveLocalVideoStream] = useState<MediaStream | null>(null);
52
- // const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user'); // <<-- حذف state محلی
53
  const [isSwitchingCamera, setIsSwitchingCamera] = useState(false);
54
  const renderCanvasRef = React.useRef<HTMLCanvasElement>(null);
55
 
56
  useEffect(() => {
57
- // این useEffect برای اتصال ویدیو استریم به المنت ویدیو است
58
  if (videoRef.current) {
59
  if (videoRef.current.srcObject !== activeLocalVideoStream) {
60
  videoRef.current.srcObject = activeLocalVideoStream;
@@ -85,18 +73,18 @@ const ControlTray: React.FC<ControlTrayProps> = ({
85
  const mediaStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: facingModeToTry }, audio: false });
86
  setActiveLocalVideoStream(mediaStream);
87
  onVideoStreamChange(mediaStream);
88
- onFacingModeChange(facingModeToTry); // <<-- به‌روزرسانی state والد
89
  } catch (error) {
90
  console.error(`❌ Start WC err ${facingModeToTry}:`, error);
91
  setActiveLocalVideoStream(null);
92
  onVideoStreamChange(null);
93
- onAppCamToggle(false); // اگر دوربین روشن نشد، دکمه را خاموش کن
94
  } finally {
95
  setIsSwitchingCamera(false);
96
  }
97
  }, [
98
  isSwitchingCamera,
99
- activeLocalVideoStream, // currentFacingMode از props اینجا استفاده نمیشود مستقیما چون facingModeToTry پاس داده میشود
100
  onVideoStreamChange,
101
  onFacingModeChange,
102
  onAppCamToggle,
@@ -104,7 +92,7 @@ const ControlTray: React.FC<ControlTrayProps> = ({
104
 
105
  useEffect(() => {
106
  if (isAppCamActive && !activeLocalVideoStream && !isSwitchingCamera) {
107
- startWebcam(currentFacingMode); // <<-- استفاده از currentFacingMode (prop)
108
  } else if (!isAppCamActive && activeLocalVideoStream) {
109
  stopWebcam();
110
  }
@@ -193,9 +181,9 @@ const ControlTray: React.FC<ControlTrayProps> = ({
193
  if (!isAppMicActive) {
194
  onAppMicToggle(true);
195
  }
196
- onAppCamToggle(true); // useEffect مربوطه startWebcam را با currentFacingMode فعلی فراخوانی می‌کند
197
  } else {
198
- onAppCamToggle(false); // useEffect مربوطه stopWebcam را فراخوانی می‌کند
199
  }
200
  };
201
 
@@ -205,30 +193,27 @@ const ControlTray: React.FC<ControlTrayProps> = ({
205
 
206
  const targetFacingMode = currentFacingMode === 'user' ? 'environment' : 'user';
207
 
208
- // توقف استریم فعلی
209
  activeLocalVideoStream.getTracks().forEach(track => track.stop());
210
- setActiveLocalVideoStream(null); // پاک کردن استریم فعلی از state
211
  onVideoStreamChange(null);
212
 
213
  try {
214
  const newStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: targetFacingMode } }, audio: false });
215
  setActiveLocalVideoStream(newStream);
216
  onVideoStreamChange(newStream);
217
- onFacingModeChange(targetFacingMode); // <<-- به‌روزرسانی state والد با جهت جدید
218
  } catch (error) {
219
  console.error(`❌ Switch Cam err to ${targetFacingMode}:`, error);
220
- // تلاش برای بازگرداندن دوربین قبلی
221
  try {
222
- console.log(`Attempting to restore to ${currentFacingMode}`); // currentFacingMode اینجا جهت قبلی است
223
  const restoredStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: {exact: currentFacingMode } }, audio: false });
224
  setActiveLocalVideoStream(restoredStream);
225
  onVideoStreamChange(restoredStream);
226
- // onFacingModeChange(currentFacingMode); // نیازی نیست چون به حالت قبلی برگشته
227
  } catch (restoreError) {
228
  console.error(`❌ Restore Cam err to ${currentFacingMode}:`, restoreError);
229
  setActiveLocalVideoStream(null);
230
  onVideoStreamChange(null);
231
- onAppCamToggle(false); // اگر بازیابی هم ناموفق بود، دوربین را خاموش کن
232
  }
233
  } finally {
234
  setIsSwitchingCamera(false);
@@ -244,12 +229,13 @@ const ControlTray: React.FC<ControlTrayProps> = ({
244
  className="control-button mic-button-color"
245
  onClick={handleMicToggle}
246
  >
247
- {isAppMicActive ? <SvgPauseIcon /> : <ReferenceMicrophoneIcon />}
248
  </div>
249
 
250
  {isAppCamActive && (
251
  <div id="small-logo-footer-container" className="small-logo-footer-html-like">
252
- <LogoAnimation isMini={true} isActive={true} type="human" forFooter={true} />
 
253
  </div>
254
  )}
255
 
@@ -259,7 +245,7 @@ const ControlTray: React.FC<ControlTrayProps> = ({
259
  className="control-button cam-button-color"
260
  onClick={handleCamToggle}
261
  >
262
- {isAppCamActive ? <SvgStopCamIcon /> : <SvgCameraIcon />}
263
  </div>
264
  <div
265
  id="switch-camera-button-container"
 
1
+ // src/components/control-tray/ControlTray.tsx
2
+
 
 
 
 
 
 
 
 
 
 
3
  import cn from "classnames";
4
+ import React, { memo, RefObject, useEffect, useState, useCallback } from "react";
5
  import { useLiveAPIContext } from "../../contexts/LiveAPIContext";
6
  import { AudioRecorder } from "../../lib/audio-recorder";
7
+ import Logo from '../logo/Logo'; // <-- کامپوننت جدید Logo ایمپورت شد
8
+
9
+ // آیکون‌ها از فایل مرکزی ایمپورت می‌شوند
10
+ import { pauseIcon, microphoneIcon, cameraIcon, stopCamIcon } from '../icons';
11
 
12
+ // آیکون Switch Camera اینجا تعریف می‌شود چون فقط در این فایل استفاده شده
 
 
13
  const SvgSwitchCameraIcon = () => <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="w-[22px] h-[22px]"><path d="M11 19H4a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h5"/><path d="M13 5h7a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2h-5"/><path d="m17 12-3-3 3-3"/><path d="m7 12 3 3-3 3"/></svg>;
14
 
15
  export type ControlTrayProps = {
 
20
  onAppMicToggle: (active: boolean) => void;
21
  isAppCamActive: boolean;
22
  onAppCamToggle: (active: boolean) => void;
23
+ // ReferenceMicrophoneIcon حذف شد
24
+ currentFacingMode: 'user' | 'environment';
25
+ onFacingModeChange: (mode: 'user' | 'environment') => void;
26
  };
27
 
28
  const ControlTray: React.FC<ControlTrayProps> = ({
 
33
  onAppMicToggle,
34
  isAppCamActive,
35
  onAppCamToggle,
36
+ currentFacingMode,
37
+ onFacingModeChange,
 
38
  }) => {
39
  const { client, connected, connect } = useLiveAPIContext();
40
  const [audioRecorder] = useState(() => new AudioRecorder());
41
  const [activeLocalVideoStream, setActiveLocalVideoStream] = useState<MediaStream | null>(null);
 
42
  const [isSwitchingCamera, setIsSwitchingCamera] = useState(false);
43
  const renderCanvasRef = React.useRef<HTMLCanvasElement>(null);
44
 
45
  useEffect(() => {
 
46
  if (videoRef.current) {
47
  if (videoRef.current.srcObject !== activeLocalVideoStream) {
48
  videoRef.current.srcObject = activeLocalVideoStream;
 
73
  const mediaStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: facingModeToTry }, audio: false });
74
  setActiveLocalVideoStream(mediaStream);
75
  onVideoStreamChange(mediaStream);
76
+ onFacingModeChange(facingModeToTry);
77
  } catch (error) {
78
  console.error(`❌ Start WC err ${facingModeToTry}:`, error);
79
  setActiveLocalVideoStream(null);
80
  onVideoStreamChange(null);
81
+ onAppCamToggle(false);
82
  } finally {
83
  setIsSwitchingCamera(false);
84
  }
85
  }, [
86
  isSwitchingCamera,
87
+ activeLocalVideoStream,
88
  onVideoStreamChange,
89
  onFacingModeChange,
90
  onAppCamToggle,
 
92
 
93
  useEffect(() => {
94
  if (isAppCamActive && !activeLocalVideoStream && !isSwitchingCamera) {
95
+ startWebcam(currentFacingMode);
96
  } else if (!isAppCamActive && activeLocalVideoStream) {
97
  stopWebcam();
98
  }
 
181
  if (!isAppMicActive) {
182
  onAppMicToggle(true);
183
  }
184
+ onAppCamToggle(true);
185
  } else {
186
+ onAppCamToggle(false);
187
  }
188
  };
189
 
 
193
 
194
  const targetFacingMode = currentFacingMode === 'user' ? 'environment' : 'user';
195
 
 
196
  activeLocalVideoStream.getTracks().forEach(track => track.stop());
197
+ setActiveLocalVideoStream(null);
198
  onVideoStreamChange(null);
199
 
200
  try {
201
  const newStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: targetFacingMode } }, audio: false });
202
  setActiveLocalVideoStream(newStream);
203
  onVideoStreamChange(newStream);
204
+ onFacingModeChange(targetFacingMode);
205
  } catch (error) {
206
  console.error(`❌ Switch Cam err to ${targetFacingMode}:`, error);
 
207
  try {
208
+ console.log(`Attempting to restore to ${currentFacingMode}`);
209
  const restoredStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: {exact: currentFacingMode } }, audio: false });
210
  setActiveLocalVideoStream(restoredStream);
211
  onVideoStreamChange(restoredStream);
 
212
  } catch (restoreError) {
213
  console.error(`❌ Restore Cam err to ${currentFacingMode}:`, restoreError);
214
  setActiveLocalVideoStream(null);
215
  onVideoStreamChange(null);
216
+ onAppCamToggle(false);
217
  }
218
  } finally {
219
  setIsSwitchingCamera(false);
 
229
  className="control-button mic-button-color"
230
  onClick={handleMicToggle}
231
  >
232
+ {isAppMicActive ? pauseIcon : microphoneIcon}
233
  </div>
234
 
235
  {isAppCamActive && (
236
  <div id="small-logo-footer-container" className="small-logo-footer-html-like">
237
+ {/* کامپوننت Logo جدید جایگزین LogoAnimation شد */}
238
+ <Logo isMini={true} isActive={true} isAi={false} />
239
  </div>
240
  )}
241
 
 
245
  className="control-button cam-button-color"
246
  onClick={handleCamToggle}
247
  >
248
+ {isAppCamActive ? stopCamIcon : cameraIcon}
249
  </div>
250
  <div
251
  id="switch-camera-button-container"