File size: 1,997 Bytes
9d3c32a
 
 
 
9a9d18a
9d3c32a
 
 
 
9a9d18a
9d3c32a
 
 
 
 
9a9d18a
9d3c32a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React from "react";
import { useNavigate } from "react-router-dom";
import VisualizerPanel from "@/components/control/VisualizerPanel";
import { useToast } from "@/hooks/use-toast";
import { useApi } from "@/contexts/ApiContext";

const TeleoperationPage = () => {
  const navigate = useNavigate();
  const { toast } = useToast();
  const { baseUrl, fetchWithHeaders } = useApi();

  const handleGoBack = async () => {
    try {
      // Stop the teleoperation process before navigating back
      console.log("🛑 Stopping teleoperation...");
      const response = await fetchWithHeaders(`${baseUrl}/stop-teleoperation`, {
        method: "POST",
      });

      if (response.ok) {
        const result = await response.json();
        console.log("✅ Teleoperation stopped:", result.message);
        toast({
          title: "Teleoperation Stopped",
          description:
            result.message ||
            "Robot teleoperation has been stopped successfully.",
        });
      } else {
        const errorText = await response.text();
        console.warn(
          "⚠️ Failed to stop teleoperation:",
          response.status,
          errorText
        );
        toast({
          title: "Warning",
          description: `Failed to stop teleoperation properly. Status: ${response.status}`,
          variant: "destructive",
        });
      }
    } catch (error) {
      console.error("❌ Error stopping teleoperation:", error);
      toast({
        title: "Error",
        description: "Failed to communicate with the robot server.",
        variant: "destructive",
      });
    } finally {
      // Navigate back regardless of the result
      navigate("/");
    }
  };

  return (
    <div className="min-h-screen bg-black flex items-center justify-center p-2 sm:p-4">
      <div className="w-full h-[95vh] flex">
        <VisualizerPanel onGoBack={handleGoBack} className="lg:w-full" />
      </div>
    </div>
  );
};

export default TeleoperationPage;