| 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 | |
| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 5 | import { DemoModule, DemoRunnerOptions, DemoNavigationEvent } from "./types"; |
| Sean McCullough | 618bfb2 | 2025-06-25 20:52:30 +0000 | [diff] [blame] | 6 | |
| 7 | export 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 McCullough | 4337aa7 | 2025-06-27 23:41:33 +0000 | [diff] [blame] | 92 | "sketch-app-shell", |
| banksean | 659b983 | 2025-06-27 00:50:41 +0000 | [diff] [blame] | 93 | "sketch-call-status", |
| Sean McCullough | 618bfb2 | 2025-06-25 20:52:30 +0000 | [diff] [blame] | 94 | "sketch-chat-input", |
| 95 | "sketch-container-status", |
| banksean | 2be768e | 2025-07-18 16:41:39 +0000 | [diff] [blame] | 96 | "sketch-diff-range-picker", |
| 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", |
| banksean | ae3724e | 2025-07-18 16:52:37 +0000 | [diff] [blame] | 102 | "sketch-theme-toggle", |
| banksean | d52d39d | 2025-07-20 14:57:38 -0700 | [diff] [blame] | 103 | "mobile-chat", |
| 104 | "sketch-diff2-view", |
| 105 | "sketch-monaco-view", |
| banksean | d52d39d | 2025-07-20 14:57:38 -0700 | [diff] [blame] | 106 | "sketch-timeline-viewport", |
| 107 | "sketch-tool-card", |
| 108 | "status-indicators", |
| Sean McCullough | 618bfb2 | 2025-06-25 20:52:30 +0000 | [diff] [blame] | 109 | ]; |
| 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 = ` |
| banksean | 3d1308e | 2025-07-29 17:20:10 +0000 | [diff] [blame] | 198 | <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 McCullough | 618bfb2 | 2025-06-25 20:52:30 +0000 | [diff] [blame] | 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 | } |