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