Ezmary commited on
Commit
8f94a86
·
verified ·
1 Parent(s): 3e36429

Update src/App.tsx

Browse files
Files changed (1) hide show
  1. src/App.tsx +78 -146
src/App.tsx CHANGED
@@ -1,6 +1,5 @@
1
  // src/App.tsx
2
 
3
- // ... (ایمپورت‌های اولیه مثل قبل) ...
4
  import React, { useEffect, useRef, useState } from "react";
5
  import './App.scss';
6
  import { LiveAPIProvider, useLiveAPIContext } from "./contexts/LiveAPIContext";
@@ -12,7 +11,6 @@ import { LiveConfig } from "./multimodal-live-types";
12
  import LogoAnimation from "./components/logo-animation/LogoAnimation";
13
  import BackButton from "./components/back-button/BackButton";
14
 
15
- // *** MODIFIED: دستورالعمل جدید ربات ***
16
  const myCustomInstruction = `
17
  تو دستیار صوتی و تصویری پیشرفته از "اپلیکیشن هوش مصنوعی هوشان" هستی
18
  وظیفه اصلی تو کمک به کاربر است.
@@ -38,7 +36,15 @@ const initialAppConfig: LiveConfig = {
38
  },
39
  };
40
 
41
- const SvgReferenceMicrophoneIcon = () => ( /* ... (بدون تغییر) ... */ );
 
 
 
 
 
 
 
 
42
 
43
  const AppInternalLogic: React.FC<{
44
  isMicActive: boolean;
@@ -50,7 +56,7 @@ const AppInternalLogic: React.FC<{
50
  notificationButtonRef: React.RefObject<HTMLButtonElement>;
51
  isNotificationOpen: boolean;
52
  setIsNotificationOpen: React.Dispatch<React.SetStateAction<boolean>>;
53
- currentFacingMode: 'user' | 'environment'; // *** NEW PROP ***
54
  }> = ({
55
  isMicActive,
56
  isCamActive,
@@ -61,7 +67,7 @@ const AppInternalLogic: React.FC<{
61
  notificationButtonRef,
62
  isNotificationOpen,
63
  setIsNotificationOpen,
64
- currentFacingMode, // *** NEW PROP ***
65
  }) => {
66
  const { connected, disconnect } = useLiveAPIContext();
67
 
@@ -94,7 +100,19 @@ const AppInternalLogic: React.FC<{
94
  </div>
95
  </div>
96
 
97
- <div ref={notificationPopoverRef} id="notification-popover-wrapper" className="notification-popover-wrapper"> {/* ... */} </div>
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  <div className="media-area w-full flex flex-col items-center justify-center flex-grow relative">
100
  <video
@@ -104,100 +122,74 @@ const AppInternalLogic: React.FC<{
104
  playsInline
105
  className={cn(
106
  "absolute top-0 left-0 w-full h-full object-cover",
107
- // *** MODIFIED: اعمال scale-x-[-1] فقط برای دوربین جلو ***
108
- { "scale-x-[-1]": currentFacingMode === 'user' },
109
  { "hidden": !isCamActive }
110
  )}
111
  />
112
  {isMicActive && !isCamActive && (
113
- <div id="large-logo-container" className="absolute top-0 left-0 w-full h-full flex items-center justify-center pointer-events-none">
 
 
 
114
  <LogoAnimation isMini={false} isActive={true} type="human" />
115
  </div>
116
  )}
117
  </div>
118
-
119
- <ControlTray
120
- videoRef={videoRef}
121
- supportsVideo={true}
122
- onVideoStreamChange={(stream) => { /* ... */ }}
123
- isAppMicActive={isMicActive}
124
- onAppMicToggle={setIsMicActive}
125
- isAppCamActive={isCamActive}
126
- onAppCamToggle={setIsCamActive}
127
- ReferenceMicrophoneIcon={SvgReferenceMicrophoneIcon}
128
- // currentFacingMode و setCurrentFacingMode از App به ControlTray پاس داده می‌شوند
129
- />
130
  </div>
131
  </div>
132
  );
133
  }
134
 
135
  function App() {
136
- const videoRef = useRef<HTMLVideoElement>(null);
137
- const [showIOSModal, setShowIOSModal] = useState(false);
138
- const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(null);
139
-
140
- const [isMicActive, setIsMicActive] = useState(false);
141
- const [isCamActive, setIsCamActive] = useState(false);
142
- const [isNotificationOpen, setIsNotificationOpen] = useState(false);
143
- // *** NEW STATE: برای نگهداری حالت دوربین (جلو یا عقب) ***
144
- const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user');
145
-
146
- const notificationButtonRef = useRef<HTMLButtonElement>(null);
147
- const notificationPopoverRef = useRef<HTMLDivElement>(null);
148
-
149
- useEffect(() => { /* ... */ }, []);
150
- useEffect(() => { /* ... */ }, [isNotificationOpen]);
151
- if (isAllowedOrigin === null) { /* ... */ }
152
-
153
- return (
154
- <LiveAPIProvider initialConfig={initialAppConfig}>
155
- <AppInternalLogic
156
- isMicActive={isMicActive}
157
- setIsMicActive={setIsMicActive}
158
- isCamActive={isCamActive}
159
- setIsCamActive={setIsCamActive}
160
- videoRef={videoRef}
161
- notificationPopoverRef={notificationPopoverRef}
162
- notificationButtonRef={notificationButtonRef}
163
- isNotificationOpen={isNotificationOpen}
164
- setIsNotificationOpen={setIsNotificationOpen}
165
- currentFacingMode={currentFacingMode} // *** پاس دادن state جدید ***
166
- />
167
- {/* ControlTray دیگر نیازی به پاس دادن setCurrentFacingMode ندارد چون این state در App.tsx مدیریت می‌شود */}
168
- {/* و خود ControlTray باید currentFacingMode را از App.tsx به عنوان prop دریافت کند */}
169
- {/* اگر ControlTray نیاز به تغییر facingMode دارد، باید یک تابع callback پاس داده شود */}
170
- <IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} />
171
- </LiveAPIProvider>
172
- );
173
- }
174
- // تابع App به یک تابع نیاز دارد که به ControlTray پاس داده شود تا بتواند currentFacingMode را تغییر دهد.
175
- // یا اینکه currentFacingMode به طور کامل در ControlTray مدیریت شود.
176
- // برای سادگی، فعلا فرض می‌کنیم ControlTray فقط currentFacingMode را از App دریافت می‌کند.
177
- // اگر نیاز به تغییر آن از ControlTray باشد، باید یک setter هم پاس دهیم.
178
-
179
- // برای اینکه ControlTray بتواند currentFacingMode را تغییر دهد، باید `setCurrentFacingMode` را هم به آن پاس دهیم.
180
- // اصلاح تابع App:
181
- function AppWithFacingModeControl() {
182
- const videoRef = useRef<HTMLVideoElement>(null);
183
  const [showIOSModal, setShowIOSModal] = useState(false);
184
  const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(null);
185
 
186
  const [isMicActive, setIsMicActive] = useState(false);
187
  const [isCamActive, setIsCamActive] = useState(false);
188
  const [isNotificationOpen, setIsNotificationOpen] = useState(false);
189
- const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user'); // State در App
190
 
191
  const notificationButtonRef = useRef<HTMLButtonElement>(null);
192
  const notificationPopoverRef = useRef<HTMLDivElement>(null);
193
 
194
- // useEffect ها مثل قبل
 
 
 
 
 
 
 
 
195
 
196
- if (isAllowedOrigin === null) { /* ... */ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
  return (
199
  <LiveAPIProvider initialConfig={initialAppConfig}>
200
- {/* AppInternalLogic currentFacingMode را برای نمایش ویدیو استفاده می‌کند */}
201
  <AppInternalLogic
202
  isMicActive={isMicActive}
203
  setIsMicActive={setIsMicActive}
@@ -208,90 +200,30 @@ function AppWithFacingModeControl() {
208
  notificationButtonRef={notificationButtonRef}
209
  isNotificationOpen={isNotificationOpen}
210
  setIsNotificationOpen={setIsNotificationOpen}
211
- currentFacingMode={currentFacingMode} // پاس دادن حالت فعلی
212
  />
213
- {/* ControlTray نیاز به setCurrentFacingMode دارد تا بتواند آن را تغییر دهد */}
214
- {/* برای این کار، ControlTray باید این تابع را به عنوان prop دریافت کند */}
215
- {/* و App.tsx باید آن را فراهم کند. این در ControlTray.tsx پیاده‌سازی می‌شود. */}
216
- {/* فعلا در این فایل، فقط currentFacingMode را به AppInternalLogic پاس می‌دهیم. */}
217
- {/* تغییر facing mode در خود ControlTray مدیریت می‌شود و state آن به App برگردانده نمی‌شود. */}
218
- {/* این برای نمایش scale-x صحیح کافی است اگر ControlTray خودش facingMode را مدیریت کند. */}
219
- {/* اما برای اینکه AppInternalLogic هم بداند، باید state مشترک باشد. */}
220
- {/* ساده‌ترین راه این است که ControlTray یک prop onFacingModeChange بگیرد */}
221
- <ControlTrayPassProps
222
- videoRef={videoRef}
223
  supportsVideo={true}
224
- onVideoStreamChange={(stream) => { /* ... */ }}
 
 
 
 
 
 
225
  isAppMicActive={isMicActive}
226
  onAppMicToggle={setIsMicActive}
227
  isAppCamActive={isCamActive}
228
  onAppCamToggle={setIsCamActive}
229
  ReferenceMicrophoneIcon={SvgReferenceMicrophoneIcon}
230
- // *** NEW: پاس دادن تابع برای تغییر facing mode ***
231
- onFacingModeChange={setCurrentFacingMode}
232
- initialFacingMode={currentFacingMode} // پاس دادن حالت اولیه
233
  />
234
  <IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} />
235
  </LiveAPIProvider>
236
  );
237
  }
238
- // برای جلوگیری از پیچیدگی زیاد، فعلا از AppWithFacingModeControl صرف نظر می‌کنیم
239
- // و فرض می‌کنیم ControlTray خودش currentFacingMode را مدیریت می‌کند و ما فقط برای نمایش از آن استفاده می‌کنیم.
240
- // این یعنی AppInternalLogic باید currentFacingMode را از ControlTray بگیرد یا ControlTray آن را به App پاس دهد.
241
- // راه حل ساده‌تر: currentFacingMode را فقط در ControlTray نگه داریم و از طریق یک prop به AppInternalLogic بدهیم.
242
- // اما چون AppInternalLogic والد ControlTray است، این کار مستقیم نیست.
243
- // بهترین راه: state در App، و پاس دادن state و setter به کامپوننت‌��ای فرزند.
244
-
245
- // اصلاح نهایی App و AppInternalLogic:
246
- // (کد SvgReferenceMicrophoneIcon و initialAppConfig و myCustomInstruction مثل قبل)
247
-
248
- const CorrectedAppInternalLogic: React.FC<{ /* props مثل قبل */ currentFacingMode: 'user' | 'environment'; }> = ({ /* ... */ currentFacingMode }) => {
249
- // ... (JSX مثل قبل، اما با استفاده از currentFacingMode برای video className)
250
- // <video className={cn(..., { "scale-x-[-1]": currentFacingMode === 'user' }, ... )} />
251
- // ...
252
- return ( /* ... JSX قبلی با currentFacingMode ... */ );
253
- };
254
 
255
-
256
- function CorrectedApp() {
257
- // ... (state های isMicActive, isCamActive, isNotificationOpen مثل قبل)
258
- const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user'); // State در App
259
- // ... (ref ها مثل قبل)
260
-
261
- // useEffect ها مثل قبل
262
-
263
- if (isAllowedOrigin === null) { /* ... */ }
264
-
265
- return (
266
- <LiveAPIProvider initialConfig={initialAppConfig}>
267
- <AppInternalLogic // استفاده از AppInternalLogic که currentFacingMode را دریافت می‌کند
268
- isMicActive={isMicActive}
269
- setIsMicActive={setIsMicActive}
270
- isCamActive={isCamActive}
271
- setIsCamActive={setIsCamActive}
272
- videoRef={useRef<HTMLVideoElement>(null)} // ref ها باید اینجا باشند یا از AppInternalLogic مدیریت شوند
273
- notificationPopoverRef={useRef<HTMLDivElement>(null)}
274
- notificationButtonRef={useRef<HTMLButtonElement>(null)}
275
- isNotificationOpen={isNotificationOpen}
276
- setIsNotificationOpen={setIsNotificationOpen}
277
- currentFacingMode={currentFacingMode} // پاس دادن state
278
- />
279
- <ControlTray
280
- videoRef={useRef<HTMLVideoElement>(null)} // این ref ها باید با AppInternalLogic هماهنگ باشند
281
- supportsVideo={true}
282
- onVideoStreamChange={(stream) => { /* ... */ }}
283
- isAppMicActive={isMicActive}
284
- onAppMicToggle={setIsMicActive}
285
- isAppCamActive={isCamActive}
286
- onAppCamToggle={setIsCamActive}
287
- ReferenceMicrophoneIcon={SvgReferenceMicrophoneIcon}
288
- onFacingModeChange={setCurrentFacingMode} // پاس دادن setter
289
- initialFacingMode={currentFacingMode} // پاس دادن مقدار اولیه
290
- />
291
- <IOSModal isOpen={useState(false)[0]} onClose={() => useState(false)[1](false)} />
292
- </LiveAPIProvider>
293
- );
294
- }
295
- // استفاده از CorrectedApp به جای App
296
- // export default CorrectedApp; // <--- این را فعال کنید
297
- export default App; // فعلا برای حفظ سادگی از App اصلی استفاده می‌کنیم و تغییرات را در ControlTray اعمال می‌کنیم
 
1
  // src/App.tsx
2
 
 
3
  import React, { useEffect, useRef, useState } from "react";
4
  import './App.scss';
5
  import { LiveAPIProvider, useLiveAPIContext } from "./contexts/LiveAPIContext";
 
11
  import LogoAnimation from "./components/logo-animation/LogoAnimation";
12
  import BackButton from "./components/back-button/BackButton";
13
 
 
14
  const myCustomInstruction = `
15
  تو دستیار صوتی و تصویری پیشرفته از "اپلیکیشن هوش مصنوعی هوشان" هستی
16
  وظیفه اصلی تو کمک به کاربر است.
 
36
  },
37
  };
38
 
39
+ // *** خط ۴۱ اینجا شروع می‌شود ***
40
+ // SVG آیکون میکروفون از HTML مرجع شما
41
+ // مطمئن شوید که هیچ کاراکتر اضافی یا پرانتز نابجایی در این تعریف وجود ندارد.
42
+ const SvgReferenceMicrophoneIcon = () => (
43
+ <svg className="reference-mic-svg" viewBox="0 0 69 68" fill="none" xmlns="http://www.w3.org/2000/svg">
44
+ <path opacity="0.4" d="M49.9479 27.1824C49.0803 27.1824 48.3907 27.872 48.3907 28.7396V32.2544C48.3907 40.1293 41.984 46.5361 34.109 46.5361C26.234 46.5361 19.8273 40.1293 19.8273 32.2544V28.7173C19.8273 27.8497 19.1377 27.1601 18.2701 27.1601C17.4025 27.1601 16.7129 27.8497 16.7129 28.7173V32.2321C16.7129 41.2861 23.6758 48.7384 32.5518 49.5393V54.2776C32.5518 55.1452 33.2414 55.8348 34.109 55.8348C34.9766 55.8348 35.6662 55.1452 35.6662 54.2776V49.5393C44.52 48.7607 51.5051 41.2861 51.5051 32.2321V28.7173C51.4829 27.872 50.7933 27.1824 49.9479 27.1824Z" fill="#BE123C"/>
45
+ <path d="M34.1099 11.3434C28.682 11.3434 24.2773 15.7481 24.2773 21.176V32.5658C24.2773 37.9938 28.682 42.3984 34.1099 42.3984C39.5379 42.3984 43.9425 37.9938 43.9425 32.5658V21.176C43.9425 15.7481 39.5379 11.3434 34.1099 11.3434ZM37.0241 26.8042C36.8684 27.3826 36.3567 27.7608 35.7784 27.7608C35.6671 27.7608 35.5559 27.7385 35.4447 27.7163C34.5771 27.4716 33.665 27.4716 32.7974 27.7163C32.0856 27.9165 31.396 27.4938 31.218 26.8042C31.0178 26.1146 31.4404 25.4027 32.1301 25.2247C33.4426 24.8688 34.8218 24.8688 36.1343 25.2247C36.8017 25.4027 37.2021 26.1146 37.0241 26.8042ZM38.2031 22.4885C38.0029 23.0224 37.5135 23.3339 36.9796 23.3339C36.8239 23.3339 36.6904 23.3116 36.5347 23.2671C34.9775 22.6887 33.2423 22.6887 31.6852 23.2671C31.0178 23.5118 30.2614 23.1559 30.0167 22.4885C29.772 21.8212 30.128 21.0648 30.7953 20.8423C32.9309 20.0637 35.289 20.0637 37.4245 20.8423C38.0919 21.087 38.4478 21.8212 38.2031 22.4885Z" fill="#BE123C"/>
46
+ </svg>
47
+ ); // <-- اطمینان از بسته شدن صحیح پرانتز و سمیکالن
48
 
49
  const AppInternalLogic: React.FC<{
50
  isMicActive: boolean;
 
56
  notificationButtonRef: React.RefObject<HTMLButtonElement>;
57
  isNotificationOpen: boolean;
58
  setIsNotificationOpen: React.Dispatch<React.SetStateAction<boolean>>;
59
+ currentFacingMode: 'user' | 'environment'; // این پراپ برای نمایش ویدیو استفاده می‌شود
60
  }> = ({
61
  isMicActive,
62
  isCamActive,
 
67
  notificationButtonRef,
68
  isNotificationOpen,
69
  setIsNotificationOpen,
70
+ currentFacingMode, // دریافت پراپ
71
  }) => {
72
  const { connected, disconnect } = useLiveAPIContext();
73
 
 
100
  </div>
101
  </div>
102
 
103
+ <div ref={notificationPopoverRef} id="notification-popover-wrapper" className="notification-popover-wrapper">
104
+ <div
105
+ id="notification-popover"
106
+ className={cn("popover-content", {
107
+ "open animate-popover-open-top-center": isNotificationOpen,
108
+ "animate-popover-close-top-center": !isNotificationOpen && document.getElementById('notification-popover')?.classList.contains('open'),
109
+ })}
110
+ >
111
+ <div className="notification-popover-text-content">
112
+ مدل‌های هوش مصنوعی می‌توانند اشتباه کنند، صحت اطلاعات مهم را بررسی کنید و از وارد کردن اطلاعات حساس بپرهیزید.
113
+ </div>
114
+ </div>
115
+ </div>
116
 
117
  <div className="media-area w-full flex flex-col items-center justify-center flex-grow relative">
118
  <video
 
122
  playsInline
123
  className={cn(
124
  "absolute top-0 left-0 w-full h-full object-cover",
125
+ { "scale-x-[-1]": currentFacingMode === 'user' }, // آینه‌ای شدن فقط برای دوربین جلو
 
126
  { "hidden": !isCamActive }
127
  )}
128
  />
129
  {isMicActive && !isCamActive && (
130
+ <div
131
+ id="large-logo-container"
132
+ className="absolute top-0 left-0 w-full h-full flex items-center justify-center pointer-events-none"
133
+ >
134
  <LogoAnimation isMini={false} isActive={true} type="human" />
135
  </div>
136
  )}
137
  </div>
138
+ {/* ControlTray در زیر AppInternalLogic و در همان سطح IOSModal قرار می‌گیرد */}
139
+ {/* و state های لازم را از App دریافت می‌کند */}
 
 
 
 
 
 
 
 
 
 
140
  </div>
141
  </div>
142
  );
143
  }
144
 
145
  function App() {
146
+ const videoRef = useRef<HTMLVideoElement>(null); // این ref به AppInternalLogic و ControlTray پاس داده می‌شود
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  const [showIOSModal, setShowIOSModal] = useState(false);
148
  const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(null);
149
 
150
  const [isMicActive, setIsMicActive] = useState(false);
151
  const [isCamActive, setIsCamActive] = useState(false);
152
  const [isNotificationOpen, setIsNotificationOpen] = useState(false);
153
+ const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment'>('user'); // State برای جهت دوربین
154
 
155
  const notificationButtonRef = useRef<HTMLButtonElement>(null);
156
  const notificationPopoverRef = useRef<HTMLDivElement>(null);
157
 
158
+ useEffect(() => {
159
+ if (isIOS()) {
160
+ setShowIOSModal(true);
161
+ }
162
+ const timer = setTimeout(() => {
163
+ setIsAllowedOrigin(true);
164
+ }, 100);
165
+ return () => clearTimeout(timer);
166
+ }, []);
167
 
168
+ useEffect(() => {
169
+ const handleClickOutside = (event: MouseEvent) => {
170
+ if (
171
+ isNotificationOpen &&
172
+ notificationPopoverRef.current &&
173
+ !notificationPopoverRef.current.contains(event.target as Node) &&
174
+ notificationButtonRef.current &&
175
+ !notificationButtonRef.current.contains(event.target as Node)
176
+ ) {
177
+ setIsNotificationOpen(false);
178
+ }
179
+ };
180
+ document.addEventListener("mousedown", handleClickOutside);
181
+ return () => {
182
+ document.removeEventListener("mousedown", handleClickOutside);
183
+ };
184
+ }, [isNotificationOpen]);
185
+
186
+ if (isAllowedOrigin === null) {
187
+ return <div style={{ padding: '20px', textAlign: 'center' }}>در حال بررسی دسترسی...</div>;
188
+ }
189
 
190
  return (
191
  <LiveAPIProvider initialConfig={initialAppConfig}>
192
+ {/* AppInternalLogic دیگر ControlTray را رندر نمی‌کند */}
193
  <AppInternalLogic
194
  isMicActive={isMicActive}
195
  setIsMicActive={setIsMicActive}
 
200
  notificationButtonRef={notificationButtonRef}
201
  isNotificationOpen={isNotificationOpen}
202
  setIsNotificationOpen={setIsNotificationOpen}
203
+ currentFacingMode={currentFacingMode} // پاس دادن حالت دوربین
204
  />
205
+ {/* ControlTray به عنوان یک کامپوننت هم‌سطح AppInternalLogic رندر می‌شود */}
206
+ <ControlTray
207
+ videoRef={videoRef} // استفاده از همان ref برای ویدیو
 
 
 
 
 
 
 
208
  supportsVideo={true}
209
+ onVideoStreamChange={(stream) => {
210
+ if (videoRef.current && stream) {
211
+ videoRef.current.srcObject = stream;
212
+ } else if (videoRef.current) {
213
+ videoRef.current.srcObject = null;
214
+ }
215
+ }}
216
  isAppMicActive={isMicActive}
217
  onAppMicToggle={setIsMicActive}
218
  isAppCamActive={isCamActive}
219
  onAppCamToggle={setIsCamActive}
220
  ReferenceMicrophoneIcon={SvgReferenceMicrophoneIcon}
221
+ initialFacingMode={currentFacingMode} // پاس دادن مقدار اولیه
222
+ onFacingModeChange={setCurrentFacingMode} // پاس دادن تابع برای آپدیت state
 
223
  />
224
  <IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} />
225
  </LiveAPIProvider>
226
  );
227
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
+ export default App;