From d1b64ddd78d8b8dc3eca76038a75071ab2a575d9 Mon Sep 17 00:00:00 2001 From: Arne Rief Date: Mon, 22 Dec 2025 12:56:20 +0100 Subject: Sidebar split into smaller components --- frontend/src/components/Sidebar.tsx | 234 ++++-------------------------------- 1 file changed, 26 insertions(+), 208 deletions(-) (limited to 'frontend/src/components/Sidebar.tsx') diff --git a/frontend/src/components/Sidebar.tsx b/frontend/src/components/Sidebar.tsx index 2e66865..8bd795b 100644 --- a/frontend/src/components/Sidebar.tsx +++ b/frontend/src/components/Sidebar.tsx @@ -1,116 +1,27 @@ -import { - useState, - type ChangeEvent, - type Dispatch, - type SetStateAction, -} from "react"; -import "../styles/Button.css"; -import "../styles/Sidebar.css"; -import type { ErrorResponse } from "../types/error"; -import type { CreateRobotResponse, Robot } from "../types/robot"; - -type ExpandedRobotsState = Record; +import { useState, type Dispatch, type SetStateAction } from "react"; +import API_URL from "../config"; +import "../styles/button.css"; +import "../styles/sidebar.css"; +import type { Robot } from "../types/robot"; +import AddRobotForm from "./AddRobotForm"; +import RobotList from "./RobotList"; +import SimulationActions from "./SimulationActions"; type Props = { - activeSimulation: boolean; errorMessage: string; - setErrorMessage: Dispatch>; - token: string | null; robots: Robot[]; - onStartAllRobots: () => Promise; - onStopAllRobots: () => Promise; + token: string | null; + setErrorMessage: Dispatch>; }; -const API_URL = import.meta.env.VITE_API_URL; - -function Sidebar({ - activeSimulation, - errorMessage, - setErrorMessage, - token, - robots, - onStartAllRobots, - onStopAllRobots, -}: Props) { - const [expandedRobots, setExpandedRobots] = useState( - {} - ); +function Sidebar({ errorMessage, robots, token, setErrorMessage }: Props) { const [isAddingRobot, setIsAddingRobot] = useState(false); - const [newRobotName, setNewRobotName] = useState(""); - const [isSubmitting, setIsSubmitting] = useState(false); - - function toggleRobotHistory(robotId: number) { - setExpandedRobots((prev) => ({ - ...prev, - [robotId]: !prev[robotId], - })); - } function handleAddClick() { setIsAddingRobot(true); - setNewRobotName(""); setErrorMessage(""); } - function handleCancel() { - setIsAddingRobot(false); - setNewRobotName(""); - setErrorMessage(""); - } - - async function handleSubmit() { - if (!newRobotName.trim()) { - setErrorMessage("Please enter a name for the robot."); - return; - } - - setIsSubmitting(true); - setErrorMessage(""); - - try { - const response = await fetch(`${API_URL}/robots`, { - method: "POST", - headers: { - Authorization: `Bearer ${token}`, - "Content-Type": "application/json", - }, - body: JSON.stringify({ name: newRobotName.trim() }), - }); - - if (!response.ok) { - const errorData: ErrorResponse = await response.json(); - throw new Error( - errorData.message || - `Error creating the new robot: ${response.status}` - ); - } - - const data: CreateRobotResponse = await response.json(); - console.log("Robot created successfully: ", data); - - // Reset form and close input field - setNewRobotName(""); - setIsAddingRobot(false); - } catch (error) { - console.error("Error creating the new robot:", error); - - if (error instanceof Error) { - setErrorMessage(error.message); - } else { - setErrorMessage("Error creating the new robot."); - } - } finally { - setIsSubmitting(false); - } - } - - function handleInputChange(event: ChangeEvent) { - setNewRobotName(event.target.value); - if (errorMessage) { - setErrorMessage(""); - } - } - return (
@@ -120,124 +31,31 @@ function Sidebar({ onClick={handleAddClick} disabled={isAddingRobot} > - + Neu + + Add
- {isAddingRobot && ( -
- -
- - -
-
+ {isAddingRobot && ( + )} {errorMessage && (
{errorMessage}
)} -
- - - -
- -
    - {robots.map((robot) => { - const isExpanded = expandedRobots[robot.id]; - - return ( -
  • -

    {robot.name}

    - -

    - Status:{" "} - - {robot.status} - -

    - -

    Position:

    -
      -
    • Lat: {robot.lat}
    • -
    • Lon: {robot.lon}
    • -
    - - + -
    -
      - {robot.robot_positions?.length ? ( - robot.robot_positions.map( - (pos, index) => ( -
    • - {`Lat: ${pos.lat}, Lon: ${pos.lon}`} -
    • - ) - ) - ) : ( -
    • - No previous positions. -
    • - )} -
    -
    -
  • - ); - })} -
+
); } -- cgit v1.2.3