From 7865609caa1bd487e2a1fcee1b5bcf63585980c2 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Fri, 21 Jun 2024 22:20:08 +0530 Subject: [PATCH] feat: global info context --- src/context/globalInfo.tsx | 83 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/context/globalInfo.tsx diff --git a/src/context/globalInfo.tsx b/src/context/globalInfo.tsx new file mode 100644 index 00000000..766996ce --- /dev/null +++ b/src/context/globalInfo.tsx @@ -0,0 +1,83 @@ +import React, { createContext, useContext, useState } from "react"; +import { AlertSnackbarProps } from "../components/atoms/AlertSnackbar"; + + +interface GlobalInfo { + browserId: string | null; + setBrowserId: (newId: string | null) => void; + lastAction: string; + setLastAction: (action: string ) => void; + notification: AlertSnackbarProps; + notify: (severity: 'error' | 'warning' | 'info' | 'success', message: string) => void; + closeNotify: () => void; + recordings: string[]; + setRecordings: (recordings: string[]) => void; + rerenderRuns: boolean; + setRerenderRuns: (rerenderRuns: boolean) => void; + recordingLength: number; + setRecordingLength: (recordingLength: number) => void; +}; + +class GlobalInfoStore implements Partial{ + browserId = null; + lastAction = ''; + recordingLength = 0; + notification: AlertSnackbarProps = { + severity: 'info', + message: '', + isOpen: false, + }; + recordings: string[] = []; + rerenderRuns = false; +}; + +const globalInfoStore = new GlobalInfoStore(); +const globalInfoContext = createContext(globalInfoStore as GlobalInfo); + +export const useGlobalInfoStore = () => useContext(globalInfoContext); + +export const GlobalInfoProvider = ({ children }: { children: JSX.Element }) => { + const [browserId, setBrowserId] = useState(globalInfoStore.browserId); + const [lastAction, setLastAction] = useState(globalInfoStore.lastAction); + const [notification, setNotification] = useState(globalInfoStore.notification); + const [recordings, setRecordings] = useState(globalInfoStore.recordings); + const [rerenderRuns, setRerenderRuns] = useState(globalInfoStore.rerenderRuns); + const [recordingLength, setRecordingLength] = useState(globalInfoStore.recordingLength); + + const notify = (severity: 'error' | 'warning' | 'info' | 'success', message: string) => { + setNotification({severity, message, isOpen: true}); + } + + const closeNotify = () => { + setNotification( globalInfoStore.notification); + } + + const setBrowserIdWithValidation = (browserId: string | null) => { + setBrowserId(browserId); + if (!browserId) { + setRecordingLength(0); + } + } + + return ( + + {children} + + ); +};