@dbx-tools/ui 0.9.185 → 0.9.188
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/README.md +6 -0
- package/index.react.ts +5 -0
- package/index.ts +1 -1
- package/package.json +7 -6
- package/src/react/clipboard.ts +27 -0
- package/src/react/debug-capture.tsx +545 -0
package/README.md
CHANGED
|
@@ -15,6 +15,12 @@ import "@dbx-tools/ui/styles.css";
|
|
|
15
15
|
`./styles.css` provides Tailwind, Streamdown, code-block, and brand-token bridge
|
|
16
16
|
styles for dbx-tools UI packages.
|
|
17
17
|
|
|
18
|
+
The same entrypoint exports `BrowserDebugCaptureButton` and
|
|
19
|
+
`useBrowserDebugCapture`. They appear only when the AppKit server publishes the
|
|
20
|
+
debug-capture plugin configuration. Pass named CSS selectors through `targets`
|
|
21
|
+
to include component rectangles, scroll metrics, dataset state, or inline CSS
|
|
22
|
+
viewport variables in each browser event snapshot.
|
|
23
|
+
|
|
18
24
|
## Choose a UI package
|
|
19
25
|
|
|
20
26
|
- Use `@dbx-tools/ui` for common AppKit components, branding, authentication,
|
package/index.react.ts
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
// Hand edits are overwritten on the next watch; this file is read-only.
|
|
4
4
|
|
|
5
5
|
export * as brandPicker from "./src/react/brand-picker.tsx";
|
|
6
|
+
export * as clipboard from "./src/react/clipboard.ts";
|
|
7
|
+
export * as debugCapture from "./src/react/debug-capture.tsx";
|
|
6
8
|
export * as errorBoundary from "./src/react/error-boundary.tsx";
|
|
7
9
|
export * as highlightedCode from "./src/react/highlighted-code.tsx";
|
|
8
10
|
export * as recordPreviewData from "./src/react/record-preview-data.ts";
|
|
@@ -10,6 +12,9 @@ export * as recordPreview from "./src/react/record-preview.tsx";
|
|
|
10
12
|
export * as shikiPlugin from "./src/react/shiki-plugin.ts";
|
|
11
13
|
export { BrandPicker } from "./src/react/brand-picker.tsx";
|
|
12
14
|
export type { BrandPreset, BrandPickerProps } from "./src/react/brand-picker.tsx";
|
|
15
|
+
export { copyText } from "./src/react/clipboard.ts";
|
|
16
|
+
export { useBrowserDebugCapture, BrowserDebugCaptureButton } from "./src/react/debug-capture.tsx";
|
|
17
|
+
export type { BrowserDebugCaptureTarget, UseBrowserDebugCaptureOptions, BrowserDebugCaptureState, BrowserDebugCaptureButtonProps } from "./src/react/debug-capture.tsx";
|
|
13
18
|
export { ErrorBoundary } from "./src/react/error-boundary.tsx";
|
|
14
19
|
export { detectCodeLanguage, useHighlightedHtml, HighlightedCodeBlock, JsonBlock } from "./src/react/highlighted-code.tsx";
|
|
15
20
|
export type { DetectedCodeLanguage } from "./src/react/highlighted-code.tsx";
|
package/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -31,11 +31,12 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@better-auth/passkey": "1.7.6",
|
|
33
33
|
"@databricks/appkit-ui": "0.81.0",
|
|
34
|
-
"@dbx-tools/shared-
|
|
35
|
-
"@dbx-tools/shared-
|
|
36
|
-
"@dbx-tools/shared-
|
|
37
|
-
"@dbx-tools/shared-email
|
|
38
|
-
"@dbx-tools/shared-
|
|
34
|
+
"@dbx-tools/shared-appkit": "^0.9.188",
|
|
35
|
+
"@dbx-tools/shared-auth": "^0.9.188",
|
|
36
|
+
"@dbx-tools/shared-core": "^0.9.188",
|
|
37
|
+
"@dbx-tools/shared-email": "^0.9.188",
|
|
38
|
+
"@dbx-tools/shared-email-template": "^0.9.188",
|
|
39
|
+
"@dbx-tools/shared-search": "^0.9.188",
|
|
39
40
|
"lucide-react": "^0.554.0",
|
|
40
41
|
"shiki": "^3.0.0",
|
|
41
42
|
"streamdown": "^2.5.0",
|
|
@@ -46,7 +47,7 @@
|
|
|
46
47
|
"publishConfig": {
|
|
47
48
|
"access": "public"
|
|
48
49
|
},
|
|
49
|
-
"version": "0.9.
|
|
50
|
+
"version": "0.9.188",
|
|
50
51
|
"types": "index.ts",
|
|
51
52
|
"dbxToolsConfig": {
|
|
52
53
|
"tags": [
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copy text through the modern Clipboard API with a plain-HTTP fallback.
|
|
3
|
+
*/
|
|
4
|
+
export async function copyText(text: string): Promise<boolean> {
|
|
5
|
+
try {
|
|
6
|
+
if (navigator.clipboard?.writeText) {
|
|
7
|
+
await navigator.clipboard.writeText(text);
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
} catch {
|
|
11
|
+
// Fall through for denied permissions and non-secure browser contexts.
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
const area = document.createElement("textarea");
|
|
15
|
+
area.value = text;
|
|
16
|
+
area.setAttribute("readonly", "");
|
|
17
|
+
area.style.position = "fixed";
|
|
18
|
+
area.style.opacity = "0";
|
|
19
|
+
document.body.appendChild(area);
|
|
20
|
+
area.select();
|
|
21
|
+
const copied = document.execCommand("copy");
|
|
22
|
+
area.remove();
|
|
23
|
+
return copied;
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Button,
|
|
3
|
+
Tooltip,
|
|
4
|
+
TooltipContent,
|
|
5
|
+
TooltipProvider,
|
|
6
|
+
TooltipTrigger,
|
|
7
|
+
cn,
|
|
8
|
+
usePluginClientConfig,
|
|
9
|
+
} from "@databricks/appkit-ui/react";
|
|
10
|
+
import {
|
|
11
|
+
DEBUG_CAPTURE_PLUGIN_NAME,
|
|
12
|
+
DebugCaptureClientConfigSchema,
|
|
13
|
+
DebugCaptureStartResponseSchema,
|
|
14
|
+
DebugCaptureStopResponseSchema,
|
|
15
|
+
debugCaptureRoute,
|
|
16
|
+
type DebugCaptureEvent,
|
|
17
|
+
type DebugCaptureScalar,
|
|
18
|
+
} from "@dbx-tools/shared-appkit/debug";
|
|
19
|
+
import { BugIcon, CheckIcon } from "lucide-react";
|
|
20
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
21
|
+
|
|
22
|
+
import { copyText } from "./clipboard.ts";
|
|
23
|
+
|
|
24
|
+
const ACTIVE_CAPTURE_STORAGE_KEY = "dbx-tools.browser-debug-capture";
|
|
25
|
+
const MAX_BUFFERED_EVENTS = 40;
|
|
26
|
+
const FLUSH_INTERVAL_MS = 500;
|
|
27
|
+
|
|
28
|
+
const DEFAULT_DOCUMENT_EVENTS = [
|
|
29
|
+
"animationend",
|
|
30
|
+
"animationstart",
|
|
31
|
+
"beforeinput",
|
|
32
|
+
"change",
|
|
33
|
+
"click",
|
|
34
|
+
"compositionend",
|
|
35
|
+
"compositionstart",
|
|
36
|
+
"compositionupdate",
|
|
37
|
+
"contextmenu",
|
|
38
|
+
"dblclick",
|
|
39
|
+
"dragover",
|
|
40
|
+
"dragstart",
|
|
41
|
+
"drop",
|
|
42
|
+
"focusin",
|
|
43
|
+
"focusout",
|
|
44
|
+
"fullscreenchange",
|
|
45
|
+
"input",
|
|
46
|
+
"keydown",
|
|
47
|
+
"keyup",
|
|
48
|
+
"pointercancel",
|
|
49
|
+
"pointerdown",
|
|
50
|
+
"pointermove",
|
|
51
|
+
"pointerup",
|
|
52
|
+
"scroll",
|
|
53
|
+
"selectionchange",
|
|
54
|
+
"submit",
|
|
55
|
+
"touchcancel",
|
|
56
|
+
"touchend",
|
|
57
|
+
"touchmove",
|
|
58
|
+
"touchstart",
|
|
59
|
+
"transitionend",
|
|
60
|
+
"transitionstart",
|
|
61
|
+
"visibilitychange",
|
|
62
|
+
"wheel",
|
|
63
|
+
] as const;
|
|
64
|
+
|
|
65
|
+
const DEFAULT_WINDOW_EVENTS = [
|
|
66
|
+
"hashchange",
|
|
67
|
+
"offline",
|
|
68
|
+
"online",
|
|
69
|
+
"orientationchange",
|
|
70
|
+
"pageshow",
|
|
71
|
+
"popstate",
|
|
72
|
+
"resize",
|
|
73
|
+
"scroll",
|
|
74
|
+
] as const;
|
|
75
|
+
|
|
76
|
+
/** A named element included in each browser debug snapshot. */
|
|
77
|
+
export interface BrowserDebugCaptureTarget {
|
|
78
|
+
/** CSS selector resolved whenever a snapshot is recorded. */
|
|
79
|
+
selector: string;
|
|
80
|
+
/** Dataset keys copied into the snapshot. */
|
|
81
|
+
dataset?: readonly string[];
|
|
82
|
+
/** Whether scroll dimensions and offsets are copied. */
|
|
83
|
+
scroll?: boolean;
|
|
84
|
+
/** Inline CSS custom properties or standard properties copied into the snapshot. */
|
|
85
|
+
styleProperties?: readonly string[];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Browser debug recorder configuration. */
|
|
89
|
+
export interface UseBrowserDebugCaptureOptions {
|
|
90
|
+
/** Application version included in capture metadata. */
|
|
91
|
+
appVersion?: string;
|
|
92
|
+
/** Additional bounded scalar metadata included when a capture starts. */
|
|
93
|
+
metadata?: Readonly<Record<string, DebugCaptureScalar>>;
|
|
94
|
+
/** Named component elements measured in every snapshot. */
|
|
95
|
+
targets?: Readonly<Record<string, string | BrowserDebugCaptureTarget>>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Browser debug recorder state and controls. */
|
|
99
|
+
export interface BrowserDebugCaptureState {
|
|
100
|
+
available: boolean;
|
|
101
|
+
captureId: string | null;
|
|
102
|
+
error: string | null;
|
|
103
|
+
phase: "idle" | "starting" | "recording" | "stopping";
|
|
104
|
+
startCapture(): Promise<string | null>;
|
|
105
|
+
stopCapture(): Promise<string | null>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function storedCaptureId(): string | null {
|
|
109
|
+
if (typeof window === "undefined") return null;
|
|
110
|
+
try {
|
|
111
|
+
return window.sessionStorage.getItem(ACTIVE_CAPTURE_STORAGE_KEY);
|
|
112
|
+
} catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function setStoredCaptureId(captureId: string | null): void {
|
|
118
|
+
try {
|
|
119
|
+
if (captureId) window.sessionStorage.setItem(ACTIVE_CAPTURE_STORAGE_KEY, captureId);
|
|
120
|
+
else window.sessionStorage.removeItem(ACTIVE_CAPTURE_STORAGE_KEY);
|
|
121
|
+
} catch {
|
|
122
|
+
// Recording remains usable when session storage is unavailable.
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function targetConfig(target: string | BrowserDebugCaptureTarget): BrowserDebugCaptureTarget {
|
|
127
|
+
return typeof target === "string" ? { selector: target } : target;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function snapshotKey(name: string): string {
|
|
131
|
+
return name.replace(/[^a-zA-Z0-9]+(.)?/g, (_, next: string | undefined) =>
|
|
132
|
+
next ? next.toUpperCase() : "",
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function addRect(
|
|
137
|
+
data: Record<string, DebugCaptureScalar>,
|
|
138
|
+
prefix: string,
|
|
139
|
+
element: Element | null,
|
|
140
|
+
): void {
|
|
141
|
+
if (!element) return;
|
|
142
|
+
const rect = element.getBoundingClientRect();
|
|
143
|
+
data[`${prefix}Top`] = rect.top;
|
|
144
|
+
data[`${prefix}Bottom`] = rect.bottom;
|
|
145
|
+
data[`${prefix}Height`] = rect.height;
|
|
146
|
+
data[`${prefix}Left`] = rect.left;
|
|
147
|
+
data[`${prefix}Right`] = rect.right;
|
|
148
|
+
data[`${prefix}Width`] = rect.width;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function describeElement(element: Element | null): string | null {
|
|
152
|
+
if (!element) return null;
|
|
153
|
+
return (
|
|
154
|
+
element.getAttribute("data-slot") ??
|
|
155
|
+
element.getAttribute("aria-label") ??
|
|
156
|
+
element.id ??
|
|
157
|
+
element.tagName.toLowerCase()
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function readSnapshot(
|
|
162
|
+
targets: Readonly<Record<string, string | BrowserDebugCaptureTarget>>,
|
|
163
|
+
): Record<string, DebugCaptureScalar> {
|
|
164
|
+
const visualViewport = window.visualViewport;
|
|
165
|
+
const data: Record<string, DebugCaptureScalar> = {
|
|
166
|
+
activeElement: describeElement(document.activeElement),
|
|
167
|
+
bodyScrollTop: document.body.scrollTop,
|
|
168
|
+
devicePixelRatio: window.devicePixelRatio,
|
|
169
|
+
documentScrollTop: document.documentElement.scrollTop,
|
|
170
|
+
fullscreen: document.fullscreenElement !== null,
|
|
171
|
+
innerHeight: window.innerHeight,
|
|
172
|
+
innerWidth: window.innerWidth,
|
|
173
|
+
keyboardInset: visualViewport ? Math.max(0, window.innerHeight - visualViewport.height) : 0,
|
|
174
|
+
online: window.navigator.onLine,
|
|
175
|
+
orientationAngle: window.screen.orientation?.angle ?? null,
|
|
176
|
+
orientationType: window.screen.orientation?.type ?? "unknown",
|
|
177
|
+
screenHeight: window.screen.height,
|
|
178
|
+
screenWidth: window.screen.width,
|
|
179
|
+
scrollX: window.scrollX,
|
|
180
|
+
scrollY: window.scrollY,
|
|
181
|
+
visibility: document.visibilityState,
|
|
182
|
+
};
|
|
183
|
+
if (visualViewport) {
|
|
184
|
+
data.visualHeight = visualViewport.height;
|
|
185
|
+
data.visualWidth = visualViewport.width;
|
|
186
|
+
data.visualOffsetLeft = visualViewport.offsetLeft;
|
|
187
|
+
data.visualOffsetTop = visualViewport.offsetTop;
|
|
188
|
+
data.visualPageLeft = visualViewport.pageLeft;
|
|
189
|
+
data.visualPageTop = visualViewport.pageTop;
|
|
190
|
+
data.visualScale = visualViewport.scale;
|
|
191
|
+
}
|
|
192
|
+
addRect(data, "body", document.body);
|
|
193
|
+
for (const [name, target] of Object.entries(targets)) {
|
|
194
|
+
const config = targetConfig(target);
|
|
195
|
+
const element = document.querySelector<HTMLElement>(config.selector);
|
|
196
|
+
const prefix = snapshotKey(name);
|
|
197
|
+
addRect(data, prefix, element);
|
|
198
|
+
if (!element) continue;
|
|
199
|
+
if (config.scroll) {
|
|
200
|
+
data[`${prefix}ClientHeight`] = element.clientHeight;
|
|
201
|
+
data[`${prefix}ClientWidth`] = element.clientWidth;
|
|
202
|
+
data[`${prefix}ScrollHeight`] = element.scrollHeight;
|
|
203
|
+
data[`${prefix}ScrollLeft`] = element.scrollLeft;
|
|
204
|
+
data[`${prefix}ScrollTop`] = element.scrollTop;
|
|
205
|
+
data[`${prefix}ScrollWidth`] = element.scrollWidth;
|
|
206
|
+
data[`${prefix}BottomDistance`] =
|
|
207
|
+
element.scrollHeight - element.scrollTop - element.clientHeight;
|
|
208
|
+
}
|
|
209
|
+
for (const key of config.dataset ?? []) {
|
|
210
|
+
data[`${prefix}Data${snapshotKey(key)}`] = element.dataset[key] ?? null;
|
|
211
|
+
}
|
|
212
|
+
for (const property of config.styleProperties ?? []) {
|
|
213
|
+
data[`${prefix}Style${snapshotKey(property)}`] = element.style.getPropertyValue(property);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return data;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function keyboardValue(event: KeyboardEvent): string {
|
|
220
|
+
return event.key.length === 1 ? "[printable]" : event.key;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function eventData(event: Event): Record<string, DebugCaptureScalar> {
|
|
224
|
+
const pointer = event instanceof PointerEvent ? event : null;
|
|
225
|
+
const touch = event instanceof TouchEvent ? event.changedTouches[0] : null;
|
|
226
|
+
const keyboard = event instanceof KeyboardEvent ? event : null;
|
|
227
|
+
const input = event instanceof InputEvent ? event : null;
|
|
228
|
+
const wheel = event instanceof WheelEvent ? event : null;
|
|
229
|
+
return {
|
|
230
|
+
defaultPrevented: event.defaultPrevented,
|
|
231
|
+
eventTarget: describeElement(event.target instanceof Element ? event.target : null),
|
|
232
|
+
inputType: input?.inputType ?? null,
|
|
233
|
+
key: keyboard ? keyboardValue(keyboard) : null,
|
|
234
|
+
metaKey: keyboard?.metaKey ?? pointer?.metaKey ?? null,
|
|
235
|
+
pointerButtons: pointer?.buttons ?? null,
|
|
236
|
+
pointerId: pointer?.pointerId ?? null,
|
|
237
|
+
pointerType: pointer?.pointerType ?? null,
|
|
238
|
+
pointerX: pointer?.clientX ?? null,
|
|
239
|
+
pointerY: pointer?.clientY ?? null,
|
|
240
|
+
shiftKey: keyboard?.shiftKey ?? pointer?.shiftKey ?? null,
|
|
241
|
+
touchX: touch?.clientX ?? null,
|
|
242
|
+
touchY: touch?.clientY ?? null,
|
|
243
|
+
wheelDeltaX: wheel?.deltaX ?? null,
|
|
244
|
+
wheelDeltaY: wheel?.deltaY ?? null,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Record browser diagnostics through the optional AppKit debug-capture plugin. */
|
|
249
|
+
export function useBrowserDebugCapture(
|
|
250
|
+
options: UseBrowserDebugCaptureOptions = {},
|
|
251
|
+
): BrowserDebugCaptureState {
|
|
252
|
+
const published = usePluginClientConfig<unknown>(DEBUG_CAPTURE_PLUGIN_NAME);
|
|
253
|
+
const clientConfig = useMemo(() => {
|
|
254
|
+
const parsed = DebugCaptureClientConfigSchema.safeParse(published);
|
|
255
|
+
return parsed.success ? parsed.data : null;
|
|
256
|
+
}, [published]);
|
|
257
|
+
const route = clientConfig ? debugCaptureRoute(clientConfig.basePath) : null;
|
|
258
|
+
const restoredCaptureId = useMemo(storedCaptureId, []);
|
|
259
|
+
const [captureId, setCaptureId] = useState<string | null>(restoredCaptureId);
|
|
260
|
+
const [phase, setPhase] = useState<BrowserDebugCaptureState["phase"]>(
|
|
261
|
+
restoredCaptureId ? "recording" : "idle",
|
|
262
|
+
);
|
|
263
|
+
const [error, setError] = useState<string | null>(null);
|
|
264
|
+
const captureIdRef = useRef(captureId);
|
|
265
|
+
const recordingRef = useRef(captureId !== null);
|
|
266
|
+
const startedAtRef = useRef(typeof performance === "undefined" ? 0 : performance.now());
|
|
267
|
+
const sequenceRef = useRef(0);
|
|
268
|
+
const eventsRef = useRef<DebugCaptureEvent[]>([]);
|
|
269
|
+
const pendingRequestRef = useRef<Promise<void>>(Promise.resolve());
|
|
270
|
+
const targets = options.targets ?? {};
|
|
271
|
+
const targetsRef = useRef(targets);
|
|
272
|
+
targetsRef.current = targets;
|
|
273
|
+
|
|
274
|
+
const post = useCallback(
|
|
275
|
+
async (body: unknown): Promise<Response> => {
|
|
276
|
+
if (!route) throw new Error("Browser debug capture is not enabled by the AppKit server.");
|
|
277
|
+
const response = await fetch(route, {
|
|
278
|
+
body: JSON.stringify(body),
|
|
279
|
+
headers: { "Content-Type": "application/json" },
|
|
280
|
+
method: "POST",
|
|
281
|
+
});
|
|
282
|
+
if (!response.ok) throw new Error(`Debug capture request failed (${response.status}).`);
|
|
283
|
+
return response;
|
|
284
|
+
},
|
|
285
|
+
[route],
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
const record = useCallback((type: string, extra: Record<string, DebugCaptureScalar> = {}) => {
|
|
289
|
+
if (!recordingRef.current) return;
|
|
290
|
+
eventsRef.current.push({
|
|
291
|
+
at: performance.now() - startedAtRef.current,
|
|
292
|
+
data: { ...readSnapshot(targetsRef.current), ...extra },
|
|
293
|
+
sequence: sequenceRef.current++,
|
|
294
|
+
type,
|
|
295
|
+
});
|
|
296
|
+
}, []);
|
|
297
|
+
|
|
298
|
+
const flush = useCallback((): Promise<void> => {
|
|
299
|
+
const activeCaptureId = captureIdRef.current;
|
|
300
|
+
if (!activeCaptureId || eventsRef.current.length === 0) return pendingRequestRef.current;
|
|
301
|
+
const events = eventsRef.current.splice(0, MAX_BUFFERED_EVENTS);
|
|
302
|
+
const request = pendingRequestRef.current
|
|
303
|
+
.catch(() => undefined)
|
|
304
|
+
.then(async () => {
|
|
305
|
+
await post({ action: "append", captureId: activeCaptureId, events });
|
|
306
|
+
})
|
|
307
|
+
.catch((requestError: unknown) => {
|
|
308
|
+
setError(requestError instanceof Error ? requestError.message : "Debug capture failed.");
|
|
309
|
+
});
|
|
310
|
+
pendingRequestRef.current = request;
|
|
311
|
+
return request;
|
|
312
|
+
}, [post]);
|
|
313
|
+
|
|
314
|
+
useEffect(() => {
|
|
315
|
+
if (phase !== "recording" || !route) return;
|
|
316
|
+
recordingRef.current = true;
|
|
317
|
+
captureIdRef.current = captureId;
|
|
318
|
+
record(restoredCaptureId === captureId ? "capture-resume" : "capture-ready");
|
|
319
|
+
|
|
320
|
+
let sampleFrame = 0;
|
|
321
|
+
let pendingType = "layout";
|
|
322
|
+
let pendingData: Record<string, DebugCaptureScalar> = {};
|
|
323
|
+
const sample = (type: string, data: Record<string, DebugCaptureScalar> = {}) => {
|
|
324
|
+
pendingType = type;
|
|
325
|
+
pendingData = data;
|
|
326
|
+
if (sampleFrame !== 0) return;
|
|
327
|
+
sampleFrame = window.requestAnimationFrame(() => {
|
|
328
|
+
sampleFrame = 0;
|
|
329
|
+
record(pendingType, pendingData);
|
|
330
|
+
if (eventsRef.current.length >= MAX_BUFFERED_EVENTS) void flush();
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
const listener = (event: Event) => sample(`browser-${event.type}`, eventData(event));
|
|
334
|
+
const listeners: Array<[EventTarget, string, EventListener, boolean]> = [];
|
|
335
|
+
const listen = (target: EventTarget | null, eventName: string, capture = false) => {
|
|
336
|
+
if (!target) return;
|
|
337
|
+
target.addEventListener(eventName, listener, { capture, passive: true });
|
|
338
|
+
listeners.push([target, eventName, listener, capture]);
|
|
339
|
+
};
|
|
340
|
+
for (const eventName of DEFAULT_DOCUMENT_EVENTS) listen(document, eventName, true);
|
|
341
|
+
for (const eventName of DEFAULT_WINDOW_EVENTS) listen(window, eventName);
|
|
342
|
+
listen(window.visualViewport, "resize");
|
|
343
|
+
listen(window.visualViewport, "scroll");
|
|
344
|
+
listen(window.visualViewport, "scrollend");
|
|
345
|
+
|
|
346
|
+
const resizeObserver = new ResizeObserver(() => sample("element-resize"));
|
|
347
|
+
for (const target of Object.values(targetsRef.current)) {
|
|
348
|
+
const element = document.querySelector(targetConfig(target).selector);
|
|
349
|
+
if (element) resizeObserver.observe(element);
|
|
350
|
+
}
|
|
351
|
+
resizeObserver.observe(document.body);
|
|
352
|
+
|
|
353
|
+
const interval = window.setInterval(() => void flush(), FLUSH_INTERVAL_MS);
|
|
354
|
+
const handlePageHide = () => {
|
|
355
|
+
record("page-hide");
|
|
356
|
+
const activeCaptureId = captureIdRef.current;
|
|
357
|
+
const events = eventsRef.current.splice(0, MAX_BUFFERED_EVENTS);
|
|
358
|
+
if (!activeCaptureId || events.length === 0) return;
|
|
359
|
+
navigator.sendBeacon(
|
|
360
|
+
route,
|
|
361
|
+
new Blob([JSON.stringify({ action: "append", captureId: activeCaptureId, events })], {
|
|
362
|
+
type: "application/json",
|
|
363
|
+
}),
|
|
364
|
+
);
|
|
365
|
+
};
|
|
366
|
+
window.addEventListener("pagehide", handlePageHide);
|
|
367
|
+
|
|
368
|
+
return () => {
|
|
369
|
+
if (sampleFrame !== 0) window.cancelAnimationFrame(sampleFrame);
|
|
370
|
+
window.clearInterval(interval);
|
|
371
|
+
window.removeEventListener("pagehide", handlePageHide);
|
|
372
|
+
for (const [target, eventName, eventListener, capture] of listeners) {
|
|
373
|
+
target.removeEventListener(eventName, eventListener, capture);
|
|
374
|
+
}
|
|
375
|
+
resizeObserver.disconnect();
|
|
376
|
+
};
|
|
377
|
+
}, [captureId, flush, phase, record, restoredCaptureId, route]);
|
|
378
|
+
|
|
379
|
+
const startCapture = useCallback(async (): Promise<string | null> => {
|
|
380
|
+
if (phase !== "idle" || !route) return null;
|
|
381
|
+
setError(null);
|
|
382
|
+
setPhase("starting");
|
|
383
|
+
try {
|
|
384
|
+
const response = await post({
|
|
385
|
+
action: "start",
|
|
386
|
+
metadata: {
|
|
387
|
+
...options.metadata,
|
|
388
|
+
appVersion: options.appVersion ?? "unknown",
|
|
389
|
+
devicePixelRatio: window.devicePixelRatio,
|
|
390
|
+
path: `${window.location.pathname}${window.location.search}${window.location.hash}`,
|
|
391
|
+
screenHeight: window.screen.height,
|
|
392
|
+
screenWidth: window.screen.width,
|
|
393
|
+
},
|
|
394
|
+
});
|
|
395
|
+
const result = DebugCaptureStartResponseSchema.parse(await response.json());
|
|
396
|
+
captureIdRef.current = result.captureId;
|
|
397
|
+
recordingRef.current = true;
|
|
398
|
+
startedAtRef.current = performance.now();
|
|
399
|
+
sequenceRef.current = 0;
|
|
400
|
+
eventsRef.current = [];
|
|
401
|
+
setStoredCaptureId(result.captureId);
|
|
402
|
+
setCaptureId(result.captureId);
|
|
403
|
+
setPhase("recording");
|
|
404
|
+
return result.captureId;
|
|
405
|
+
} catch (startError) {
|
|
406
|
+
setError(startError instanceof Error ? startError.message : "Could not start capture.");
|
|
407
|
+
setPhase("idle");
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
}, [options.appVersion, options.metadata, phase, post, route]);
|
|
411
|
+
|
|
412
|
+
const stopCapture = useCallback(async (): Promise<string | null> => {
|
|
413
|
+
const activeCaptureId = captureIdRef.current;
|
|
414
|
+
if (!activeCaptureId || phase !== "recording") return null;
|
|
415
|
+
record("capture-stop-requested");
|
|
416
|
+
recordingRef.current = false;
|
|
417
|
+
setPhase("stopping");
|
|
418
|
+
const events = eventsRef.current.splice(0);
|
|
419
|
+
await pendingRequestRef.current.catch(() => undefined);
|
|
420
|
+
try {
|
|
421
|
+
const response = await post({ action: "stop", captureId: activeCaptureId, events });
|
|
422
|
+
DebugCaptureStopResponseSchema.parse(await response.json());
|
|
423
|
+
setStoredCaptureId(null);
|
|
424
|
+
captureIdRef.current = null;
|
|
425
|
+
setCaptureId(null);
|
|
426
|
+
setError(null);
|
|
427
|
+
setPhase("idle");
|
|
428
|
+
return activeCaptureId;
|
|
429
|
+
} catch (stopError) {
|
|
430
|
+
recordingRef.current = true;
|
|
431
|
+
setError(stopError instanceof Error ? stopError.message : "Could not stop capture.");
|
|
432
|
+
setPhase("recording");
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
}, [phase, post, record]);
|
|
436
|
+
|
|
437
|
+
return {
|
|
438
|
+
available: route !== null,
|
|
439
|
+
captureId,
|
|
440
|
+
error,
|
|
441
|
+
phase,
|
|
442
|
+
startCapture,
|
|
443
|
+
stopCapture,
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** Props for the reusable AppKit browser debug-capture button. */
|
|
448
|
+
export interface BrowserDebugCaptureButtonProps extends UseBrowserDebugCaptureOptions {
|
|
449
|
+
className?: string;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** Toggle browser diagnostics and copy the completed capture identifier. */
|
|
453
|
+
export function BrowserDebugCaptureButton({
|
|
454
|
+
className,
|
|
455
|
+
...options
|
|
456
|
+
}: BrowserDebugCaptureButtonProps) {
|
|
457
|
+
const { available, captureId, error, phase, startCapture, stopCapture } =
|
|
458
|
+
useBrowserDebugCapture(options);
|
|
459
|
+
const [copiedId, setCopiedId] = useState<string | null>(null);
|
|
460
|
+
const copiedTimerRef = useRef<number | null>(null);
|
|
461
|
+
const recording = phase === "recording";
|
|
462
|
+
const busy = phase === "starting" || phase === "stopping";
|
|
463
|
+
|
|
464
|
+
useEffect(
|
|
465
|
+
() => () => {
|
|
466
|
+
if (copiedTimerRef.current !== null) window.clearTimeout(copiedTimerRef.current);
|
|
467
|
+
},
|
|
468
|
+
[],
|
|
469
|
+
);
|
|
470
|
+
|
|
471
|
+
if (!available) return null;
|
|
472
|
+
|
|
473
|
+
const tooltip = error
|
|
474
|
+
? error
|
|
475
|
+
: recording
|
|
476
|
+
? "Stop browser capture and copy ID"
|
|
477
|
+
: copiedId
|
|
478
|
+
? `Copied ${copiedId}`
|
|
479
|
+
: "Start browser debug capture";
|
|
480
|
+
|
|
481
|
+
const toggle = (button: HTMLButtonElement) => {
|
|
482
|
+
button.blur();
|
|
483
|
+
|
|
484
|
+
if (!recording || !captureId) {
|
|
485
|
+
void startCapture();
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
// Begin the clipboard write inside the original iOS tap gesture.
|
|
489
|
+
void copyText(captureId).then((copied) => {
|
|
490
|
+
if (!copied) return;
|
|
491
|
+
setCopiedId(captureId);
|
|
492
|
+
if (copiedTimerRef.current !== null) window.clearTimeout(copiedTimerRef.current);
|
|
493
|
+
copiedTimerRef.current = window.setTimeout(() => setCopiedId(null), 5_000);
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
void stopCapture();
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
return (
|
|
500
|
+
<div className="relative flex shrink-0 items-center">
|
|
501
|
+
<TooltipProvider delayDuration={200}>
|
|
502
|
+
<Tooltip>
|
|
503
|
+
<TooltipTrigger asChild>
|
|
504
|
+
<Button
|
|
505
|
+
type="button"
|
|
506
|
+
size="icon"
|
|
507
|
+
variant="ghost"
|
|
508
|
+
disabled={busy}
|
|
509
|
+
aria-label={recording ? "Stop browser capture and copy ID" : "Start browser capture"}
|
|
510
|
+
aria-pressed={recording}
|
|
511
|
+
data-capture-id={captureId ?? copiedId ?? undefined}
|
|
512
|
+
data-capture-state={phase}
|
|
513
|
+
className={cn(
|
|
514
|
+
"relative size-8",
|
|
515
|
+
recording && "bg-destructive/15 text-destructive hover:bg-destructive/25",
|
|
516
|
+
error && "text-destructive",
|
|
517
|
+
className,
|
|
518
|
+
)}
|
|
519
|
+
onClick={(event) => toggle(event.currentTarget)}
|
|
520
|
+
>
|
|
521
|
+
{copiedId && !recording ? (
|
|
522
|
+
<CheckIcon className="size-4" />
|
|
523
|
+
) : (
|
|
524
|
+
<BugIcon className="size-4" />
|
|
525
|
+
)}
|
|
526
|
+
|
|
527
|
+
{recording ? (
|
|
528
|
+
<span className="absolute right-1 top-1 size-1.5 rounded-full bg-destructive" />
|
|
529
|
+
) : null}
|
|
530
|
+
</Button>
|
|
531
|
+
</TooltipTrigger>
|
|
532
|
+
<TooltipContent>{tooltip}</TooltipContent>
|
|
533
|
+
</Tooltip>
|
|
534
|
+
</TooltipProvider>
|
|
535
|
+
{copiedId && !recording ? (
|
|
536
|
+
<span
|
|
537
|
+
role="status"
|
|
538
|
+
className="absolute right-0 top-full z-50 mt-2 whitespace-nowrap rounded-md border bg-popover px-2 py-1 text-xs text-popover-foreground shadow-md"
|
|
539
|
+
>
|
|
540
|
+
Copied {copiedId}
|
|
541
|
+
</span>
|
|
542
|
+
) : null}
|
|
543
|
+
</div>
|
|
544
|
+
);
|
|
545
|
+
}
|