/** * ds-storybook-figma-compare.js * Side-by-side Storybook and Figma component comparison * UI Team Tool #1 */ import { createComparisonView, setupComparisonHandlers } from '../../utils/tool-templates.js'; import { ComponentHelpers } from '../../utils/component-helpers.js'; import contextStore from '../../stores/context-store.js'; import apiClient from '../../services/api-client.js'; class DSStorybookFigmaCompare extends HTMLElement { constructor() { super(); this.storybookUrl = ''; this.figmaUrl = ''; this.selectedComponent = null; } async connectedCallback() { await this.loadProjectConfig(); this.render(); this.setupEventListeners(); } async loadProjectConfig() { try { const context = contextStore.getMCPContext(); if (!context.project_id) { throw new Error('No project selected'); } // Fetch project configuration to get Storybook URL and Figma file const project = await apiClient.getProject(context.project_id); this.storybookUrl = project.storybook_url || ''; this.figmaUrl = project.figma_ui_file || ''; } catch (error) { console.error('[DSStorybookFigmaCompare] Failed to load project config:', error); } } setupEventListeners() { const storybookInput = this.querySelector('#storybook-url-input'); const figmaInput = this.querySelector('#figma-url-input'); const loadBtn = this.querySelector('#load-comparison-btn'); if (storybookInput) { storybookInput.value = this.storybookUrl; } if (figmaInput) { figmaInput.value = this.figmaUrl; } if (loadBtn) { loadBtn.addEventListener('click', () => this.loadComparison()); } // Setup comparison handlers (sync scroll, zoom, etc.) const comparisonContainer = this.querySelector('#comparison-container'); if (comparisonContainer) { setupComparisonHandlers(comparisonContainer, {}); } } loadComparison() { const storybookInput = this.querySelector('#storybook-url-input'); const figmaInput = this.querySelector('#figma-url-input'); this.storybookUrl = storybookInput?.value || ''; this.figmaUrl = figmaInput?.value || ''; if (!this.storybookUrl || !this.figmaUrl) { ComponentHelpers.showToast?.('Please enter both Storybook and Figma URLs', 'error'); return; } // Validate URLs try { new URL(this.storybookUrl); new URL(this.figmaUrl); } catch (error) { ComponentHelpers.showToast?.('Invalid URL format', 'error'); return; } // Update comparison view const comparisonContainer = this.querySelector('#comparison-container'); if (comparisonContainer) { comparisonContainer.innerHTML = createComparisonView({ leftTitle: 'Storybook', rightTitle: 'Figma', leftSrc: this.storybookUrl, rightSrc: this.figmaUrl }); // Re-setup handlers after re-render setupComparisonHandlers(comparisonContainer, {}); ComponentHelpers.showToast?.('Comparison loaded', 'success'); } } render() { this.innerHTML = `

Component Comparison Configuration

💡 Tip: Navigate to the same component in both Storybook and Figma for accurate comparison
${this.storybookUrl && this.figmaUrl ? createComparisonView({ leftTitle: 'Storybook', rightTitle: 'Figma', leftSrc: this.storybookUrl, rightSrc: this.figmaUrl }) : `
🔍

No Comparison Loaded

Enter Storybook and Figma URLs above to start comparing components

`}
`; } } customElements.define('ds-storybook-figma-compare', DSStorybookFigmaCompare); export default DSStorybookFigmaCompare;