Skip to content

Commit 24fac7e

Browse files
sync(dfaebfdb): feat: App Shell + Isolated mode preview routing, framework-aware (HYP-342) (#168)
Source: https://github.com/hyperide/hyper-saas/commit/dfaebfdb913e053cd7681bb0eb03927cec606847
1 parent b23052e commit 24fac7e

35 files changed

Lines changed: 5287 additions & 214 deletions

‎bun.lock‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎client/App.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ const App = () => (
122122
);
123123

124124
// Mount the app
125-
// Preview mode uses separate entry point: CanvasPreviewEntry.tsx
126125
// biome-ignore lint/style/noNonNullAssertion: root element is guaranteed by index.html
127126
createRoot(document.getElementById('root')!).render(<App />);
128127

‎client/CanvasPreviewEntry.tsx‎

Lines changed: 0 additions & 32 deletions
This file was deleted.

‎client/components/IframeCanvas.tsx‎

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ import type { RuntimeError } from '../../shared/runtime-error';
99
// Canvas composition loaded from server only (no localStorage cache)
1010
import type { CanvasComposition, CanvasMode } from '../../shared/types/canvas';
1111

12-
// Module-level cache for registered preview components
13-
// Clears automatically on page reload (browser refresh)
14-
// Prevents redundant /api/generate-preview calls for already-registered components
15-
const registeredComponentsCache = new Map<string, Set<string>>();
16-
1712
interface IframeCanvasProps {
1813
componentPath: string;
1914
iframeLoadedCounter?: number;
@@ -265,56 +260,14 @@ export default function IframeCanvas({
265260
return () => window.removeEventListener('canvasCompositionChanged', handleCanvasChanged);
266261
}, [meta?.projectId, componentPath, onCanvasModeChange]);
267262

268-
// Auto-register component in __canvas_preview__.tsx before loading iframe
269-
// Uses module-level cache to avoid redundant API calls for same component
263+
// __canvas_preview__.tsx is pre-populated at init time with all project components
264+
// (via PreviewFileManager init-time full scan). No registration API call needed.
270265
useEffect(() => {
271266
if (!meta?.projectId || !componentPath) {
272267
setPreviewReady(false);
273268
return;
274269
}
275-
276-
// Check cache first - skip HTTP request if already registered
277-
const projectCache = registeredComponentsCache.get(meta.projectId);
278-
if (projectCache?.has(componentPath)) {
279-
console.log('[IframeCanvas] Component already registered (cached):', componentPath);
280-
setPreviewReady(true);
281-
return;
282-
}
283-
284-
setLoading(true);
285-
setPreviewReady(false);
286-
setError(null);
287-
288-
// Call API to ensure component is registered in __canvas_preview__.tsx
289-
authFetch('/api/generate-preview', {
290-
method: 'POST',
291-
headers: { 'Content-Type': 'application/json' },
292-
body: JSON.stringify({
293-
projectId: meta.projectId,
294-
components: [componentPath],
295-
}),
296-
})
297-
.then(async (response) => {
298-
if (!response.ok) {
299-
const errorData = await response.json();
300-
throw new Error(errorData.error || 'Failed to register component');
301-
}
302-
return response.json();
303-
})
304-
.then(() => {
305-
console.log('[IframeCanvas] Component registered:', componentPath);
306-
// Update cache after successful registration
307-
if (!registeredComponentsCache.has(meta.projectId)) {
308-
registeredComponentsCache.set(meta.projectId, new Set());
309-
}
310-
registeredComponentsCache.get(meta.projectId)?.add(componentPath);
311-
setPreviewReady(true);
312-
})
313-
.catch((err) => {
314-
console.error('[IframeCanvas] Failed to register component:', err);
315-
setError(`Failed to register component: ${err.message}`);
316-
setLoading(false);
317-
});
270+
setPreviewReady(true);
318271
}, [meta?.projectId, componentPath]);
319272

320273
// Inject styles on iframe load + check for gateway errors

‎client/contexts/ComponentMetaContext.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ interface ComponentMeta {
1111
relativeFilePath?: string;
1212
}
1313

14+
export type PreviewSetupStatus = 'ok' | 'unsupported' | 'needs-patch';
15+
1416
interface ComponentMetaContextType {
1517
meta: ComponentMeta | null;
1618
setMeta: (meta: ComponentMeta) => void;
1719
loadComponent: (componentPath: string, sampleName?: string) => Promise<void>;
1820
loadingComponent: string | null;
1921
parseError: string | null;
2022
setParseError: (error: string | null) => void;
23+
previewSetup: PreviewSetupStatus | null;
24+
setPreviewSetup: (status: PreviewSetupStatus | null) => void;
25+
needsPatchPrompt: string | null;
2126
currentSampleName: string | null;
2227
setCurrentSampleName: (name: string | null) => void;
2328
}
@@ -28,6 +33,8 @@ export function ComponentMetaProvider({ children }: { children: ReactNode }) {
2833
const [meta, setMetaInternal] = useState<ComponentMeta | null>(null);
2934
const [loadingComponent, setLoadingComponent] = useState<string | null>(null);
3035
const [parseError, setParseError] = useState<string | null>(null);
36+
const [previewSetup, setPreviewSetup] = useState<PreviewSetupStatus | null>(null);
37+
const [needsPatchPrompt, setNeedsPatchPrompt] = useState<string | null>(null);
3138
const [currentSampleName, setCurrentSampleName] = useState<string | null>(null);
3239

3340
const setMeta = useCallback((newMeta: ComponentMeta) => {
@@ -42,6 +49,8 @@ export function ComponentMetaProvider({ children }: { children: ReactNode }) {
4249
try {
4350
setLoadingComponent(componentPath);
4451
setParseError(null);
52+
setPreviewSetup(null);
53+
setNeedsPatchPrompt(null);
4554

4655
const effectiveSampleName = sampleName ?? 'default';
4756
setCurrentSampleName(effectiveSampleName);
@@ -56,6 +65,15 @@ export function ComponentMetaProvider({ children }: { children: ReactNode }) {
5665
// Don't setMeta here - let App.tsx do it after updating metadata
5766
// This ensures metadata is updated before LeftSidebar re-renders
5867

68+
const validStatuses: PreviewSetupStatus[] = ['ok', 'unsupported', 'needs-patch'];
69+
if (data.previewSetup && data.previewSetup !== 'ok' && validStatuses.includes(data.previewSetup)) {
70+
setPreviewSetup(data.previewSetup as PreviewSetupStatus);
71+
setNeedsPatchPrompt(typeof data.needsPatchPrompt === 'string' ? data.needsPatchPrompt : null);
72+
} else {
73+
setPreviewSetup(null);
74+
setNeedsPatchPrompt(null);
75+
}
76+
5977
// Emit event для перезагрузки canvas
6078
window.dispatchEvent(new CustomEvent('component-loaded', { detail: data }));
6179
} else if (data.error) {
@@ -78,6 +96,9 @@ export function ComponentMetaProvider({ children }: { children: ReactNode }) {
7896
loadingComponent,
7997
parseError,
8098
setParseError,
99+
previewSetup,
100+
setPreviewSetup,
101+
needsPatchPrompt,
81102
currentSampleName,
82103
setCurrentSampleName,
83104
}}

‎client/lib/platform/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export type PlatformMessage =
195195
// Preview lifecycle (preview webview → extension host)
196196
| { type: 'previewLoaded' }
197197
| { type: 'previewError'; error: string }
198+
| { type: 'preview:setScope'; scope: 'full-app' | 'component-only' }
198199

199200
// Diagnostics (cross-webview sync in ext, local in SaaS)
200201
| { type: 'diagnostic:log'; entries: DiagnosticLogEntry[] }

‎client/main.tsx‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import { StrictMode } from 'react';
22
import { createRoot } from 'react-dom/client';
3-
import CanvasPreview from './__canvas_preview__';
43
import App from './App';
54
import './index.css';
65

7-
const PreviewGuard = () => {
8-
if (process.env.NODE_ENV !== 'development') {
9-
return <div style={{ padding: '20px' }}>Preview not available in production</div>;
10-
}
11-
return <CanvasPreview />;
12-
};
13-
14-
// Use conditional render instead of nested Routes to avoid BrowserRouter conflict
15-
// App.tsx already has its own BrowserRouter, so we render App directly
16-
const isPreviewPath = window.location.pathname.match(/^\/project-preview\/[^/]+\/test-preview$/);
17-
186
const element = document.getElementById('root');
197
if (element) {
20-
createRoot(element).render(<StrictMode>{isPreviewPath ? <PreviewGuard /> : <App />}</StrictMode>);
8+
createRoot(element).render(
9+
<StrictMode>
10+
<App />
11+
</StrictMode>,
12+
);
2113
}

‎client/pages/Editor/CanvasEditor.tsx‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import { IframeFailed } from './components/IframeFailed';
8383
import { LogsPanel } from './components/LogsPanel';
8484
import { NoComponentsOverlay } from './components/NoComponentsOverlay';
8585
import { PendingCommentInputOverlay } from './components/PendingCommentInputOverlay';
86+
import { PreviewSetupOverlay } from './components/PreviewSetupOverlay';
8687
import { ProjectStartOverlay } from './components/ProjectStartOverlay';
8788
import { SizeSelectionDialog } from './components/SizeSelectionDialog';
8889

@@ -227,7 +228,16 @@ export function CanvasEditor({ onOpenSettings }: Props) {
227228
const engine = useCanvasEngine();
228229
const store = useCanvasStore();
229230
const storeUpdateCounter = useStore(store, (state) => state._updateCounter);
230-
const { meta, loadComponent, parseError, currentSampleName, setCurrentSampleName } = useComponentMeta();
231+
const {
232+
meta,
233+
loadComponent,
234+
parseError,
235+
previewSetup,
236+
setPreviewSetup,
237+
needsPatchPrompt,
238+
currentSampleName,
239+
setCurrentSampleName,
240+
} = useComponentMeta();
231241

232242
// Reparse with new sampleName when active design instance changes
233243
useEffect(() => {
@@ -1125,6 +1135,12 @@ export function CanvasEditor({ onOpenSettings }: Props) {
11251135

11261136
{/* LogsPanel moved outside scroll container — see below */}
11271137
</>
1138+
) : previewSetup && previewSetup !== 'ok' ? (
1139+
<PreviewSetupOverlay
1140+
status={previewSetup}
1141+
needsPatchPrompt={needsPatchPrompt}
1142+
onDismiss={() => setPreviewSetup(null)}
1143+
/>
11281144
) : parseError ? (
11291145
<div
11301146
data-uniq-id="d548dedd-3433-477e-80b4-f89cf0e5c4c8"

0 commit comments

Comments
 (0)