Ezmary commited on
Commit
a306201
·
verified ·
1 Parent(s): 848947d

Update src/contexts/LiveAPIContext.tsx

Browse files
Files changed (1) hide show
  1. src/contexts/LiveAPIContext.tsx +40 -51
src/contexts/LiveAPIContext.tsx CHANGED
@@ -1,71 +1,60 @@
1
  /**
2
- * Copyright 2024 Google LLC
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
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}>
58
- {children}
59
- </LiveAPIContext.Provider>
60
- );
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
  };
 
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
  // --- 👇 ایمپورت‌های لازم اضافه شد 👇 ---
14
  import React, { createContext, FC, type ReactNode, useContext, useEffect } from "react"; // React و useEffect اضافه شد
15
+ import { useLiveAPI, type UseLiveAPIResults } from "../hooks/use-live-api"; // مسیر "../hooks/use-live-api" را بررسی کنید
16
+ import { LiveConfig } from "../multimodal-live-types"; // مسیر "../multimodal-live-types" را بررسی کنید
17
  // --- 👆 پایان ایمپورت‌های اضافه شده 👆 ---
18
 
19
  const LiveAPIContext = createContext<UseLiveAPIResults | undefined>(undefined);
20
 
21
  // --- 👇 پراپرتی initialConfig به Props اضافه شد 👇 ---
22
  export type LiveAPIProviderProps = {
23
+ children: ReactNode;
24
+ url?: string;
25
+ initialConfig?: LiveConfig; // این پراپرتی برای دریافت تنظیمات اولیه از App.tsx است
26
  };
27
  // --- 👆 پایان تغییر Props 👆 ---
28
 
29
  export const LiveAPIProvider: FC<LiveAPIProviderProps> = ({
30
+ url = process.env.NODE_ENV === 'development'
31
+ ? `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//localhost:3001/ws` // مطمئن شوید پورت ۳۰۰۱ صحیح است
32
+ : `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}/ws`,
33
+ initialConfig, // <-- initialConfig از props گرفته شد
34
+ children,
35
  }) => {
36
+ const liveAPI = useLiveAPI({ url });
 
 
37
 
38
+ // --- 👇 استفاده از useEffect برای اعمال initialConfig 👇 ---
39
+ useEffect(() => {
40
+ if (initialConfig && liveAPI.setConfig) {
41
+ console.log("Applying initial config from Provider:", initialConfig);
42
+ liveAPI.setConfig(initialConfig);
43
+ }
44
+ }, [initialConfig, liveAPI.setConfig]);
45
+ // --- 👆 پایان بخش useEffect 👆 ---
 
 
 
46
 
47
+ return (
48
+ <LiveAPIContext.Provider value={liveAPI}>
49
+ {children}
50
+ </LiveAPIContext.Provider>
51
+ );
52
  };
53
 
54
  export const useLiveAPIContext = () => {
55
+ const context = useContext(LiveAPIContext);
56
+ if (!context) {
57
+ throw new Error("useLiveAPIContext must be used within a LiveAPIProvider");
58
+ }
59
+ return context;
 
 
60
  };