File size: 4,002 Bytes
6ce4ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
	import { Button } from "@/components/ui/button";
	import { Badge } from "@/components/ui/badge";
	import * as DropdownMenu from "@/components/ui/dropdown-menu";
	import { toast } from "svelte-sonner";
	import { cn } from "$lib/utils";
	import { robotManager } from "$lib/elements/robot/RobotManager.svelte";
	import { robotUrdfConfigMap } from "$lib/configs/robotUrdfConfig";
	import { generateName } from "@/utils/generateName";

	interface Props {
		open: boolean;
	}
	let { open = $bindable() }: Props = $props();

	const robotTypes = Object.keys(robotUrdfConfigMap);

	async function addRobot(robotType: string) {
		console.log(`Creating ${robotType} robot...`);

		try {
			const urdfConfig = robotUrdfConfigMap[robotType];

			if (!urdfConfig) {
				throw new Error(`Unknown robot type: ${robotType}`);
			}

			const robotId = generateName();
			console.log("Creating robot with ID:", robotId, "and config:", urdfConfig);

			const robot = await robotManager.createRobotFromUrdf(robotId, urdfConfig);
			console.log("Robot created successfully:", robot);

			toast.success("Robot Added", {
				description: `${robotType} robot ${robotId.slice(0, 12)}... created successfully.`
			});

			open = false; // Close dropdown on success
		} catch (error) {
			console.error("Robot creation failed:", error);
			toast.error("Failed to Add Robot", {
				description: `Could not create ${robotType} robot: ${error}`
			});
		}
	}

	async function quickAddSO100() {
		await addRobot("so-arm100");
	}

</script>

<!-- Main Add Button (SO-100) -->
<Button
	variant="default"
	size="sm"
	onclick={quickAddSO100}
	class="group rounded-r-none border-0 bg-emerald-600 text-white transition-all duration-200 hover:bg-emerald-500"
>
	<span
		class={[
			"mr-2 size-4 transition-transform duration-200",
			"icon-[mdi--plus] group-hover:rotate-90"
		]}
	></span>
	Add Robot
</Button>

<!-- Dropdown Menu Button -->
<DropdownMenu.Root bind:open>
	<DropdownMenu.Trigger>
		{#snippet child({ props })}
			<Button
				{...props}
				variant="default"
				size="sm"
				class={cn(
					"rounded-l-none border-0 border-l border-emerald-500/30 bg-emerald-600 px-2 text-white transition-all duration-200 hover:bg-emerald-500",
					open && "bg-emerald-700 shadow-inner"
				)}
			>
				<span
					class={cn(
						"size-4 transition-transform duration-200",
						"icon-[mdi--chevron-down]",
						open && "rotate-180"
					)}
				></span>
			</Button>
		{/snippet}
	</DropdownMenu.Trigger>

	<DropdownMenu.Content
		class="w-56 border-emerald-500/30 bg-emerald-600 backdrop-blur-sm"
		align="center"
	>
		<DropdownMenu.Group>
			<DropdownMenu.GroupHeading
				class="text-xs font-semibold tracking-wider text-emerald-200 uppercase"
			>
				Robot Types
			</DropdownMenu.GroupHeading>

			{#each robotTypes as robotType}
				<DropdownMenu.Item
					class={[
						"group group cursor-pointer bg-emerald-600 text-white transition-all duration-200",
						"data-highlighted:bg-emerald-700 "
					]}
					onclick={async () => await addRobot(robotType)}
				>
					<span
						class={[
							robotType === "so-arm100" ? "icon-[ix--robotic-arm]" : "icon-[mdi--robot-industrial]",
							"mr-3 size-4 text-emerald-200  transition-colors duration-200"
						]}
					></span>
					<div class="flex flex-1 flex-col">
						<span class="font-medium text-white transition-colors duration-200"
							>{robotType.replace(/-/g, " ").toUpperCase()}</span
						>
						<span class="text-xs text-emerald-200 transition-colors duration-200">
							{robotType === "so-arm100" ? "6-DOF Robotic Arm" : "Industrial Robot"}
						</span>
					</div>
					{#if robotType === "so-arm100"}
						<Badge
							variant="secondary"
							class="ml-2 bg-emerald-700 text-xs text-emerald-100 group-data-highlighted:bg-emerald-400 group-data-highlighted:text-emerald-900"
						>
							Default
						</Badge>
					{/if}
				</DropdownMenu.Item>
			{/each}
		</DropdownMenu.Group>
	</DropdownMenu.Content>
</DropdownMenu.Root>