Spaces:
Running
Running
Update src/contexts/LiveAPIContext.tsx
Browse files
src/contexts/LiveAPIContext.tsx
CHANGED
@@ -14,24 +14,45 @@
|
|
14 |
* limitations under the License.
|
15 |
*/
|
16 |
|
17 |
-
|
|
|
18 |
import { useLiveAPI, type UseLiveAPIResults } from "../hooks/use-live-api";
|
|
|
|
|
19 |
|
20 |
const LiveAPIContext = createContext<UseLiveAPIResults | undefined>(undefined);
|
21 |
|
|
|
22 |
export type LiveAPIProviderProps = {
|
23 |
children: ReactNode;
|
24 |
url?: string;
|
|
|
25 |
};
|
|
|
26 |
|
27 |
export const LiveAPIProvider: FC<LiveAPIProviderProps> = ({
|
28 |
-
url = process.env.NODE_ENV === 'development'
|
29 |
? `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//localhost:3001/ws`
|
30 |
: `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}/ws`,
|
|
|
31 |
children,
|
32 |
}) => {
|
|
|
|
|
33 |
const liveAPI = useLiveAPI({ url });
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
return (
|
36 |
<LiveAPIContext.Provider value={liveAPI}>
|
37 |
{children}
|
@@ -40,11 +61,11 @@ export const LiveAPIProvider: FC<LiveAPIProviderProps> = ({
|
|
40 |
};
|
41 |
|
42 |
export const useLiveAPIContext = () => {
|
43 |
-
console.log('🎯 LiveAPIContext: Hook being accessed');
|
44 |
const context = useContext(LiveAPIContext);
|
45 |
if (!context) {
|
46 |
throw new Error("useLiveAPIContext must be used within a LiveAPIProvider");
|
47 |
}
|
48 |
-
console.log('✅ LiveAPIContext successfully retrieved');
|
49 |
return context;
|
50 |
-
};
|
|
|
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}
|
|
|
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 |
+
};
|