@air-jam/sdk 0.1.2 → 0.1.3
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.
- package/dist/index.cjs +1621 -1770
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -41
- package/dist/index.d.ts +6 -41
- package/dist/index.js +1622 -1768
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/controller-shell.tsx +94 -69
- package/src/components/{air-jam-overlay.tsx → host-shell.tsx} +58 -50
- package/src/index.ts +1 -3
- package/src/components/air-jam-debug.tsx +0 -77
- package/src/components/debug-state-dialog.tsx +0 -83
package/package.json
CHANGED
|
@@ -7,12 +7,19 @@ import {
|
|
|
7
7
|
RefreshCw,
|
|
8
8
|
} from "lucide-react";
|
|
9
9
|
import type { JSX } from "react";
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
useCallback,
|
|
12
|
+
useEffect,
|
|
13
|
+
useMemo,
|
|
14
|
+
useState,
|
|
15
|
+
type ReactNode,
|
|
16
|
+
} from "react";
|
|
11
17
|
import { useStore } from "zustand";
|
|
12
18
|
import { useShallow } from "zustand/react/shallow";
|
|
13
19
|
import { useAirJamContext } from "../context/air-jam-context";
|
|
14
20
|
import { useFullscreen } from "../hooks/use-fullscreen";
|
|
15
|
-
import type {
|
|
21
|
+
import type { PlayerProfile } from "../protocol";
|
|
22
|
+
import { controllerSystemSchema } from "../protocol";
|
|
16
23
|
import { cn } from "../utils/cn";
|
|
17
24
|
import { detectRunMode } from "../utils/mode";
|
|
18
25
|
import { PlayerAvatar } from "./player-avatar";
|
|
@@ -20,19 +27,16 @@ import { QRScannerDialog } from "./qr-scanner-dialog";
|
|
|
20
27
|
import { Button } from "./ui/button";
|
|
21
28
|
|
|
22
29
|
interface ControllerShellProps {
|
|
23
|
-
roomId?: string | null;
|
|
24
|
-
connectionStatus: ConnectionStatus;
|
|
25
30
|
/** Optional forced orientation. If not set, no orientation enforcement is applied. */
|
|
26
31
|
forceOrientation?: "portrait" | "landscape";
|
|
27
32
|
children: ReactNode;
|
|
28
|
-
|
|
29
|
-
onTogglePlayPause?: () => void;
|
|
30
|
-
onReconnect?: (roomCode: string) => void;
|
|
31
|
-
onRefresh?: () => void;
|
|
33
|
+
/** Optional custom actions to display in the header */
|
|
32
34
|
customActions?: ReactNode;
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
const describeStatus = (
|
|
37
|
+
const describeStatus = (
|
|
38
|
+
status: "connected" | "connecting" | "reconnecting" | "disconnected" | "idle",
|
|
39
|
+
): string => {
|
|
36
40
|
switch (status) {
|
|
37
41
|
case "connected":
|
|
38
42
|
return "Connected to host";
|
|
@@ -58,16 +62,12 @@ const orientationMatches = (
|
|
|
58
62
|
};
|
|
59
63
|
|
|
60
64
|
export const ControllerShell = ({
|
|
61
|
-
roomId,
|
|
62
|
-
connectionStatus,
|
|
63
65
|
forceOrientation,
|
|
64
66
|
children,
|
|
65
|
-
gameState = "paused",
|
|
66
|
-
onTogglePlayPause,
|
|
67
|
-
onReconnect,
|
|
68
|
-
onRefresh,
|
|
69
67
|
customActions,
|
|
70
68
|
}: ControllerShellProps): JSX.Element => {
|
|
69
|
+
const { store, getSocket, disconnectSocket } = useAirJamContext();
|
|
70
|
+
|
|
71
71
|
const isChildMode = useMemo(() => detectRunMode() === "platform", []);
|
|
72
72
|
const [isOrientationOk, setOrientationOk] = useState(() =>
|
|
73
73
|
orientationMatches(forceOrientation),
|
|
@@ -75,19 +75,21 @@ export const ControllerShell = ({
|
|
|
75
75
|
const [isScannerOpen, setIsScannerOpen] = useState(false);
|
|
76
76
|
const { isFullscreen, toggleFullscreen } = useFullscreen();
|
|
77
77
|
|
|
78
|
-
//
|
|
79
|
-
const {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
78
|
+
// Read all state from the store (works with both useAirJamController and useAirJamShell)
|
|
79
|
+
const { roomId, connectionStatus, gameState, controllerId, players } =
|
|
80
|
+
useStore(
|
|
81
|
+
store,
|
|
82
|
+
useShallow((state) => ({
|
|
83
|
+
roomId: state.roomId,
|
|
84
|
+
connectionStatus: state.connectionStatus,
|
|
85
|
+
gameState: state.gameState,
|
|
86
86
|
controllerId: state.controllerId,
|
|
87
87
|
players: state.players,
|
|
88
|
-
}),
|
|
89
|
-
)
|
|
90
|
-
|
|
88
|
+
})),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// Get socket for callbacks
|
|
92
|
+
const socket = useMemo(() => getSocket("controller"), [getSocket]);
|
|
91
93
|
|
|
92
94
|
// Find current player profile - must exist from server (single source of truth)
|
|
93
95
|
const currentPlayer = useMemo(() => {
|
|
@@ -102,6 +104,33 @@ export const ControllerShell = ({
|
|
|
102
104
|
return found || null;
|
|
103
105
|
}, [controllerId, players, connectionStatus]);
|
|
104
106
|
|
|
107
|
+
// Implement sendSystemCommand using socket manager
|
|
108
|
+
const sendSystemCommand = useCallback(
|
|
109
|
+
(command: "exit" | "ready" | "toggle_pause") => {
|
|
110
|
+
const storeState = store.getState();
|
|
111
|
+
if (!roomId || !storeState.controllerId || !socket) return;
|
|
112
|
+
if (!socket.connected) return;
|
|
113
|
+
|
|
114
|
+
const payload = controllerSystemSchema.safeParse({
|
|
115
|
+
roomId,
|
|
116
|
+
command,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
if (payload.success) {
|
|
120
|
+
socket.emit("controller:system", payload.data);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
[roomId, socket, store],
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
// Implement reconnect
|
|
127
|
+
const reconnect = useCallback(() => {
|
|
128
|
+
if (!roomId) return;
|
|
129
|
+
disconnectSocket("controller");
|
|
130
|
+
// Force reconnection by updating room ID (triggers hook reconnection)
|
|
131
|
+
store.getState().setRoomId(roomId);
|
|
132
|
+
}, [roomId, disconnectSocket, store]);
|
|
133
|
+
|
|
105
134
|
useEffect(() => {
|
|
106
135
|
if (!forceOrientation) {
|
|
107
136
|
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
@@ -136,7 +165,7 @@ export const ControllerShell = ({
|
|
|
136
165
|
if (isChildMode) {
|
|
137
166
|
return (
|
|
138
167
|
<div className="dark">
|
|
139
|
-
<div className="text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden bg-
|
|
168
|
+
<div className="text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden bg-black select-none">
|
|
140
169
|
<main className="relative flex min-h-0 flex-1 items-center justify-center overflow-hidden p-2 select-none sm:p-4">
|
|
141
170
|
{!orientationOk && (
|
|
142
171
|
<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,7 +194,7 @@ export const ControllerShell = ({
|
|
|
165
194
|
|
|
166
195
|
return (
|
|
167
196
|
<div className="dark">
|
|
168
|
-
<div className="
|
|
197
|
+
<div className="text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden bg-black select-none">
|
|
169
198
|
<header className="pointer-events-none sticky top-0 z-50 flex items-center justify-between border-b px-6 py-2">
|
|
170
199
|
<div className="pointer-events-auto flex items-center gap-3">
|
|
171
200
|
{typeof window !== "undefined" &&
|
|
@@ -212,12 +241,12 @@ export const ControllerShell = ({
|
|
|
212
241
|
</div>
|
|
213
242
|
<div className="pointer-events-auto flex items-center gap-3">
|
|
214
243
|
{customActions}
|
|
215
|
-
{
|
|
244
|
+
{roomId && (
|
|
216
245
|
<Button
|
|
217
246
|
type="button"
|
|
218
247
|
variant="outline"
|
|
219
248
|
size="icon"
|
|
220
|
-
onClick={
|
|
249
|
+
onClick={() => window.location.reload()}
|
|
221
250
|
disabled={
|
|
222
251
|
connectionStatus === "connecting" ||
|
|
223
252
|
connectionStatus === "reconnecting"
|
|
@@ -235,34 +264,30 @@ export const ControllerShell = ({
|
|
|
235
264
|
/>
|
|
236
265
|
</Button>
|
|
237
266
|
)}
|
|
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
|
-
<Play className="h-5 w-5" />
|
|
263
|
-
)}
|
|
264
|
-
</Button>
|
|
265
|
-
)}
|
|
267
|
+
<Button
|
|
268
|
+
type="button"
|
|
269
|
+
variant="outline"
|
|
270
|
+
size="icon"
|
|
271
|
+
onClick={() => setIsScannerOpen(true)}
|
|
272
|
+
aria-label="Scan QR code to reconnect"
|
|
273
|
+
title="Scan QR code to reconnect"
|
|
274
|
+
>
|
|
275
|
+
<QrCode className="h-5 w-5" />
|
|
276
|
+
</Button>
|
|
277
|
+
<Button
|
|
278
|
+
type="button"
|
|
279
|
+
variant="outline"
|
|
280
|
+
size="icon"
|
|
281
|
+
onClick={() => sendSystemCommand("toggle_pause")}
|
|
282
|
+
aria-label={gameState === "playing" ? "Pause" : "Play"}
|
|
283
|
+
title={gameState === "playing" ? "Pause" : "Play"}
|
|
284
|
+
>
|
|
285
|
+
{gameState === "playing" ? (
|
|
286
|
+
<Pause className="h-5 w-5" />
|
|
287
|
+
) : (
|
|
288
|
+
<Play className="h-5 w-5" />
|
|
289
|
+
)}
|
|
290
|
+
</Button>
|
|
266
291
|
<Button
|
|
267
292
|
type="button"
|
|
268
293
|
variant="outline"
|
|
@@ -304,17 +329,17 @@ export const ControllerShell = ({
|
|
|
304
329
|
</div>
|
|
305
330
|
</main>
|
|
306
331
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
332
|
+
<QRScannerDialog
|
|
333
|
+
isOpen={isScannerOpen}
|
|
334
|
+
onClose={() => setIsScannerOpen(false)}
|
|
335
|
+
onScan={(roomCode) => {
|
|
336
|
+
// Update room ID in the store, which will trigger reconnection
|
|
337
|
+
store.getState().setRoomId(roomCode);
|
|
338
|
+
reconnect();
|
|
339
|
+
setIsScannerOpen(false);
|
|
340
|
+
}}
|
|
341
|
+
currentRoomId={roomId}
|
|
342
|
+
/>
|
|
318
343
|
</div>
|
|
319
344
|
</div>
|
|
320
345
|
);
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
} from "lucide-react";
|
|
11
11
|
import QRCode from "qrcode";
|
|
12
12
|
import type { JSX } from "react";
|
|
13
|
-
import { useEffect, useMemo, useState } from "react";
|
|
13
|
+
import { useEffect, useMemo, useState, type ReactNode } from "react";
|
|
14
14
|
import { useAirJamHost } from "../hooks/use-air-jam-host";
|
|
15
15
|
import type { ConnectionStatus } from "../protocol";
|
|
16
16
|
import { PlayerAvatar } from "./player-avatar";
|
|
@@ -29,7 +29,11 @@ const statusCopy: Record<ConnectionStatus, string> = {
|
|
|
29
29
|
reconnecting: "Reconnecting…",
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
interface HostShellProps {
|
|
33
|
+
children: ReactNode;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const HostShell = ({ children }: HostShellProps): JSX.Element => {
|
|
33
37
|
const host = useAirJamHost();
|
|
34
38
|
const {
|
|
35
39
|
roomId,
|
|
@@ -149,64 +153,64 @@ export const AirJamOverlay = (): JSX.Element | null => {
|
|
|
149
153
|
|
|
150
154
|
// In Child Mode (Platform), we hide the overlay completely
|
|
151
155
|
// because the Platform handles the UI/Header.
|
|
152
|
-
if (isChildMode)
|
|
156
|
+
if (isChildMode) {
|
|
157
|
+
return <>{children}</>;
|
|
158
|
+
}
|
|
153
159
|
|
|
154
160
|
// Always render the navbar (unless child mode + playing)
|
|
155
161
|
return (
|
|
156
|
-
<div className="dark">
|
|
162
|
+
<div className="dark relative">
|
|
157
163
|
{/* Top Navbar - Always Visible */}
|
|
158
|
-
|
|
159
|
-
<div className="
|
|
160
|
-
<div className="
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
<div
|
|
164
|
-
<
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
</p>
|
|
171
|
-
</div>
|
|
164
|
+
<div className="pointer-events-none absolute inset-x-0 top-0 z-99999">
|
|
165
|
+
<div className="mx-auto w-full">
|
|
166
|
+
<div className="flex items-center justify-between px-4 py-2">
|
|
167
|
+
{/* Left: Room name */}
|
|
168
|
+
<div className="flex items-center gap-3">
|
|
169
|
+
<div>
|
|
170
|
+
<p className="text-muted-foreground text-xs tracking-[0.18em] uppercase">
|
|
171
|
+
Room
|
|
172
|
+
</p>
|
|
173
|
+
<p className="text-foreground text-lg font-semibold">
|
|
174
|
+
{roomId}
|
|
175
|
+
</p>
|
|
172
176
|
</div>
|
|
177
|
+
</div>
|
|
173
178
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
)}
|
|
187
|
-
</div>
|
|
188
|
-
)}
|
|
189
|
-
|
|
190
|
-
{/* Play/Pause button */}
|
|
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" />
|
|
179
|
+
{/* Right: Avatar stack and play/pause */}
|
|
180
|
+
<div className="pointer-events-auto flex items-center gap-3">
|
|
181
|
+
{/* Avatar stack */}
|
|
182
|
+
{players.length > 0 && (
|
|
183
|
+
<div className="flex items-center -space-x-2">
|
|
184
|
+
{players.slice(0, 4).map((player) => (
|
|
185
|
+
<PlayerAvatar key={player.id} player={player} size="sm" />
|
|
186
|
+
))}
|
|
187
|
+
{players.length > 4 && (
|
|
188
|
+
<div className="border-background bg-muted flex h-8 w-8 items-center justify-center rounded-full border-2 text-xs font-medium">
|
|
189
|
+
+{players.length - 4}
|
|
190
|
+
</div>
|
|
203
191
|
)}
|
|
204
|
-
</
|
|
205
|
-
|
|
192
|
+
</div>
|
|
193
|
+
)}
|
|
194
|
+
|
|
195
|
+
{/* Play/Pause button */}
|
|
196
|
+
<Button
|
|
197
|
+
type="button"
|
|
198
|
+
variant="ghost"
|
|
199
|
+
size="icon"
|
|
200
|
+
onClick={toggleGameState}
|
|
201
|
+
aria-label={gameState === "playing" ? "Pause" : "Play"}
|
|
202
|
+
className="h-8 w-8"
|
|
203
|
+
>
|
|
204
|
+
{gameState === "playing" ? (
|
|
205
|
+
<Pause className="h-4 w-4" />
|
|
206
|
+
) : (
|
|
207
|
+
<Play className="h-4 w-4" />
|
|
208
|
+
)}
|
|
209
|
+
</Button>
|
|
206
210
|
</div>
|
|
207
211
|
</div>
|
|
208
212
|
</div>
|
|
209
|
-
|
|
213
|
+
</div>
|
|
210
214
|
|
|
211
215
|
{/* Paused Overlay - Only visible when paused */}
|
|
212
216
|
{gameState === "paused" && (
|
|
@@ -508,6 +512,10 @@ export const AirJamOverlay = (): JSX.Element | null => {
|
|
|
508
512
|
</div>
|
|
509
513
|
</div>
|
|
510
514
|
)}
|
|
515
|
+
|
|
516
|
+
{/* Game content - rendered as children */}
|
|
517
|
+
{children}
|
|
511
518
|
</div>
|
|
512
519
|
);
|
|
513
520
|
};
|
|
521
|
+
|
package/src/index.ts
CHANGED
|
@@ -5,10 +5,8 @@ export * from "./context";
|
|
|
5
5
|
export * from "./audio";
|
|
6
6
|
|
|
7
7
|
// Components
|
|
8
|
-
export * from "./components/air-jam-debug";
|
|
9
|
-
export * from "./components/air-jam-overlay";
|
|
10
8
|
export * from "./components/controller-shell";
|
|
11
|
-
export * from "./components/
|
|
9
|
+
export * from "./components/host-shell";
|
|
12
10
|
export * from "./components/player-avatar";
|
|
13
11
|
export * from "./components/ui/button";
|
|
14
12
|
export * from "./components/ui/slider";
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { Bug } from "lucide-react";
|
|
2
|
-
import type { JSX } from "react";
|
|
3
|
-
import { useEffect, useState } from "react";
|
|
4
|
-
import { DebugStateDialog } from "./debug-state-dialog";
|
|
5
|
-
import { Button } from "./ui/button";
|
|
6
|
-
|
|
7
|
-
interface AirJamDebugProps {
|
|
8
|
-
state: unknown;
|
|
9
|
-
title?: string;
|
|
10
|
-
updateInterval?: number;
|
|
11
|
-
className?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A debug component that displays state in a dialog.
|
|
16
|
-
* Place this component anywhere in your React tree and pass the state you want to debug.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```tsx
|
|
20
|
-
* const { phase, scores } = useGameStore((state) => state);
|
|
21
|
-
*
|
|
22
|
-
* return (
|
|
23
|
-
* <div>
|
|
24
|
-
* <AirJamDebug state={{ phase, scores }} />
|
|
25
|
-
* {/* Your game UI *\/}
|
|
26
|
-
* </div>
|
|
27
|
-
* );
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
export const AirJamDebug = ({
|
|
31
|
-
state,
|
|
32
|
-
title = "Debug State",
|
|
33
|
-
updateInterval = 100,
|
|
34
|
-
className,
|
|
35
|
-
}: AirJamDebugProps): JSX.Element => {
|
|
36
|
-
const [isOpen, setIsOpen] = useState(false);
|
|
37
|
-
const [currentState, setCurrentState] = useState(state);
|
|
38
|
-
|
|
39
|
-
// Update state periodically when dialog is open
|
|
40
|
-
useEffect(() => {
|
|
41
|
-
if (!isOpen) return;
|
|
42
|
-
|
|
43
|
-
const interval = setInterval(() => {
|
|
44
|
-
setCurrentState(state);
|
|
45
|
-
}, updateInterval);
|
|
46
|
-
|
|
47
|
-
// Update immediately
|
|
48
|
-
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
49
|
-
setCurrentState(state);
|
|
50
|
-
|
|
51
|
-
return () => clearInterval(interval);
|
|
52
|
-
}, [isOpen, state, updateInterval]);
|
|
53
|
-
|
|
54
|
-
return (
|
|
55
|
-
<>
|
|
56
|
-
<Button
|
|
57
|
-
type="button"
|
|
58
|
-
variant="ghost"
|
|
59
|
-
size="icon"
|
|
60
|
-
onClick={() => setIsOpen(true)}
|
|
61
|
-
aria-label="Debug state"
|
|
62
|
-
title={title}
|
|
63
|
-
className={`text-white hover:bg-white/20 hover:text-white ${className || ""}`}
|
|
64
|
-
>
|
|
65
|
-
<Bug className="h-4 w-4" />
|
|
66
|
-
</Button>
|
|
67
|
-
|
|
68
|
-
<DebugStateDialog
|
|
69
|
-
isOpen={isOpen}
|
|
70
|
-
onClose={() => setIsOpen(false)}
|
|
71
|
-
getState={() => currentState}
|
|
72
|
-
title={title}
|
|
73
|
-
updateInterval={updateInterval}
|
|
74
|
-
/>
|
|
75
|
-
</>
|
|
76
|
-
);
|
|
77
|
-
};
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { Check, Copy } from "lucide-react";
|
|
2
|
-
import type { JSX } from "react";
|
|
3
|
-
import { useEffect, useState } from "react";
|
|
4
|
-
import { Button } from "./ui/button";
|
|
5
|
-
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "./ui/dialog";
|
|
6
|
-
|
|
7
|
-
interface DebugStateDialogProps {
|
|
8
|
-
isOpen: boolean;
|
|
9
|
-
onClose: () => void;
|
|
10
|
-
getState: () => unknown;
|
|
11
|
-
title?: string;
|
|
12
|
-
updateInterval?: number;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const DebugStateDialog = ({
|
|
16
|
-
isOpen,
|
|
17
|
-
onClose,
|
|
18
|
-
getState,
|
|
19
|
-
title = "Debug State",
|
|
20
|
-
updateInterval = 100,
|
|
21
|
-
}: DebugStateDialogProps): JSX.Element => {
|
|
22
|
-
const [state, setState] = useState<unknown>(() => getState());
|
|
23
|
-
const [copied, setCopied] = useState(false);
|
|
24
|
-
|
|
25
|
-
useEffect(() => {
|
|
26
|
-
if (!isOpen) return;
|
|
27
|
-
|
|
28
|
-
// Update state periodically to see changes
|
|
29
|
-
const interval = setInterval(() => {
|
|
30
|
-
setState(getState());
|
|
31
|
-
}, updateInterval);
|
|
32
|
-
|
|
33
|
-
// Update immediately on mount
|
|
34
|
-
const timeoutId = setTimeout(() => {
|
|
35
|
-
setState(getState());
|
|
36
|
-
}, 0);
|
|
37
|
-
|
|
38
|
-
return () => {
|
|
39
|
-
clearInterval(interval);
|
|
40
|
-
clearTimeout(timeoutId);
|
|
41
|
-
};
|
|
42
|
-
}, [isOpen, getState, updateInterval]);
|
|
43
|
-
|
|
44
|
-
const jsonString = JSON.stringify(state, null, 2);
|
|
45
|
-
|
|
46
|
-
const handleCopy = async () => {
|
|
47
|
-
try {
|
|
48
|
-
await navigator.clipboard.writeText(jsonString);
|
|
49
|
-
setCopied(true);
|
|
50
|
-
setTimeout(() => setCopied(false), 2000);
|
|
51
|
-
} catch (error) {
|
|
52
|
-
console.error("Failed to copy:", error);
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
return (
|
|
57
|
-
<Dialog open={isOpen} onOpenChange={onClose}>
|
|
58
|
-
<DialogContent className="dark max-h-[90vh] max-w-2xl flex-col">
|
|
59
|
-
<DialogHeader>
|
|
60
|
-
<DialogTitle className="text-foreground">{title}</DialogTitle>
|
|
61
|
-
</DialogHeader>
|
|
62
|
-
<div className="relative flex-1 overflow-auto pt-4">
|
|
63
|
-
<Button
|
|
64
|
-
variant="ghost"
|
|
65
|
-
size="icon-sm"
|
|
66
|
-
onClick={handleCopy}
|
|
67
|
-
className="text-muted-foreground hover:text-foreground absolute top-6 right-2 z-10"
|
|
68
|
-
aria-label="Copy state"
|
|
69
|
-
>
|
|
70
|
-
{copied ? (
|
|
71
|
-
<Check className="h-4 w-4" />
|
|
72
|
-
) : (
|
|
73
|
-
<Copy className="h-4 w-4" />
|
|
74
|
-
)}
|
|
75
|
-
</Button>
|
|
76
|
-
<pre className="bg-muted text-muted-foreground overflow-auto rounded-lg border p-4 font-mono text-xs">
|
|
77
|
-
{jsonString}
|
|
78
|
-
</pre>
|
|
79
|
-
</div>
|
|
80
|
-
</DialogContent>
|
|
81
|
-
</Dialog>
|
|
82
|
-
);
|
|
83
|
-
};
|