blob: 396da39036f75e1390ed745c96538efdcc052926 [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",
Sean McCullough4337aa72025-06-27 23:41:33 +000098 "sketch-app-shell",
banksean659b9832025-06-27 00:50:41 +000099 "sketch-call-status",
Sean McCullough618bfb22025-06-25 20:52:30 +0000100 "sketch-chat-input",
101 "sketch-container-status",
102 "sketch-tool-calls",
bankseand5c849d2025-06-26 15:48:31 +0000103 "sketch-view-mode-select",
Sean McCullough618bfb22025-06-25 20:52:30 +0000104 ];
105
106 // Filter to only components that actually have demo files
107 const availableComponents: string[] = [];
108 for (const component of knownComponents) {
109 try {
110 // Test if the demo module exists by attempting to import it
111 const demoModule = await import(
112 /* @vite-ignore */ `../${component}.demo.ts`
113 );
114 if (demoModule.default) {
115 availableComponents.push(component);
116 }
117 } catch (error) {
118 console.warn(`Demo not available for ${component}:`, error);
119 // Component demo doesn't exist, skip it
120 }
121 }
122
123 return availableComponents;
124 }
125
126 /**
127 * Cleanup current demo
128 */
129 private async cleanup(): Promise<void> {
130 if (this.currentDemo?.cleanup) {
131 await this.currentDemo.cleanup();
132 }
133
134 // Remove custom styles
135 if (this.currentComponentName) {
136 this.removeCustomStyles(this.currentComponentName);
137 }
138
139 this.currentDemo = null;
140 this.currentComponentName = null;
141 }
142
143 /**
144 * Load a CSS stylesheet dynamically
145 */
146 private async loadStylesheet(url: string): Promise<void> {
147 return new Promise((resolve, reject) => {
148 const link = document.createElement("link");
149 link.rel = "stylesheet";
150 link.href = url;
151 link.onload = () => resolve();
152 link.onerror = () =>
153 reject(new Error(`Failed to load stylesheet: ${url}`));
154 document.head.appendChild(link);
155 });
156 }
157
158 /**
159 * Add custom CSS styles for a demo
160 */
161 private addCustomStyles(css: string, componentName: string): void {
162 const styleId = `demo-custom-styles-${componentName}`;
163
164 // Remove existing styles for this component
165 const existing = document.getElementById(styleId);
166 if (existing) {
167 existing.remove();
168 }
169
170 // Add new styles
171 const style = document.createElement("style");
172 style.id = styleId;
173 style.textContent = css;
174 document.head.appendChild(style);
175 }
176
177 /**
178 * Remove custom styles for a component
179 */
180 private removeCustomStyles(componentName: string): void {
181 const styleId = `demo-custom-styles-${componentName}`;
182 const existing = document.getElementById(styleId);
183 if (existing) {
184 existing.remove();
185 }
186 }
187
188 /**
189 * Show error message in the demo container
190 */
191 private showError(message: string, error: any): void {
192 this.container.innerHTML = `
193 <div style="
194 padding: 20px;
195 background: #fee;
196 border: 1px solid #fcc;
197 border-radius: 4px;
198 color: #800;
199 font-family: monospace;
200 ">
201 <h3>Demo Error</h3>
202 <p><strong>${message}</strong></p>
203 <details>
204 <summary>Error Details</summary>
205 <pre>${error.stack || error.message || error}</pre>
206 </details>
207 </div>
208 `;
209 }
210
211 /**
212 * Get current demo info
213 */
214 getCurrentDemo(): { componentName: string; demo: DemoModule } | null {
215 if (this.currentComponentName && this.currentDemo) {
216 return {
217 componentName: this.currentComponentName,
218 demo: this.currentDemo,
219 };
220 }
221 return null;
222 }
223}