import React, { useState, useContext, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { useNavigate } from 'react-router-dom'; import { Button, Grid, LinearProgress, Typography, Alert, Select, MenuItem, Box, useTheme, type SelectChangeEvent, } from '@mui/material'; import { Key, Bolt, Add, DeleteSweep, Download } from '@mui/icons-material'; import RobotAvatar from '../../components/RobotAvatar'; import TokenInput from './TokenInput'; import { type Slot, type Robot } from '../../models'; import { AppContext, type UseAppStoreType } from '../../contexts/AppContext'; import { genBase62Token } from '../../utils'; import { LoadingButton } from '@mui/lab'; import { GarageContext, type UseGarageStoreType } from '../../contexts/GarageContext'; import { type UseFederationStoreType, FederationContext } from '../../contexts/FederationContext'; interface RobotProfileProps { robot: Robot; setRobot: (state: Robot) => void; setView: (state: 'welcome' | 'onboarding' | 'recovery' | 'profile') => void; inputToken: string; setInputToken: (state: string) => void; width: number; baseUrl: string; } const RobotProfile = ({ inputToken, setInputToken, setView, width, }: RobotProfileProps): JSX.Element => { const { windowSize, client, setOpen } = useContext(AppContext); const { garage, slotUpdatedAt } = useContext(GarageContext); const { federation } = useContext(FederationContext); const { t } = useTranslation(); const theme = useTheme(); const navigate = useNavigate(); const [loading, setLoading] = useState(true); useEffect(() => { const slot = garage.getSlot(); if (slot?.hashId) { setLoading(false); } }, [slotUpdatedAt, loading]); const handleAddRobot = (): void => { const token = genBase62Token(36); void garage.createRobot(federation, token); setInputToken(token); setLoading(true); }; const handleChangeSlot = (e: SelectChangeEvent): void => { if (e?.target?.value) { garage.setCurrentSlot(e.target.value as string); setInputToken(garage.getSlot()?.token ?? ''); setLoading(true); } }; const slot = garage.getSlot(); const robot = slot?.getRobot(); return ( {slot?.nickname ? (
{width < 19 ? null : ( )} {slot?.nickname} {width < 19 ? null : ( )}
) : ( <> {t('Building your robot!')} )}
{robot?.found && Boolean(slot?.lastOrder?.id) ? ( {t('Welcome back!')} ) : ( <> )} {federation.loading && !slot?.activeOrder?.id ? ( {t('Looking for orders!')} ) : null} {slot?.activeOrder ? ( ) : null} {!slot?.activeOrder?.id && Boolean(slot?.lastOrder?.id) ? ( {t( 'Reusing trading identity degrades your privacy against other users, coordinators and observers.', )} ) : null} {!slot?.activeOrder && !slot?.lastOrder && !federation.loading ? ( {t('No existing orders found')} ) : null} null} />
{t('Robot Garage')}
{t('Add Robot')} {client !== 'mobile' ? ( ) : null} ); }; export default RobotProfile;