|
| 1 | +import { el } from './dom' |
| 2 | +import type { ServiceInfo } from './services' |
| 3 | + |
| 4 | +function renderListSection(label: string, items: readonly string[]): HTMLElement { |
| 5 | + const section = el('div', { className: 'card-section' }) |
| 6 | + const labelEl = el('div', { className: 'card-label' }) |
| 7 | + labelEl.textContent = label |
| 8 | + section.appendChild(labelEl) |
| 9 | + |
| 10 | + const valueEl = el('div', { className: 'card-value' }) |
| 11 | + valueEl.textContent = items.join('\n') |
| 12 | + section.appendChild(valueEl) |
| 13 | + |
| 14 | + return section |
| 15 | +} |
| 16 | + |
| 17 | +function renderMapSection(label: string, entries: ReadonlyMap<string, string>): HTMLElement { |
| 18 | + const section = el('div', { className: 'card-section' }) |
| 19 | + const labelEl = el('div', { className: 'card-label' }) |
| 20 | + labelEl.textContent = label |
| 21 | + section.appendChild(labelEl) |
| 22 | + |
| 23 | + const valueEl = el('div', { className: 'card-value' }) |
| 24 | + const lines = Array.from(entries).map(([k, v]) => `${k}=${v}`) |
| 25 | + valueEl.textContent = lines.join('\n') |
| 26 | + section.appendChild(valueEl) |
| 27 | + |
| 28 | + return section |
| 29 | +} |
| 30 | + |
| 31 | +function renderCard(service: ServiceInfo): HTMLElement { |
| 32 | + const card = el('div', { className: 'card' }) |
| 33 | + |
| 34 | + const header = el('div', { className: 'card-header' }) |
| 35 | + header.textContent = service.name |
| 36 | + card.appendChild(header) |
| 37 | + |
| 38 | + // Image (always shown if present) |
| 39 | + if (service.image) { |
| 40 | + const section = el('div', { className: 'card-section' }) |
| 41 | + const label = el('div', { className: 'card-label' }) |
| 42 | + label.textContent = 'Image' |
| 43 | + section.appendChild(label) |
| 44 | + const value = el('div', { className: 'card-value' }) |
| 45 | + value.textContent = service.image |
| 46 | + section.appendChild(value) |
| 47 | + card.appendChild(section) |
| 48 | + } |
| 49 | + |
| 50 | + if (service.ports.length > 0) { |
| 51 | + card.appendChild(renderListSection('Ports', service.ports)) |
| 52 | + } |
| 53 | + |
| 54 | + if (service.volumes.length > 0) { |
| 55 | + card.appendChild(renderListSection('Volumes', service.volumes)) |
| 56 | + } |
| 57 | + |
| 58 | + if (service.networks.length > 0) { |
| 59 | + card.appendChild(renderListSection('Networks', service.networks)) |
| 60 | + } |
| 61 | + |
| 62 | + if (service.environment.size > 0) { |
| 63 | + card.appendChild(renderMapSection('Environment', service.environment)) |
| 64 | + } |
| 65 | + |
| 66 | + // Extras rendered as individual labeled fields |
| 67 | + for (const [key, value] of service.extras) { |
| 68 | + const section = el('div', { className: 'card-section' }) |
| 69 | + const label = el('div', { className: 'card-label' }) |
| 70 | + label.textContent = key |
| 71 | + section.appendChild(label) |
| 72 | + const valueEl = el('div', { className: 'card-value' }) |
| 73 | + valueEl.textContent = value |
| 74 | + section.appendChild(valueEl) |
| 75 | + card.appendChild(section) |
| 76 | + } |
| 77 | + |
| 78 | + return card |
| 79 | +} |
| 80 | + |
| 81 | +export function renderCards(services: readonly ServiceInfo[]): HTMLElement { |
| 82 | + const container = el('div', { className: 'cards-container' }) |
| 83 | + for (const service of services) { |
| 84 | + container.appendChild(renderCard(service)) |
| 85 | + } |
| 86 | + return container |
| 87 | +} |
0 commit comments