Repository Public
1import MCCCore2import SwiftUI34enum StorageLogic {5 /// Fullest first; volumes without usage figures last (unknown is not empty).6 static func sorted(_ volumes: [API.Volume]) -> [API.Volume] {7 volumes.sorted {8 let a = NodesLogic.usageRatio(used: $0.usedBytes, total: $0.totalBytes), b = NodesLogic.usageRatio(used: $1.usedBytes, total: $1.totalBytes)9 switch (a, b) {10 case let (l?, r?): return l == r ? $0.id < $1.id : l > r11 case (_?, nil): return true12 case (nil, _?): return false13 case (nil, nil): return ($0.nodeAlias, $0.mount) < ($1.nodeAlias, $1.mount)14 }15 }16 }1718 static func usageDetail(used: Int64?, total: Int64?) -> String? {19 switch (used, total) {20 case let (u?, t?): tr("overview.capacity.used_of", Format.bytes(u), Format.bytes(t))21 case let (nil, t?): Format.bytes(t)22 default: nil23 }24 }2526 static func nasTone(_ s: API.NasTarget.StatePayload) -> StatusTone {27 switch s {28 case .online: .ok29 case .offline: .critical30 case .unknown: .neutral31 }32 }3334 static func backupTone(_ s: API.Backup.LastStatusPayload) -> StatusTone {35 switch s {36 case .ok: .ok37 case .failed: .critical38 case .unknown: .neutral39 }40 }41}4243/// One volume (node detail and storage summary).44struct VolumeRow: View {45 @Environment(\.mccTheme) private var theme46 let volume: API.Volume47 var showsNode = true48 var body: some View {49 let v = volume50 VStack(alignment: .leading, spacing: Tokens.Space.s2) {51 UsageMeter(label: (showsNode ? v.nodeAlias + " · " : "") + v.mount, ratio: NodesLogic.usageRatio(used: v.usedBytes, total: v.totalBytes),52 detail: StorageLogic.usageDetail(used: v.usedBytes, total: v.totalBytes))53 Text([v.kind.label, v.fs, v.device, v.remote].compactMap { $0 }.joined(separator: " · ")).font(.footnote).foregroundStyle(theme.textSecondary)54 AdaptiveLine {55 HeadlineChip(label: tr("storage.smart"), value: v.smartStatus)56 if v.goneAt != nil { StatusLabel(tr("storage.volume.gone"), tone: .neutral).font(.footnote) }57 }58 FreshnessLine(v.freshness, observedAt: v.observedAt ?? v.lastSeenAt).font(.footnote)59 }60 .frame(maxWidth: .infinity, alignment: .leading)61 .accessibilityElement(children: .combine)62 .accessibilityIdentifier("volume-row")63 }64}6566struct StorageScreen: View {67 @Environment(\.mccTheme) private var theme68 @Session private var session6970 var body: some View {71 ScreenStack {72 SectionCard(tr("storage.volumes")) {73 InlineResource(resource: session.infrastructure.volumes) { volumes in74 if volumes.isEmpty { Text(tr("storage.volumes.none")).font(.callout).foregroundStyle(theme.textSecondary) }75 ForEach(StorageLogic.sorted(volumes), id: \.id) { v in76 VolumeRow(volume: v)77 Divider().overlay(theme.borderSubtle)78 }79 }80 }81 SectionCard(tr("storage.nas")) {82 InlineResource(resource: session.infrastructure.nas) { targets in83 if targets.isEmpty { Text(tr("storage.nas.none")).font(.callout).foregroundStyle(theme.textSecondary) }84 ForEach(targets, id: \.id) { n in85 VStack(alignment: .leading, spacing: Tokens.Space.s2) {86 AdaptiveLine(spacing: Tokens.Space.s3) {87 Text(n.name).font(.callout.weight(.medium)).foregroundStyle(theme.textPrimary)88 StatusLabel(trDynamic("storage.nas.state." + n.state.rawValue), tone: StorageLogic.nasTone(n.state)).font(.footnote)89 if let at = n.stateAt { Text(Format.relative(at)).font(.footnote).foregroundStyle(theme.textTertiary) }90 }91 Text([n._protocol.rawValue.uppercased(), n.host, n.share.nilIfEmpty].compactMap { $0 }.joined(separator: " · "))92 .font(.footnote.monospaced()).foregroundStyle(theme.textSecondary)93 if !n.note.isEmpty { Text(n.note).font(.footnote).foregroundStyle(theme.textSecondary) }94 if n.totalBytes != nil {95 UsageMeter(label: tr("storage.usage"), ratio: NodesLogic.usageRatio(used: n.usedBytes, total: n.totalBytes),96 detail: StorageLogic.usageDetail(used: n.usedBytes, total: n.totalBytes))97 } else {98 HeadlineChip(label: tr("storage.usage"), value: nil)99 }100 AdaptiveLine {101 HeadlineChip(label: tr("network.latency"), value: n.latencyMs.map { Format.milliseconds($0) })102 HeadlineChip(label: tr("storage.nas.mounted_on"), value: tr("storage.nas.mounted_count", n.mountedOn))103 if let by = n.observedBy { HeadlineChip(label: tr("storage.nas.observed_by"), value: session.nodes.alias(by) ?? by) }104 }105 }106 .frame(maxWidth: .infinity, alignment: .leading)107 .accessibilityElement(children: .combine)108 .accessibilityIdentifier("nas-row-" + n.name)109 Divider().overlay(theme.borderSubtle)110 }111 }112 }113 SectionCard(tr("storage.backups")) {114 InlineResource(resource: session.infrastructure.backups) { backups in115 if backups.isEmpty { Text(tr("storage.backups.none")).font(.callout).foregroundStyle(theme.textSecondary) }116 ForEach(backups, id: \.id) { b in117 VStack(alignment: .leading, spacing: Tokens.Space.s1) {118 AdaptiveLine(spacing: Tokens.Space.s3) {119 Text(b.name).font(.callout.weight(.medium)).foregroundStyle(theme.textPrimary)120 StatusLabel(trDynamic("storage.backup.status." + b.lastStatus.rawValue), tone: StorageLogic.backupTone(b.lastStatus)).font(.footnote)121 }122 Text([b.nodeAlias, b.kind].compactMap { $0 }.joined(separator: " · ")).font(.footnote).foregroundStyle(theme.textSecondary)123 InfoRow(tr("storage.backup.last_run"), b.lastRunAt.map { Format.dateTime($0) } ?? tr("storage.backup.never"))124 InfoRow(tr("storage.backup.last_success"), b.lastSuccessAt.map { Format.dateTime($0) } ?? tr("storage.backup.never"))125 InfoRow(tr("storage.backup.size"), b.sizeBytes.map { Format.bytes($0) } ?? tr("metric.not_measured"))126 if !b.detail.isEmpty { Text(b.detail).font(.footnote).foregroundStyle(theme.textTertiary) }127 }128 .frame(maxWidth: .infinity, alignment: .leading)129 .accessibilityIdentifier("backup-row-" + b.name)130 Divider().overlay(theme.borderSubtle)131 }132 }133 }134 }135 .screen("storage", title: tr("section.storage"), load: { await load(force: false) }, reload: { await load(force: true) })136 }137138 private func load(force: Bool) async {139 let i = session.infrastructure140 async let a: Void = force ? i.volumes.load() : i.volumes.loadIfNeeded()141 async let b: Void = force ? i.nas.load() : i.nas.loadIfNeeded()142 async let c: Void = force ? i.backups.load() : i.backups.loadIfNeeded()143 _ = await (a, b, c)144 }145}146