Spaces:
Running
Running
File size: 2,837 Bytes
7f2a14a a306201 6eb6278 a306201 6eb6278 a3aa287 7f2a14a 6eb6278 7f2a14a 6eb6278 7f2a14a 6eb6278 7f2a14a 6eb6278 a3aa287 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
/**
Copyright 2024 Google LLC
(لایسنس مثل قبل)
*/
import React, { createContext, FC, type ReactNode, useContext, useEffect, useState, useCallback } from "react"; // useState, useCallback added
import { useLiveAPI, type UseLiveAPIResults as OriginalUseLiveAPIResults } from "../hooks/use-live-api";
import { LiveConfig } from "../multimodal-live-types";
// Extend UseLiveAPIResults to include new functions
export type UseLiveAPIResults = OriginalUseLiveAPIResults & {
currentFacingMode?: 'user' | 'environment' | null;
setCurrentFacingMode?: React.Dispatch<React.SetStateAction<'user' | 'environment' | null>>;
rotateWebcam?: () => Promise<void>;
changeStreams?: (streamType: 'webcam' | 'screen' | 'none') => void;
// Allow updating the live config from ControlTray (or other components)
updateLiveConfig?: (updates: Partial<UseLiveAPIResults>) => void;
};
const LiveAPIContext = createContext<UseLiveAPIResults | undefined>(undefined);
export type LiveAPIProviderProps = {
children: ReactNode;
url?: string;
initialConfig?: LiveConfig;
};
export const LiveAPIProvider: FC<LiveAPIProviderProps> = ({
url = process.env.NODE_ENV === 'development'
? `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//localhost:3001/ws`
: `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}/ws`,
initialConfig,
children,
}) => {
const liveAPIOriginal = useLiveAPI({ url });
// State and functions to be managed by context for wider use
const [currentFacingMode, setCurrentFacingMode] = useState<'user' | 'environment' | null>(null);
const [dynamicConfig, setDynamicConfig] = useState<Partial<UseLiveAPIResults>>({});
const updateLiveConfig = useCallback((updates: Partial<UseLiveAPIResults>) => {
setDynamicConfig(prev => ({ ...prev, ...updates }));
}, []);
useEffect(() => {
if (initialConfig && liveAPIOriginal.setConfig) {
console.log("Applying initial config from Provider:", initialConfig);
liveAPIOriginal.setConfig(initialConfig);
}
}, [initialConfig, liveAPIOriginal.setConfig]);
const contextValue: UseLiveAPIResults = {
...liveAPIOriginal,
currentFacingMode,
setCurrentFacingMode,
updateLiveConfig,
// Spread dynamic functions if they are provided by a component like ControlTray
...(dynamicConfig.rotateWebcam && { rotateWebcam: dynamicConfig.rotateWebcam }),
...(dynamicConfig.changeStreams && { changeStreams: dynamicConfig.changeStreams }),
};
return (
<LiveAPIContext.Provider value={contextValue}>
{children}
</LiveAPIContext.Provider>
);
};
export const useLiveAPIContext = () => {
const context = useContext(LiveAPIContext);
if (!context) {
throw new Error("useLiveAPIContext must be used within a LiveAPIProvider");
}
return context;
}; |