SPB Git

Repository Public

Sections.swift 6.3 KB 165 lines swift

1// Navigation sections and the presentation metadata of items (icons, subtitles, hosts).2import Foundation3import SharedVaultKit45enum SidebarSection: Hashable, Sendable {6    case allItems, favorites, vault(String)7    case passwords, passkeys, oneTimeCodes, cards, notes, documents, developer8    case shared, security, generator, devices, activity, connect, trash, settings, vaults910    static let categories: [SidebarSection] = [.passwords, .passkeys, .oneTimeCodes, .cards, .notes, .documents, .developer]11    static let tools: [SidebarSection] = [.shared, .security, .generator, .devices, .activity, .connect, .trash, .settings]1213    /// Sections that show a list of items (the others are tools with their own page).14    var isItemList: Bool {15        switch self {16        case .allItems, .favorites, .vault, .passwords, .passkeys, .oneTimeCodes, .cards, .notes, .documents, .developer, .trash: return true17        default: return false18        }19    }2021    var title: String {22        switch self {23        case .allItems: "All Items"24        case .favorites: "Favorites"25        case .vault: "Vault"26        case .passwords: "Passwords"27        case .passkeys: "Passkeys"28        case .oneTimeCodes: "One-time codes"29        case .cards: "Cards"30        case .notes: "Secure Notes"31        case .documents: "Documents"32        case .developer: "Developer Secrets"33        case .shared: "Shared"34        case .security: "Security Center"35        case .generator: "Generator"36        case .devices: "Devices"37        case .activity: "Activity"38        case .connect: "Connect & Agents"39        case .trash: "Trash"40        case .settings: "Settings"41        case .vaults: "Vaults"42        }43    }4445    var symbol: String {46        switch self {47        case .allItems: "square.grid.2x2"48        case .favorites: "star"49        case .vault, .vaults: "lock.square"50        case .passwords: "key"51        case .passkeys: "person.badge.key"52        case .oneTimeCodes: "clock"53        case .cards: "creditcard"54        case .notes: "note.text"55        case .documents: "doc.text"56        case .developer: "chevron.left.forwardslash.chevron.right"57        case .shared: "tray"58        case .security: "checkmark.shield"59        case .generator: "dice"60        case .devices: "laptopcomputer.and.iphone"61        case .activity: "waveform.path.ecg"62        case .connect: "cpu"63        case .trash: "trash"64        case .settings: "gearshape"65        }66    }6768    /// Stable identifier for UI tests and state restoration.69    var testID: String {70        switch self {71        case .vault(let id): "section-vault-\(id.prefix(8))"72        default: "section-\(title.lowercased().replacingOccurrences(of: " & ", with: "-").replacingOccurrences(of: " ", with: "-"))"73        }74    }7576    func contains(_ v: ItemView) -> Bool {77        let trashed = !(v.item.trashedAt ?? "").isEmpty78        if case .trash = self { return trashed }79        if trashed { return false }80        switch self {81        case .allItems: return true82        case .favorites: return v.item.favorite83        case .vault(let id): return v.vaultId == id84        case .oneTimeCodes: return !(v.item.fields["totp"] ?? "").isEmpty85        case .passwords: return v.item.type.template.section == .passwords86        case .passkeys: return v.item.type.template.section == .passkeys87        case .cards: return v.item.type.template.section == .cards88        case .notes: return v.item.type.template.section == .notes89        case .documents: return v.item.type.template.section == .documents90        case .developer: return v.item.type.template.section == .developer91        default: return false92        }93    }94}9596enum ItemMeta {97    static func symbol(_ type: ItemType) -> String {98        switch type {99        case .login: "key"100        case .passkey: "person.badge.key"101        case .card: "creditcard"102        case .identity: "person.text.rectangle"103        case .note: "note.text"104        case .wifi: "wifi"105        case .license: "seal"106        case .document: "doc.text"107        case .sshKey: "terminal"108        case .apiToken: "curlybraces"109        case .certificate: "rosette"110        case .database: "cylinder.split.1x2"111        case .env: "list.bullet.rectangle"112        }113    }114115    /// Glyph symbol for non-login items (logins show their initial).116    static func glyphSymbol(_ item: Item) -> String? { item.type == .login ? nil : symbol(item.type) }117118    static func host(_ item: Item) -> String? {119        for u in item.urls { if let o = SPV1.parseOrigin(u.url) { return o.host } }120        return nil121    }122123    /// Second line of a list row: who and where, never a secret.124    static func subtitle(_ item: Item) -> String {125        var parts: [String] = []126        for k in ["username", "userName", "holder", "fullName", "ssid", "service", "product", "subject", "host", "project"] {127            if let v = item.fields[k], !v.isEmpty { parts.append(v); break }128        }129        if item.type == .card, let n = item.fields["number"], n.count >= 4 { parts.append("•••• \(n.filter(\.isNumber).suffix(4))") }130        if let h = host(item) { parts.append(h) } else if let rp = item.fields["rpId"], !rp.isEmpty { parts.append(rp) }131        return parts.isEmpty ? item.type.template.label : parts.joined(separator: " · ")132    }133134    static func displayTitle(_ item: Item) -> String { item.title.isEmpty ? "Untitled" : item.title }135136    static func date(_ iso: String?) -> String {137        guard let iso, let d = parseISO(iso) else { return "—" }138        return d.formatted(date: .abbreviated, time: .shortened)139    }140141    static func relative(_ iso: String?) -> String {142        guard let iso, let d = parseISO(iso) else { return "never" }143        if abs(d.timeIntervalSinceNow) < 45 { return "just now" }144        return d.formatted(.relative(presentation: .named))145    }146147    static func parseISO(_ s: String) -> Date? {148        let f = ISO8601DateFormatter()149        f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]150        if let d = f.date(from: s) { return d }151        f.formatOptions = [.withInternetDateTime]152        if let d = f.date(from: s) { return d }153        f.formatOptions = [.withFullDate]154        return f.date(from: String(s.prefix(10)))155    }156}157158/// What the editor sheet is asked to open.159struct EditorRequest: Identifiable, Equatable {160    let id = UUID()161    var vaultId: String162    var recordId: String?163    var item: Item164}165
Files main
Menu

Theme