/** * ds-project-analysis.js * Project analysis results viewer showing token usage, component adoption, etc. * UI Team Tool #4 */ import { ComponentHelpers } from '../../utils/component-helpers.js'; import contextStore from '../../stores/context-store.js'; import toolBridge from '../../services/tool-bridge.js'; class DSProjectAnalysis extends HTMLElement { constructor() { super(); this.analysisResults = null; this.isAnalyzing = false; } async connectedCallback() { this.render(); this.setupEventListeners(); await this.loadCachedResults(); } async loadCachedResults() { try { const context = contextStore.getMCPContext(); if (!context.project_id) return; const cached = localStorage.getItem(`analysis_${context.project_id}`); if (cached) { this.analysisResults = JSON.parse(cached); this.renderResults(); } } catch (error) { console.error('[DSProjectAnalysis] Failed to load cached results:', error); } } setupEventListeners() { const analyzeBtn = this.querySelector('#analyze-project-btn'); const pathInput = this.querySelector('#project-path-input'); if (analyzeBtn) { analyzeBtn.addEventListener('click', () => this.analyzeProject()); } if (pathInput) { pathInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') { this.analyzeProject(); } }); } } async analyzeProject() { const pathInput = this.querySelector('#project-path-input'); const projectPath = pathInput?.value.trim() || ''; if (!projectPath) { ComponentHelpers.showToast?.('Please enter a project path', 'error'); return; } this.isAnalyzing = true; this.updateLoadingState(); try { // Call dss_analyze_project MCP tool const result = await toolBridge.executeTool('dss_analyze_project', { path: projectPath }); this.analysisResults = result; // Cache results const context = contextStore.getMCPContext(); if (context.project_id) { localStorage.setItem(`analysis_${context.project_id}`, JSON.stringify(result)); } this.renderResults(); ComponentHelpers.showToast?.('Project analysis complete', 'success'); } catch (error) { console.error('[DSProjectAnalysis] Analysis failed:', error); ComponentHelpers.showToast?.(`Analysis failed: ${error.message}`, 'error'); const resultsContainer = this.querySelector('#results-container'); if (resultsContainer) { resultsContainer.innerHTML = ComponentHelpers.renderError('Project analysis failed', error); } } finally { this.isAnalyzing = false; this.updateLoadingState(); } } updateLoadingState() { const analyzeBtn = this.querySelector('#analyze-project-btn'); const resultsContainer = this.querySelector('#results-container'); if (!analyzeBtn || !resultsContainer) return; if (this.isAnalyzing) { analyzeBtn.disabled = true; analyzeBtn.textContent = '⏳ Analyzing...'; resultsContainer.innerHTML = ComponentHelpers.renderLoading('Analyzing project structure and token usage...'); } else { analyzeBtn.disabled = false; analyzeBtn.textContent = '🔍 Analyze Project'; } } renderResults() { const resultsContainer = this.querySelector('#results-container'); if (!resultsContainer || !this.analysisResults) return; const { patterns, components, tokens, dependencies } = this.analysisResults; resultsContainer.innerHTML = `
| Component | Path | DS Adoption |
|---|---|---|
| ${ComponentHelpers.escapeHtml(comp.name)} | ${ComponentHelpers.escapeHtml(comp.path)} | ${this.renderAdoptionBadge(comp.dsAdoption || 0)} |
Enter your project path above to analyze component usage and design system adoption