Spaces:
Running
Running
Update src/contexts/LiveAPIContext.tsx
Browse files- src/contexts/LiveAPIContext.tsx +33 -41
src/contexts/LiveAPIContext.tsx
CHANGED
|
@@ -1,76 +1,68 @@
|
|
| 1 |
/**
|
| 2 |
Copyright 2024 Google LLC
|
| 3 |
-
(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
*/
|
| 5 |
-
import React, { createContext, FC, type ReactNode, useContext, useEffect, useState, useCallback } from "react"; // useState, useCallback added
|
| 6 |
-
import { useLiveAPI, type UseLiveAPIResults as OriginalUseLiveAPIResults } from "../hooks/use-live-api";
|
| 7 |
-
import { LiveConfig } from "../multimodal-live-types";
|
| 8 |
-
|
| 9 |
-
// Extend UseLiveAPIResults to include new functions
|
| 10 |
-
export type UseLiveAPIResults = OriginalUseLiveAPIResults & {
|
| 11 |
-
currentFacingMode?: 'user' | 'environment' | null;
|
| 12 |
-
setCurrentFacingMode?: React.Dispatch<React.SetStateAction<'user' | 'environment' | null>>;
|
| 13 |
-
rotateWebcam?: () => Promise<void>;
|
| 14 |
-
changeStreams?: (streamType: 'webcam' | 'screen' | 'none') => void;
|
| 15 |
-
// Allow updating the live config from ControlTray (or other components)
|
| 16 |
-
updateLiveConfig?: (updates: Partial<UseLiveAPIResults>) => void;
|
| 17 |
-
};
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
const LiveAPIContext = createContext<UseLiveAPIResults | undefined>(undefined);
|
| 21 |
|
|
|
|
| 22 |
export type LiveAPIProviderProps = {
|
| 23 |
children: ReactNode;
|
| 24 |
url?: string;
|
| 25 |
-
initialConfig?: LiveConfig;
|
| 26 |
};
|
|
|
|
| 27 |
|
| 28 |
export const LiveAPIProvider: FC<LiveAPIProviderProps> = ({
|
| 29 |
url = process.env.NODE_ENV === 'development'
|
| 30 |
? `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//localhost:3001/ws`
|
| 31 |
: `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}/ws`,
|
| 32 |
-
initialConfig,
|
| 33 |
children,
|
| 34 |
}) => {
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment' | null>(null);
|
| 39 |
-
const [dynamicConfig, setDynamicConfig] = useState<Partial<UseLiveAPIResults>>({});
|
| 40 |
-
|
| 41 |
-
const updateLiveConfig = useCallback((updates: Partial<UseLiveAPIResults>) => {
|
| 42 |
-
setDynamicConfig(prev => ({ ...prev, ...updates }));
|
| 43 |
-
}, []);
|
| 44 |
-
|
| 45 |
|
|
|
|
|
|
|
| 46 |
useEffect(() => {
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
}
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
...liveAPIOriginal,
|
| 55 |
-
currentFacingMode,
|
| 56 |
-
setCurrentFacingMode,
|
| 57 |
-
updateLiveConfig,
|
| 58 |
-
// Spread dynamic functions if they are provided by a component like ControlTray
|
| 59 |
-
...(dynamicConfig.rotateWebcam && { rotateWebcam: dynamicConfig.rotateWebcam }),
|
| 60 |
-
...(dynamicConfig.changeStreams && { changeStreams: dynamicConfig.changeStreams }),
|
| 61 |
-
};
|
| 62 |
|
| 63 |
return (
|
| 64 |
-
<LiveAPIContext.Provider value={
|
| 65 |
{children}
|
| 66 |
</LiveAPIContext.Provider>
|
| 67 |
);
|
| 68 |
};
|
| 69 |
|
| 70 |
export const useLiveAPIContext = () => {
|
|
|
|
| 71 |
const context = useContext(LiveAPIContext);
|
| 72 |
if (!context) {
|
| 73 |
throw new Error("useLiveAPIContext must be used within a LiveAPIProvider");
|
| 74 |
}
|
|
|
|
| 75 |
return context;
|
| 76 |
};
|
|
|
|
| 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 |
+
// --- 👇 ایمپورتهای لازم اضافه شد 👇 ---
|
| 15 |
+
import React, { createContext, FC, type ReactNode, useContext, useEffect } from "react"; // React و useEffect اضافه شد
|
| 16 |
+
import { useLiveAPI, type UseLiveAPIResults } from "../hooks/use-live-api";
|
| 17 |
+
import { LiveConfig } from "../multimodal-live-types"; // LiveConfig ایمپورت شد
|
| 18 |
+
// --- 👆 پایان ایمپورتهای اضافه شده 👆 ---
|
| 19 |
|
| 20 |
const LiveAPIContext = createContext<UseLiveAPIResults | undefined>(undefined);
|
| 21 |
|
| 22 |
+
// --- 👇 پراپرتی initialConfig به Props اضافه شد 👇 ---
|
| 23 |
export type LiveAPIProviderProps = {
|
| 24 |
children: ReactNode;
|
| 25 |
url?: string;
|
| 26 |
+
initialConfig?: LiveConfig; // این پراپرتی برای دریافت تنظیمات اولیه از App.tsx است
|
| 27 |
};
|
| 28 |
+
// --- 👆 پایان تغییر Props 👆 ---
|
| 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, // <-- initialConfig از props گرفته شد
|
| 35 |
children,
|
| 36 |
}) => {
|
| 37 |
+
// هوک useLiveAPI مثل قبل فراخوانی میشود
|
| 38 |
+
// توجه: فرض شده که apiKey در جای دیگری مدیریت میشود یا نیاز نیست
|
| 39 |
+
const liveAPI = useLiveAPI({ url });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
// --- 👇 استفاده از useEffect برای اعمال initialConfig 👇 ---
|
| 42 |
+
// این effect یک بار بعد از اولین رندر (و اگر initialConfig تغییر کند) اجرا میشود
|
| 43 |
useEffect(() => {
|
| 44 |
+
// فقط اگر initialConfig پاس داده شده باشد و تابع setConfig در دسترس باشد، آن را اعمال میکنیم
|
| 45 |
+
if (initialConfig && liveAPI.setConfig) {
|
| 46 |
+
console.log("Applying initial config from Provider:", initialConfig); // برای دیباگ
|
| 47 |
+
liveAPI.setConfig(initialConfig); // تنظیمات اولیه پاس داده شده از App.tsx را اعمال میکنیم
|
| 48 |
}
|
| 49 |
+
// setConfig معمولاً تغییر نمیکند، اما برای کامل بودن در dependency array قرار میگیرد
|
| 50 |
+
}, [initialConfig, liveAPI.setConfig]);
|
| 51 |
+
// --- 👆 پایان بخش useEffect 👆 ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
return (
|
| 54 |
+
<LiveAPIContext.Provider value={liveAPI}>
|
| 55 |
{children}
|
| 56 |
</LiveAPIContext.Provider>
|
| 57 |
);
|
| 58 |
};
|
| 59 |
|
| 60 |
export const useLiveAPIContext = () => {
|
| 61 |
+
// console.log('🎯 LiveAPIContext: Hook being accessed'); // لاگها رو حذف میکنیم یا نگه میداریم
|
| 62 |
const context = useContext(LiveAPIContext);
|
| 63 |
if (!context) {
|
| 64 |
throw new Error("useLiveAPIContext must be used within a LiveAPIProvider");
|
| 65 |
}
|
| 66 |
+
// console.log('✅ LiveAPIContext successfully retrieved');
|
| 67 |
return context;
|
| 68 |
};
|