File size: 6,615 Bytes
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React, { useState, useEffect } from "react";
import URDFViewer from "../URDFViewer";
import { Sidebar, SidebarBody } from "@/components/ui/sidebar";
import { LayoutDashboard, PanelLeft, RotateCcw } from "lucide-react";
import { useUrdf } from "@/hooks/useUrdf";
import { Button } from "../ui/button";
import { useTheme } from "@/hooks/useTheme";
import AnimationDialog from "../AnimationDialog";
import RobotParameters from "../RobotParameters";

// Calculate header height - should match your Header component height
const HEADER_HEIGHT = 0; // in pixels - adjust if your header is different

const Layout: React.FC = () => {
  const [open, setOpen] = useState(true);
  const [animationDialogOpen, setAnimationDialogOpen] = useState(false);
  const { theme } = useTheme();

  // Use the centralized UrdfContext for all robot data
  const {
    currentRobotData,
    isDefaultModel,
    resetToDefaultModel,
    isSelectionModalOpen,
    currentAnimationConfig,
  } = useUrdf();

  // Log when robot data changes to verify the component is re-rendering
  useEffect(() => {
    console.log("Layout: Robot data updated", {
      isDefaultModel,
      hasData: !!currentRobotData,
      name: currentRobotData?.name,
    });
  }, [isDefaultModel, currentRobotData]);

  // Only set the initial state of the sidebar once
  useEffect(() => {
    setOpen(true);
  }, []);

  // Get the robot name from currentRobotData
  const robotName = currentRobotData?.name || "Robot";

  // Calculate if we have a custom animation
  const hasCustomAnimation = currentAnimationConfig !== null;

  // If there's a selection modal open, render a simplified layout
  if (isSelectionModalOpen) {
    return (
      <div className="absolute inset-0 overflow-hidden">
        {/* Main content that fills the container */}
        <div className="w-full h-full">
          <URDFViewer hasAnimation={hasCustomAnimation} />
        </div>
      </div>
    );
  }

  return (
    <div className="relative w-full h-full overflow-hidden">
      {/* Main content that fills the container */}
      <div className="w-full h-full">
        <URDFViewer
          hasAnimation={hasCustomAnimation}
          headerHeight={HEADER_HEIGHT}
        />
      </div>

      {/* Sidebar positioned above with z-index, with top padding for header */}
      <div
        className="absolute inset-y-0 left-0 z-10"
        style={{ paddingTop: `${HEADER_HEIGHT}px` }}
      >
        {open ? (
          <Sidebar open={open} setOpen={setOpen} animate={true}>
            <SidebarBody className="justify-between gap-10">
              <div className="flex flex-col flex-1 overflow-y-auto overflow-x-hidden">
                <div className="flex items-center justify-between mb-4 mt-2 px-2">
                  <h2 className="text-xl font-bold tracking-tight font-mono">
                    {currentRobotData?.name || "Robot Model"}
                  </h2>
                  <div className="flex items-center space-x-2">
                    {!isDefaultModel && (
                      <Button
                        variant="ghost"
                        size="icon"
                        onClick={resetToDefaultModel}
                        className={`rounded-full ${
                          theme === "dark"
                            ? "bg-gray-700 hover:bg-gray-600"
                            : "bg-white/80 hover:bg-white"
                        }`}
                        aria-label="Reset to default model"
                      >
                        <RotateCcw className="h-4 w-4" />
                        <span className="sr-only">Reset to default model</span>
                      </Button>
                    )}
                    <Button
                      variant="ghost"
                      size="icon"
                      onClick={() => setOpen(false)}
                      className={`rounded-full ${
                        theme === "dark"
                          ? "bg-gray-700 hover:bg-gray-600"
                          : "bg-white/80 hover:bg-white"
                      }`}
                    >
                      <PanelLeft className="h-4 w-4" />
                      <span className="sr-only">Close sidebar</span>
                    </Button>
                  </div>
                </div>
                <div className="px-2">
                  <p className="text-muted-foreground font-mono text-xs">
                    {currentRobotData?.description ||
                      "A detailed 3D model of a robotic system with articulated joints and components."}
                  </p>

                  <div className="sidebar-divider" />
                  <RobotParameters />
                  <div className="sidebar-divider" />

                  <div className="sidebar-section">
                    <div
                      className="bg-sidebar-accent rounded-lg p-3 flex items-center justify-center hover:bg-sidebar-accent/80 cursor-pointer transition-colors"
                      onClick={() => setAnimationDialogOpen(true)}
                    >
                      <span className="text-sidebar-accent-foreground font-medium font-mono text-sm">
                        Run an animation
                      </span>
                    </div>
                    <div className="text-xs text-muted-foreground mt-2 text-left font-mono">
                      See the robot move any way you want.
                    </div>
                  </div>
                </div>
              </div>
            </SidebarBody>
          </Sidebar>
        ) : (
          <div
            className="flex flex-col items-center justify-center"
            style={{ paddingTop: `${HEADER_HEIGHT}px` }}
          >
            <div className="flex items-center mt-4">
              <LayoutDashboard className="h-7 w-7" />
            </div>
            <div
              className={`flex items-center justify-center hover:bg-muted/80 cursor-pointer rounded-md p-2 m-2 ${
                theme === "dark" ? "bg-gray-700" : "bg-white/80"
              } shadow-sm`}
              onClick={() => setOpen(true)}
            >
              <div className="flex items-center space-x-1">
                <PanelLeft className="h-4 w-4" />
              </div>
            </div>
          </div>
        )}
      </div>

      {/* Animation Dialog - moved to root level to prevent accessibility issues */}
      <AnimationDialog
        open={animationDialogOpen}
        onOpenChange={setAnimationDialogOpen}
        robotName={robotName}
      />
    </div>
  );
};

export default Layout;