import * as plugins from '../plugins.js'; import { apiService, type ICollectionStats } from '../services/index.js'; import { formatSize, formatCount } from '../utilities/index.js'; import { themeStyles } from '../styles/index.js'; const { html, css, cssManager, customElement, property, state, DeesElement } = plugins; type TViewTab = 'documents' | 'indexes' | 'aggregation'; @customElement('tsview-mongo-browser') export class TsviewMongoBrowser extends DeesElement { @property({ type: String }) public accessor databaseName: string = ''; @property({ type: String }) public accessor collectionName: string = ''; @state() private accessor activeTab: TViewTab = 'documents'; @state() private accessor selectedDocumentId: string = ''; @state() private accessor stats: ICollectionStats | null = null; public static styles = [ cssManager.defaultStyles, themeStyles, css` :host { display: block; height: 100%; } .browser-container { display: flex; flex-direction: column; height: 100%; } .header { display: flex; align-items: center; justify-content: space-between; padding: 12px; background: rgba(0, 0, 0, 0.2); border-radius: 8px; margin-bottom: 16px; } .collection-info { display: flex; align-items: center; gap: 16px; } .collection-title { font-size: 16px; font-weight: 500; } .collection-stats { display: flex; gap: 16px; font-size: 13px; color: #888; } .stat-item { display: flex; align-items: center; gap: 4px; } .tabs { display: flex; gap: 4px; } .tab { padding: 8px 16px; background: transparent; border: none; color: #888; cursor: pointer; font-size: 14px; border-radius: 6px; transition: all 0.15s; } .tab:hover { background: rgba(255, 255, 255, 0.05); color: #aaa; } .tab.active { background: rgba(255, 255, 255, 0.1); color: #e0e0e0; } .content { flex: 1; display: grid; grid-template-columns: 1fr 400px; gap: 16px; overflow: hidden; } .main-panel { overflow: auto; background: rgba(0, 0, 0, 0.2); border-radius: 8px; } .detail-panel { background: rgba(0, 0, 0, 0.2); border-radius: 8px; overflow: hidden; } @media (max-width: 1200px) { .content { grid-template-columns: 1fr; } .detail-panel { display: none; } } `, ]; async connectedCallback() { super.connectedCallback(); await this.loadStats(); } updated(changedProperties: Map) { if (changedProperties.has('databaseName') || changedProperties.has('collectionName')) { this.loadStats(); this.selectedDocumentId = ''; } } private async loadStats() { if (!this.databaseName || !this.collectionName) return; try { this.stats = await apiService.getCollectionStats(this.databaseName, this.collectionName); } catch (err) { console.error('Error loading stats:', err); this.stats = null; } } private setActiveTab(tab: TViewTab) { this.activeTab = tab; } private handleDocumentSelected(e: CustomEvent) { this.selectedDocumentId = e.detail.documentId; } render() { return html`
${this.collectionName} ${this.stats ? html`
${formatCount(this.stats.count)} docs ${formatSize(this.stats.size)} ${this.stats.indexCount} indexes
` : ''}
${this.activeTab === 'documents' ? html` ` : this.activeTab === 'indexes' ? html` ` : html`
Aggregation pipeline builder coming soon
`}
`; } }