From 826ec272364fcd483803a2538ef9c8e6cc451707 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Fri, 26 Jul 2024 20:20:11 +0530 Subject: [PATCH] feat: browser steps context --- src/context/browserSteps.tsx | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/context/browserSteps.tsx diff --git a/src/context/browserSteps.tsx b/src/context/browserSteps.tsx new file mode 100644 index 00000000..c1606c0d --- /dev/null +++ b/src/context/browserSteps.tsx @@ -0,0 +1,53 @@ +import React, { createContext, useContext, useState } from 'react'; + +interface BrowserStep { + id: number; + label: string; + description: string; +} + +interface BrowserStepsContextType { + browserSteps: BrowserStep[]; + addBrowserStep: (label: string, description: string) => void; + deleteBrowserStep: (id: number) => void; + updateBrowserStepLabel: (id: number, newLabel: string) => void; +} + +const BrowserStepsContext = createContext(undefined); + +export const BrowserStepsProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { + const [browserSteps, setBrowserSteps] = useState([]); + + const addBrowserStep = (label: string, description: string) => { + setBrowserSteps(prevSteps => [ + ...prevSteps, + { id: Date.now(), label, description } + ]); + }; + + const deleteBrowserStep = (id: number) => { + setBrowserSteps(prevSteps => prevSteps.filter(step => step.id !== id)); + }; + + const updateBrowserStepLabel = (id: number, newLabel: string) => { + setBrowserSteps(prevSteps => + prevSteps.map(step => + step.id === id ? { ...step, label: newLabel } : step + ) + ); + }; + + return ( + + {children} + + ); +}; + +export const useBrowserSteps = () => { + const context = useContext(BrowserStepsContext); + if (!context) { + throw new Error('useBrowserSteps must be used within a BrowserStepsProvider'); + } + return context; +};