| 1 | import RastrilloApple |
| 2 | import SwiftUI |
| 3 | |
| 4 | /// Docs' account menu for an Oficina ID (spec §9): who is signed in, the |
| 5 | /// kit's team list for the identity from its team cache, filtered to teams |
| 6 | /// that have Docs, "Use another Oficina ID" (Paul, 2026-11-19), "Other ways |
| 7 | /// to sign in…" and "Sign out of <label>". The several-identities menu is |
| 8 | /// slice A2; this is one identity's teams. |
| 9 | @MainActor public struct DocsTeamPicker: View { |
| 10 | @ObservedObject private var model: OficinaIDModel |
| 11 | private let current: OficinaSignedIn |
| 12 | private let leaveCheck: @MainActor () async -> DocsLeaveBlock? |
| 13 | private let refresh: @MainActor () async -> Void |
| 14 | @State private var block: DocsLeaveBlock? |
| 15 | @State private var checked = false |
| 16 | private let otherWays: () -> Void |
| 17 | private let useAnother: (() -> Void)? |
| 18 | private let signOut: () -> Void |
| 19 | private let done: () -> Void |
| 20 | |
| 21 | /// `leaveCheck` says why switching team or signing out would strand |
| 22 | /// something (open document windows, changes not yet sent), and the |
| 23 | /// picker shows that line by the controls it disables. `refresh` fetches |
| 24 | /// the team list again if it is five minutes old (spec §9). `useAnother` |
| 25 | /// is "Use another Oficina ID", nil while it can't be asked (over an |
| 26 | /// outage); it is disabled by the same guard as signing out. |
| 27 | public init(model: OficinaIDModel, current: OficinaSignedIn, |
| 28 | leaveCheck: @escaping @MainActor () async -> DocsLeaveBlock? = { nil }, |
| 29 | refresh: @escaping @MainActor () async -> Void = {}, |
| 30 | otherWays: @escaping () -> Void, useAnother: (() -> Void)? = nil, |
| 31 | signOut: @escaping () -> Void, done: @escaping () -> Void) { |
| 32 | self.model = model; self.current = current; self.leaveCheck = leaveCheck; self.refresh = refresh |
| 33 | self.otherWays = otherWays; self.useAnother = useAnother; self.signOut = signOut; self.done = done |
| 34 | } |
| 35 | |
| 36 | private var canLeave: Bool { checked && block == nil } |
| 37 | |
| 38 | /// The cached teams that have Docs, the current one always among them. |
| 39 | public static func teams(_ cached: [OficinaTeam]?, current: OficinaTeam) -> [OficinaTeam] { |
| 40 | var teams = (cached ?? []).filter { $0.origin(for: DocsOficinaSession.configuration.app) != nil } |
| 41 | if !teams.contains(where: { $0.sqid == current.sqid }) { teams.append(current) } |
| 42 | return teams.sorted { ($0.name.localizedLowercase, $0.sqid) < ($1.name.localizedLowercase, $1.sqid) } |
| 43 | } |
| 44 | |
| 45 | private var teams: [OficinaTeam] { Self.teams(model.store.teams(current.identity.id)?.teams, current: current.team) } |
| 46 | |
| 47 | public var body: some View { |
| 48 | NavigationStack { |
| 49 | List { |
| 50 | Section { |
| 51 | OficinaIdentityRow(identity: current.identity, accent: .docs) |
| 52 | } header: { Text(verbatim: "Oficina ID") } |
| 53 | Section { |
| 54 | ForEach(teams) { team in |
| 55 | Button { choose(team) } label: { |
| 56 | OficinaTeamRow(team: team).overlay(alignment: .trailing) { |
| 57 | if team.sqid == current.team.sqid { |
| 58 | Image(systemName: "checkmark").font(.headline).foregroundStyle(OficinaPalette.accent(.docs)) |
| 59 | .padding(.trailing, 14).accessibilityHidden(true) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | .buttonStyle(.plain) |
| 64 | .disabled(team.sqid != current.team.sqid && (!canLeave || model.busy)) |
| 65 | .accessibilityAddTraits(team.sqid == current.team.sqid ? .isSelected : []) |
| 66 | .accessibilityIdentifier("docs.team-\(team.label)") |
| 67 | } |
| 68 | } header: { Text("native.oficinaid.teams", bundle: DocsResources.bundle) } footer: { |
| 69 | if let block { DocsBlockLine(block) } |
| 70 | else if let error = model.error { OficinaErrorLine(error) } |
| 71 | } |
| 72 | Section { |
| 73 | if let useAnother { |
| 74 | Button(OficinaIDText.text("continue.another")) { useAnother() } |
| 75 | .buttonStyle(.borderless) |
| 76 | .disabled(!canLeave || model.busy) |
| 77 | .accessibilityIdentifier("docs.oficinaid.useAnother") |
| 78 | } |
| 79 | Button(OficinaIDText.text("otherWays")) { otherWays() } |
| 80 | .buttonStyle(.borderless) |
| 81 | .accessibilityIdentifier("docs.otherWays") |
| 82 | Button(role: .destructive) { signOut() } label: { |
| 83 | Text(verbatim: OficinaIDText.text("signOut", current.identity.label)) |
| 84 | } |
| 85 | .buttonStyle(.borderless) |
| 86 | // Its own colour would hide that it is disabled. |
| 87 | .foregroundStyle(canLeave ? OficinaPalette.negative : OficinaPalette.textMuted) |
| 88 | .disabled(!canLeave) |
| 89 | .accessibilityIdentifier("docs.oficinaid.signOut") |
| 90 | } |
| 91 | } |
| 92 | .navigationTitle(Text("native.oficinaid.teams", bundle: DocsResources.bundle)) |
| 93 | .task { |
| 94 | block = await leaveCheck(); checked = true |
| 95 | await refresh() |
| 96 | } |
| 97 | #if os(iOS) |
| 98 | .navigationBarTitleDisplayMode(.inline) |
| 99 | #endif |
| 100 | .toolbar { |
| 101 | ToolbarItem(placement: .confirmationAction) { |
| 102 | Button { done() } label: { Text("native.done", bundle: DocsResources.bundle) } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | .tint(OficinaPalette.accent(.docs)) |
| 107 | #if os(macOS) |
| 108 | .frame(minWidth: 420, idealWidth: 460, minHeight: 440, idealHeight: 520) |
| 109 | #endif |
| 110 | } |
| 111 | |
| 112 | private func choose(_ team: OficinaTeam) { |
| 113 | done() |
| 114 | guard team.sqid != current.team.sqid else { return } |
| 115 | Task { await model.choose(team) } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /// The one line that says why a control is disabled. |
| 120 | public struct DocsBlockLine: View { |
| 121 | private let block: DocsLeaveBlock |
| 122 | public init(_ block: DocsLeaveBlock) { self.block = block } |
| 123 | public var body: some View { |
| 124 | Label(block.message, systemImage: "info.circle").font(.callout).foregroundStyle(OficinaPalette.textMuted) |
| 125 | .fixedSize(horizontal: false, vertical: true) |
| 126 | .accessibilityIdentifier("docs.leaveBlock") |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// "Other ways to sign in…": the server address and browser approval that |
| 131 | /// stay for self-hosted servers and local accounts (spec §11), with the way |
| 132 | /// back to Oficina ID beneath. With no account kept yet it is the sign-in |
| 133 | /// itself; with some, their list and "Add account". |
| 134 | @MainActor public struct DocsOtherWaysView: View { |
| 135 | @ObservedObject private var connection: NativeConnection |
| 136 | private let oficinaID: () -> Void |
| 137 | |
| 138 | public init(connection: NativeConnection, oficinaID: @escaping () -> Void) { |
| 139 | self.connection = connection; self.oficinaID = oficinaID |
| 140 | } |
| 141 | |
| 142 | public var body: some View { |
| 143 | Group { |
| 144 | if connection.accounts.isEmpty { |
| 145 | NavigationStack { NativeSignInView("Docs", systemImage: "doc.text", connection: connection) } |
| 146 | } else { |
| 147 | NativeAccountsView("Docs", systemImage: "doc.text", connection: connection) |
| 148 | } |
| 149 | } |
| 150 | .safeAreaInset(edge: .bottom, spacing: 0) { |
| 151 | Button { connection.cancel(); oficinaID() } label: { |
| 152 | Label { Text("native.oficinaid.signin", bundle: DocsResources.bundle) } icon: { Image(systemName: "person.crop.circle") } |
| 153 | } |
| 154 | .buttonStyle(.borderless) |
| 155 | .accessibilityIdentifier("docs.oficinaid.signin") |
| 156 | .padding(16) |
| 157 | .frame(maxWidth: .infinity) |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | #if os(macOS) |
| 163 | /// Docs ▸ Settings: who Docs is signed in as, and the version. SwiftUI would |
| 164 | /// title the window "Oficina Docs Settings" from the display name; the plain |
| 165 | /// name comes from the content's navigation title (rastrillo/native |
| 166 | /// docs/mac-updates.md, "Not covered: the Settings window"). |
| 167 | public struct DocsSettingsView: View { |
| 168 | public enum Account: Equatable { |
| 169 | case oficinaID(identity: String, team: String, teamLabel: String) |
| 170 | case server(name: String, host: String) |
| 171 | case none |
| 172 | } |
| 173 | |
| 174 | private let account: Account |
| 175 | private let block: DocsLeaveBlock? |
| 176 | private let manage: () -> Void |
| 177 | private let signOut: () -> Void |
| 178 | private let signIn: () -> Void |
| 179 | private let version: String |
| 180 | |
| 181 | /// `block` is the home window's guard: while it says why, "Change |
| 182 | /// team…" and "Sign out" are disabled and the line is shown. |
| 183 | public init(account: Account, block: DocsLeaveBlock?, version: String = DocsSettingsView.bundleVersion, |
| 184 | manage: @escaping () -> Void, signOut: @escaping () -> Void, signIn: @escaping () -> Void) { |
| 185 | self.account = account; self.block = block; self.version = version |
| 186 | self.manage = manage; self.signOut = signOut; self.signIn = signIn |
| 187 | } |
| 188 | |
| 189 | public static var bundleVersion: String { |
| 190 | let info = Bundle.main.infoDictionary ?? [:] |
| 191 | let short = info["CFBundleShortVersionString"] as? String ?? "0.1.0" |
| 192 | return (info["CFBundleVersion"] as? String).map { "\(short) (\($0))" } ?? short |
| 193 | } |
| 194 | |
| 195 | public var body: some View { |
| 196 | Form { |
| 197 | Section { |
| 198 | switch account { |
| 199 | case .oficinaID(let identity, let team, let teamLabel): |
| 200 | LabeledContent { Text(verbatim: identity).textSelection(.enabled) } label: { Text(verbatim: "Oficina ID") } |
| 201 | LabeledContent { Text(verbatim: "\(team) (\(teamLabel))") } label: { Text("native.settings.team", bundle: DocsResources.bundle) } |
| 202 | HStack { |
| 203 | Button { manage() } label: { Text("native.settings.changeTeam", bundle: DocsResources.bundle) } |
| 204 | .disabled(block != nil) |
| 205 | Spacer() |
| 206 | Button(role: .destructive) { signOut() } label: { Text(verbatim: OficinaIDText.text("signOut", identity)) } |
| 207 | .disabled(block != nil) |
| 208 | } |
| 209 | if let block { DocsBlockLine(block) } |
| 210 | case .server(let name, let host): |
| 211 | LabeledContent { Text(verbatim: name.isEmpty ? host : "\(name) (\(host))").textSelection(.enabled) } label: { |
| 212 | Text("native.settings.server", bundle: DocsResources.bundle) |
| 213 | } |
| 214 | HStack { |
| 215 | // Not guarded: a server account's list switches nothing |
| 216 | // by itself, and the home window guards what it does. |
| 217 | Button { manage() } label: { Text("native.settings.accounts", bundle: DocsResources.bundle) } |
| 218 | Spacer() |
| 219 | Button(role: .destructive) { signOut() } label: { Text("docs.signout.label", bundle: DocsResources.bundle) } |
| 220 | .disabled(block != nil) |
| 221 | } |
| 222 | if let block { DocsBlockLine(block) } |
| 223 | case .none: |
| 224 | LabeledContent { Text("native.settings.signedOut", bundle: DocsResources.bundle) } label: { Text(verbatim: "Oficina ID") } |
| 225 | Button { signIn() } label: { Text("native.settings.signIn", bundle: DocsResources.bundle) } |
| 226 | } |
| 227 | } header: { Text("native.settings.account", bundle: DocsResources.bundle) } |
| 228 | Section { |
| 229 | LabeledContent { Text(verbatim: version) } label: { Text("native.colophon.version", bundle: DocsResources.bundle) } |
| 230 | } |
| 231 | } |
| 232 | .formStyle(.grouped) |
| 233 | .frame(width: 480) |
| 234 | .fixedSize(horizontal: false, vertical: true) |
| 235 | .navigationTitle(Text("native.docs", bundle: DocsResources.bundle)) |
| 236 | .tint(OficinaPalette.accent(.docs)) |
| 237 | } |
| 238 | } |
| 239 | #endif |
| 240 | |