Ezmary commited on
Commit
92ea507
·
verified ·
1 Parent(s): ad9a81e

Update src/contexts/LiveAPIContext.tsx

Browse files
Files changed (1) hide show
  1. src/contexts/LiveAPIContext.tsx +26 -8
src/contexts/LiveAPIContext.tsx CHANGED
@@ -14,28 +14,44 @@
14
  * limitations under the License.
15
  */
16
 
17
- import { createContext, FC, type ReactNode, useContext } from "react";
 
18
  import { useLiveAPI, type UseLiveAPIResults } from "../hooks/use-live-api";
19
- import { LiveConfig } from "../multimodal-live-types";
 
20
 
21
  const LiveAPIContext = createContext<UseLiveAPIResults | undefined>(undefined);
22
 
 
23
  export type LiveAPIProviderProps = {
24
  children: ReactNode;
25
  url?: string;
26
- initialConfig?: LiveConfig;
27
- apiKey?: string;
28
  };
 
29
 
30
  export const LiveAPIProvider: FC<LiveAPIProviderProps> = ({
31
- url = process.env.NODE_ENV === 'development'
32
  ? `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//localhost:3001/ws`
33
  : `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}/ws`,
34
- initialConfig,
35
- apiKey,
36
  children,
37
  }) => {
38
- const liveAPI = useLiveAPI({ url, apiKey, initialConfig });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  return (
41
  <LiveAPIContext.Provider value={liveAPI}>
@@ -45,9 +61,11 @@ export const LiveAPIProvider: FC<LiveAPIProviderProps> = ({
45
  };
46
 
47
  export const useLiveAPIContext = () => {
 
48
  const context = useContext(LiveAPIContext);
49
  if (!context) {
50
  throw new Error("useLiveAPIContext must be used within a LiveAPIProvider");
51
  }
 
52
  return context;
53
  };
 
14
  * limitations under the License.
15
  */
16
 
17
+ // --- 👇 ایمپورت‌های لازم اضافه شد 👇 ---
18
+ import React, { createContext, FC, type ReactNode, useContext, useEffect } from "react"; // React و useEffect اضافه شد
19
  import { useLiveAPI, type UseLiveAPIResults } from "../hooks/use-live-api";
20
+ import { LiveConfig } from "../multimodal-live-types"; // LiveConfig ایمپورت شد
21
+ // --- 👆 پایان ایمپورت‌های اضافه شده 👆 ---
22
 
23
  const LiveAPIContext = createContext<UseLiveAPIResults | undefined>(undefined);
24
 
25
+ // --- 👇 پراپرتی initialConfig به Props اضافه شد 👇 ---
26
  export type LiveAPIProviderProps = {
27
  children: ReactNode;
28
  url?: string;
29
+ initialConfig?: LiveConfig; // این پراپرتی برای دریافت تنظیمات اولیه از App.tsx است
 
30
  };
31
+ // --- 👆 پایان تغییر Props 👆 ---
32
 
33
  export const LiveAPIProvider: FC<LiveAPIProviderProps> = ({
34
+ url = process.env.NODE_ENV === 'development'
35
  ? `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//localhost:3001/ws`
36
  : `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}/ws`,
37
+ initialConfig, // <-- initialConfig از props گرفته شد
 
38
  children,
39
  }) => {
40
+ // هوک useLiveAPI مثل قبل فراخوانی می‌شود
41
+ // توجه: فرض شده که apiKey در جای دیگری مدیریت می‌شود یا نیاز نیست
42
+ const liveAPI = useLiveAPI({ url });
43
+
44
+ // --- 👇 استفاده از useEffect برای اعمال initialConfig 👇 ---
45
+ // این effect یک بار بعد از اولین رندر (و اگر initialConfig تغییر کند) اجرا می‌شود
46
+ useEffect(() => {
47
+ // فقط اگر initialConfig پاس داده شده باشد و تابع setConfig در دسترس باشد، آن را اعمال می‌کنیم
48
+ if (initialConfig && liveAPI.setConfig) {
49
+ console.log("Applying initial config from Provider:", initialConfig); // برای دیباگ
50
+ liveAPI.setConfig(initialConfig); // تنظیمات اولیه پاس داده شده از App.tsx را اعمال می‌کنیم
51
+ }
52
+ // setConfig معمولاً تغییر نمی‌کند، اما برای کامل بودن در dependency array قرار می‌گیرد
53
+ }, [initialConfig, liveAPI.setConfig]);
54
+ // --- 👆 پایان بخش useEffect 👆 ---
55
 
56
  return (
57
  <LiveAPIContext.Provider value={liveAPI}>
 
61
  };
62
 
63
  export const useLiveAPIContext = () => {
64
+ // console.log('🎯 LiveAPIContext: Hook being accessed'); // لاگ‌ها رو حذف می‌کنیم یا نگه می‌داریم
65
  const context = useContext(LiveAPIContext);
66
  if (!context) {
67
  throw new Error("useLiveAPIContext must be used within a LiveAPIProvider");
68
  }
69
+ // console.log('✅ LiveAPIContext successfully retrieved');
70
  return context;
71
  };