Files
parcer/src/context/globalInfo.tsx

196 lines
6.4 KiB
TypeScript
Raw Normal View History

2024-06-21 22:20:08 +05:30
import React, { createContext, useContext, useState } from "react";
2025-01-09 19:49:20 +05:30
import { AlertSnackbarProps } from "../components/ui/AlertSnackbar";
2025-01-30 23:25:13 +05:30
import { WhereWhatPair } from "maxun-core";
2024-06-21 22:20:08 +05:30
2025-01-30 23:25:13 +05:30
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;
}
2024-06-21 22:20:08 +05:30
interface GlobalInfo {
browserId: string | null;
setBrowserId: (newId: string | null) => void;
lastAction: string;
2024-06-21 22:20:26 +05:30
setLastAction: (action: string) => void;
2024-06-21 22:20:08 +05:30
notification: AlertSnackbarProps;
notify: (severity: 'error' | 'warning' | 'info' | 'success', message: string) => void;
closeNotify: () => void;
isLogin: boolean;
setIsLogin: (isLogin: boolean) => void;
2024-06-21 22:20:08 +05:30
recordings: string[];
setRecordings: (recordings: string[]) => void;
rerenderRuns: boolean;
setRerenderRuns: (rerenderRuns: boolean) => void;
2025-02-01 11:39:43 +05:30
rerenderRobots: boolean;
setRerenderRobots: (rerenderRuns: boolean) => void;
2024-06-21 22:20:08 +05:30
recordingLength: number;
setRecordingLength: (recordingLength: number) => void;
2024-10-09 23:10:06 +05:30
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;
2024-10-11 04:51:19 +05:30
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;
2024-06-21 22:20:08 +05:30
};
2024-06-21 22:20:26 +05:30
class GlobalInfoStore implements Partial<GlobalInfo> {
2024-06-21 22:20:08 +05:30
browserId = null;
lastAction = '';
recordingLength = 0;
notification: AlertSnackbarProps = {
severity: 'info',
message: '',
isOpen: false,
};
2024-10-09 23:10:06 +05:30
recordingId = null;
retrainRobotId = null;
2024-06-21 22:20:08 +05:30
recordings: string[] = [];
rerenderRuns = false;
2025-02-01 11:39:43 +05:30
rerenderRobots = false;
recordingName = '';
initialUrl = 'https://';
2024-10-11 05:01:01 +05:30
recordingUrl = 'https://';
isLogin = false;
currentWorkflowActionsState = {
hasScrapeListAction: false,
hasScreenshotAction: false,
hasScrapeSchemaAction: false,
};
shouldResetInterpretationLog = false;
2024-06-21 22:20:08 +05:30
};
const globalInfoStore = new GlobalInfoStore();
const globalInfoContext = createContext<GlobalInfo>(globalInfoStore as GlobalInfo);
export const useGlobalInfoStore = () => useContext(globalInfoContext);
export const GlobalInfoProvider = ({ children }: { children: JSX.Element }) => {
const [browserId, setBrowserId] = useState<string | null>(globalInfoStore.browserId);
const [lastAction, setLastAction] = useState<string>(globalInfoStore.lastAction);
const [notification, setNotification] = useState<AlertSnackbarProps>(globalInfoStore.notification);
const [recordings, setRecordings] = useState<string[]>(globalInfoStore.recordings);
const [rerenderRuns, setRerenderRuns] = useState<boolean>(globalInfoStore.rerenderRuns);
2025-02-01 11:39:43 +05:30
const [rerenderRobots, setRerenderRobots] = useState<boolean>(globalInfoStore.rerenderRobots);
2024-06-21 22:20:08 +05:30
const [recordingLength, setRecordingLength] = useState<number>(globalInfoStore.recordingLength);
2024-10-09 23:10:06 +05:30
const [recordingId, setRecordingId] = useState<string | null>(globalInfoStore.recordingId);
const [retrainRobotId, setRetrainRobotId] = useState<string | null>(globalInfoStore.retrainRobotId);
const [recordingName, setRecordingName] = useState<string>(globalInfoStore.recordingName);
const [isLogin, setIsLogin] = useState<boolean>(globalInfoStore.isLogin);
const [initialUrl, setInitialUrl] = useState<string>(globalInfoStore.initialUrl);
2024-10-11 04:51:19 +05:30
const [recordingUrl, setRecordingUrl] = useState<string>(globalInfoStore.recordingUrl);
const [currentWorkflowActionsState, setCurrentWorkflowActionsState] = useState(globalInfoStore.currentWorkflowActionsState);
const [shouldResetInterpretationLog, setShouldResetInterpretationLog] = useState<boolean>(globalInfoStore.shouldResetInterpretationLog);
2024-06-21 22:20:08 +05:30
const notify = (severity: 'error' | 'warning' | 'info' | 'success', message: string) => {
2024-06-21 22:20:26 +05:30
setNotification({ severity, message, isOpen: true });
2024-06-21 22:20:08 +05:30
}
const closeNotify = () => {
2024-06-21 22:20:26 +05:30
setNotification(globalInfoStore.notification);
2024-06-21 22:20:08 +05:30
}
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);
}
2024-06-21 22:20:08 +05:30
return (
<globalInfoContext.Provider
value={{
browserId,
setBrowserId: setBrowserIdWithValidation,
lastAction,
setLastAction,
notification,
notify,
closeNotify,
recordings,
setRecordings,
rerenderRuns,
setRerenderRuns,
2025-02-01 11:39:43 +05:30
rerenderRobots,
setRerenderRobots,
2024-06-21 22:20:08 +05:30
recordingLength,
setRecordingLength,
2024-10-09 23:10:06 +05:30
recordingId,
setRecordingId,
retrainRobotId,
setRetrainRobotId,
recordingName,
2024-10-11 04:51:19 +05:30
setRecordingName,
initialUrl,
setInitialUrl,
2024-10-11 04:51:19 +05:30
recordingUrl,
setRecordingUrl,
isLogin,
setIsLogin,
currentWorkflowActionsState,
setCurrentWorkflowActionsState,
shouldResetInterpretationLog,
resetInterpretationLog,
2024-06-21 22:20:08 +05:30
}}
>
{children}
</globalInfoContext.Provider>
);
};