import React, { createContext, useContext, useState } from "react"; import { AlertSnackbarProps } from "../components/ui/AlertSnackbar"; import { WhereWhatPair } from "maxun-core"; interface RobotMeta { name: string; id: string; createdAt: string; pairs: number; updatedAt: string; params: any[]; } interface RobotWorkflow { workflow: WhereWhatPair[]; } interface ScheduleConfig { runEvery: number; runEveryUnit: 'MINUTES' | 'HOURS' | 'DAYS' | 'WEEKS' | 'MONTHS'; startFrom: 'SUNDAY' | 'MONDAY' | 'TUESDAY' | 'WEDNESDAY' | 'THURSDAY' | 'FRIDAY' | 'SATURDAY'; atTimeStart?: string; atTimeEnd?: string; timezone: string; lastRunAt?: Date; nextRunAt?: Date; cronExpression?: string; } export interface RobotSettings { id: string; userId?: number; recording_meta: RobotMeta; recording: RobotWorkflow; google_sheet_email?: string | null; google_sheet_name?: string | null; google_sheet_id?: string | null; google_access_token?: string | null; google_refresh_token?: string | null; schedule?: ScheduleConfig | null; } 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; isLogin: boolean; setIsLogin: (isLogin: boolean) => void; recordings: string[]; setRecordings: (recordings: string[]) => void; rerenderRuns: boolean; setRerenderRuns: (rerenderRuns: boolean) => void; rerenderRobots: boolean; setRerenderRobots: (rerenderRuns: boolean) => void; recordingLength: number; setRecordingLength: (recordingLength: number) => void; recordingId: string | null; setRecordingId: (newId: string | null) => void; retrainRobotId: string | null; setRetrainRobotId: (newId: string | null) => void; recordingName: string; setRecordingName: (recordingName: string) => void; initialUrl: string; setInitialUrl: (initialUrl: string) => void; recordingUrl: string; setRecordingUrl: (recordingUrl: string) => void; currentWorkflowActionsState: { hasScrapeListAction: boolean; hasScreenshotAction: boolean; hasScrapeSchemaAction: boolean; }; setCurrentWorkflowActionsState: (actionsState: { hasScrapeListAction: boolean; hasScreenshotAction: boolean; hasScrapeSchemaAction: boolean; }) => void; shouldResetInterpretationLog: boolean; resetInterpretationLog: () => void; }; class GlobalInfoStore implements Partial { browserId = null; lastAction = ''; recordingLength = 0; notification: AlertSnackbarProps = { severity: 'info', message: '', isOpen: false, }; recordingId = null; retrainRobotId = null; recordings: string[] = []; rerenderRuns = false; rerenderRobots = false; recordingName = ''; initialUrl = 'https://'; recordingUrl = 'https://'; isLogin = false; currentWorkflowActionsState = { hasScrapeListAction: false, hasScreenshotAction: false, hasScrapeSchemaAction: false, }; shouldResetInterpretationLog = 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 [rerenderRobots, setRerenderRobots] = useState(globalInfoStore.rerenderRobots); const [recordingLength, setRecordingLength] = useState(globalInfoStore.recordingLength); const [recordingId, setRecordingId] = useState(globalInfoStore.recordingId); const [retrainRobotId, setRetrainRobotId] = useState(globalInfoStore.retrainRobotId); const [recordingName, setRecordingName] = useState(globalInfoStore.recordingName); const [isLogin, setIsLogin] = useState(globalInfoStore.isLogin); const [initialUrl, setInitialUrl] = useState(globalInfoStore.initialUrl); const [recordingUrl, setRecordingUrl] = useState(globalInfoStore.recordingUrl); const [currentWorkflowActionsState, setCurrentWorkflowActionsState] = useState(globalInfoStore.currentWorkflowActionsState); const [shouldResetInterpretationLog, setShouldResetInterpretationLog] = useState(globalInfoStore.shouldResetInterpretationLog); 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); } } const resetInterpretationLog = () => { setShouldResetInterpretationLog(true); // Reset the flag after a short delay to allow components to respond setTimeout(() => { setShouldResetInterpretationLog(false); }, 100); } return ( {children} ); };