blob: 680651b57b050ae37657f4ba9b9c8b75cbac39d2 [file] [log] [blame]
Sean McCullough618bfb22025-06-25 20:52:30 +00001/**
2 * Demo runner that dynamically loads and executes demo modules
3 */
4
philip.zeyliger26bc6592025-06-30 20:15:30 -07005import { DemoModule, DemoRunnerOptions, DemoNavigationEvent } from "./types";
Sean McCullough618bfb22025-06-25 20:52:30 +00006
7export class DemoRunner {
8 private container: HTMLElement;
9 private basePath: string;
10 private currentDemo: DemoModule | null = null;
11 private currentComponentName: string | null = null;
12 private onDemoChange?: (componentName: string, demo: DemoModule) => void;
13
14 constructor(options: DemoRunnerOptions) {
15 this.container = options.container;
16 this.basePath = options.basePath || "../";
17 this.onDemoChange = options.onDemoChange;
18 }
19
20 /**
21 * Load and display a demo for the specified component
22 */
23 async loadDemo(componentName: string): Promise<void> {
24 try {
25 // Cleanup current demo if any
26 await this.cleanup();
27
28 // Dynamically import the demo module
29 const demoModule = await import(
30 /* @vite-ignore */ `../${componentName}.demo.ts`
31 );
32 const demo: DemoModule = demoModule.default;
33
34 if (!demo) {
35 throw new Error(
36 `Demo module for ${componentName} does not export a default DemoModule`,
37 );
38 }
39
40 // Clear container
41 this.container.innerHTML = "";
42
43 // Load additional styles if specified
44 if (demo.styles) {
45 for (const styleUrl of demo.styles) {
46 await this.loadStylesheet(styleUrl);
47 }
48 }
49
50 // Add custom styles if specified
51 if (demo.customStyles) {
52 this.addCustomStyles(demo.customStyles, componentName);
53 }
54
55 // Import required component modules
56 if (demo.imports) {
57 for (const importPath of demo.imports) {
58 await import(/* @vite-ignore */ this.basePath + importPath);
59 }
60 }
61
62 // Set up the demo
63 await demo.setup(this.container);
64
65 // Update current state
66 this.currentDemo = demo;
67 this.currentComponentName = componentName;
68
69 // Notify listeners
70 if (this.onDemoChange) {
71 this.onDemoChange(componentName, demo);
72 }
73
74 // Dispatch navigation event
75 const event: DemoNavigationEvent = new CustomEvent("demo-navigation", {
76 detail: { componentName, demo },
77 });
78 document.dispatchEvent(event);
79 } catch (error) {
80 console.error(`Failed to load demo for ${componentName}:`, error);
81 this.showError(`Failed to load demo for ${componentName}`, error);
82 }
83 }
84
85 /**
86 * Get list of available demo components by scanning for .demo.ts files
87 */
88 async getAvailableComponents(): Promise<string[]> {
89 // For now, we'll maintain a registry of known demo components
90 // This could be improved with build-time generation
91 const knownComponents = [
Sean McCullough4337aa72025-06-27 23:41:33 +000092 "sketch-app-shell",
banksean659b9832025-06-27 00:50:41 +000093 "sketch-call-status",
Sean McCullough618bfb22025-06-25 20:52:30 +000094 "sketch-chat-input",
95 "sketch-container-status",
banksean2be768e2025-07-18 16:41:39 +000096 "sketch-diff-range-picker",
bankseane59a2e12025-06-28 01:38:19 +000097 "sketch-timeline",
bankseanc5147482025-06-29 00:41:58 +000098 "sketch-timeline-message",
bankseancdb08a52025-07-02 20:28:29 +000099 "sketch-todo-panel",
Sean McCullough618bfb22025-06-25 20:52:30 +0000100 "sketch-tool-calls",
bankseand5c849d2025-06-26 15:48:31 +0000101 "sketch-view-mode-select",
bankseanae3724e2025-07-18 16:52:37 +0000102 "sketch-theme-toggle",
bankseand52d39d2025-07-20 14:57:38 -0700103 "mobile-chat",
104 "sketch-diff2-view",
105 "sketch-monaco-view",
bankseand52d39d2025-07-20 14:57:38 -0700106 "sketch-timeline-viewport",
107 "sketch-tool-card",
108 "status-indicators",
Sean McCullough618bfb22025-06-25 20:52:30 +0000109 ];
110
111 // Filter to only components that actually have demo files
112 const availableComponents: string[] = [];
113 for (const component of knownComponents) {
114 try {
115 // Test if the demo module exists by attempting to import it
116 const demoModule = await import(
117 /* @vite-ignore */ `../${component}.demo.ts`
118 );
119 if (demoModule.default) {
120 availableComponents.push(component);
121 }
122 } catch (error) {
123 console.warn(`Demo not available for ${component}:`, error);
124 // Component demo doesn't exist, skip it
125 }
126 }
127
128 return availableComponents;
129 }
130
131 /**
132 * Cleanup current demo
133 */
134 private async cleanup(): Promise<void> {
135 if (this.currentDemo?.cleanup) {
136 await this.currentDemo.cleanup();
137 }
138
139 // Remove custom styles
140 if (this.currentComponentName) {
141 this.removeCustomStyles(this.currentComponentName);
142 }
143
144 this.currentDemo = null;
145 this.currentComponentName = null;
146 }
147
148 /**
149 * Load a CSS stylesheet dynamically
150 */
151 private async loadStylesheet(url: string): Promise<void> {
152 return new Promise((resolve, reject) => {
153 const link = document.createElement("link");
154 link.rel = "stylesheet";
155 link.href = url;
156 link.onload = () => resolve();
157 link.onerror = () =>
158 reject(new Error(`Failed to load stylesheet: ${url}`));
159 document.head.appendChild(link);
160 });
161 }
162
163 /**
164 * Add custom CSS styles for a demo
165 */
166 private addCustomStyles(css: string, componentName: string): void {
167 const styleId = `demo-custom-styles-${componentName}`;
168
169 // Remove existing styles for this component
170 const existing = document.getElementById(styleId);
171 if (existing) {
172 existing.remove();
173 }
174
175 // Add new styles
176 const style = document.createElement("style");
177 style.id = styleId;
178 style.textContent = css;
179 document.head.appendChild(style);
180 }
181
182 /**
183 * Remove custom styles for a component
184 */
185 private removeCustomStyles(componentName: string): void {
186 const styleId = `demo-custom-styles-${componentName}`;
187 const existing = document.getElementById(styleId);
188 if (existing) {
189 existing.remove();
190 }
191 }
192
193 /**
194 * Show error message in the demo container
195 */
196 private showError(message: string, error: any): void {
197 this.container.innerHTML = `
banksean3d1308e2025-07-29 17:20:10 +0000198 <div class="p-5 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded text-red-800 dark:text-red-200 font-mono">
199 <h3 class="text-lg font-semibold mb-2">Demo Error</h3>
200 <p class="mb-4"><strong>${message}</strong></p>
201 <details class="text-sm">
202 <summary class="cursor-pointer hover:text-red-600 dark:hover:text-red-300">Error Details</summary>
203 <pre class="mt-2 p-2 bg-red-100 dark:bg-red-800/30 rounded text-xs overflow-auto">${error.stack || error.message || error}</pre>
Sean McCullough618bfb22025-06-25 20:52:30 +0000204 </details>
205 </div>
206 `;
207 }
208
209 /**
210 * Get current demo info
211 */
212 getCurrentDemo(): { componentName: string; demo: DemoModule } | null {
213 if (this.currentComponentName && this.currentDemo) {
214 return {
215 componentName: this.currentComponentName,
216 demo: this.currentDemo,
217 };
218 }
219 return null;
220 }
221}