Spaces:
Running
Running
Update src/contexts/LiveAPIContext.tsx
Browse files- src/contexts/LiveAPIContext.tsx +61 -45
src/contexts/LiveAPIContext.tsx
CHANGED
@@ -1,60 +1,76 @@
|
|
1 |
/**
|
2 |
Copyright 2024 Google LLC
|
3 |
-
|
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
|
15 |
-
import {
|
16 |
-
|
17 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
const LiveAPIContext = createContext<UseLiveAPIResults | undefined>(undefined);
|
20 |
|
21 |
-
// --- 👇 پراپرتی initialConfig به Props اضافه شد 👇 ---
|
22 |
export type LiveAPIProviderProps = {
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
};
|
27 |
-
// --- 👆 پایان تغییر Props 👆 ---
|
28 |
|
29 |
export const LiveAPIProvider: FC<LiveAPIProviderProps> = ({
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
}) => {
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
};
|
53 |
|
54 |
export const useLiveAPIContext = () => {
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
};
|
|
|
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 |
+
const liveAPIOriginal = useLiveAPI({ url });
|
36 |
+
|
37 |
+
// State and functions to be managed by context for wider use
|
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 |
+
if (initialConfig && liveAPIOriginal.setConfig) {
|
48 |
+
console.log("Applying initial config from Provider:", initialConfig);
|
49 |
+
liveAPIOriginal.setConfig(initialConfig);
|
50 |
+
}
|
51 |
+
}, [initialConfig, liveAPIOriginal.setConfig]);
|
52 |
+
|
53 |
+
const contextValue: UseLiveAPIResults = {
|
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={contextValue}>
|
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 |
};
|