File size: 8,295 Bytes
02eac4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<script lang="ts">
	import { onMount } from 'svelte';
	import { robotics } from 'lerobot-arena-client';
	import type { robotics as roboticsTypes } from 'lerobot-arena-client';
	import { goto } from '$app/navigation';

	// Server status
	let serverStatus = $state<'checking' | 'connected' | 'error'>('checking');
	let serverInfo = $state<{ rooms: number; version: string }>({ rooms: 0, version: 'Unknown' });
	let rooms = $state<roboticsTypes.RoomInfo[]>([]);
	let lastError = $state<string>('');
	let debugInfo = $state<{
		lastCheck: string;
		connectionAttempts: number;
		responseTime: number;
	}>({
		lastCheck: '',
		connectionAttempts: 0,
		responseTime: 0
	});

	// Workspace management
	let workspaceInput = $state<string>('');
	let recentWorkspaces = $state<string[]>([]);
	let error = $state<string>('');

	async function checkServerStatus() {
		const startTime = Date.now();
		debugInfo.connectionAttempts++;

		try {
			// Use relative URL so it works in both development and production
			const baseUrl = typeof window !== 'undefined' ? window.location.origin : 'http://localhost:7860';
			const apiUrl = `${baseUrl}/api`;
			const client = new robotics.RoboticsClientCore(apiUrl);
			const roomList = await client.listRooms('');
			rooms = roomList;
			serverInfo = { rooms: roomList.length, version: '1.0.0' };
			serverStatus = 'connected';
			lastError = '';
			debugInfo.responseTime = Date.now() - startTime;
		} catch (error) {
			console.error('Server check failed:', error);
			serverStatus = 'error';
			lastError = error instanceof Error ? error.message : String(error);
			debugInfo.responseTime = Date.now() - startTime;
		}

		debugInfo.lastCheck = new Date().toLocaleTimeString();
	}

	// Generate a new workspace ID
	function generateWorkspaceId(): string {
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
			const r = Math.random() * 16 | 0;
			const v = c === 'x' ? r : (r & 0x3 | 0x8);
			return v.toString(16);
		});
	}

	// Load recent workspaces from localStorage
	function loadRecentWorkspaces() {
		try {
			const stored = localStorage.getItem('lerobot-arena-workspaces');
			if (stored) {
				recentWorkspaces = JSON.parse(stored);
			}
		} catch (err) {
			console.warn('Failed to load recent workspaces:', err);
		}
	}

	// Save workspace to recent list
	function saveToRecentWorkspaces(workspaceId: string) {
		const updated = [workspaceId, ...recentWorkspaces.filter(id => id !== workspaceId)].slice(0, 10);
		recentWorkspaces = updated;
		try {
			localStorage.setItem('lerobot-arena-workspaces', JSON.stringify(updated));
		} catch (err) {
			console.warn('Failed to save recent workspaces:', err);
		}
	}

	// Create new workspace
	function createNewWorkspace() {
		const newWorkspaceId = generateWorkspaceId();
		saveToRecentWorkspaces(newWorkspaceId);
		goto(`/${newWorkspaceId}`);
	}

	// Join existing workspace
	function joinWorkspace() {
		if (!workspaceInput.trim()) {
			error = 'Please enter a workspace ID';
			return;
		}

		const workspaceId = workspaceInput.trim();
		saveToRecentWorkspaces(workspaceId);
		goto(`/${workspaceId}`);
	}

	// Join workspace from recent list
	function joinRecentWorkspace(workspaceId: string) {
		saveToRecentWorkspaces(workspaceId); // Move to top of recent list
		goto(`/${workspaceId}`);
	}

	// Remove workspace from recent list
	function removeRecentWorkspace(workspaceId: string) {
		recentWorkspaces = recentWorkspaces.filter(id => id !== workspaceId);
		try {
			localStorage.setItem('lerobot-arena-workspaces', JSON.stringify(recentWorkspaces));
		} catch (err) {
			console.warn('Failed to save recent workspaces:', err);
		}
	}

	onMount(() => {
		checkServerStatus();
		// Check server status every 10 seconds
		const interval = setInterval(checkServerStatus, 10000);
		loadRecentWorkspaces();
		return () => clearInterval(interval);
	});
</script>

<svelte:head>
	<title>LeRobot Arena - Workspace Selection</title>
</svelte:head>

<div class="mx-auto max-w-4xl p-4">
	<!-- Header -->
	<div class="mb-8 text-center">
		<h1 class="font-mono text-3xl font-bold text-gray-900">πŸ€– LeRobot Arena</h1>
		<p class="mt-2 font-mono text-lg text-gray-600">
			Real-time robotics control and monitoring platform
		</p>
		<p class="mt-1 font-mono text-sm text-gray-500">
			Select or create a workspace to get started
		</p>
	</div>

	<!-- Workspace Selection -->
	<div class="mb-8 space-y-6">
		<!-- Create New Workspace -->
		<div class="rounded border p-6">
			<div class="mb-4 text-center">
				<h2 class="font-mono text-xl font-semibold text-gray-900">Create New Workspace</h2>
				<p class="mt-1 font-mono text-sm text-gray-600">
					Start fresh with a new isolated workspace
				</p>
			</div>
			
			<div class="text-center">
				<button
					onclick={createNewWorkspace}
					class="rounded border bg-blue-600 px-6 py-3 font-mono text-white hover:bg-blue-700"
				>
					✨ Generate New Workspace
				</button>
			</div>
		</div>

		<!-- Join Existing Workspace -->
		<div class="rounded border p-6">
			<div class="mb-4 text-center">
				<h2 class="font-mono text-xl font-semibold text-gray-900">Join Existing Workspace</h2>
				<p class="mt-1 font-mono text-sm text-gray-600">
					Enter a workspace ID to join an existing session
				</p>
			</div>

			<div class="mx-auto max-w-md space-y-4">
				<div>
					<label for="workspaceInput" class="mb-1 block font-mono text-sm font-medium text-gray-700">
						Workspace ID
					</label>
					<input
						id="workspaceInput"
						type="text"
						bind:value={workspaceInput}
						placeholder="Enter workspace ID (UUID format)"
						class="w-full rounded border border-gray-300 px-3 py-2 font-mono focus:border-blue-500 focus:ring-blue-500"
						onkeydown={(e) => {
							if (e.key === 'Enter') {
								joinWorkspace();
							}
						}}
					/>
				</div>
				
				<button
					onclick={joinWorkspace}
					disabled={!workspaceInput.trim()}
					class={[
						'w-full rounded border px-4 py-2 font-mono',
						workspaceInput.trim()
							? 'bg-green-600 text-white hover:bg-green-700'
							: 'bg-gray-200 text-gray-500'
					]}
				>
					πŸš€ Join Workspace
				</button>

				{#if error}
					<div class="mt-2 rounded border border-red-200 bg-red-50 p-3">
						<p class="font-mono text-sm text-red-700">{error}</p>
					</div>
				{/if}
			</div>
		</div>

		<!-- Recent Workspaces -->
		{#if recentWorkspaces.length > 0}
			<div class="rounded border p-6">
				<div class="mb-4 text-center">
					<h2 class="font-mono text-xl font-semibold text-gray-900">Recent Workspaces</h2>
					<p class="mt-1 font-mono text-sm text-gray-600">
						Quick access to your previously used workspaces
					</p>
				</div>

				<div class="space-y-3">
					{#each recentWorkspaces as workspaceId}
						<div class="flex items-center justify-between rounded border bg-gray-50 p-3">
							<div>
								<div class="font-mono text-sm font-medium">{workspaceId}</div>
								<div class="font-mono text-xs text-gray-500">UUID Workspace</div>
							</div>
							<div class="flex space-x-2">
								<button
									onclick={() => joinRecentWorkspace(workspaceId)}
									class="rounded border bg-blue-100 px-3 py-1 font-mono text-sm text-blue-700 hover:bg-blue-200"
								>
									πŸ“‚ Open
								</button>
								<button
									onclick={() => removeRecentWorkspace(workspaceId)}
									class="rounded border bg-red-100 px-3 py-1 font-mono text-sm text-red-700 hover:bg-red-200"
								>
									πŸ—‘οΈ
								</button>
							</div>
						</div>
					{/each}
				</div>
			</div>
		{/if}
	</div>

	<!-- About Workspaces -->
	<div class="rounded border bg-blue-50 p-6">
		<h3 class="mb-3 font-mono text-lg font-semibold text-blue-900">About Workspaces</h3>
		<div class="space-y-2 font-mono text-sm text-blue-800">
			<p>πŸ”’ <strong>Isolation:</strong> Each workspace provides a secure, isolated environment for your robotics sessions</p>
			<p>🏠 <strong>Rooms:</strong> Create multiple rooms within a workspace for different robot configurations</p>
			<p>πŸ‘₯ <strong>Sharing:</strong> Share the workspace ID with others to collaborate on the same robots</p>
			<p>πŸ”„ <strong>Real-time:</strong> All participants in a workspace see live updates from producers</p>
		</div>
	</div>
</div>