SPB Git

Repository Public

NodeViews.swift 4.9 KB 119 lines swift

1import MCCCore2import SwiftUI34// Small views shared by the group-A screens (nodes, services, models, jobs, network, storage).56/// "Label value" chip; a missing value is spelled out ("non rapporté"), never drawn as 0.7struct HeadlineChip: View {8    @Environment(\.mccTheme) private var theme9    let label: String10    let value: String?11    var body: some View {12        HStack(spacing: Tokens.Space.s2) {13            Text(label).foregroundStyle(theme.textSecondary)14            Text(value ?? tr("metric.not_reported")).monospacedDigit().foregroundStyle(value == nil ? theme.textTertiary : theme.textPrimary)15        }16        .font(.footnote)17        .accessibilityElement(children: .ignore)18        .accessibilityLabel(label)19        .accessibilityValue(value ?? tr("metric.not_reported"))20    }21}2223/// Horizontal when it fits, vertical at accessibility text sizes: nothing is truncated.24struct AdaptiveLine<Content: View>: View {25    @Environment(\.dynamicTypeSize) private var size26    var spacing: CGFloat = Tokens.Space.s427    @ViewBuilder var content: Content28    var body: some View {29        if size.isAccessibilitySize {30            VStack(alignment: .leading, spacing: Tokens.Space.s2) { content }31        } else {32            ViewThatFits(in: .horizontal) {33                HStack(alignment: .firstTextBaseline, spacing: spacing) { content }34                VStack(alignment: .leading, spacing: Tokens.Space.s2) { content }35            }36        }37    }38}3940/// Maintenance badge with its text (state is never colour alone).41struct MaintenanceBadge: View {42    var body: some View { StatusBadge(tr("nodes.maintenance"), tone: .maintenance, compact: true) }43}4445/// Inline state of a keyed resource shown inside a card: loading, unavailable, error — or the content.46struct InlineResource<Value: Codable & Sendable, Content: View>: View {47    @Environment(\.mccTheme) private var theme48    let resource: Resource<Value>49    @ViewBuilder var content: (Value) -> Content50    var body: some View {51        VStack(alignment: .leading, spacing: Tokens.Space.s3) {52            if let v = resource.value {53                content(v)54                DataStamp(receivedAt: resource.receivedAt, stale: resource.isShowingStaleData)55            } else if let reason = resource.state.unavailableReason {56                UnavailableStateView(reason: reason, error: resource.state.error) { Task { await resource.load() } }57            } else if let e = resource.state.error {58                InlineErrorView(e)59            } else {60                ProgressView(tr("state.loading")).frame(maxWidth: .infinity, minHeight: Tokens.Size.touchMin)61            }62        }63    }64}6566/// Toolbar menu entry that shows a checkmark when selected.67struct CheckItem: View {68    let title: String69    let selected: Bool70    let action: () -> Void71    var body: some View {72        Button(action: action) {73            if selected { Label(title, systemImage: "checkmark") } else { Text(title) }74        }75    }76}7778/// Row of a node in the list.79struct NodeRowContent: View {80    @Environment(\.mccTheme) private var theme81    @Environment(\.dynamicTypeSize) private var size82    @Session private var session83    let node: API.Node8485    private var sparkQuery: MetricQuery { MetricQuery(nodeID: node.id, metrics: ["cpu.usage"], range: ._1h) }8687    var body: some View {88        VStack(alignment: .leading, spacing: Tokens.Space.s2) {89            AdaptiveLine(spacing: Tokens.Space.s3) {90                Label(node.alias, systemImage: node.kind.symbol).font(.headline).foregroundStyle(theme.textPrimary)91                if node.maintenance { MaintenanceBadge() }92                if let n = node.openAlerts, n > 0 {93                    StatusBadge(tr("nodes.open_alerts", n), tone: .warning, compact: true)94                }95            }96            FreshnessLine(node.freshness, observedAt: node.observedAt ?? node.lastSeenAt).font(.footnote)97            AdaptiveLine {98                HeadlineChip(label: tr("nodes.cpu"), value: NodesLogic.percentText(node.headline?.cpuUsagePct))99                HeadlineChip(label: tr("nodes.memory"), value: NodesLogic.percentText(node.headline?.memUsedPct))100                HeadlineChip(label: tr("nodes.load"), value: NodesLogic.loadText(node.headline?.load1))101            }102            // Trend of the CPU over the last hour. Offline nodes have no recent samples: nothing is requested.103            if node.freshness != .offline {104                let r = session.nodes.metrics[sparkQuery]105                Group {106                    if let s = r.value?.series.first, !s.points.isEmpty {107                        Sparkline(ChartSeries(id: node.id, name: node.alias, metricPoints: s.points), tone: nil)108                    } else {109                        Color.clear110                    }111                }112                .frame(height: 24)113                .frame(maxWidth: size.isAccessibilitySize ? .infinity : 220)114                .loads(r, maxAge: 120)115            }116        }117    }118}119
Files main
Menu

Theme