SPB Git

Repository Public

NodesScreen.swift 6.2 KB 122 lines swift

1import MCCCore2import SwiftUI34/// Searchable, filterable list of the nodes. On iPad, a comparison mode selects two to four nodes.5struct NodesScreen: View {6    @Environment(Navigator.self) private var nav7    @Environment(\.mccTheme) private var theme8    @Session private var session9    @State private var search = ""10    @State private var filter = NodeFilter()11    @State private var sort: NodeSort = .alias12    @State private var comparing = false13    @State private var selection: [String] = []1415    var body: some View {16        ScreenStack {17            ResourceSection(resource: session.nodes.list) { nodes in18                let shown = NodesLogic.apply(nodes, search: search, filter: filter, sort: sort)19                VStack(alignment: .leading, spacing: Tokens.Space.s3) {20                    Text(tr("nodes.count", shown.count, nodes.count)).font(.footnote).foregroundStyle(theme.textSecondary)21                        .accessibilityIdentifier("nodes-count")22                    if comparing {23                        InfoBanner(tone: .info, title: tr("nodes.compare.hint_title"), message: tr("nodes.compare.hint", selection.count))24                    }25                    if shown.isEmpty {26                        EmptyStateView(tr(nodes.isEmpty ? "nodes.empty" : "nodes.no_match"), systemImage: "desktopcomputer")27                    }28                    ForEach(shown, id: \.id) { node in29                        if comparing { selectableRow(node) } else {30                            RouteRow(route: .node(node.id), identifier: "node-row-" + node.alias) { NodeRowContent(node: node) }31                        }32                    }33                }34            }35        }36        .screen("nodes", title: tr("section.nodes"), load: { await session.nodes.list.loadIfNeeded() }, reload: { await session.nodes.list.load() })37        .searchable(text: $search, prompt: tr("nodes.search_prompt"))38        .textInputAutocapitalization(.never)39        .autocorrectionDisabled()40        .toolbar { toolbar }41    }4243    private func selectableRow(_ node: API.Node) -> some View {44        let on = selection.contains(node.id)45        let full = !on && selection.count >= NodesLogic.compareRange.upperBound46        return Button { selection = NodesLogic.toggle(node.id, in: selection) } label: {47            HStack(alignment: .top, spacing: Tokens.Space.s3) {48                Image(systemName: on ? "checkmark.circle.fill" : "circle").font(.title3)49                    .foregroundStyle(on ? theme.accent : theme.textTertiary).accessibilityHidden(true)50                NodeRowContent(node: node).frame(maxWidth: .infinity, alignment: .leading)51            }52            .padding(Tokens.Space.s4)53            .frame(minHeight: Tokens.Size.touchMin)54            .background(on ? theme.accentMuted : theme.surface, in: RoundedRectangle(cornerRadius: Tokens.Radius.lg, style: .continuous))55            .overlay(RoundedRectangle(cornerRadius: Tokens.Radius.lg, style: .continuous).strokeBorder(on ? theme.accent : theme.borderSubtle, lineWidth: 1))56            .contentShape(RoundedRectangle(cornerRadius: Tokens.Radius.lg, style: .continuous))57        }58        .buttonStyle(.plain)59        .disabled(full)60        .hoverEffect(.highlight)61        .accessibilityAddTraits(on ? [.isSelected] : [])62        .accessibilityIdentifier("node-select-" + node.alias)63    }6465    @ToolbarContentBuilder private var toolbar: some ToolbarContent {66        if nav.layout == .pad {67            ToolbarItem(placement: .topBarLeading) {68                if comparing {69                    Button(tr("common.cancel")) { comparing = false; selection = [] }.accessibilityIdentifier("nodes-compare-cancel")70                } else {71                    Button { comparing = true } label: { Label(tr("nodes.compare"), systemImage: "rectangle.split.3x1") }72                        .accessibilityIdentifier("nodes-compare")73                }74            }75            if comparing {76                ToolbarItem(placement: .topBarTrailing) {77                    Button(tr("nodes.compare.open", selection.count)) {78                        let ids = selection79                        comparing = false; selection = []80                        nav.open(.compare(ids))81                    }82                    .disabled(!NodesLogic.canCompare(selection))83                    .accessibilityIdentifier("nodes-compare-open")84                }85            }86        }87        ToolbarItem(placement: .topBarTrailing) {88            Menu {89                Picker(tr("nodes.sort"), selection: $sort) {90                    ForEach(NodeSort.allCases) { Text($0.title).tag($0) }91                }92                Menu(tr("nodes.filter.freshness")) {93                    CheckItem(title: tr("common.all"), selected: filter.freshness == nil) { filter.freshness = nil }94                    ForEach(API.Freshness.allCases, id: \.self) { f in95                        CheckItem(title: f.label, selected: filter.freshness == f) { filter.freshness = f }96                    }97                }98                Menu(tr("nodes.filter.kind")) {99                    CheckItem(title: tr("common.all"), selected: filter.kind == nil) { filter.kind = nil }100                    ForEach(API.NodeKind.allCases, id: \.self) { k in101                        CheckItem(title: k.label, selected: filter.kind == k) { filter.kind = k }102                    }103                }104                Menu(tr("nodes.filter.site")) {105                    CheckItem(title: tr("common.all"), selected: filter.site == nil) { filter.site = nil }106                    ForEach(NodesLogic.sites(session.nodes.list.value ?? []), id: \.self) { s in107                        CheckItem(title: s, selected: filter.site == s) { filter.site = s }108                    }109                }110                Toggle(tr("nodes.filter.maintenance"), isOn: $filter.maintenanceOnly)111                if filter.isActive {112                    Button(tr("nodes.filter.clear"), role: .destructive) { filter = NodeFilter() }113                }114            } label: {115                Label(filter.isActive ? tr("nodes.filter.active", filter.activeCount) : tr("nodes.filter"),116                      systemImage: filter.isActive ? "line.3.horizontal.decrease.circle.fill" : "line.3.horizontal.decrease.circle")117            }118            .accessibilityIdentifier("nodes-filter")119        }120    }121}122
Files main
Menu

Theme