import React, { useState, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { TextField, Typography, Box, Card, CardContent } from "@mui/material"; import { Settings, Info } from "@mui/icons-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[]; url?: string; } 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 handleCancel = () => { const basePath = location.pathname.includes("/prebuilt-robots") ? "/prebuilt-robots" : "/robots"; navigate(basePath); }; const targetUrl = getTargetUrl(); return ( <> {robot && ( <> {robot.recording.workflow?.[0]?.what?.[0]?.args?.[0]?.limit !== undefined && ( )} )} ); };