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