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