@air-jam/sdk 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,33 +1,26 @@
1
- import { MonitorSmartphone, Pause, Play, QrCode, Settings } from "lucide-react";
1
+ import {
2
+ Check,
3
+ Copy,
4
+ ExternalLink,
5
+ MonitorSmartphone,
6
+ Pause,
7
+ Play,
8
+ QrCode,
9
+ Settings,
10
+ } from "lucide-react";
2
11
  import QRCode from "qrcode";
3
12
  import type { JSX } from "react";
4
13
  import { useEffect, useMemo, useState } from "react";
5
- import type { ConnectionStatus, GameState, PlayerProfile } from "../protocol";
14
+ import { useAirJamHost } from "../hooks/use-air-jam-host";
15
+ import type { ConnectionStatus } from "../protocol";
16
+ import { PlayerAvatar } from "./player-avatar";
6
17
  import { Alert, AlertDescription } from "./ui/alert";
7
- import { Avatar, AvatarImage } from "./ui/avatar";
8
18
  import { Badge } from "./ui/badge";
9
19
  import { Button } from "./ui/button";
10
- import {
11
- Card,
12
- CardContent,
13
- CardDescription,
14
- CardHeader,
15
- CardTitle,
16
- } from "./ui/card";
20
+ import { Card, CardContent, CardDescription, CardTitle } from "./ui/card";
17
21
  import { Tabs, TabsContent, TabsList, TabsTrigger } from "./ui/tabs";
18
22
  import { VolumeControls } from "./volume-controls";
19
23
 
20
- interface AirJamOverlayProps {
21
- roomId: string;
22
- joinUrl: string;
23
- connectionStatus: ConnectionStatus;
24
- players: PlayerProfile[];
25
- lastError?: string;
26
- gameState: GameState;
27
- onTogglePlayPause?: () => void;
28
- isChildMode?: boolean;
29
- }
30
-
31
24
  const statusCopy: Record<ConnectionStatus, string> = {
32
25
  idle: "Idle",
33
26
  connecting: "Waiting for server…",
@@ -36,27 +29,22 @@ const statusCopy: Record<ConnectionStatus, string> = {
36
29
  reconnecting: "Reconnecting…",
37
30
  };
38
31
 
39
- // Generate a consistent avatar URL for a player based on their ID
40
- const getPlayerAvatarUrl = (playerId: string): string => {
41
- // Use DiceBear API with identicon style for GitHub-like avatars
42
- // The seed ensures the same player ID always gets the same avatar
43
- return `https://api.dicebear.com/9.x/adventurer-neutral/svg?seed=${encodeURIComponent(
44
- playerId,
45
- )}`;
46
- };
47
-
48
- export const AirJamOverlay = ({
49
- roomId,
50
- joinUrl,
51
- connectionStatus,
52
- players,
53
- lastError,
54
- gameState,
55
- onTogglePlayPause,
56
- isChildMode = false,
57
- }: AirJamOverlayProps): JSX.Element | null => {
32
+ export const AirJamOverlay = (): JSX.Element | null => {
33
+ const host = useAirJamHost();
34
+ const {
35
+ roomId,
36
+ joinUrl,
37
+ connectionStatus,
38
+ players,
39
+ lastError,
40
+ gameState,
41
+ toggleGameState,
42
+ isChildMode,
43
+ } = host;
58
44
  const [qrUrl, setQrUrl] = useState<string | null>(null);
59
45
  const [qrError, setQrError] = useState<string | null>(null);
46
+ const [copied, setCopied] = useState(false);
47
+ const [copiedPlayerId, setCopiedPlayerId] = useState<string | null>(null);
60
48
 
61
49
  useEffect(() => {
62
50
  // No need to generate QR in child mode
@@ -88,6 +76,63 @@ export const AirJamOverlay = ({
88
76
  };
89
77
  }, [joinUrl, isChildMode]);
90
78
 
79
+ const handleCopyUrl = async (): Promise<void> => {
80
+ if (!joinUrl) return;
81
+ try {
82
+ await navigator.clipboard.writeText(joinUrl);
83
+ setCopied(true);
84
+ setTimeout(() => setCopied(false), 2000);
85
+ } catch {
86
+ // Fallback for older browsers
87
+ const textArea = document.createElement("textarea");
88
+ textArea.value = joinUrl;
89
+ textArea.style.position = "fixed";
90
+ textArea.style.opacity = "0";
91
+ document.body.appendChild(textArea);
92
+ textArea.select();
93
+ try {
94
+ document.execCommand("copy");
95
+ setCopied(true);
96
+ setTimeout(() => setCopied(false), 2000);
97
+ } catch {
98
+ // Ignore
99
+ }
100
+ document.body.removeChild(textArea);
101
+ }
102
+ };
103
+
104
+ const handleOpenInNewTab = (): void => {
105
+ if (joinUrl) {
106
+ window.open(joinUrl, "_blank", "noopener,noreferrer");
107
+ }
108
+ };
109
+
110
+ const handleCopyPlayerId = async (playerId: string): Promise<void> => {
111
+ try {
112
+ await navigator.clipboard.writeText(playerId);
113
+ console.log(`Copied player ID: ${playerId}`);
114
+ setCopiedPlayerId(playerId);
115
+ setTimeout(() => setCopiedPlayerId(null), 1000);
116
+ } catch {
117
+ // Fallback for older browsers
118
+ const textArea = document.createElement("textarea");
119
+ textArea.value = playerId;
120
+ textArea.style.position = "fixed";
121
+ textArea.style.opacity = "0";
122
+ document.body.appendChild(textArea);
123
+ textArea.select();
124
+ try {
125
+ document.execCommand("copy");
126
+ console.log(`Copied player ID: ${playerId}`);
127
+ setCopiedPlayerId(playerId);
128
+ setTimeout(() => setCopiedPlayerId(null), 1000);
129
+ } catch {
130
+ // Ignore
131
+ }
132
+ document.body.removeChild(textArea);
133
+ }
134
+ };
135
+
91
136
  const connectionVariant = useMemo<
92
137
  "default" | "secondary" | "destructive" | "outline"
93
138
  >(() => {
@@ -108,7 +153,7 @@ export const AirJamOverlay = ({
108
153
 
109
154
  // Always render the navbar (unless child mode + playing)
110
155
  return (
111
- <>
156
+ <div className="dark">
112
157
  {/* Top Navbar - Always Visible */}
113
158
  {!isChildMode && (
114
159
  <div className="pointer-events-none absolute inset-x-0 top-0 z-99999">
@@ -132,18 +177,7 @@ export const AirJamOverlay = ({
132
177
  {players.length > 0 && (
133
178
  <div className="flex items-center -space-x-2">
134
179
  {players.slice(0, 4).map((player) => (
135
- <Avatar
136
- key={player.id}
137
- className="h-8 w-8 border-2"
138
- style={{
139
- borderColor: player.color || "hsl(var(--border))",
140
- }}
141
- >
142
- <AvatarImage
143
- src={getPlayerAvatarUrl(player.id)}
144
- alt={player.label}
145
- />
146
- </Avatar>
180
+ <PlayerAvatar key={player.id} player={player} size="sm" />
147
181
  ))}
148
182
  {players.length > 4 && (
149
183
  <div className="border-background bg-muted flex h-8 w-8 items-center justify-center rounded-full border-2 text-xs font-medium">
@@ -154,22 +188,20 @@ export const AirJamOverlay = ({
154
188
  )}
155
189
 
156
190
  {/* Play/Pause button */}
157
- {onTogglePlayPause && (
158
- <Button
159
- type="button"
160
- variant="ghost"
161
- size="icon"
162
- onClick={onTogglePlayPause}
163
- aria-label={gameState === "playing" ? "Pause" : "Play"}
164
- className="h-8 w-8"
165
- >
166
- {gameState === "playing" ? (
167
- <Pause className="h-4 w-4" />
168
- ) : (
169
- <Play className="h-4 w-4" />
170
- )}
171
- </Button>
172
- )}
191
+ <Button
192
+ type="button"
193
+ variant="ghost"
194
+ size="icon"
195
+ onClick={toggleGameState}
196
+ aria-label={gameState === "playing" ? "Pause" : "Play"}
197
+ className="h-8 w-8"
198
+ >
199
+ {gameState === "playing" ? (
200
+ <Pause className="h-4 w-4" />
201
+ ) : (
202
+ <Play className="h-4 w-4" />
203
+ )}
204
+ </Button>
173
205
  </div>
174
206
  </div>
175
207
  </div>
@@ -180,7 +212,7 @@ export const AirJamOverlay = ({
180
212
  {gameState === "paused" && (
181
213
  <div className="pointer-events-none fixed inset-0 z-99998 flex items-center justify-center p-4">
182
214
  <div className="pointer-events-auto w-full max-w-2xl">
183
- <Card className="bg-card/20 flex h-[80vh] flex-col overflow-hidden border py-0 shadow-lg backdrop-blur-sm">
215
+ <Card className="bg-card/20 flex max-h-[85vh] flex-col overflow-hidden border py-0 shadow-lg backdrop-blur-sm">
184
216
  <Tabs
185
217
  defaultValue={isChildMode ? "settings" : "room-code"}
186
218
  className="flex h-full w-full flex-col gap-0"
@@ -218,29 +250,23 @@ export const AirJamOverlay = ({
218
250
  value="room-code"
219
251
  className="mt-0 min-h-0 flex-1 overflow-y-auto"
220
252
  >
221
- <div className="space-y-6 pb-6">
222
- <CardHeader className="pt-6 text-center">
223
- <CardTitle className="text-4xl font-bold tracking-wider">
224
- {roomId}
225
- </CardTitle>
226
- <Badge
227
- variant={connectionVariant}
228
- className="mx-auto mt-3"
229
- >
230
- {statusCopy[connectionStatus]}
231
- </Badge>
232
- </CardHeader>
233
-
234
- <CardContent className="space-y-6">
235
- <div className="flex flex-col items-center space-y-4">
236
- <CardDescription className="text-xs tracking-[0.18em] uppercase">
237
- Join URL
238
- </CardDescription>
239
- <p className="text-muted-foreground text-center font-mono text-xs break-all">
240
- {joinUrl}
241
- </p>
253
+ <div className="p-6">
254
+ <div className="grid grid-cols-1 gap-6 md:grid-cols-[auto_1fr]">
255
+ {/* Left: QR Code Section */}
256
+ <div className="flex flex-col items-start gap-4">
257
+ <div className="space-y-2">
258
+ <CardTitle className="text-3xl font-bold tracking-wider">
259
+ {roomId}
260
+ </CardTitle>
261
+ <Badge
262
+ variant={connectionVariant}
263
+ className="w-fit"
264
+ >
265
+ {statusCopy[connectionStatus]}
266
+ </Badge>
267
+ </div>
242
268
 
243
- <div className="border-border bg-card w-48 overflow-hidden rounded-lg border-2 shadow-md">
269
+ <div className="border-border bg-card w-48 shrink-0 overflow-hidden rounded-lg border-2 shadow-md">
244
270
  {qrUrl ? (
245
271
  <img
246
272
  src={qrUrl}
@@ -255,45 +281,90 @@ export const AirJamOverlay = ({
255
281
  </div>
256
282
  )}
257
283
  </div>
284
+ </div>
285
+
286
+ {/* Right: Info Section */}
287
+ <div className="flex flex-col gap-6">
288
+ <div className="space-y-3">
289
+ <CardDescription className="text-xs tracking-[0.18em] uppercase">
290
+ Join URL
291
+ </CardDescription>
292
+ <div className="flex gap-2">
293
+ <input
294
+ type="text"
295
+ readOnly
296
+ value={joinUrl}
297
+ className="bg-background border-input text-foreground focus:ring-ring flex h-10 w-full rounded-md border px-3 py-2 font-mono text-xs focus:ring-2 focus:ring-offset-2 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50"
298
+ />
299
+ <Button
300
+ type="button"
301
+ variant="outline"
302
+ size="icon"
303
+ onClick={handleCopyUrl}
304
+ className="h-10 shrink-0"
305
+ aria-label="Copy join URL"
306
+ >
307
+ {copied ? (
308
+ <Check className="h-4 w-4" />
309
+ ) : (
310
+ <Copy className="h-4 w-4" />
311
+ )}
312
+ </Button>
313
+ <Button
314
+ type="button"
315
+ variant="outline"
316
+ size="icon"
317
+ onClick={handleOpenInNewTab}
318
+ className="h-10 shrink-0"
319
+ aria-label="Open join URL in new tab"
320
+ >
321
+ <ExternalLink className="h-4 w-4" />
322
+ </Button>
323
+ </div>
324
+ </div>
258
325
 
259
- <div className="w-full">
326
+ <div className="space-y-3">
260
327
  {players.length === 0 ? (
261
- <p className="text-muted-foreground py-4 text-center text-sm">
328
+ <p className="text-sm text-white">
262
329
  Waiting for controllers to join…
263
330
  </p>
264
331
  ) : (
265
- <div className="space-y-3">
266
- <p className="text-foreground text-center text-sm font-medium">
332
+ <>
333
+ <p className="text-foreground text-sm font-medium">
267
334
  Connected Players ({players.length})
268
335
  </p>
269
- <ul className="flex flex-wrap items-center justify-center gap-4">
336
+ <ul className="grid grid-cols-2 gap-3 sm:grid-cols-3">
270
337
  {players.map((player) => (
271
338
  <li
272
339
  key={player.id}
273
- className="flex flex-col items-center gap-2"
340
+ className="flex min-w-0 flex-col items-start gap-2"
274
341
  >
275
- <img
276
- src={getPlayerAvatarUrl(player.id)}
277
- alt={player.label}
278
- className="bg-secondary/30 h-16 w-16 rounded-full border-4 shadow-md"
279
- style={{
280
- borderColor:
281
- player.color ||
282
- "hsl(var(--border))",
283
- }}
284
- />
285
- <div className="flex flex-col items-center gap-0.5">
286
- <span className="text-card-foreground max-w-[100px] truncate text-center text-sm font-medium">
287
- {player.label}
288
- </span>
289
- <span className="text-muted-foreground font-mono text-xs">
290
- {player.id.slice(0, 8)}
291
- </span>
342
+ <div className="flex w-full min-w-0 items-center gap-2">
343
+ <PlayerAvatar
344
+ player={player}
345
+ size="md"
346
+ />
347
+ <div className="flex min-w-0 flex-1 flex-col gap-0.5 overflow-hidden">
348
+ <span className="text-card-foreground truncate text-sm font-medium">
349
+ {player.label}
350
+ </span>
351
+ <div
352
+ onClick={() =>
353
+ handleCopyPlayerId(player.id)
354
+ }
355
+ className="text-muted-foreground m-0 inline min-w-0 cursor-pointer truncate border-0 font-mono text-xs transition-colors hover:text-white"
356
+ title="Click to copy player ID"
357
+ >
358
+ {copiedPlayerId === player.id
359
+ ? "Copied!"
360
+ : player.id.slice(0, 8)}
361
+ </div>
362
+ </div>
292
363
  </div>
293
364
  </li>
294
365
  ))}
295
366
  </ul>
296
- </div>
367
+ </>
297
368
  )}
298
369
  </div>
299
370
 
@@ -303,21 +374,19 @@ export const AirJamOverlay = ({
303
374
  </Alert>
304
375
  )}
305
376
 
306
- {/* Play button in paused state */}
307
- {onTogglePlayPause && (
308
- <div className="pt-2">
309
- <Button
310
- type="button"
311
- onClick={onTogglePlayPause}
312
- size="lg"
313
- >
314
- <Play className="mr-2 h-5 w-5" />
315
- Start Game
316
- </Button>
317
- </div>
318
- )}
377
+ {/* Play button - bottom right */}
378
+ <div className="mt-auto flex justify-end">
379
+ <Button
380
+ type="button"
381
+ onClick={toggleGameState}
382
+ size="lg"
383
+ >
384
+ <Play className="mr-2 h-5 w-5" />
385
+ Start Game
386
+ </Button>
387
+ </div>
319
388
  </div>
320
- </CardContent>
389
+ </div>
321
390
  </div>
322
391
  </TabsContent>
323
392
  )}
@@ -327,64 +396,52 @@ export const AirJamOverlay = ({
327
396
  value="info"
328
397
  className="mt-0 min-h-0 flex-1 overflow-y-auto"
329
398
  >
330
- <div className="space-y-6 pb-6">
331
- <CardHeader className="pt-6 text-center">
332
- <CardTitle className="text-2xl font-bold tracking-wider">
333
- Connected to Platform
334
- </CardTitle>
335
- <CardDescription className="mt-2">
336
- This game is running inside the Air Jam Platform.
337
- </CardDescription>
338
- </CardHeader>
339
-
340
- <CardContent className="space-y-6">
341
- <div className="flex flex-col items-center space-y-4">
342
- <div className="w-full">
343
- <div className="space-y-3">
344
- <p className="text-foreground text-center text-sm font-medium">
345
- Connected Players ({players.length})
346
- </p>
347
- <ul className="flex flex-wrap items-center justify-center gap-4">
348
- {players.map((player) => (
349
- <li
350
- key={player.id}
351
- className="flex flex-col items-center gap-2"
352
- >
353
- <img
354
- src={getPlayerAvatarUrl(player.id)}
355
- alt={player.label}
356
- className="bg-secondary/30 h-16 w-16 rounded-full border-4 shadow-md"
357
- style={{
358
- borderColor:
359
- player.color || "hsl(var(--border))",
360
- }}
361
- />
362
- <div className="flex flex-col items-center gap-0.5">
363
- <span className="text-card-foreground max-w-[100px] truncate text-center text-sm font-medium">
364
- {player.label}
365
- </span>
366
- </div>
367
- </li>
368
- ))}
369
- </ul>
370
- </div>
371
- </div>
399
+ <div className="p-6">
400
+ <div className="space-y-6">
401
+ <div className="space-y-2">
402
+ <CardTitle className="text-2xl font-bold tracking-wider">
403
+ Connected to Platform
404
+ </CardTitle>
405
+ <CardDescription>
406
+ This game is running inside the Air Jam Platform.
407
+ </CardDescription>
408
+ </div>
372
409
 
373
- {/* Play button in paused state */}
374
- {onTogglePlayPause && (
375
- <div className="pt-2">
376
- <Button
377
- type="button"
378
- onClick={onTogglePlayPause}
379
- size="lg"
410
+ <div className="space-y-3">
411
+ <p className="text-foreground text-sm font-medium">
412
+ Connected Players ({players.length})
413
+ </p>
414
+ <ul className="grid grid-cols-2 gap-3 sm:grid-cols-3">
415
+ {players.map((player) => (
416
+ <li
417
+ key={player.id}
418
+ className="flex min-w-0 flex-col items-start gap-2"
380
419
  >
381
- <Play className="mr-2 h-5 w-5" />
382
- Resume Game
383
- </Button>
384
- </div>
385
- )}
420
+ <div className="flex w-full min-w-0 items-center gap-2">
421
+ <PlayerAvatar player={player} size="md" />
422
+ <div className="flex min-w-0 flex-1 flex-col gap-0.5 overflow-hidden">
423
+ <span className="text-card-foreground truncate text-sm font-medium">
424
+ {player.label}
425
+ </span>
426
+ </div>
427
+ </div>
428
+ </li>
429
+ ))}
430
+ </ul>
386
431
  </div>
387
- </CardContent>
432
+
433
+ {/* Play button in paused state */}
434
+ <div className="pt-2">
435
+ <Button
436
+ type="button"
437
+ onClick={toggleGameState}
438
+ size="lg"
439
+ >
440
+ <Play className="mr-2 h-5 w-5" />
441
+ Resume Game
442
+ </Button>
443
+ </div>
444
+ </div>
388
445
  </div>
389
446
  </TabsContent>
390
447
  )}
@@ -451,6 +508,6 @@ export const AirJamOverlay = ({
451
508
  </div>
452
509
  </div>
453
510
  )}
454
- </>
511
+ </div>
455
512
  );
456
513
  };
@@ -16,21 +16,12 @@ import type { ConnectionStatus, PlayerProfile } from "../protocol";
16
16
  import { cn } from "../utils/cn";
17
17
  import { detectRunMode } from "../utils/mode";
18
18
  import { QRScannerDialog } from "./qr-scanner-dialog";
19
- import { Avatar, AvatarImage } from "./ui/avatar";
20
19
  import { Button } from "./ui/button";
20
+ import { PlayerAvatar } from "./player-avatar";
21
21
  import { VolumeControls } from "./volume-controls";
22
22
 
23
23
  type OrientationRequirement = "portrait" | "landscape" | "any";
24
24
 
25
- // Generate a consistent avatar URL for a player based on their ID
26
- const getPlayerAvatarUrl = (playerId: string): string => {
27
- // Use DiceBear API with identicon style for GitHub-like avatars
28
- // The seed ensures the same player ID always gets the same avatar
29
- return `https://api.dicebear.com/9.x/adventurer-neutral/svg?seed=${encodeURIComponent(
30
- playerId,
31
- )}`;
32
- };
33
-
34
25
  interface ControllerShellProps {
35
26
  roomId?: string | null;
36
27
  connectionStatus: ConnectionStatus;
@@ -142,7 +133,8 @@ export const ControllerShell = ({
142
133
  // The parent frame provides the shell (header, volume, etc).
143
134
  if (isChildMode) {
144
135
  return (
145
- <div className="text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden bg-transparent select-none">
136
+ <div className="dark">
137
+ <div className="text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden bg-transparent select-none">
146
138
  <main className="relative flex min-h-0 flex-1 items-center justify-center overflow-hidden p-2 select-none sm:p-4">
147
139
  {!orientationOk && (
148
140
  <div className="bg-background/95 absolute inset-0 z-50 flex flex-col items-center justify-center p-6 text-center shadow-lg backdrop-blur-sm">
@@ -165,28 +157,28 @@ export const ControllerShell = ({
165
157
  </div>
166
158
  </main>
167
159
  </div>
160
+ </div>
168
161
  );
169
162
  }
170
163
 
171
164
  return (
172
- <div className="bg-background text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden select-none">
165
+ <div className="dark">
166
+ <div className="bg-background text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden select-none">
173
167
  <header className="pointer-events-none sticky top-0 z-50 flex items-center justify-between border-b px-6 py-2">
174
168
  <div className="pointer-events-auto flex items-center gap-3">
175
169
  {typeof window !== "undefined" && (currentPlayer || controllerId) && (
176
170
  <div className="relative" title={describeStatus(connectionStatus)}>
177
- <Avatar
178
- className="h-8 w-8 border-2"
179
- style={{
180
- borderColor: currentPlayer?.color || "hsl(var(--border))",
181
- }}
182
- >
183
- <AvatarImage
184
- src={getPlayerAvatarUrl(
185
- currentPlayer?.id || controllerId || "",
186
- )}
187
- alt={currentPlayer?.label || "Player"}
171
+ {currentPlayer ? (
172
+ <PlayerAvatar player={currentPlayer} size="sm" />
173
+ ) : controllerId ? (
174
+ <PlayerAvatar
175
+ player={{
176
+ id: controllerId,
177
+ label: "Player",
178
+ }}
179
+ size="sm"
188
180
  />
189
- </Avatar>
181
+ ) : null}
190
182
  {/* Black border circle - always visible behind */}
191
183
  <span
192
184
  className="absolute right-0 bottom-0 h-3 w-3 rounded-full bg-black"
@@ -324,6 +316,7 @@ export const ControllerShell = ({
324
316
  currentRoomId={roomId}
325
317
  />
326
318
  )}
319
+ </div>
327
320
  </div>
328
321
  );
329
322
  };