Spaces:
Running
Running
Update src/contexts/LiveAPIContext.tsx
Browse files- src/contexts/LiveAPIContext.tsx +40 -51
src/contexts/LiveAPIContext.tsx
CHANGED
@@ -1,71 +1,60 @@
|
|
1 |
/**
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
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"; //
|
21 |
// --- 👆 پایان ایمپورتهای اضافه شده 👆 ---
|
22 |
|
23 |
const LiveAPIContext = createContext<UseLiveAPIResults | undefined>(undefined);
|
24 |
|
25 |
// --- 👇 پراپرتی initialConfig به Props اضافه شد 👇 ---
|
26 |
export type LiveAPIProviderProps = {
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
};
|
31 |
// --- 👆 پایان تغییر Props 👆 ---
|
32 |
|
33 |
export const LiveAPIProvider: FC<LiveAPIProviderProps> = ({
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
}) => {
|
40 |
-
|
41 |
-
// توجه: فرض شده که apiKey در جای دیگری مدیریت میشود یا نیاز نیست
|
42 |
-
const liveAPI = useLiveAPI({ url });
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
// setConfig معمولاً تغییر نمیکند، اما برای کامل بودن در dependency array قرار میگیرد
|
53 |
-
}, [initialConfig, liveAPI.setConfig]);
|
54 |
-
// --- 👆 پایان بخش useEffect 👆 ---
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
};
|
62 |
|
63 |
export const useLiveAPIContext = () => {
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
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 |
};
|