@air-jam/sdk 0.1.1 → 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/{chunk-7M5D3Y3L.js → chunk-B5DURUWT.js} +11 -3
- package/dist/chunk-B5DURUWT.js.map +1 -0
- package/dist/index.cjs +1733 -1783
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -66
- package/dist/index.d.ts +23 -66
- package/dist/index.js +1722 -1779
- package/dist/index.js.map +1 -1
- package/dist/protocol.cjs +10 -0
- package/dist/protocol.cjs.map +1 -1
- package/dist/protocol.d.cts +19 -1
- package/dist/protocol.d.ts +19 -1
- package/dist/protocol.js +1 -1
- package/package.json +4 -1
- package/src/AirJamClient.ts +1 -552
- package/src/audio/audio-manager.ts +12 -1
- package/src/audio/volume-store.ts +47 -0
- package/src/components/controller-shell.tsx +201 -177
- package/src/components/{air-jam-overlay.tsx → host-shell.tsx} +59 -51
- package/src/components/qr-scanner-dialog.tsx +2 -2
- package/src/constants.ts +3 -2
- package/src/context/air-jam-context.tsx +3 -14
- package/src/hooks/use-air-jam-controller.ts +1 -1
- package/src/hooks/use-air-jam-host.ts +94 -49
- package/src/hooks/use-air-jam-shell.ts +36 -0
- package/src/index.ts +1 -3
- package/src/internal/input-manager.ts +28 -14
- package/src/protocol.ts +28 -0
- package/src/store/create-air-jam-store.ts +69 -7
- package/src/utils/url-builder.ts +3 -3
- package/dist/chunk-7M5D3Y3L.js.map +0 -1
- package/src/components/air-jam-debug.tsx +0 -77
- package/src/components/debug-state-dialog.tsx +0 -83
package/src/protocol.ts
CHANGED
|
@@ -227,6 +227,26 @@ export const hostJoinAsChildSchema = z.object({
|
|
|
227
227
|
|
|
228
228
|
export type HostJoinAsChildPayload = z.infer<typeof hostJoinAsChildSchema>;
|
|
229
229
|
|
|
230
|
+
/**
|
|
231
|
+
* Schema for creating a new room (server-issued room ID)
|
|
232
|
+
*/
|
|
233
|
+
export const hostCreateRoomSchema = z.object({
|
|
234
|
+
maxPlayers: z.number().int().min(1).max(16).default(8),
|
|
235
|
+
apiKey: z.string().optional(),
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
export type HostCreateRoomPayload = z.infer<typeof hostCreateRoomSchema>;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Schema for reconnecting to an existing room
|
|
242
|
+
*/
|
|
243
|
+
export const hostReconnectSchema = z.object({
|
|
244
|
+
roomId: roomCodeSchema,
|
|
245
|
+
apiKey: z.string().optional(),
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
export type HostReconnectPayload = z.infer<typeof hostReconnectSchema>;
|
|
249
|
+
|
|
230
250
|
export interface SystemLaunchGameAck {
|
|
231
251
|
ok: boolean;
|
|
232
252
|
joinToken?: string;
|
|
@@ -270,6 +290,14 @@ export interface ClientToServerEvents {
|
|
|
270
290
|
payload: HostRegisterSystemPayload,
|
|
271
291
|
callback: (ack: HostRegistrationAck) => void,
|
|
272
292
|
) => void;
|
|
293
|
+
"host:createRoom": (
|
|
294
|
+
payload: HostCreateRoomPayload,
|
|
295
|
+
callback: (ack: HostRegistrationAck) => void,
|
|
296
|
+
) => void;
|
|
297
|
+
"host:reconnect": (
|
|
298
|
+
payload: HostReconnectPayload,
|
|
299
|
+
callback: (ack: HostRegistrationAck) => void,
|
|
300
|
+
) => void;
|
|
273
301
|
"system:launchGame": (
|
|
274
302
|
payload: SystemLaunchGamePayload,
|
|
275
303
|
callback: (ack: SystemLaunchGameAck) => void,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect } from "react";
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
2
|
import { create, type StateCreator } from "zustand";
|
|
3
3
|
import { useAirJamContext, useAirJamState } from "../context/air-jam-context";
|
|
4
4
|
import type {
|
|
@@ -58,6 +58,9 @@ export function createAirJamStore<
|
|
|
58
58
|
});
|
|
59
59
|
|
|
60
60
|
// 2. The Hook Component
|
|
61
|
+
// Track if game UI is unloaded to disable proxy actions
|
|
62
|
+
const gameUiUnloadedRef = { current: false };
|
|
63
|
+
|
|
61
64
|
const useSyncedStore = <U>(
|
|
62
65
|
selector: (state: T) => U = (s) => s as unknown as U,
|
|
63
66
|
): U => {
|
|
@@ -71,6 +74,12 @@ export function createAirJamStore<
|
|
|
71
74
|
const socket =
|
|
72
75
|
role === "host" ? getSocket("host") : getSocket("controller");
|
|
73
76
|
|
|
77
|
+
// Keep a ref to the socket so we can access the LATEST one inside closures
|
|
78
|
+
const socketRef = useRef(socket);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
socketRef.current = socket;
|
|
81
|
+
}, [socket]);
|
|
82
|
+
|
|
74
83
|
// --- NETWORKING LOGIC ---
|
|
75
84
|
useEffect(() => {
|
|
76
85
|
if (!socket || !roomId || !role) {
|
|
@@ -109,6 +118,9 @@ export function createAirJamStore<
|
|
|
109
118
|
|
|
110
119
|
// CONTROLLER: Listen for state updates
|
|
111
120
|
if (role === "controller") {
|
|
121
|
+
// Reset unloaded flag when we have a new room/socket (game is loading)
|
|
122
|
+
gameUiUnloadedRef.current = false;
|
|
123
|
+
|
|
112
124
|
const handleSync = (payload: AirJamStateSyncPayload) => {
|
|
113
125
|
const { data } = payload;
|
|
114
126
|
// Merge incoming state, but KEEP the local actions
|
|
@@ -128,9 +140,39 @@ export function createAirJamStore<
|
|
|
128
140
|
};
|
|
129
141
|
|
|
130
142
|
socket.on("airjam:state_sync", handleSync);
|
|
131
|
-
|
|
143
|
+
|
|
144
|
+
// Also listen for client:unloadUi to clean up when game exits
|
|
145
|
+
// This ensures listeners are removed even if the component doesn't unmount
|
|
146
|
+
const handleUnloadUi = () => {
|
|
147
|
+
gameUiUnloadedRef.current = true; // Disable proxy actions
|
|
148
|
+
socket.off("airjam:state_sync", handleSync);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// Also listen for client:loadUi to re-enable proxy actions when new game loads
|
|
152
|
+
const handleLoadUi = () => {
|
|
153
|
+
gameUiUnloadedRef.current = false; // Re-enable proxy actions when game loads
|
|
154
|
+
};
|
|
155
|
+
socket.on("client:loadUi", handleLoadUi);
|
|
156
|
+
|
|
157
|
+
// Also listen for socket disconnect as a fallback to disable proxy actions
|
|
158
|
+
const handleDisconnect = () => {
|
|
159
|
+
gameUiUnloadedRef.current = true; // Disable proxy actions on disconnect
|
|
160
|
+
};
|
|
161
|
+
socket.on("disconnect", handleDisconnect);
|
|
162
|
+
|
|
163
|
+
// Register client:unloadUi listener
|
|
164
|
+
// Socket.IO listeners work even if registered before connection, but we include socket.id
|
|
165
|
+
// in dependencies to ensure the effect re-runs when socket connects (socket.id changes from undefined to a string)
|
|
166
|
+
socket.on("client:unloadUi", handleUnloadUi);
|
|
167
|
+
|
|
168
|
+
return () => {
|
|
169
|
+
socket.off("airjam:state_sync", handleSync);
|
|
170
|
+
socket.off("client:loadUi", handleLoadUi);
|
|
171
|
+
socket.off("client:unloadUi", handleUnloadUi);
|
|
172
|
+
socket.off("disconnect", handleDisconnect);
|
|
173
|
+
};
|
|
132
174
|
}
|
|
133
|
-
}, [socket, role, roomId]);
|
|
175
|
+
}, [socket, role, roomId, socket?.id]);
|
|
134
176
|
|
|
135
177
|
// --- PROXY ACTIONS (The Magic) ---
|
|
136
178
|
// On the Controller, we replace actions with Network Calls
|
|
@@ -176,17 +218,37 @@ export function createAirJamStore<
|
|
|
176
218
|
// We cast to the same type as T["actions"] to maintain type compatibility
|
|
177
219
|
(proxyActions as Record<string, (...args: unknown[]) => void>)[key] =
|
|
178
220
|
(...args: unknown[]) => {
|
|
179
|
-
//
|
|
180
|
-
//
|
|
221
|
+
// --- EXECUTION TIME GUARDS (THE FIX) ---
|
|
222
|
+
// Check guards at EXECUTION time, not creation time
|
|
223
|
+
// This ensures old closures are still safe even if they're called after game exit
|
|
224
|
+
|
|
225
|
+
// 1. Check if UI is unloaded right now (at execution time)
|
|
226
|
+
if (gameUiUnloadedRef.current) {
|
|
227
|
+
console.warn(
|
|
228
|
+
`[AirJamStore] Action "${key}" blocked: Game UI unloaded.`,
|
|
229
|
+
);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
181
232
|
|
|
182
|
-
// 2.
|
|
233
|
+
// 2. Check if we have a valid socket right now (at execution time)
|
|
234
|
+
const activeSocket = socketRef.current;
|
|
235
|
+
if (!activeSocket || !activeSocket.connected) {
|
|
236
|
+
console.warn(
|
|
237
|
+
`[AirJamStore] Action "${key}" blocked: Socket disconnected.`,
|
|
238
|
+
);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// 3. Check controllerId
|
|
183
243
|
if (!controllerId) {
|
|
184
244
|
console.warn(
|
|
185
245
|
"[AirJamStore] Cannot send action RPC: controllerId not set",
|
|
186
246
|
);
|
|
187
247
|
return;
|
|
188
248
|
}
|
|
189
|
-
|
|
249
|
+
|
|
250
|
+
// 4. Send RPC to Host (include controllerId to handle reconnections)
|
|
251
|
+
activeSocket.emit("controller:action_rpc", {
|
|
190
252
|
roomId,
|
|
191
253
|
actionName: key,
|
|
192
254
|
args,
|
package/src/utils/url-builder.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CONTROLLER_PATH, DEFAULT_SERVER_PORT } from "../constants";
|
|
2
2
|
import type { RoomCode } from "../protocol";
|
|
3
3
|
import { getLocalNetworkIp } from "./network-ip";
|
|
4
4
|
|
|
@@ -76,16 +76,16 @@ class UrlBuilder {
|
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
78
|
* Build a controller URL with room code.
|
|
79
|
+
* Controller is always at {baseUrl}/controller (standardized convention).
|
|
79
80
|
* Automatically uses local IP when running on localhost.
|
|
80
81
|
*/
|
|
81
82
|
async buildControllerUrl(
|
|
82
83
|
roomId: RoomCode,
|
|
83
84
|
options?: {
|
|
84
|
-
path?: string;
|
|
85
85
|
host?: string;
|
|
86
86
|
},
|
|
87
87
|
): Promise<string> {
|
|
88
|
-
const path =
|
|
88
|
+
const path = CONT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pB,EAAAA,WAAA,iBAAA,CAAA,GAAkB,iBAAA;AAClB,EAAAA,WAAA,mBAAA,CAAA,GAAoB,mBAAA;AAGpB,EAAAA,WAAA,uBAAA,CAAA,GAAwB,uBAAA;AACxB,EAAAA,WAAA,qBAAA,CAAA,GAAsB,qBAAA;AAGtB,EAAAA,WAAA,eAAA,CAAA,GAAgB,eAAA;AA1BN,EAAA,OAAAA,UAAAA;AAAA,CAAA,EAAA,SAAA,IAAA,EAAA;AAgGL,IAAM,wBAAA,GAA2B,EAAE,MAAA,CAAO;AAAA,EAC/C,MAAA,EAAQ,cAAA;AAAA,EACR,MAAA,EAAQ,CAAA,CAAE,MAAA,EAAO,CAAE,QAAA;AACrB,CAAC;AAMM,IAAM,sBAAA,GAAyB,EAAE,MAAA,CAAO;AAAA,EAC7C,MAAA,EAAQ,cAAA;AAAA,EACR,MAAA,EAAQ,EAAE,MAAA,EAAO;AAAA,EACjB,OAAA,EAAS,CAAA,CAAE,MAAA,EAAO,CAAE,GAAA;AACtB,CAAC;AAIM,IAAM,qBAAA,GAAwB,EAAE,MAAA,CAAO;AAAA,EAC5C,MAAA,EAAQ,cAAA;AAAA,EACR,SAAA,EAAW,EAAE,MAAA;AACf,CAAC;AA6GM,IAAM,mBAAA,GAAsB","file":"chunk-7M5D3Y3L.js","sourcesContent":["import { z } from \"zod\";\n\nexport type RoomCode = string;\n\nexport type ConnectionRole = \"host\" | \"controller\";\n\nexport type ConnectionStatus =\n | \"idle\"\n | \"connecting\"\n | \"connected\"\n | \"disconnected\"\n | \"reconnecting\";\n\nexport type RunMode = \"standalone\" | \"platform\";\n\nexport type GameState = \"paused\" | \"playing\";\n\nexport const roomCodeSchema = z\n .string()\n .trim()\n .min(4)\n .max(8)\n .regex(/^[A-Z0-9]+$/, { message: \"room code must contain only A-Z or 0-9\" });\n\nexport const controllerInputSchema = z.object({\n roomId: roomCodeSchema,\n controllerId: z.string().min(3),\n input: z.record(z.string(), z.unknown()), // Arbitrary JSON object - developers define their own structure\n});\n\nexport type ControllerInputPayload = Record<string, unknown>;\n\nexport interface ControllerInputEvent {\n roomId: RoomCode;\n controllerId: string;\n input: ControllerInputPayload;\n}\n\nexport const controllerStateSchema = z.object({\n roomId: roomCodeSchema,\n state: z.object({\n orientation: z.enum([\"portrait\", \"landscape\"]).optional(),\n message: z.string().optional(),\n gameState: z.enum([\"paused\", \"playing\"]).optional(),\n }),\n});\n\nexport type ControllerStateMessage = z.infer<typeof controllerStateSchema>;\n\nexport type ControllerStatePayload = z.infer<\n typeof controllerStateSchema\n>[\"state\"];\n\nexport const hostRegistrationSchema = z.object({\n roomId: roomCodeSchema,\n maxPlayers: z.number().int().min(1).max(16).default(8),\n apiKey: z.string().optional(),\n mode: z.enum([\"master\", \"child\"]).default(\"child\"),\n controllerUrl: z.string().url().optional(),\n});\n\nexport type HostRegistrationPayload = z.infer<typeof hostRegistrationSchema>;\n\nexport const controllerJoinSchema = z.object({\n roomId: roomCodeSchema,\n controllerId: z.string().min(3),\n nickname: z.string().trim().min(1).max(24).optional(),\n});\n\nexport type ControllerJoinPayload = z.infer<typeof controllerJoinSchema>;\n\nexport const controllerLeaveSchema = z.object({\n roomId: roomCodeSchema,\n controllerId: z.string().min(3),\n});\n\nexport type ControllerLeavePayload = z.infer<typeof controllerLeaveSchema>;\n\nexport interface PlayerProfile {\n id: string;\n label: string;\n color?: string;\n}\n\nexport interface HostRegistrationAck {\n ok: boolean;\n roomId?: RoomCode;\n message?: string;\n code?: ErrorCode | string;\n}\n\nexport interface ControllerJoinAck {\n ok: boolean;\n controllerId?: string;\n roomId?: RoomCode;\n message?: string;\n code?: ErrorCode | string;\n}\n\nexport const controllerSystemSchema = z.object({\n roomId: roomCodeSchema,\n command: z.enum([\"exit\", \"ready\", \"toggle_pause\"]),\n});\n\nexport type ControllerSystemPayload = z.infer<typeof controllerSystemSchema>;\n\n/**\n * Standard error codes for Air Jam\n */\nexport enum ErrorCode {\n // Room errors\n ROOM_NOT_FOUND = \"ROOM_NOT_FOUND\",\n ROOM_FULL = \"ROOM_FULL\",\n\n // Auth errors\n INVALID_API_KEY = \"INVALID_API_KEY\",\n UNAUTHORIZED = \"UNAUTHORIZED\",\n\n // Token errors\n INVALID_TOKEN = \"INVALID_TOKEN\",\n TOKEN_EXPIRED = \"TOKEN_EXPIRED\",\n\n // Connection errors\n CONNECTION_FAILED = \"CONNECTION_FAILED\",\n ALREADY_CONNECTED = \"ALREADY_CONNECTED\",\n\n // Validation errors\n INVALID_PAYLOAD = \"INVALID_PAYLOAD\",\n INVALID_ROOM_CODE = \"INVALID_ROOM_CODE\",\n\n // Server errors\n INTERNAL_SERVER_ERROR = \"INTERNAL_SERVER_ERROR\",\n SERVICE_UNAVAILABLE = \"SERVICE_UNAVAILABLE\",\n\n // Generic\n UNKNOWN_ERROR = \"UNKNOWN_ERROR\",\n}\n\n/**\n * Standardized error payload\n */\nexport interface ServerErrorPayload {\n code: ErrorCode | string; // Allow custom codes\n message: string;\n details?: unknown;\n}\n\nexport interface ControllerWelcomePayload {\n controllerId: string;\n roomId: RoomCode;\n player?: PlayerProfile;\n}\n\nexport interface ControllerJoinedNotice {\n controllerId: string;\n nickname?: string;\n player?: PlayerProfile;\n}\n\nexport interface ControllerLeftNotice {\n controllerId: string;\n}\n\nexport interface RoomReadyNotice {\n roomId: RoomCode;\n}\n\nexport interface HostLeftNotice {\n roomId: RoomCode;\n reason: string;\n}\n\nexport type SignalType = \"HAPTIC\" | \"TOAST\";\n\nexport interface HapticSignalPayload {\n pattern: \"light\" | \"medium\" | \"heavy\" | \"success\" | \"failure\" | \"custom\";\n sequence?: number | number[];\n}\n\nexport interface ToastSignalPayload {\n message: string;\n color?: string;\n duration?: number;\n}\n\nexport type SignalPayload =\n | {\n targetId?: string;\n type: \"HAPTIC\";\n payload: HapticSignalPayload;\n }\n | {\n targetId?: string;\n type: \"TOAST\";\n payload: ToastSignalPayload;\n };\n\nexport interface PlaySoundEventPayload {\n roomId: string;\n targetControllerId?: string; // If null, broadcast to all\n soundId: string;\n volume?: number;\n loop?: boolean;\n}\n\nexport const hostRegisterSystemSchema = z.object({\n roomId: roomCodeSchema,\n apiKey: z.string().optional(),\n});\n\nexport type HostRegisterSystemPayload = z.infer<\n typeof hostRegisterSystemSchema\n>;\n\nexport const systemLaunchGameSchema = z.object({\n roomId: roomCodeSchema,\n gameId: z.string(),\n gameUrl: z.string().url(),\n});\n\nexport type SystemLaunchGamePayload = z.infer<typeof systemLaunchGameSchema>;\n\nexport const hostJoinAsChildSchema = z.object({\n roomId: roomCodeSchema,\n joinToken: z.string(),\n});\n\nexport type HostJoinAsChildPayload = z.infer<typeof hostJoinAsChildSchema>;\n\nexport interface SystemLaunchGameAck {\n ok: boolean;\n joinToken?: string;\n message?: string;\n code?: ErrorCode | string;\n}\n\nexport interface ClientLoadUiPayload {\n url: string;\n}\n\nexport interface HostStateSyncPayload {\n roomId: string;\n data: Record<string, unknown>;\n}\n\nexport interface ControllerActionRpcPayload {\n roomId: string;\n actionName: string;\n args: unknown[];\n controllerId: string;\n}\n\nexport interface AirJamStateSyncPayload {\n roomId: string;\n data: Record<string, unknown>;\n}\n\nexport interface AirJamActionRpcPayload {\n actionName: string;\n args: unknown[];\n controllerId: string;\n}\n\nexport interface ClientToServerEvents {\n \"host:register\": (\n payload: z.infer<typeof hostRegistrationSchema>,\n callback: (ack: HostRegistrationAck) => void,\n ) => void;\n \"host:registerSystem\": (\n payload: HostRegisterSystemPayload,\n callback: (ack: HostRegistrationAck) => void,\n ) => void;\n \"system:launchGame\": (\n payload: SystemLaunchGamePayload,\n callback: (ack: SystemLaunchGameAck) => void,\n ) => void;\n \"host:joinAsChild\": (\n payload: HostJoinAsChildPayload,\n callback: (ack: HostRegistrationAck) => void,\n ) => void;\n \"system:closeGame\": (payload: { roomId: string }) => void;\n \"host:state\": (payload: z.infer<typeof controllerStateSchema>) => void;\n \"controller:join\": (\n payload: z.infer<typeof controllerJoinSchema>,\n callback: (ack: ControllerJoinAck) => void,\n ) => void;\n \"controller:leave\": (payload: z.infer<typeof controllerLeaveSchema>) => void;\n \"controller:input\": (payload: z.infer<typeof controllerInputSchema>) => void;\n \"controller:system\": (\n payload: z.infer<typeof controllerSystemSchema>,\n ) => void;\n \"host:system\": (payload: z.infer<typeof controllerSystemSchema>) => void;\n \"host:signal\": (payload: SignalPayload) => void;\n \"host:play_sound\": (payload: PlaySoundEventPayload) => void;\n \"controller:play_sound\": (payload: PlaySoundEventPayload) => void;\n \"host:state_sync\": (payload: HostStateSyncPayload) => void;\n \"controller:action_rpc\": (payload: ControllerActionRpcPayload) => void;\n}\n\nexport interface PlaySoundPayload {\n id: string;\n volume?: number;\n loop?: boolean;\n}\n\nexport interface ServerToClientEvents {\n \"server:roomReady\": (payload: RoomReadyNotice) => void;\n \"server:controllerJoined\": (payload: ControllerJoinedNotice) => void;\n \"server:controllerLeft\": (payload: ControllerLeftNotice) => void;\n \"server:input\": (payload: ControllerInputEvent) => void;\n \"server:error\": (payload: ServerErrorPayload) => void;\n \"server:state\": (payload: z.infer<typeof controllerStateSchema>) => void;\n \"server:welcome\": (payload: ControllerWelcomePayload) => void;\n \"server:hostLeft\": (payload: HostLeftNotice) => void;\n \"server:signal\": (payload: SignalPayload) => void;\n \"server:playSound\": (payload: PlaySoundPayload) => void;\n \"server:redirect\": (url: string) => void;\n \"server:closeChild\": () => void;\n \"client:loadUi\": (payload: ClientLoadUiPayload) => void;\n \"client:unloadUi\": () => void;\n \"airjam:state_sync\": (payload: AirJamStateSyncPayload) => void;\n \"airjam:action_rpc\": (payload: AirJamActionRpcPayload) => void;\n}\n\nexport interface InterServerEvents {\n ping: () => void;\n}\n\nexport type SocketData = Record<string, unknown>;\n\n// --- Proxy Protocol (iframe <-> parent) ---\n// Note: Proxy protocol is largely deprecated in favor of Direct Connect (airjam_force_connect=true),\n// but might still be used for UI coordination (e.g. hiding headers).\n\nexport const AIRJAM_PROXY_PREFIX = \"AIRJAM_\";\n\nexport interface ProxyMessageBase {\n type: string;\n}\n\nexport interface ProxyReadyMessage extends ProxyMessageBase {\n type: \"AIRJAM_READY\";\n}\n\nexport interface ProxyInputMessage extends ProxyMessageBase {\n type: \"AIRJAM_INPUT\";\n payload: ControllerInputPayload;\n}\n\nexport interface ProxyStateMessage extends ProxyMessageBase {\n type: \"AIRJAM_STATE\";\n payload: ControllerStatePayload;\n}\n\nexport type AirJamProxyMessage =\n | ProxyReadyMessage\n | ProxyInputMessage\n | ProxyStateMessage;\n"]}
|
|
@@ -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
|
-
};
|