feat: stop browser actions context

This commit is contained in:
karishmas6
2024-07-24 21:34:28 +05:30
parent c2b35a216a
commit d0442381c9

View File

@@ -1,43 +1,37 @@
import React, { createContext, useState, useContext, ReactNode } from 'react';
import React, { createContext, useContext, useState, ReactNode } from 'react';
interface ActionContextType {
interface ActionContextProps {
getText: boolean;
getScreenshot: boolean;
handleGetText: () => void;
handleGetScreenshot: () => void;
resetActions: () => void;
startGetText: () => void;
stopGetText: () => void;
startGetScreenshot: () => void;
stopGetScreenshot: () => void;
}
const ActionContext = createContext<ActionContextType | undefined>(undefined);
const ActionContext = createContext<ActionContextProps | undefined>(undefined);
export const useActionContext = (): ActionContextType => {
export const ActionProvider = ({ children }: { children: ReactNode }) => {
const [getText, setGetText] = useState<boolean>(false);
const [getScreenshot, setGetScreenshot] = useState<boolean>(false);
const startGetText = () => setGetText(true);
const stopGetText = () => setGetText(false);
const startGetScreenshot = () => setGetScreenshot(true);
const stopGetScreenshot = () => setGetScreenshot(false);
return (
<ActionContext.Provider value={{ getText, getScreenshot, startGetText, stopGetText, startGetScreenshot, stopGetScreenshot }}>
{children}
</ActionContext.Provider>
);
};
export const useActionContext = () => {
const context = useContext(ActionContext);
if (context === undefined) {
throw new Error('useActionContext must be used within an ActionProvider');
}
return context;
};
interface ActionProviderProps {
children: ReactNode;
}
export const ActionProvider: React.FC<ActionProviderProps> = ({ children }) => {
const [getText, setGetText] = useState<boolean>(false);
const [getScreenshot, setGetScreenshot] = useState<boolean>(false);
const handleGetText = () => setGetText(true);
const handleGetScreenshot = () => setGetScreenshot(true);
// Reset actions (optional)
const resetActions = () => {
setGetText(false);
setGetScreenshot(false);
};
return (
<ActionContext.Provider value={{ getText, getScreenshot, handleGetText, handleGetScreenshot, resetActions }}>
{children}
</ActionContext.Provider>
);
};