@fragments-sdk/viewer 0.2.1 → 0.2.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/package.json +10 -3
- package/src/components/App.tsx +67 -4
- package/src/components/BottomPanel.tsx +31 -1
- package/src/components/ComponentGraph.tsx +1 -1
- package/src/components/EmptyVariantMessage.tsx +1 -1
- package/src/components/ErrorBoundary.tsx +2 -4
- package/src/components/FragmentRenderer.tsx +27 -4
- package/src/components/HeaderSearch.tsx +1 -1
- package/src/components/InteractionsPanel.tsx +5 -5
- package/src/components/LoadErrorMessage.tsx +26 -30
- package/src/components/NoVariantsMessage.tsx +1 -1
- package/src/components/PanelShell.tsx +1 -1
- package/src/components/PerformancePanel.tsx +4 -4
- package/src/components/PreviewArea.tsx +6 -0
- package/src/components/PropsEditor.tsx +33 -17
- package/src/components/SkeletonLoader.tsx +0 -1
- package/src/components/TokenStylePanel.tsx +3 -3
- package/src/components/TopToolbar.tsx +1 -1
- package/src/components/VariantMatrix.tsx +11 -1
- package/src/components/ViewerHeader.tsx +1 -1
- package/src/components/WebMCPDevTools.tsx +2 -2
- package/src/entry.tsx +4 -6
- package/src/hooks/useAppState.ts +1 -1
- package/src/preview-frame-entry.tsx +0 -2
- package/src/shared/DocsHeaderBar.module.scss +174 -0
- package/src/shared/DocsHeaderBar.tsx +149 -14
- package/src/shared/DocsSidebarNav.tsx +3 -3
- package/src/shared/index.ts +4 -1
- package/src/shared/types.ts +29 -3
- package/src/style-utils.ts +1 -1
- package/src/webmcp/runtime-tools.ts +1 -1
- package/tsconfig.json +2 -2
- package/src/assets/fragments-logo.ts +0 -4
- package/src/components/ActionsPanel.tsx +0 -332
- package/src/components/ComponentHeader.tsx +0 -88
- package/src/components/ContractPanel.tsx +0 -241
- package/src/components/FragmentEditor.tsx +0 -525
- package/src/components/HmrStatusIndicator.tsx +0 -61
- package/src/components/LandingPage.tsx +0 -420
- package/src/components/PropsTable.tsx +0 -111
- package/src/components/RelationsSection.tsx +0 -88
- package/src/components/ResizablePanel.tsx +0 -271
- package/src/components/RightSidebar.tsx +0 -102
- package/src/components/ScreenshotButton.tsx +0 -90
- package/src/components/Sidebar.tsx +0 -169
- package/src/components/UsageSection.tsx +0 -95
- package/src/components/VariantTabs.tsx +0 -40
- package/src/components/ViewportSelector.tsx +0 -172
- package/src/components/_future/CreatePage.tsx +0 -835
- package/src/composition-renderer.ts +0 -381
- package/src/constants/index.ts +0 -1
- package/src/hooks/index.ts +0 -2
- package/src/hooks/useHmrStatus.ts +0 -109
- package/src/hooks/useScrollSpy.ts +0 -78
- package/src/intelligence/healthReport.ts +0 -505
- package/src/intelligence/styleDrift.ts +0 -340
- package/src/intelligence/usageScanner.ts +0 -309
- package/src/utils/actionExport.ts +0 -372
- package/src/utils/colorSchemes.ts +0 -201
- package/src/webmcp/index.ts +0 -3
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Composition Renderer
|
|
3
|
-
*
|
|
4
|
-
* Generates render scripts for multi-component compositions.
|
|
5
|
-
* Takes a parsed JSX component tree and generates code that can
|
|
6
|
-
* render the composition in an isolated browser context.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { ComponentNode } from "./jsx-parser.js";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Fragment file information for component lookup
|
|
13
|
-
*/
|
|
14
|
-
export interface FragmentFileInfo {
|
|
15
|
-
absolutePath: string;
|
|
16
|
-
relativePath: string;
|
|
17
|
-
componentName: string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Options for generating a composition render script
|
|
22
|
-
*/
|
|
23
|
-
export interface CompositionRenderOptions {
|
|
24
|
-
/** The parsed component tree */
|
|
25
|
-
tree: ComponentNode | ComponentNode[];
|
|
26
|
-
/** Map of component names to their fragment file paths */
|
|
27
|
-
componentPaths: Map<string, string>;
|
|
28
|
-
/** Optional theme ('light' or 'dark') */
|
|
29
|
-
theme?: "light" | "dark";
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Generate a render script for a composition.
|
|
34
|
-
*
|
|
35
|
-
* This generates JavaScript code that:
|
|
36
|
-
* 1. Imports all required components in parallel
|
|
37
|
-
* 2. Builds a component map
|
|
38
|
-
* 3. Renders the nested composition using React.createElement
|
|
39
|
-
*
|
|
40
|
-
* @param options - The render options
|
|
41
|
-
* @returns JavaScript code string ready to be injected into an HTML page
|
|
42
|
-
*/
|
|
43
|
-
export function generateCompositionRenderScript(
|
|
44
|
-
options: CompositionRenderOptions
|
|
45
|
-
): string {
|
|
46
|
-
const { tree, componentPaths, theme = "light" } = options;
|
|
47
|
-
|
|
48
|
-
// Extract all unique component names that need to be imported
|
|
49
|
-
const componentsToImport = getUniqueComponents(tree);
|
|
50
|
-
|
|
51
|
-
// Generate import statements
|
|
52
|
-
const imports = componentsToImport
|
|
53
|
-
.map((name) => {
|
|
54
|
-
const path = componentPaths.get(name);
|
|
55
|
-
if (!path) {
|
|
56
|
-
return ` // Component "${name}" not found - will use placeholder`;
|
|
57
|
-
}
|
|
58
|
-
return ` import("${path}").then(m => ({ name: "${name}", mod: m }))`;
|
|
59
|
-
})
|
|
60
|
-
.join(",\n");
|
|
61
|
-
|
|
62
|
-
// Generate the createElement tree
|
|
63
|
-
const createElementCode = generateCreateElementTree(tree);
|
|
64
|
-
|
|
65
|
-
return `
|
|
66
|
-
import React from "react";
|
|
67
|
-
import { createRoot } from "react-dom/client";
|
|
68
|
-
|
|
69
|
-
async function render() {
|
|
70
|
-
const root = document.getElementById("render-root");
|
|
71
|
-
|
|
72
|
-
// Apply theme
|
|
73
|
-
document.documentElement.classList.toggle("dark", ${theme === "dark"});
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
// Parallel import all needed components
|
|
77
|
-
const componentPromises = [
|
|
78
|
-
${imports}
|
|
79
|
-
].filter(Boolean);
|
|
80
|
-
|
|
81
|
-
const loadedComponents = await Promise.all(componentPromises);
|
|
82
|
-
|
|
83
|
-
// Build component map
|
|
84
|
-
const C = {};
|
|
85
|
-
for (const item of loadedComponents) {
|
|
86
|
-
if (item && item.mod) {
|
|
87
|
-
// Handle both default export and named component export
|
|
88
|
-
const fragment = item.mod.default;
|
|
89
|
-
if (fragment && fragment.component) {
|
|
90
|
-
C[item.name] = fragment.component;
|
|
91
|
-
} else if (typeof item.mod.default === "function") {
|
|
92
|
-
C[item.name] = item.mod.default;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Placeholder component for missing components
|
|
98
|
-
const Placeholder = ({ name, children }) => {
|
|
99
|
-
return React.createElement("div", {
|
|
100
|
-
style: {
|
|
101
|
-
border: "2px dashed #e5e7eb",
|
|
102
|
-
borderRadius: "8px",
|
|
103
|
-
padding: "16px",
|
|
104
|
-
margin: "8px 0",
|
|
105
|
-
backgroundColor: "#f9fafb",
|
|
106
|
-
color: "#6b7280",
|
|
107
|
-
fontSize: "14px",
|
|
108
|
-
fontFamily: "system-ui, sans-serif",
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
React.createElement("div", { style: { fontWeight: 500, marginBottom: "4px" } }, "Missing: " + name),
|
|
112
|
-
children
|
|
113
|
-
);
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
// Helper to get component or placeholder
|
|
117
|
-
const getComponent = (name) => {
|
|
118
|
-
return C[name] || ((props) => React.createElement(Placeholder, { name, ...props }));
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
// Render composition
|
|
122
|
-
const reactRoot = createRoot(root);
|
|
123
|
-
const composition = ${createElementCode};
|
|
124
|
-
reactRoot.render(composition);
|
|
125
|
-
|
|
126
|
-
// Signal that rendering is complete
|
|
127
|
-
requestAnimationFrame(() => {
|
|
128
|
-
requestAnimationFrame(() => {
|
|
129
|
-
root.classList.add("ready");
|
|
130
|
-
window.__RENDER_READY__ = true;
|
|
131
|
-
window.__RENDER_COMPONENTS__ = ${JSON.stringify(componentsToImport)};
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
} catch (error) {
|
|
135
|
-
console.error("Composition render error:", error);
|
|
136
|
-
root.innerHTML = \`
|
|
137
|
-
<div class="render-error" style="
|
|
138
|
-
padding: 20px;
|
|
139
|
-
background: #fef2f2;
|
|
140
|
-
border: 1px solid #fecaca;
|
|
141
|
-
border-radius: 8px;
|
|
142
|
-
color: #991b1b;
|
|
143
|
-
font-family: system-ui, sans-serif;
|
|
144
|
-
">
|
|
145
|
-
<strong style="display: block; margin-bottom: 8px;">Render Error</strong>
|
|
146
|
-
<pre style="
|
|
147
|
-
background: #fee2e2;
|
|
148
|
-
padding: 12px;
|
|
149
|
-
border-radius: 4px;
|
|
150
|
-
overflow: auto;
|
|
151
|
-
font-size: 13px;
|
|
152
|
-
margin: 0;
|
|
153
|
-
">\${error.message}</pre>
|
|
154
|
-
</div>
|
|
155
|
-
\`;
|
|
156
|
-
root.classList.add("ready");
|
|
157
|
-
window.__RENDER_READY__ = true;
|
|
158
|
-
window.__RENDER_ERROR__ = error.message;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
render();
|
|
163
|
-
`;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Get all unique component names from a tree
|
|
168
|
-
*/
|
|
169
|
-
function getUniqueComponents(tree: ComponentNode | ComponentNode[]): string[] {
|
|
170
|
-
const components = new Set<string>();
|
|
171
|
-
|
|
172
|
-
const visit = (node: ComponentNode) => {
|
|
173
|
-
if (!node.isHtmlElement && node.name !== "Fragment") {
|
|
174
|
-
components.add(node.name);
|
|
175
|
-
}
|
|
176
|
-
for (const child of node.children) {
|
|
177
|
-
if (typeof child !== "string") {
|
|
178
|
-
visit(child);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
if (Array.isArray(tree)) {
|
|
184
|
-
for (const node of tree) {
|
|
185
|
-
visit(node);
|
|
186
|
-
}
|
|
187
|
-
} else {
|
|
188
|
-
visit(tree);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return Array.from(components).sort();
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Generate React.createElement tree from parsed component tree
|
|
196
|
-
*/
|
|
197
|
-
function generateCreateElementTree(
|
|
198
|
-
tree: ComponentNode | ComponentNode[]
|
|
199
|
-
): string {
|
|
200
|
-
if (Array.isArray(tree)) {
|
|
201
|
-
if (tree.length === 0) {
|
|
202
|
-
return "null";
|
|
203
|
-
}
|
|
204
|
-
if (tree.length === 1) {
|
|
205
|
-
return generateCreateElementNode(tree[0]);
|
|
206
|
-
}
|
|
207
|
-
// Multiple root elements - wrap in Fragment
|
|
208
|
-
const children = tree.map(generateCreateElementNode).join(",\n ");
|
|
209
|
-
return `React.createElement(React.Fragment, null,\n ${children}\n )`;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return generateCreateElementNode(tree);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Generate a single React.createElement call
|
|
217
|
-
*/
|
|
218
|
-
function generateCreateElementNode(node: ComponentNode): string {
|
|
219
|
-
const { name, isHtmlElement, props, children } = node;
|
|
220
|
-
|
|
221
|
-
// Determine the element type
|
|
222
|
-
let elementType: string;
|
|
223
|
-
if (isHtmlElement) {
|
|
224
|
-
elementType = `"${name}"`;
|
|
225
|
-
} else if (name === "Fragment") {
|
|
226
|
-
elementType = "React.Fragment";
|
|
227
|
-
} else {
|
|
228
|
-
elementType = `getComponent("${name}")`;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// Serialize props
|
|
232
|
-
const propsCode = serializeProps(props);
|
|
233
|
-
|
|
234
|
-
// Generate children
|
|
235
|
-
if (children.length === 0) {
|
|
236
|
-
return `React.createElement(${elementType}, ${propsCode})`;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const childrenCode = children
|
|
240
|
-
.map((child) => {
|
|
241
|
-
if (typeof child === "string") {
|
|
242
|
-
return JSON.stringify(child);
|
|
243
|
-
}
|
|
244
|
-
return generateCreateElementNode(child);
|
|
245
|
-
})
|
|
246
|
-
.join(",\n ");
|
|
247
|
-
|
|
248
|
-
return `React.createElement(${elementType}, ${propsCode},\n ${childrenCode}\n )`;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Serialize props object to JavaScript code
|
|
253
|
-
*/
|
|
254
|
-
function serializeProps(props: Record<string, unknown>): string {
|
|
255
|
-
const entries = Object.entries(props);
|
|
256
|
-
|
|
257
|
-
if (entries.length === 0) {
|
|
258
|
-
return "null";
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const propsStr = entries
|
|
262
|
-
.map(([key, value]) => {
|
|
263
|
-
const serializedValue = serializeValue(value);
|
|
264
|
-
// Use computed property name if key contains special characters
|
|
265
|
-
if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key)) {
|
|
266
|
-
return `${key}: ${serializedValue}`;
|
|
267
|
-
}
|
|
268
|
-
return `${JSON.stringify(key)}: ${serializedValue}`;
|
|
269
|
-
})
|
|
270
|
-
.join(", ");
|
|
271
|
-
|
|
272
|
-
return `{ ${propsStr} }`;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Serialize a value to JavaScript code
|
|
277
|
-
*/
|
|
278
|
-
function serializeValue(value: unknown): string {
|
|
279
|
-
if (value === null) return "null";
|
|
280
|
-
if (value === undefined) return "undefined";
|
|
281
|
-
if (typeof value === "string") return JSON.stringify(value);
|
|
282
|
-
if (typeof value === "number") return String(value);
|
|
283
|
-
if (typeof value === "boolean") return String(value);
|
|
284
|
-
|
|
285
|
-
if (Array.isArray(value)) {
|
|
286
|
-
const items = value.map(serializeValue).join(", ");
|
|
287
|
-
return `[${items}]`;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
if (typeof value === "object") {
|
|
291
|
-
const entries = Object.entries(value)
|
|
292
|
-
.map(([k, v]) => `${JSON.stringify(k)}: ${serializeValue(v)}`)
|
|
293
|
-
.join(", ");
|
|
294
|
-
return `{ ${entries} }`;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// For placeholders like [Function] or {variable}
|
|
298
|
-
if (typeof value === "string" && value.startsWith("[") && value.endsWith("]")) {
|
|
299
|
-
// Return a no-op function for function placeholders
|
|
300
|
-
if (value === "[Function]") {
|
|
301
|
-
return "() => {}";
|
|
302
|
-
}
|
|
303
|
-
return "null";
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// For identifier references like {variable}
|
|
307
|
-
if (typeof value === "string" && value.startsWith("{") && value.endsWith("}")) {
|
|
308
|
-
return "null"; // Can't resolve variables in this context
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
return "undefined";
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Build a map of component names to their fragment file paths
|
|
316
|
-
*/
|
|
317
|
-
export function buildComponentPathMap(
|
|
318
|
-
components: string[],
|
|
319
|
-
fragments: Array<{ path: string; name: string; absolutePath: string }>
|
|
320
|
-
): Map<string, string> {
|
|
321
|
-
const map = new Map<string, string>();
|
|
322
|
-
|
|
323
|
-
for (const component of components) {
|
|
324
|
-
const fragment = fragments.find(
|
|
325
|
-
(s) => s.name.toLowerCase() === component.toLowerCase()
|
|
326
|
-
);
|
|
327
|
-
if (fragment) {
|
|
328
|
-
map.set(component, fragment.absolutePath);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
return map;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Generate a simple composition preview HTML
|
|
337
|
-
*/
|
|
338
|
-
export function generateCompositionPreviewHTML(
|
|
339
|
-
jsx: string,
|
|
340
|
-
theme: "light" | "dark" = "light"
|
|
341
|
-
): string {
|
|
342
|
-
return `
|
|
343
|
-
<!DOCTYPE html>
|
|
344
|
-
<html lang="en" class="${theme === "dark" ? "dark" : ""}">
|
|
345
|
-
<head>
|
|
346
|
-
<meta charset="UTF-8">
|
|
347
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
348
|
-
<title>Composition Preview</title>
|
|
349
|
-
<style>
|
|
350
|
-
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
351
|
-
body {
|
|
352
|
-
font-family: system-ui, -apple-system, sans-serif;
|
|
353
|
-
background: ${theme === "dark" ? "#1a1a1a" : "#ffffff"};
|
|
354
|
-
color: ${theme === "dark" ? "#ffffff" : "#000000"};
|
|
355
|
-
min-height: 100vh;
|
|
356
|
-
display: flex;
|
|
357
|
-
align-items: center;
|
|
358
|
-
justify-content: center;
|
|
359
|
-
padding: 20px;
|
|
360
|
-
}
|
|
361
|
-
#render-root {
|
|
362
|
-
width: 100%;
|
|
363
|
-
max-width: 100%;
|
|
364
|
-
}
|
|
365
|
-
#render-root.ready { opacity: 1; }
|
|
366
|
-
.render-error {
|
|
367
|
-
padding: 20px;
|
|
368
|
-
background: #fef2f2;
|
|
369
|
-
border: 1px solid #fecaca;
|
|
370
|
-
border-radius: 8px;
|
|
371
|
-
color: #991b1b;
|
|
372
|
-
}
|
|
373
|
-
</style>
|
|
374
|
-
</head>
|
|
375
|
-
<body>
|
|
376
|
-
<div id="render-root"></div>
|
|
377
|
-
<!-- RENDER_SCRIPT_PLACEHOLDER -->
|
|
378
|
-
</body>
|
|
379
|
-
</html>
|
|
380
|
-
`;
|
|
381
|
-
}
|
package/src/constants/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './ui.js';
|
package/src/hooks/index.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { useState, useEffect, useCallback } from 'react';
|
|
2
|
-
import type { HmrStatus } from '../constants/ui.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to track Vite HMR connection status.
|
|
6
|
-
* Returns the current connection status and any recent file changes.
|
|
7
|
-
*/
|
|
8
|
-
export function useHmrStatus() {
|
|
9
|
-
const [status, setStatus] = useState<HmrStatus>('connected');
|
|
10
|
-
const [lastUpdate, setLastUpdate] = useState<string | null>(null);
|
|
11
|
-
|
|
12
|
-
useEffect(() => {
|
|
13
|
-
// Check if we're in a Vite environment
|
|
14
|
-
if (typeof import.meta.hot === 'undefined') {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const hot = import.meta.hot;
|
|
19
|
-
|
|
20
|
-
// Listen for HMR connection status
|
|
21
|
-
// Vite emits these events on the WebSocket connection
|
|
22
|
-
const handleConnect = () => {
|
|
23
|
-
setStatus('connected');
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const handleDisconnect = () => {
|
|
27
|
-
setStatus('disconnected');
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const handleReconnecting = () => {
|
|
31
|
-
setStatus('reconnecting');
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// Listen for module updates
|
|
35
|
-
const handleUpdate = (data: { type: string; path?: string }) => {
|
|
36
|
-
if (data.path) {
|
|
37
|
-
setLastUpdate(data.path);
|
|
38
|
-
// Clear the update notification after 3 seconds
|
|
39
|
-
setTimeout(() => setLastUpdate(null), 3000);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// Vite's HMR API
|
|
44
|
-
// @ts-expect-error Vite internal events
|
|
45
|
-
hot.on('vite:beforeUpdate', handleUpdate);
|
|
46
|
-
|
|
47
|
-
// Listen for WebSocket events via custom events
|
|
48
|
-
// These are dispatched by Vite's client
|
|
49
|
-
window.addEventListener('vite:ws-connect', handleConnect);
|
|
50
|
-
window.addEventListener('vite:ws-disconnect', handleDisconnect);
|
|
51
|
-
|
|
52
|
-
// For Vite 5+, we can use the connection status directly
|
|
53
|
-
// Check current status
|
|
54
|
-
try {
|
|
55
|
-
// @ts-expect-error Vite internal
|
|
56
|
-
if (hot.connection?.socket?.readyState === 1) {
|
|
57
|
-
setStatus('connected');
|
|
58
|
-
}
|
|
59
|
-
} catch {
|
|
60
|
-
// Ignore - may not be available
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return () => {
|
|
64
|
-
window.removeEventListener('vite:ws-connect', handleConnect);
|
|
65
|
-
window.removeEventListener('vite:ws-disconnect', handleDisconnect);
|
|
66
|
-
};
|
|
67
|
-
}, []);
|
|
68
|
-
|
|
69
|
-
// Manual check for connection status every 5 seconds
|
|
70
|
-
useEffect(() => {
|
|
71
|
-
const checkConnection = () => {
|
|
72
|
-
if (typeof import.meta.hot === 'undefined') {
|
|
73
|
-
setStatus('disconnected');
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
// @ts-expect-error Vite internal
|
|
79
|
-
const socket = import.meta.hot.connection?.socket;
|
|
80
|
-
if (socket) {
|
|
81
|
-
switch (socket.readyState) {
|
|
82
|
-
case 0: // CONNECTING
|
|
83
|
-
setStatus('reconnecting');
|
|
84
|
-
break;
|
|
85
|
-
case 1: // OPEN
|
|
86
|
-
setStatus('connected');
|
|
87
|
-
break;
|
|
88
|
-
case 2: // CLOSING
|
|
89
|
-
case 3: // CLOSED
|
|
90
|
-
setStatus('disconnected');
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
} catch {
|
|
95
|
-
// Ignore errors - HMR may not be available
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
// Initial check
|
|
100
|
-
checkConnection();
|
|
101
|
-
|
|
102
|
-
// Periodic check
|
|
103
|
-
const interval = setInterval(checkConnection, 5000);
|
|
104
|
-
|
|
105
|
-
return () => clearInterval(interval);
|
|
106
|
-
}, []);
|
|
107
|
-
|
|
108
|
-
return { status, lastUpdate };
|
|
109
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { useState, useEffect, useRef } from 'react';
|
|
2
|
-
|
|
3
|
-
interface UseScrollSpyOptions {
|
|
4
|
-
/** IDs of sections to observe */
|
|
5
|
-
sectionIds: string[];
|
|
6
|
-
/** Offset from top of viewport to consider section as active */
|
|
7
|
-
offset?: number;
|
|
8
|
-
/** Root element to observe within (default: document) */
|
|
9
|
-
root?: Element | null;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function useScrollSpy({ sectionIds, offset = 100, root = null }: UseScrollSpyOptions) {
|
|
13
|
-
const [activeId, setActiveId] = useState<string | null>(sectionIds[0] ?? null);
|
|
14
|
-
const observerRef = useRef<IntersectionObserver | null>(null);
|
|
15
|
-
|
|
16
|
-
useEffect(() => {
|
|
17
|
-
const elements = sectionIds
|
|
18
|
-
.map((id) => document.getElementById(id))
|
|
19
|
-
.filter((el): el is HTMLElement => el !== null);
|
|
20
|
-
|
|
21
|
-
if (elements.length === 0) return;
|
|
22
|
-
|
|
23
|
-
// Track which sections are currently visible
|
|
24
|
-
const visibleSections = new Map<string, number>();
|
|
25
|
-
|
|
26
|
-
observerRef.current = new IntersectionObserver(
|
|
27
|
-
(entries) => {
|
|
28
|
-
entries.forEach((entry) => {
|
|
29
|
-
const id = entry.target.id;
|
|
30
|
-
if (entry.isIntersecting) {
|
|
31
|
-
// Store the top position of visible sections
|
|
32
|
-
visibleSections.set(id, entry.boundingClientRect.top);
|
|
33
|
-
} else {
|
|
34
|
-
visibleSections.delete(id);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// Find the section closest to the top
|
|
39
|
-
if (visibleSections.size > 0) {
|
|
40
|
-
let closestId = '';
|
|
41
|
-
let closestDistance = Infinity;
|
|
42
|
-
|
|
43
|
-
visibleSections.forEach((top, id) => {
|
|
44
|
-
const distance = Math.abs(top - offset);
|
|
45
|
-
if (distance < closestDistance) {
|
|
46
|
-
closestDistance = distance;
|
|
47
|
-
closestId = id;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
if (closestId) {
|
|
52
|
-
setActiveId(closestId);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
root,
|
|
58
|
-
rootMargin: `-${offset}px 0px -50% 0px`,
|
|
59
|
-
threshold: [0, 0.25, 0.5, 0.75, 1],
|
|
60
|
-
}
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
elements.forEach((el) => observerRef.current?.observe(el));
|
|
64
|
-
|
|
65
|
-
return () => {
|
|
66
|
-
observerRef.current?.disconnect();
|
|
67
|
-
};
|
|
68
|
-
}, [sectionIds, offset, root]);
|
|
69
|
-
|
|
70
|
-
const scrollToSection = (id: string) => {
|
|
71
|
-
const element = document.getElementById(id);
|
|
72
|
-
if (element) {
|
|
73
|
-
element.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
return { activeId, scrollToSection };
|
|
78
|
-
}
|