import { useState, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { TextField, Box } from "@mui/material"; import { useGlobalInfoStore } from "../../../context/globalInfo"; import { getStoredRecording } from "../../../api/storage"; import { WhereWhatPair } from "maxun-core"; import { getUserById } from "../../../api/auth"; import { RobotConfigPage } from "./RobotConfigPage"; import { useNavigate, useLocation } from "react-router-dom"; interface RobotMeta { name: string; id: string; createdAt: string; pairs: number; updatedAt: string; params: any[]; type?: 'extract' | 'scrape' | 'crawl' | 'search'; url?: string; formats?: ('markdown' | 'html' | 'screenshot-visible' | 'screenshot-fullpage')[]; isLLM?: boolean; } 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 RobotSettingsProps { handleStart: (settings: RobotSettings) => void; } export const RobotSettingsPage = ({ handleStart }: RobotSettingsProps) => { const { t } = useTranslation(); const navigate = useNavigate(); const location = useLocation(); const [userEmail, setUserEmail] = useState(null); const [robot, setRobot] = useState(null); const { recordingId, notify } = useGlobalInfoStore(); useEffect(() => { getRobot(); }, []); const getRobot = async () => { if (recordingId) { try { const robot = await getStoredRecording(recordingId); setRobot(robot); } catch (error) { notify("error", t("robot_settings.errors.robot_not_found")); } } else { notify("error", t("robot_settings.errors.robot_not_found")); } }; const getTargetUrl = () => { let url = robot?.recording_meta.url; if (!url) { const lastPair = robot?.recording.workflow[robot?.recording.workflow.length - 1]; url = lastPair?.what.find((action) => action.action === "goto") ?.args?.[0]; } return url; }; useEffect(() => { const fetchUserEmail = async () => { if (robot && robot.userId) { try { const userData = await getUserById(robot.userId.toString()); if (userData && userData.user) { setUserEmail(userData.user.email); } } catch (error) { console.error("Failed to fetch user email:", error); } } }; fetchUserEmail(); }, [robot?.userId]); const targetUrl = getTargetUrl(); return ( <> {robot && ( <> {robot.recording_meta.type !== 'search' && ( )} {(() => { let listCounter = 1; return robot.recording.workflow.flatMap((wf, wfIndex) => wf.what.flatMap((action, actionIndex) => { const argsWithLimit = action.args?.filter( (arg: any) => arg && typeof arg === "object" && arg.limit !== undefined ); if (!argsWithLimit?.length) return []; return argsWithLimit.map((arg, limitIndex) => { const labelName = action.name || `List ${listCounter++}`; return ( ); }); }) ); })()} )} ); };