@fragments-sdk/cli 0.2.2
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/LICENSE +21 -0
- package/README.md +106 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +4783 -0
- package/dist/bin.js.map +1 -0
- package/dist/chunk-4FDQSGKX.js +786 -0
- package/dist/chunk-4FDQSGKX.js.map +1 -0
- package/dist/chunk-7H2MMGYG.js +369 -0
- package/dist/chunk-7H2MMGYG.js.map +1 -0
- package/dist/chunk-BSCG3IP7.js +619 -0
- package/dist/chunk-BSCG3IP7.js.map +1 -0
- package/dist/chunk-LY2CFFPY.js +898 -0
- package/dist/chunk-LY2CFFPY.js.map +1 -0
- package/dist/chunk-MUZ6CM66.js +6636 -0
- package/dist/chunk-MUZ6CM66.js.map +1 -0
- package/dist/chunk-OAENNG3G.js +1489 -0
- package/dist/chunk-OAENNG3G.js.map +1 -0
- package/dist/chunk-XHNKNI6J.js +235 -0
- package/dist/chunk-XHNKNI6J.js.map +1 -0
- package/dist/core-DWKLGY4N.js +68 -0
- package/dist/core-DWKLGY4N.js.map +1 -0
- package/dist/generate-4LQNJ7SX.js +249 -0
- package/dist/generate-4LQNJ7SX.js.map +1 -0
- package/dist/index.d.ts +775 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/init-EMVI47QG.js +416 -0
- package/dist/init-EMVI47QG.js.map +1 -0
- package/dist/mcp-bin.d.ts +1 -0
- package/dist/mcp-bin.js +1117 -0
- package/dist/mcp-bin.js.map +1 -0
- package/dist/scan-4YPRF7FV.js +12 -0
- package/dist/scan-4YPRF7FV.js.map +1 -0
- package/dist/service-QSZMZJBJ.js +208 -0
- package/dist/service-QSZMZJBJ.js.map +1 -0
- package/dist/static-viewer-MIPGZ4Z7.js +12 -0
- package/dist/static-viewer-MIPGZ4Z7.js.map +1 -0
- package/dist/test-SQ5ZHXWU.js +1067 -0
- package/dist/test-SQ5ZHXWU.js.map +1 -0
- package/dist/tokens-HSGMYK64.js +173 -0
- package/dist/tokens-HSGMYK64.js.map +1 -0
- package/dist/viewer-YRF4SQE4.js +11101 -0
- package/dist/viewer-YRF4SQE4.js.map +1 -0
- package/package.json +107 -0
- package/src/ai.ts +266 -0
- package/src/analyze.ts +265 -0
- package/src/bin.ts +916 -0
- package/src/build.ts +248 -0
- package/src/commands/a11y.ts +302 -0
- package/src/commands/add.ts +313 -0
- package/src/commands/audit.ts +195 -0
- package/src/commands/baseline.ts +221 -0
- package/src/commands/build.ts +144 -0
- package/src/commands/compare.ts +337 -0
- package/src/commands/context.ts +107 -0
- package/src/commands/dev.ts +107 -0
- package/src/commands/enhance.ts +858 -0
- package/src/commands/generate.ts +391 -0
- package/src/commands/init.ts +531 -0
- package/src/commands/link/figma.ts +645 -0
- package/src/commands/link/index.ts +10 -0
- package/src/commands/link/storybook.ts +267 -0
- package/src/commands/list.ts +49 -0
- package/src/commands/metrics.ts +114 -0
- package/src/commands/reset.ts +242 -0
- package/src/commands/scan.ts +537 -0
- package/src/commands/storygen.ts +207 -0
- package/src/commands/tokens.ts +251 -0
- package/src/commands/validate.ts +93 -0
- package/src/commands/verify.ts +215 -0
- package/src/core/composition.test.ts +262 -0
- package/src/core/composition.ts +255 -0
- package/src/core/config.ts +84 -0
- package/src/core/constants.ts +111 -0
- package/src/core/context.ts +380 -0
- package/src/core/defineSegment.ts +137 -0
- package/src/core/discovery.ts +337 -0
- package/src/core/figma.ts +263 -0
- package/src/core/fragment-types.ts +214 -0
- package/src/core/generators/context.ts +389 -0
- package/src/core/generators/index.ts +23 -0
- package/src/core/generators/registry.ts +364 -0
- package/src/core/generators/typescript-extractor.ts +374 -0
- package/src/core/importAnalyzer.ts +217 -0
- package/src/core/index.ts +149 -0
- package/src/core/loader.ts +155 -0
- package/src/core/node.ts +63 -0
- package/src/core/parser.ts +551 -0
- package/src/core/previewLoader.ts +172 -0
- package/src/core/schema/fragment.schema.json +189 -0
- package/src/core/schema/registry.schema.json +137 -0
- package/src/core/schema.ts +182 -0
- package/src/core/storyAdapter.test.ts +571 -0
- package/src/core/storyAdapter.ts +761 -0
- package/src/core/token-types.ts +287 -0
- package/src/core/types.ts +754 -0
- package/src/diff.ts +323 -0
- package/src/index.ts +43 -0
- package/src/mcp/__tests__/projectFields.test.ts +130 -0
- package/src/mcp/bin.ts +36 -0
- package/src/mcp/index.ts +8 -0
- package/src/mcp/server.ts +1310 -0
- package/src/mcp/utils.ts +54 -0
- package/src/mcp-bin.ts +36 -0
- package/src/migrate/__tests__/argTypes/argTypes.test.ts +189 -0
- package/src/migrate/__tests__/args/args.test.ts +452 -0
- package/src/migrate/__tests__/meta/meta.test.ts +198 -0
- package/src/migrate/__tests__/stories/stories.test.ts +278 -0
- package/src/migrate/__tests__/utils/utils.test.ts +371 -0
- package/src/migrate/__tests__/values/values.test.ts +303 -0
- package/src/migrate/bin.ts +108 -0
- package/src/migrate/converter.ts +658 -0
- package/src/migrate/detect.ts +196 -0
- package/src/migrate/index.ts +45 -0
- package/src/migrate/migrate.ts +163 -0
- package/src/migrate/parser.ts +1136 -0
- package/src/migrate/report.ts +624 -0
- package/src/migrate/types.ts +169 -0
- package/src/screenshot.ts +249 -0
- package/src/service/__tests__/ast-utils.test.ts +426 -0
- package/src/service/__tests__/enhance-scanner.test.ts +200 -0
- package/src/service/__tests__/figma/figma.test.ts +652 -0
- package/src/service/__tests__/metrics-store.test.ts +409 -0
- package/src/service/__tests__/patch-generator.test.ts +186 -0
- package/src/service/__tests__/props-extractor.test.ts +365 -0
- package/src/service/__tests__/token-registry.test.ts +267 -0
- package/src/service/analytics.ts +659 -0
- package/src/service/ast-utils.ts +444 -0
- package/src/service/browser-pool.ts +339 -0
- package/src/service/capture.ts +267 -0
- package/src/service/diff.ts +279 -0
- package/src/service/enhance/aggregator.ts +489 -0
- package/src/service/enhance/cache.ts +275 -0
- package/src/service/enhance/codebase-scanner.ts +357 -0
- package/src/service/enhance/context-generator.ts +529 -0
- package/src/service/enhance/doc-extractor.ts +523 -0
- package/src/service/enhance/index.ts +131 -0
- package/src/service/enhance/props-extractor.ts +665 -0
- package/src/service/enhance/scanner.ts +445 -0
- package/src/service/enhance/storybook-parser.ts +552 -0
- package/src/service/enhance/types.ts +346 -0
- package/src/service/enhance/variant-renderer.ts +479 -0
- package/src/service/figma.ts +1008 -0
- package/src/service/index.ts +249 -0
- package/src/service/metrics-store.ts +333 -0
- package/src/service/patch-generator.ts +349 -0
- package/src/service/report.ts +854 -0
- package/src/service/storage.ts +401 -0
- package/src/service/token-fixes.ts +281 -0
- package/src/service/token-parser.ts +504 -0
- package/src/service/token-registry.ts +721 -0
- package/src/service/utils.ts +172 -0
- package/src/setup.ts +241 -0
- package/src/shared/command-wrapper.ts +81 -0
- package/src/shared/dev-server-client.ts +199 -0
- package/src/shared/index.ts +8 -0
- package/src/shared/segment-loader.ts +59 -0
- package/src/shared/types.ts +147 -0
- package/src/static-viewer.ts +715 -0
- package/src/test/discovery.ts +172 -0
- package/src/test/index.ts +281 -0
- package/src/test/reporters/console.ts +194 -0
- package/src/test/reporters/json.ts +190 -0
- package/src/test/reporters/junit.ts +186 -0
- package/src/test/runner.ts +598 -0
- package/src/test/types.ts +245 -0
- package/src/test/watch.ts +200 -0
- package/src/validators.ts +152 -0
- package/src/viewer/__tests__/jsx-parser.test.ts +502 -0
- package/src/viewer/__tests__/render-utils.test.ts +232 -0
- package/src/viewer/__tests__/style-utils.test.ts +404 -0
- package/src/viewer/bin.ts +86 -0
- package/src/viewer/cli/health.ts +256 -0
- package/src/viewer/cli/index.ts +33 -0
- package/src/viewer/cli/scan.ts +124 -0
- package/src/viewer/cli/utils.ts +174 -0
- package/src/viewer/components/AccessibilityPanel.tsx +1404 -0
- package/src/viewer/components/ActionCapture.tsx +172 -0
- package/src/viewer/components/ActionsPanel.tsx +371 -0
- package/src/viewer/components/App.tsx +638 -0
- package/src/viewer/components/BottomPanel.tsx +224 -0
- package/src/viewer/components/CodePanel.tsx +589 -0
- package/src/viewer/components/CommandPalette.tsx +336 -0
- package/src/viewer/components/ComponentGraph.tsx +394 -0
- package/src/viewer/components/ComponentHeader.tsx +85 -0
- package/src/viewer/components/ContractPanel.tsx +234 -0
- package/src/viewer/components/ErrorBoundary.tsx +85 -0
- package/src/viewer/components/FigmaEmbed.tsx +231 -0
- package/src/viewer/components/FragmentEditor.tsx +485 -0
- package/src/viewer/components/HealthDashboard.tsx +452 -0
- package/src/viewer/components/HmrStatusIndicator.tsx +71 -0
- package/src/viewer/components/Icons.tsx +417 -0
- package/src/viewer/components/InteractionsPanel.tsx +720 -0
- package/src/viewer/components/IsolatedPreviewFrame.tsx +321 -0
- package/src/viewer/components/IsolatedRender.tsx +111 -0
- package/src/viewer/components/KeyboardShortcutsHelp.tsx +89 -0
- package/src/viewer/components/LandingPage.tsx +441 -0
- package/src/viewer/components/Layout.tsx +22 -0
- package/src/viewer/components/LeftSidebar.tsx +391 -0
- package/src/viewer/components/MultiViewportPreview.tsx +429 -0
- package/src/viewer/components/PreviewArea.tsx +404 -0
- package/src/viewer/components/PreviewFrameHost.tsx +310 -0
- package/src/viewer/components/PreviewPane.tsx +150 -0
- package/src/viewer/components/PreviewToolbar.tsx +176 -0
- package/src/viewer/components/PropsEditor.tsx +512 -0
- package/src/viewer/components/PropsTable.tsx +98 -0
- package/src/viewer/components/RelationsSection.tsx +57 -0
- package/src/viewer/components/ResizablePanel.tsx +328 -0
- package/src/viewer/components/RightSidebar.tsx +118 -0
- package/src/viewer/components/ScreenshotButton.tsx +90 -0
- package/src/viewer/components/Sidebar.tsx +169 -0
- package/src/viewer/components/SkeletonLoader.tsx +156 -0
- package/src/viewer/components/StoryRenderer.tsx +128 -0
- package/src/viewer/components/ThemeProvider.tsx +96 -0
- package/src/viewer/components/Toast.tsx +67 -0
- package/src/viewer/components/TokenStylePanel.tsx +708 -0
- package/src/viewer/components/UsageSection.tsx +95 -0
- package/src/viewer/components/VariantMatrix.tsx +350 -0
- package/src/viewer/components/VariantRenderer.tsx +131 -0
- package/src/viewer/components/VariantTabs.tsx +84 -0
- package/src/viewer/components/ViewportSelector.tsx +165 -0
- package/src/viewer/components/_future/CreatePage.tsx +836 -0
- package/src/viewer/composition-renderer.ts +381 -0
- package/src/viewer/constants/index.ts +1 -0
- package/src/viewer/constants/ui.ts +185 -0
- package/src/viewer/entry.tsx +299 -0
- package/src/viewer/hooks/index.ts +2 -0
- package/src/viewer/hooks/useA11yCache.ts +383 -0
- package/src/viewer/hooks/useA11yService.ts +498 -0
- package/src/viewer/hooks/useActions.ts +138 -0
- package/src/viewer/hooks/useAppState.ts +124 -0
- package/src/viewer/hooks/useFigmaIntegration.ts +132 -0
- package/src/viewer/hooks/useHmrStatus.ts +109 -0
- package/src/viewer/hooks/useKeyboardShortcuts.ts +222 -0
- package/src/viewer/hooks/usePreviewBridge.ts +347 -0
- package/src/viewer/hooks/useScrollSpy.ts +78 -0
- package/src/viewer/hooks/useUrlState.ts +330 -0
- package/src/viewer/hooks/useViewSettings.ts +125 -0
- package/src/viewer/index.html +28 -0
- package/src/viewer/index.ts +14 -0
- package/src/viewer/intelligence/healthReport.ts +505 -0
- package/src/viewer/intelligence/styleDrift.ts +340 -0
- package/src/viewer/intelligence/usageScanner.ts +309 -0
- package/src/viewer/jsx-parser.ts +485 -0
- package/src/viewer/postcss.config.js +6 -0
- package/src/viewer/preview-frame-entry.tsx +25 -0
- package/src/viewer/preview-frame.html +109 -0
- package/src/viewer/render-template.html +68 -0
- package/src/viewer/render-utils.ts +170 -0
- package/src/viewer/server.ts +276 -0
- package/src/viewer/style-utils.ts +414 -0
- package/src/viewer/styles/globals.css +355 -0
- package/src/viewer/tailwind.config.js +37 -0
- package/src/viewer/types/a11y.ts +197 -0
- package/src/viewer/utils/a11y-fixes.ts +471 -0
- package/src/viewer/utils/actionExport.ts +372 -0
- package/src/viewer/utils/colorSchemes.ts +201 -0
- package/src/viewer/utils/detectRelationships.ts +256 -0
- package/src/viewer/vite-plugin.ts +2143 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { createRoot, type Root } from "react-dom/client";
|
|
2
|
+
import { Component, type ReactNode, type ErrorInfo } from "react";
|
|
3
|
+
import { App } from "./components/App.js";
|
|
4
|
+
import { ThemeProvider } from "./components/ThemeProvider.js";
|
|
5
|
+
import { AppSkeleton } from "./components/SkeletonLoader.js";
|
|
6
|
+
// Viewer shell styles - independent from UI library
|
|
7
|
+
// UI library styles are only loaded in the isolated preview area
|
|
8
|
+
import "./styles/globals.css";
|
|
9
|
+
|
|
10
|
+
// App-level error boundary that catches any unhandled errors
|
|
11
|
+
class AppErrorBoundary extends Component<{ children: ReactNode }, { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null }> {
|
|
12
|
+
constructor(props: { children: ReactNode }) {
|
|
13
|
+
super(props);
|
|
14
|
+
this.state = { hasError: false, error: null, errorInfo: null };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static getDerivedStateFromError(error: Error) {
|
|
18
|
+
return { hasError: true, error };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
22
|
+
console.error('[Fragments] App Error:', error);
|
|
23
|
+
console.error('[Fragments] Component Stack:', errorInfo.componentStack);
|
|
24
|
+
this.setState({ errorInfo });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
handleRetry = () => {
|
|
28
|
+
this.setState({ hasError: false, error: null, errorInfo: null });
|
|
29
|
+
window.location.reload();
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
render() {
|
|
33
|
+
if (this.state.hasError) {
|
|
34
|
+
const { error, errorInfo } = this.state;
|
|
35
|
+
return (
|
|
36
|
+
<div style={{
|
|
37
|
+
fontFamily: 'system-ui, -apple-system, sans-serif',
|
|
38
|
+
padding: '40px',
|
|
39
|
+
maxWidth: '800px',
|
|
40
|
+
margin: '0 auto',
|
|
41
|
+
}}>
|
|
42
|
+
<div style={{
|
|
43
|
+
background: '#fef2f2',
|
|
44
|
+
border: '1px solid #fecaca',
|
|
45
|
+
borderRadius: '12px',
|
|
46
|
+
padding: '24px',
|
|
47
|
+
}}>
|
|
48
|
+
<h1 style={{ color: '#dc2626', marginBottom: '16px', fontSize: '20px', fontWeight: 600 }}>
|
|
49
|
+
Application Error
|
|
50
|
+
</h1>
|
|
51
|
+
<p style={{ color: '#991b1b', marginBottom: '16px', fontSize: '14px' }}>
|
|
52
|
+
An error occurred while rendering the application:
|
|
53
|
+
</p>
|
|
54
|
+
<pre style={{
|
|
55
|
+
background: '#fee2e2',
|
|
56
|
+
border: '1px solid #fecaca',
|
|
57
|
+
borderRadius: '8px',
|
|
58
|
+
padding: '16px',
|
|
59
|
+
overflow: 'auto',
|
|
60
|
+
whiteSpace: 'pre-wrap',
|
|
61
|
+
wordBreak: 'break-word',
|
|
62
|
+
color: '#991b1b',
|
|
63
|
+
fontSize: '13px',
|
|
64
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
|
|
65
|
+
marginBottom: '16px',
|
|
66
|
+
}}>
|
|
67
|
+
{error?.message || 'Unknown error'}
|
|
68
|
+
</pre>
|
|
69
|
+
{errorInfo?.componentStack && (
|
|
70
|
+
<details style={{ marginBottom: '16px' }}>
|
|
71
|
+
<summary style={{
|
|
72
|
+
color: '#b91c1c',
|
|
73
|
+
cursor: 'pointer',
|
|
74
|
+
fontSize: '13px',
|
|
75
|
+
marginBottom: '8px',
|
|
76
|
+
}}>
|
|
77
|
+
Show component stack trace
|
|
78
|
+
</summary>
|
|
79
|
+
<pre style={{
|
|
80
|
+
background: '#fee2e2',
|
|
81
|
+
border: '1px solid #fecaca',
|
|
82
|
+
borderRadius: '8px',
|
|
83
|
+
padding: '12px',
|
|
84
|
+
overflow: 'auto',
|
|
85
|
+
whiteSpace: 'pre-wrap',
|
|
86
|
+
wordBreak: 'break-word',
|
|
87
|
+
color: '#7f1d1d',
|
|
88
|
+
fontSize: '11px',
|
|
89
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
|
|
90
|
+
maxHeight: '300px',
|
|
91
|
+
}}>
|
|
92
|
+
{error?.stack || errorInfo.componentStack}
|
|
93
|
+
</pre>
|
|
94
|
+
</details>
|
|
95
|
+
)}
|
|
96
|
+
<button
|
|
97
|
+
onClick={this.handleRetry}
|
|
98
|
+
style={{
|
|
99
|
+
background: '#dc2626',
|
|
100
|
+
color: 'white',
|
|
101
|
+
border: 'none',
|
|
102
|
+
borderRadius: '6px',
|
|
103
|
+
padding: '10px 16px',
|
|
104
|
+
fontSize: '14px',
|
|
105
|
+
fontWeight: 500,
|
|
106
|
+
cursor: 'pointer',
|
|
107
|
+
}}
|
|
108
|
+
>
|
|
109
|
+
Reload Application
|
|
110
|
+
</button>
|
|
111
|
+
</div>
|
|
112
|
+
<p style={{ marginTop: '16px', color: '#6b7280', fontSize: '13px' }}>
|
|
113
|
+
Check the browser console for more details. Common causes:
|
|
114
|
+
</p>
|
|
115
|
+
<ul style={{ color: '#6b7280', marginTop: '8px', paddingLeft: '20px', fontSize: '13px' }}>
|
|
116
|
+
<li>Syntax errors in segment or component files</li>
|
|
117
|
+
<li>Missing or undefined imports</li>
|
|
118
|
+
<li>Components that crash during render</li>
|
|
119
|
+
<li>Invalid props passed to components</li>
|
|
120
|
+
</ul>
|
|
121
|
+
</div>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return this.props.children;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Declare global types
|
|
130
|
+
declare global {
|
|
131
|
+
interface Window {
|
|
132
|
+
__SEGMENTS__?: Array<{
|
|
133
|
+
path: string;
|
|
134
|
+
segment: import("../core/index.js").SegmentDefinition;
|
|
135
|
+
}>;
|
|
136
|
+
__SEGMENTS_CONFIG__?: import("../core/index.js").SegmentsConfig;
|
|
137
|
+
__SEGMENTS_ERROR__?: string;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
type SegmentItem = {
|
|
142
|
+
path: string;
|
|
143
|
+
segment: import("../core/index.js").SegmentDefinition;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// Initialize segments from window or empty array
|
|
147
|
+
let segments: SegmentItem[] = window.__SEGMENTS__ ?? [];
|
|
148
|
+
let loadError: string | null = window.__SEGMENTS_ERROR__ ?? null;
|
|
149
|
+
let appRoot: Root | null = null;
|
|
150
|
+
|
|
151
|
+
// Filter helper
|
|
152
|
+
function filterValidSegments(items: SegmentItem[]): SegmentItem[] {
|
|
153
|
+
return items.filter(s => s && s.segment && s.segment.meta);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Async loader for virtual module (runs after initial render)
|
|
157
|
+
async function loadSegmentsFromVirtualModule(): Promise<void> {
|
|
158
|
+
if (segments.length > 0 || loadError) {
|
|
159
|
+
// Already have segments or error from window
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
// @ts-expect-error Virtual module
|
|
165
|
+
const virtualModule = await import("virtual:fragments");
|
|
166
|
+
// Wait for lazy-loaded segments
|
|
167
|
+
if (virtualModule.segmentsPromise) {
|
|
168
|
+
segments = await virtualModule.segmentsPromise;
|
|
169
|
+
} else {
|
|
170
|
+
segments = virtualModule.segments;
|
|
171
|
+
}
|
|
172
|
+
segments = filterValidSegments(segments);
|
|
173
|
+
} catch (e) {
|
|
174
|
+
const error = e as Error;
|
|
175
|
+
loadError = error.message || "Failed to load segments";
|
|
176
|
+
console.error("[Fragments] Failed to load:", error);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Filter initial segments
|
|
181
|
+
segments = filterValidSegments(segments);
|
|
182
|
+
|
|
183
|
+
// Error display component
|
|
184
|
+
function ErrorDisplay({ error }: { error: string }) {
|
|
185
|
+
return (
|
|
186
|
+
<div style={{
|
|
187
|
+
fontFamily: 'system-ui, -apple-system, sans-serif',
|
|
188
|
+
padding: '40px',
|
|
189
|
+
maxWidth: '600px',
|
|
190
|
+
margin: '0 auto',
|
|
191
|
+
}}>
|
|
192
|
+
<h1 style={{ color: '#dc2626', marginBottom: '16px' }}>
|
|
193
|
+
Failed to Load Segments
|
|
194
|
+
</h1>
|
|
195
|
+
<pre style={{
|
|
196
|
+
background: '#fef2f2',
|
|
197
|
+
border: '1px solid #fecaca',
|
|
198
|
+
borderRadius: '8px',
|
|
199
|
+
padding: '16px',
|
|
200
|
+
overflow: 'auto',
|
|
201
|
+
whiteSpace: 'pre-wrap',
|
|
202
|
+
wordBreak: 'break-word',
|
|
203
|
+
color: '#991b1b',
|
|
204
|
+
fontSize: '14px',
|
|
205
|
+
}}>
|
|
206
|
+
{error}
|
|
207
|
+
</pre>
|
|
208
|
+
<p style={{ marginTop: '16px', color: '#6b7280' }}>
|
|
209
|
+
Check the console for more details. Common issues:
|
|
210
|
+
</p>
|
|
211
|
+
<ul style={{ color: '#6b7280', marginTop: '8px', paddingLeft: '20px' }}>
|
|
212
|
+
<li>Segment files have syntax errors</li>
|
|
213
|
+
<li>Components are not exported correctly</li>
|
|
214
|
+
<li>Missing dependencies</li>
|
|
215
|
+
</ul>
|
|
216
|
+
</div>
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Render the app
|
|
221
|
+
const rootElement = document.getElementById("root");
|
|
222
|
+
if (rootElement) {
|
|
223
|
+
appRoot = createRoot(rootElement);
|
|
224
|
+
|
|
225
|
+
const renderApp = (showSkeleton = false) => {
|
|
226
|
+
const currentSegments = window.__SEGMENTS__ ?? segments;
|
|
227
|
+
const currentError = loadError || window.__SEGMENTS_ERROR__;
|
|
228
|
+
|
|
229
|
+
// Show skeleton while loading
|
|
230
|
+
if (showSkeleton && currentSegments.length === 0 && !currentError) {
|
|
231
|
+
appRoot?.render(
|
|
232
|
+
<ThemeProvider>
|
|
233
|
+
<AppSkeleton />
|
|
234
|
+
</ThemeProvider>
|
|
235
|
+
);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Show error if we have one and no valid segments
|
|
240
|
+
if (currentError && currentSegments.length === 0) {
|
|
241
|
+
appRoot?.render(<ErrorDisplay error={currentError} />);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
appRoot?.render(
|
|
246
|
+
<ThemeProvider>
|
|
247
|
+
<AppErrorBoundary>
|
|
248
|
+
<App segments={currentSegments} />
|
|
249
|
+
</AppErrorBoundary>
|
|
250
|
+
</ThemeProvider>
|
|
251
|
+
);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// Show skeleton immediately if no segments yet
|
|
255
|
+
if (segments.length === 0 && !loadError) {
|
|
256
|
+
renderApp(true);
|
|
257
|
+
|
|
258
|
+
// Load segments asynchronously and re-render
|
|
259
|
+
loadSegmentsFromVirtualModule().then(() => {
|
|
260
|
+
renderApp(false);
|
|
261
|
+
});
|
|
262
|
+
} else {
|
|
263
|
+
// We have segments from window, render immediately
|
|
264
|
+
renderApp(false);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Listen for HMR updates from Segments plugin
|
|
268
|
+
window.addEventListener("segments:update", () => {
|
|
269
|
+
console.log("[Fragments] Refreshing viewer...");
|
|
270
|
+
// Re-render with updated segments
|
|
271
|
+
renderApp(false);
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// HMR support for entry file and a11y invalidation
|
|
276
|
+
// @ts-expect-error Vite HMR types
|
|
277
|
+
if (import.meta.hot) {
|
|
278
|
+
// @ts-expect-error Vite HMR types
|
|
279
|
+
import.meta.hot.accept();
|
|
280
|
+
|
|
281
|
+
// Subscribe to a11y invalidation events from Vite plugin
|
|
282
|
+
// @ts-expect-error Vite HMR types
|
|
283
|
+
import.meta.hot.on("segments:a11y-invalidate", (data: {
|
|
284
|
+
components: string[];
|
|
285
|
+
file: string;
|
|
286
|
+
timestamp: number;
|
|
287
|
+
}) => {
|
|
288
|
+
console.log("[Fragments] A11y invalidation triggered for:", data.components.join(", "));
|
|
289
|
+
|
|
290
|
+
// Dispatch custom event for a11y service to handle
|
|
291
|
+
window.dispatchEvent(new CustomEvent("a11y-cache-invalidated", {
|
|
292
|
+
detail: {
|
|
293
|
+
components: data.components,
|
|
294
|
+
file: data.file,
|
|
295
|
+
timestamp: data.timestamp,
|
|
296
|
+
},
|
|
297
|
+
}));
|
|
298
|
+
});
|
|
299
|
+
}
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared A11y Cache
|
|
3
|
+
*
|
|
4
|
+
* Provides a global cache for accessibility scan results that:
|
|
5
|
+
* 1. Persists in sessionStorage across page navigations
|
|
6
|
+
* 2. Is shared between Dashboard and AccessibilityPanel
|
|
7
|
+
* 3. Stores full violation details for rich UI display
|
|
8
|
+
* 4. Supports component-level invalidation
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { BRAND } from '../../core/index.js';
|
|
12
|
+
import type {
|
|
13
|
+
CachedA11yResult,
|
|
14
|
+
SerializedViolation,
|
|
15
|
+
A11ySummary,
|
|
16
|
+
} from '../types/a11y.js';
|
|
17
|
+
|
|
18
|
+
const CACHE_KEY = `${BRAND.storagePrefix}a11y-cache`;
|
|
19
|
+
const CACHE_VERSION = 2; // Bumped for new schema with full violations
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Legacy interface for backward compatibility
|
|
23
|
+
* @deprecated Use CachedA11yResult instead
|
|
24
|
+
*/
|
|
25
|
+
export interface ComponentA11yData {
|
|
26
|
+
violations: number;
|
|
27
|
+
passes: number;
|
|
28
|
+
incomplete: number;
|
|
29
|
+
critical: number;
|
|
30
|
+
serious: number;
|
|
31
|
+
moderate: number;
|
|
32
|
+
minor: number;
|
|
33
|
+
scannedAt: number;
|
|
34
|
+
variant?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface A11yCacheData {
|
|
38
|
+
version: number;
|
|
39
|
+
components: Record<string, CachedA11yResult>;
|
|
40
|
+
lastFullScan?: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Load cache from sessionStorage
|
|
45
|
+
*/
|
|
46
|
+
function loadCache(): A11yCacheData {
|
|
47
|
+
try {
|
|
48
|
+
const stored = sessionStorage.getItem(CACHE_KEY);
|
|
49
|
+
if (stored) {
|
|
50
|
+
const data = JSON.parse(stored) as A11yCacheData;
|
|
51
|
+
if (data.version === CACHE_VERSION) {
|
|
52
|
+
return data;
|
|
53
|
+
}
|
|
54
|
+
// Version mismatch - clear old cache
|
|
55
|
+
sessionStorage.removeItem(CACHE_KEY);
|
|
56
|
+
}
|
|
57
|
+
} catch (e) {
|
|
58
|
+
// Ignore parse errors
|
|
59
|
+
}
|
|
60
|
+
return { version: CACHE_VERSION, components: {} };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Save cache to sessionStorage
|
|
65
|
+
*/
|
|
66
|
+
function saveCache(data: A11yCacheData): void {
|
|
67
|
+
try {
|
|
68
|
+
sessionStorage.setItem(CACHE_KEY, JSON.stringify(data));
|
|
69
|
+
} catch (e) {
|
|
70
|
+
// Storage might be full - try clearing old data
|
|
71
|
+
try {
|
|
72
|
+
sessionStorage.removeItem(CACHE_KEY);
|
|
73
|
+
sessionStorage.setItem(CACHE_KEY, JSON.stringify(data));
|
|
74
|
+
} catch {
|
|
75
|
+
// Ignore storage errors
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get cached a11y result for a component (full data)
|
|
82
|
+
*/
|
|
83
|
+
export function getComponentA11yResult(componentName: string): CachedA11yResult | null {
|
|
84
|
+
const cache = loadCache();
|
|
85
|
+
return cache.components[componentName] || null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get cached a11y data for a component (legacy format for backward compatibility)
|
|
90
|
+
*/
|
|
91
|
+
export function getComponentA11y(componentName: string): ComponentA11yData | null {
|
|
92
|
+
const result = getComponentA11yResult(componentName);
|
|
93
|
+
if (!result) return null;
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
violations: result.violations.length,
|
|
97
|
+
passes: result.passes,
|
|
98
|
+
incomplete: result.incomplete,
|
|
99
|
+
critical: result.counts.critical,
|
|
100
|
+
serious: result.counts.serious,
|
|
101
|
+
moderate: result.counts.moderate,
|
|
102
|
+
minor: result.counts.minor,
|
|
103
|
+
scannedAt: result.scannedAt,
|
|
104
|
+
variant: result.variant,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get all cached a11y data (full results)
|
|
110
|
+
*/
|
|
111
|
+
export function getAllA11yResults(): Record<string, CachedA11yResult> {
|
|
112
|
+
const cache = loadCache();
|
|
113
|
+
return cache.components;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Get all cached a11y data (legacy format)
|
|
118
|
+
*/
|
|
119
|
+
export function getAllA11yData(): Record<string, ComponentA11yData> {
|
|
120
|
+
const cache = loadCache();
|
|
121
|
+
const result: Record<string, ComponentA11yData> = {};
|
|
122
|
+
|
|
123
|
+
for (const [name, data] of Object.entries(cache.components)) {
|
|
124
|
+
result[name] = {
|
|
125
|
+
violations: data.violations.length,
|
|
126
|
+
passes: data.passes,
|
|
127
|
+
incomplete: data.incomplete,
|
|
128
|
+
critical: data.counts.critical,
|
|
129
|
+
serious: data.counts.serious,
|
|
130
|
+
moderate: data.counts.moderate,
|
|
131
|
+
minor: data.counts.minor,
|
|
132
|
+
scannedAt: data.scannedAt,
|
|
133
|
+
variant: data.variant,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Get timestamp of last full scan
|
|
142
|
+
*/
|
|
143
|
+
export function getLastFullScanTime(): number | undefined {
|
|
144
|
+
const cache = loadCache();
|
|
145
|
+
return cache.lastFullScan;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Update cache with full a11y result for a component
|
|
150
|
+
*/
|
|
151
|
+
export function updateComponentA11yResult(
|
|
152
|
+
componentName: string,
|
|
153
|
+
result: Omit<CachedA11yResult, 'componentName' | 'scannedAt'> & { scannedAt?: number }
|
|
154
|
+
): void {
|
|
155
|
+
const cache = loadCache();
|
|
156
|
+
cache.components[componentName] = {
|
|
157
|
+
componentName,
|
|
158
|
+
...result,
|
|
159
|
+
scannedAt: result.scannedAt || Date.now(),
|
|
160
|
+
};
|
|
161
|
+
saveCache(cache);
|
|
162
|
+
|
|
163
|
+
// Dispatch event for listeners
|
|
164
|
+
window.dispatchEvent(new CustomEvent('a11y-cache-updated', {
|
|
165
|
+
detail: {
|
|
166
|
+
componentName,
|
|
167
|
+
data: cache.components[componentName],
|
|
168
|
+
// Include legacy format for backward compatibility
|
|
169
|
+
legacyData: {
|
|
170
|
+
violations: result.violations.length,
|
|
171
|
+
passes: result.passes,
|
|
172
|
+
incomplete: result.incomplete,
|
|
173
|
+
critical: result.counts.critical,
|
|
174
|
+
serious: result.counts.serious,
|
|
175
|
+
moderate: result.counts.moderate,
|
|
176
|
+
minor: result.counts.minor,
|
|
177
|
+
scannedAt: result.scannedAt || Date.now(),
|
|
178
|
+
variant: result.variant,
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
}));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Update cache for a single component (legacy format, converted to new format)
|
|
186
|
+
*/
|
|
187
|
+
export function updateComponentA11y(
|
|
188
|
+
componentName: string,
|
|
189
|
+
data: Omit<ComponentA11yData, 'scannedAt'> & { scannedAt?: number },
|
|
190
|
+
violations?: SerializedViolation[]
|
|
191
|
+
): void {
|
|
192
|
+
const cache = loadCache();
|
|
193
|
+
|
|
194
|
+
// Convert to new format
|
|
195
|
+
cache.components[componentName] = {
|
|
196
|
+
componentName,
|
|
197
|
+
violations: violations || [], // Use provided violations or empty array
|
|
198
|
+
passes: data.passes,
|
|
199
|
+
incomplete: data.incomplete,
|
|
200
|
+
scannedAt: data.scannedAt || Date.now(),
|
|
201
|
+
variant: data.variant,
|
|
202
|
+
counts: {
|
|
203
|
+
critical: data.critical,
|
|
204
|
+
serious: data.serious,
|
|
205
|
+
moderate: data.moderate,
|
|
206
|
+
minor: data.minor,
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
saveCache(cache);
|
|
210
|
+
|
|
211
|
+
// Dispatch event for listeners
|
|
212
|
+
window.dispatchEvent(new CustomEvent('a11y-cache-updated', {
|
|
213
|
+
detail: { componentName, data: cache.components[componentName] },
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Invalidate cache for specific components
|
|
219
|
+
* This marks them for re-scan without removing the data
|
|
220
|
+
*/
|
|
221
|
+
export function invalidateComponents(componentNames: string[]): void {
|
|
222
|
+
const cache = loadCache();
|
|
223
|
+
let changed = false;
|
|
224
|
+
|
|
225
|
+
for (const name of componentNames) {
|
|
226
|
+
if (cache.components[name]) {
|
|
227
|
+
// Set scannedAt to 0 to mark as stale
|
|
228
|
+
cache.components[name].scannedAt = 0;
|
|
229
|
+
changed = true;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (changed) {
|
|
234
|
+
saveCache(cache);
|
|
235
|
+
|
|
236
|
+
// Dispatch event for listeners
|
|
237
|
+
window.dispatchEvent(new CustomEvent('a11y-cache-invalidated', {
|
|
238
|
+
detail: { components: componentNames },
|
|
239
|
+
}));
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Check if a component's cache is stale (needs re-scan)
|
|
245
|
+
*/
|
|
246
|
+
export function isComponentStale(componentName: string, maxAgeMs: number = 30000): boolean {
|
|
247
|
+
const cache = loadCache();
|
|
248
|
+
const data = cache.components[componentName];
|
|
249
|
+
|
|
250
|
+
if (!data) return true;
|
|
251
|
+
if (data.scannedAt === 0) return true; // Explicitly invalidated
|
|
252
|
+
|
|
253
|
+
return Date.now() - data.scannedAt > maxAgeMs;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Mark that a full scan was completed
|
|
258
|
+
*/
|
|
259
|
+
export function markFullScanComplete(): void {
|
|
260
|
+
const cache = loadCache();
|
|
261
|
+
cache.lastFullScan = Date.now();
|
|
262
|
+
saveCache(cache);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Clear all cached data
|
|
267
|
+
*/
|
|
268
|
+
export function clearA11yCache(): void {
|
|
269
|
+
try {
|
|
270
|
+
sessionStorage.removeItem(CACHE_KEY);
|
|
271
|
+
} catch (e) {
|
|
272
|
+
// Ignore
|
|
273
|
+
}
|
|
274
|
+
window.dispatchEvent(new CustomEvent('a11y-cache-cleared'));
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Clear cache for a specific component
|
|
279
|
+
*/
|
|
280
|
+
export function clearComponentCache(componentName: string): void {
|
|
281
|
+
const cache = loadCache();
|
|
282
|
+
if (cache.components[componentName]) {
|
|
283
|
+
delete cache.components[componentName];
|
|
284
|
+
saveCache(cache);
|
|
285
|
+
|
|
286
|
+
window.dispatchEvent(new CustomEvent('a11y-cache-updated', {
|
|
287
|
+
detail: { componentName, data: null },
|
|
288
|
+
}));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Calculate summary from cached data
|
|
294
|
+
*/
|
|
295
|
+
export function getA11ySummary(): A11ySummary {
|
|
296
|
+
const cache = loadCache();
|
|
297
|
+
const components = Object.values(cache.components);
|
|
298
|
+
|
|
299
|
+
const summary: A11ySummary = {
|
|
300
|
+
accessibleComponents: 0,
|
|
301
|
+
totalComponents: components.length,
|
|
302
|
+
violationsByImpact: {
|
|
303
|
+
critical: 0,
|
|
304
|
+
serious: 0,
|
|
305
|
+
moderate: 0,
|
|
306
|
+
minor: 0,
|
|
307
|
+
},
|
|
308
|
+
topViolations: [],
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
// Track violations by rule ID for top violations
|
|
312
|
+
const violationsByRule = new Map<string, {
|
|
313
|
+
ruleId: string;
|
|
314
|
+
description: string;
|
|
315
|
+
impact: 'critical' | 'serious' | 'moderate' | 'minor' | null;
|
|
316
|
+
affectedComponents: Set<string>;
|
|
317
|
+
}>();
|
|
318
|
+
|
|
319
|
+
for (const comp of components) {
|
|
320
|
+
// Count by impact
|
|
321
|
+
summary.violationsByImpact.critical += comp.counts.critical;
|
|
322
|
+
summary.violationsByImpact.serious += comp.counts.serious;
|
|
323
|
+
summary.violationsByImpact.moderate += comp.counts.moderate;
|
|
324
|
+
summary.violationsByImpact.minor += comp.counts.minor;
|
|
325
|
+
|
|
326
|
+
// Check if accessible (no critical/serious)
|
|
327
|
+
if (comp.counts.critical === 0 && comp.counts.serious === 0) {
|
|
328
|
+
summary.accessibleComponents++;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Aggregate violations by rule
|
|
332
|
+
for (const violation of comp.violations) {
|
|
333
|
+
const existing = violationsByRule.get(violation.id);
|
|
334
|
+
if (existing) {
|
|
335
|
+
existing.affectedComponents.add(comp.componentName);
|
|
336
|
+
} else {
|
|
337
|
+
violationsByRule.set(violation.id, {
|
|
338
|
+
ruleId: violation.id,
|
|
339
|
+
description: violation.description,
|
|
340
|
+
impact: violation.impact,
|
|
341
|
+
affectedComponents: new Set([comp.componentName]),
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Sort by number of affected components and take top 5
|
|
348
|
+
summary.topViolations = Array.from(violationsByRule.values())
|
|
349
|
+
.sort((a, b) => b.affectedComponents.size - a.affectedComponents.size)
|
|
350
|
+
.slice(0, 5)
|
|
351
|
+
.map(v => ({
|
|
352
|
+
ruleId: v.ruleId,
|
|
353
|
+
description: v.description,
|
|
354
|
+
impact: v.impact,
|
|
355
|
+
affectedComponents: Array.from(v.affectedComponents),
|
|
356
|
+
}));
|
|
357
|
+
|
|
358
|
+
return summary;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Get legacy summary format for backward compatibility
|
|
363
|
+
*/
|
|
364
|
+
export function getLegacyA11ySummary(): {
|
|
365
|
+
totalComponents: number;
|
|
366
|
+
accessibleComponents: number;
|
|
367
|
+
totalViolations: number;
|
|
368
|
+
totalCritical: number;
|
|
369
|
+
totalSerious: number;
|
|
370
|
+
} {
|
|
371
|
+
const summary = getA11ySummary();
|
|
372
|
+
return {
|
|
373
|
+
totalComponents: summary.totalComponents,
|
|
374
|
+
accessibleComponents: summary.accessibleComponents,
|
|
375
|
+
totalViolations:
|
|
376
|
+
summary.violationsByImpact.critical +
|
|
377
|
+
summary.violationsByImpact.serious +
|
|
378
|
+
summary.violationsByImpact.moderate +
|
|
379
|
+
summary.violationsByImpact.minor,
|
|
380
|
+
totalCritical: summary.violationsByImpact.critical,
|
|
381
|
+
totalSerious: summary.violationsByImpact.serious,
|
|
382
|
+
};
|
|
383
|
+
}
|