@air-jam/sdk 0.1.0

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.
Files changed (56) hide show
  1. package/README.md +87 -0
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/chunk-XSXI3RIS.js +72 -0
  4. package/dist/chunk-XSXI3RIS.js.map +1 -0
  5. package/dist/index.cjs +3514 -0
  6. package/dist/index.cjs.map +1 -0
  7. package/dist/index.d.cts +1529 -0
  8. package/dist/index.d.ts +1529 -0
  9. package/dist/index.js +3366 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/protocol.cjs +85 -0
  12. package/dist/protocol.cjs.map +1 -0
  13. package/dist/protocol.d.cts +242 -0
  14. package/dist/protocol.d.ts +242 -0
  15. package/dist/protocol.js +3 -0
  16. package/dist/protocol.js.map +1 -0
  17. package/package.json +81 -0
  18. package/src/AirJamClient.ts +775 -0
  19. package/src/audio/audio-manager.ts +395 -0
  20. package/src/audio/hooks.tsx +140 -0
  21. package/src/audio/index.ts +3 -0
  22. package/src/audio/volume-store.ts +77 -0
  23. package/src/components/air-jam-overlay.tsx +456 -0
  24. package/src/components/controller-shell.tsx +329 -0
  25. package/src/components/qr-scanner-dialog.tsx +373 -0
  26. package/src/components/ui/alert.tsx +66 -0
  27. package/src/components/ui/avatar.tsx +46 -0
  28. package/src/components/ui/badge.tsx +47 -0
  29. package/src/components/ui/button.tsx +61 -0
  30. package/src/components/ui/card.tsx +92 -0
  31. package/src/components/ui/slider.tsx +61 -0
  32. package/src/components/ui/tabs.tsx +64 -0
  33. package/src/components/volume-controls.tsx +93 -0
  34. package/src/constants.ts +45 -0
  35. package/src/context/air-jam-context.tsx +478 -0
  36. package/src/context/index.ts +2 -0
  37. package/src/context/socket-manager.ts +98 -0
  38. package/src/events.ts +151 -0
  39. package/src/hooks/use-air-jam-controller.ts +608 -0
  40. package/src/hooks/use-air-jam-host.ts +624 -0
  41. package/src/hooks/use-air-jam-shell.ts +343 -0
  42. package/src/hooks/use-fullscreen.ts +108 -0
  43. package/src/hooks/use-get-input.ts +97 -0
  44. package/src/hooks/use-send-signal.ts +164 -0
  45. package/src/index.ts +33 -0
  46. package/src/internal/input-manager.ts +362 -0
  47. package/src/protocol.ts +331 -0
  48. package/src/state/connection-store.ts +84 -0
  49. package/src/styles.css +128 -0
  50. package/src/types/browser.ts +42 -0
  51. package/src/utils/cn.ts +4 -0
  52. package/src/utils/ids.ts +31 -0
  53. package/src/utils/index.ts +1 -0
  54. package/src/utils/mode.ts +13 -0
  55. package/src/utils/network-ip.ts +145 -0
  56. package/src/utils/url-builder.ts +170 -0
@@ -0,0 +1,456 @@
1
+ import { MonitorSmartphone, Pause, Play, QrCode, Settings } from "lucide-react";
2
+ import QRCode from "qrcode";
3
+ import type { JSX } from "react";
4
+ import { useEffect, useMemo, useState } from "react";
5
+ import type { ConnectionStatus, GameState, PlayerProfile } from "../protocol";
6
+ import { Alert, AlertDescription } from "./ui/alert";
7
+ import { Avatar, AvatarImage } from "./ui/avatar";
8
+ import { Badge } from "./ui/badge";
9
+ import { Button } from "./ui/button";
10
+ import {
11
+ Card,
12
+ CardContent,
13
+ CardDescription,
14
+ CardHeader,
15
+ CardTitle,
16
+ } from "./ui/card";
17
+ import { Tabs, TabsContent, TabsList, TabsTrigger } from "./ui/tabs";
18
+ import { VolumeControls } from "./volume-controls";
19
+
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
+ const statusCopy: Record<ConnectionStatus, string> = {
32
+ idle: "Idle",
33
+ connecting: "Waiting for server…",
34
+ connected: "Ready for controllers",
35
+ disconnected: "Disconnected",
36
+ reconnecting: "Reconnecting…",
37
+ };
38
+
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 => {
58
+ const [qrUrl, setQrUrl] = useState<string | null>(null);
59
+ const [qrError, setQrError] = useState<string | null>(null);
60
+
61
+ useEffect(() => {
62
+ // No need to generate QR in child mode
63
+ if (isChildMode) return;
64
+
65
+ let mounted = true;
66
+ QRCode.toDataURL(joinUrl, {
67
+ margin: 1,
68
+ color: {
69
+ dark: "#0f172a",
70
+ light: "#ffffff",
71
+ },
72
+ width: 320,
73
+ })
74
+ .then((value) => {
75
+ if (mounted) {
76
+ setQrUrl(value);
77
+ setQrError(null);
78
+ }
79
+ })
80
+ .catch((err) => {
81
+ if (mounted) {
82
+ setQrError(err.message);
83
+ }
84
+ });
85
+
86
+ return () => {
87
+ mounted = false;
88
+ };
89
+ }, [joinUrl, isChildMode]);
90
+
91
+ const connectionVariant = useMemo<
92
+ "default" | "secondary" | "destructive" | "outline"
93
+ >(() => {
94
+ switch (connectionStatus) {
95
+ case "connected":
96
+ return "default";
97
+ case "connecting":
98
+ case "reconnecting":
99
+ return "secondary";
100
+ default:
101
+ return "destructive";
102
+ }
103
+ }, [connectionStatus]);
104
+
105
+ // In Child Mode (Platform), we hide the overlay completely
106
+ // because the Platform handles the UI/Header.
107
+ if (isChildMode) return null;
108
+
109
+ // Always render the navbar (unless child mode + playing)
110
+ return (
111
+ <>
112
+ {/* Top Navbar - Always Visible */}
113
+ {!isChildMode && (
114
+ <div className="pointer-events-none absolute inset-x-0 top-0 z-99999">
115
+ <div className="mx-auto w-full">
116
+ <div className="flex items-center justify-between px-4 py-2">
117
+ {/* Left: Room name */}
118
+ <div className="flex items-center gap-3">
119
+ <div>
120
+ <p className="text-muted-foreground text-xs tracking-[0.18em] uppercase">
121
+ Room
122
+ </p>
123
+ <p className="text-foreground text-lg font-semibold">
124
+ {roomId}
125
+ </p>
126
+ </div>
127
+ </div>
128
+
129
+ {/* Right: Avatar stack and play/pause */}
130
+ <div className="pointer-events-auto flex items-center gap-3">
131
+ {/* Avatar stack */}
132
+ {players.length > 0 && (
133
+ <div className="flex items-center -space-x-2">
134
+ {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>
147
+ ))}
148
+ {players.length > 4 && (
149
+ <div className="border-background bg-muted flex h-8 w-8 items-center justify-center rounded-full border-2 text-xs font-medium">
150
+ +{players.length - 4}
151
+ </div>
152
+ )}
153
+ </div>
154
+ )}
155
+
156
+ {/* 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
+ )}
173
+ </div>
174
+ </div>
175
+ </div>
176
+ </div>
177
+ )}
178
+
179
+ {/* Paused Overlay - Only visible when paused */}
180
+ {gameState === "paused" && (
181
+ <div className="pointer-events-none fixed inset-0 z-99998 flex items-center justify-center p-4">
182
+ <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">
184
+ <Tabs
185
+ defaultValue={isChildMode ? "settings" : "room-code"}
186
+ className="flex h-full w-full flex-col gap-0"
187
+ >
188
+ <TabsList className="border-border grid w-full shrink-0 grid-cols-2 rounded-none border-b bg-transparent p-0">
189
+ {!isChildMode && (
190
+ <TabsTrigger
191
+ value="room-code"
192
+ className="hover:bg-background/50 data-[state=active]:bg-background/50 flex items-center gap-2 rounded-none border-none bg-transparent"
193
+ >
194
+ <QrCode className="h-4 w-4" />
195
+ Room Code
196
+ </TabsTrigger>
197
+ )}
198
+ {isChildMode && (
199
+ <TabsTrigger
200
+ value="info"
201
+ className="hover:bg-background/50 data-[state=active]:bg-background/50 flex items-center gap-2 rounded-none border-none bg-transparent"
202
+ >
203
+ <MonitorSmartphone className="h-4 w-4" />
204
+ Platform
205
+ </TabsTrigger>
206
+ )}
207
+ <TabsTrigger
208
+ value="settings"
209
+ className="hover:bg-background/50 data-[state=active]:bg-background/50 flex items-center gap-2 rounded-none border-none bg-transparent"
210
+ >
211
+ <Settings className="h-4 w-4" />
212
+ Settings
213
+ </TabsTrigger>
214
+ </TabsList>
215
+
216
+ {!isChildMode && (
217
+ <TabsContent
218
+ value="room-code"
219
+ className="mt-0 min-h-0 flex-1 overflow-y-auto"
220
+ >
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>
242
+
243
+ <div className="border-border bg-card w-48 overflow-hidden rounded-lg border-2 shadow-md">
244
+ {qrUrl ? (
245
+ <img
246
+ src={qrUrl}
247
+ alt={`Join room ${roomId}`}
248
+ className="w-full rounded bg-white"
249
+ />
250
+ ) : (
251
+ <div className="text-muted-foreground flex h-48 items-center justify-center text-sm">
252
+ {qrError
253
+ ? `QR failed: ${qrError}`
254
+ : "Generating QR code…"}
255
+ </div>
256
+ )}
257
+ </div>
258
+
259
+ <div className="w-full">
260
+ {players.length === 0 ? (
261
+ <p className="text-muted-foreground py-4 text-center text-sm">
262
+ Waiting for controllers to join…
263
+ </p>
264
+ ) : (
265
+ <div className="space-y-3">
266
+ <p className="text-foreground text-center text-sm font-medium">
267
+ Connected Players ({players.length})
268
+ </p>
269
+ <ul className="flex flex-wrap items-center justify-center gap-4">
270
+ {players.map((player) => (
271
+ <li
272
+ key={player.id}
273
+ className="flex flex-col items-center gap-2"
274
+ >
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>
292
+ </div>
293
+ </li>
294
+ ))}
295
+ </ul>
296
+ </div>
297
+ )}
298
+ </div>
299
+
300
+ {lastError && (
301
+ <Alert variant="destructive" className="w-full">
302
+ <AlertDescription>{lastError}</AlertDescription>
303
+ </Alert>
304
+ )}
305
+
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
+ )}
319
+ </div>
320
+ </CardContent>
321
+ </div>
322
+ </TabsContent>
323
+ )}
324
+
325
+ {isChildMode && (
326
+ <TabsContent
327
+ value="info"
328
+ className="mt-0 min-h-0 flex-1 overflow-y-auto"
329
+ >
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>
372
+
373
+ {/* Play button in paused state */}
374
+ {onTogglePlayPause && (
375
+ <div className="pt-2">
376
+ <Button
377
+ type="button"
378
+ onClick={onTogglePlayPause}
379
+ size="lg"
380
+ >
381
+ <Play className="mr-2 h-5 w-5" />
382
+ Resume Game
383
+ </Button>
384
+ </div>
385
+ )}
386
+ </div>
387
+ </CardContent>
388
+ </div>
389
+ </TabsContent>
390
+ )}
391
+
392
+ <TabsContent
393
+ value="settings"
394
+ className="mt-0 min-h-0 flex-1 overflow-y-auto"
395
+ >
396
+ <CardContent className="pt-6 pb-6">
397
+ <div className="space-y-6">
398
+ {/* Audio Controls Section */}
399
+ <div className="space-y-4">
400
+ <VolumeControls />
401
+ </div>
402
+
403
+ {/* Connection Settings Section */}
404
+ <div className="space-y-4 border-t pt-4">
405
+ <h3 className="text-sm font-semibold">Connection</h3>
406
+ <div className="space-y-2">
407
+ <div className="flex items-center justify-between">
408
+ <span className="text-muted-foreground text-sm">
409
+ Status
410
+ </span>
411
+ <Badge variant={connectionVariant}>
412
+ {statusCopy[connectionStatus]}
413
+ </Badge>
414
+ </div>
415
+ <div className="flex items-center justify-between">
416
+ <span className="text-muted-foreground text-sm">
417
+ Room ID
418
+ </span>
419
+ <span className="font-mono text-sm">{roomId}</span>
420
+ </div>
421
+ <div className="flex items-center justify-between">
422
+ <span className="text-muted-foreground text-sm">
423
+ Players
424
+ </span>
425
+ <span className="text-sm font-medium">
426
+ {players.length}
427
+ </span>
428
+ </div>
429
+ </div>
430
+ </div>
431
+
432
+ {/* Display Settings Section */}
433
+ <div className="space-y-4 border-t pt-4">
434
+ <h3 className="text-sm font-semibold">Display</h3>
435
+ <div className="space-y-2">
436
+ <div className="flex items-center justify-between">
437
+ <span className="text-muted-foreground text-sm">
438
+ Game State
439
+ </span>
440
+ <Badge variant="outline" className="capitalize">
441
+ {gameState}
442
+ </Badge>
443
+ </div>
444
+ </div>
445
+ </div>
446
+ </div>
447
+ </CardContent>
448
+ </TabsContent>
449
+ </Tabs>
450
+ </Card>
451
+ </div>
452
+ </div>
453
+ )}
454
+ </>
455
+ );
456
+ };