Ezmary commited on
Commit
6eb6278
·
verified ·
1 Parent(s): 7da7877

Update src/contexts/LiveAPIContext.tsx

Browse files
Files changed (1) hide show
  1. src/contexts/LiveAPIContext.tsx +61 -45
src/contexts/LiveAPIContext.tsx CHANGED
@@ -1,60 +1,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
- 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
  };
 
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
  };