import SwiftUI /// What the app does for Ask beyond Base's routes: opening links (the /// browser, or another app), and setting Ask up (Task 12 runs Oficina ID's /// set-up; until then the web's Settings). public struct AskActions { /// Set Ask up (Task 12: with Oficina ID, silently). public var setUp: () -> Void /// Set Ask up on the web, with the code flow: the way on when Equipo /// doesn't know this person (not_linked, P2). Never retried by itself. public var setUpOnWeb: () -> Void public init(setUp: @escaping () -> Void, setUpOnWeb: @escaping () -> Void = {}) { self.setUp = setUp; self.setUpOnWeb = setUpOnWeb } /// What each of Ask's not-ready states offers: its line, and its /// button's text and whether it sets Ask up here or on the web. public enum Way: Equatable, Sendable { case here(String), web(String) } public static func note(_ phase: AskPhase) -> (lede: String, way: Way?) { switch phase { case .connect: ("ask.connect.lede", .here("ask.connect.button")) case .pending: ("ask.pending.lede", .here("ask.pending.button")) case .reconnect: ("ask.reconnect.lede", .here("ask.reconnect.button")) case .notLinked: ("native.ask.not_linked", .web("native.ask.not_linked.button")) default: ("ask.unavailable", nil) } } } /// Ask Base, drawn: the conversation, its status line and the composer, or /// in their place the state Ask is in, with the web's approved copy /// (chat.html `ask-body`). The window's bar, title and the Mac's sidebar /// are the apps' own (Tasks 13 and 14). public struct AskView: View { @Bindable var model: AskModel let layout: AskLayout let timeZone: TimeZone? let actions: AskActions let focusOnAppear: Bool @FocusState private var focused: Bool @Environment(\.colorScheme) private var scheme /// `focusOnAppear`: the field takes focus as Ask shows (the menu-bar /// panel; never on the iPhone, where it would raise the keyboard). public init(model: AskModel, layout: AskLayout, timeZone: TimeZone?, actions: AskActions, focusOnAppear: Bool = false) { self.model = model; self.layout = layout; self.timeZone = timeZone; self.actions = actions; self.focusOnAppear = focusOnAppear } public var body: some View { let p = Palette(scheme) Group { if model.phase.chatting { VStack(spacing: 0) { if model.isEmpty { Spacer(minLength: 0) Text(BaseText.text("ask.empty")) .font(BaseFont.body(14, relativeTo: .subheadline)).foregroundStyle(p[.haze]) .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal, 20).padding(.bottom, 12) } else { AskThread(model: model, layout: layout, timeZone: timeZone) } foot(p) } } else { note(p) } } .foregroundStyle(p[.frost]) .onAppear { if focusOnAppear { focused = true } } } @ViewBuilder private func foot(_ p: Palette) -> some View { VStack(alignment: .leading, spacing: 6) { if model.phase == .norunner || model.phase == .oldrunner { Text(runnerNote(model.phase == .norunner ? "ask.norunner" : "ask.oldrunner")) .font(BaseFont.body(14, relativeTo: .subheadline)).foregroundStyle(p[.haze]).tint(p[.focus]) .accessibilityAddTraits(.isStaticText) } if !model.status.isEmpty { Text(verbatim: model.status) .font(BaseFont.body(14, relativeTo: .subheadline)).foregroundStyle(p[.haze]) .accessibilityAddTraits(.updatesFrequently) } Composer(model: model, focused: $focused) } .padding(.horizontal, layout == .window ? 24 : 10) .padding(.top, 6) .padding(.bottom, layout == .window ? 20 : 12) } private func runnerNote(_ key: String) -> AttributedString { Self.runnerNote(key, equipo: model.equipoOrigin) } /// The web's note (norunner, oldrunner), its Markdown link to Equipo /// drawn as a real link, underlined, that opens in the browser (the /// environment's openURL); never as brackets. With no Equipo known yet /// the words stay, without a link. public static func runnerNote(_ key: String, equipo: URL?) -> AttributedString { let raw = BaseText.text(key) let text: String if let equipo, BaseJSON.webURL(equipo.absoluteString) != nil { text = raw.replacingOccurrences(of: "{equipo}", with: "<\(equipo.absoluteString)>") } else { // [words]({equipo}) → words text = raw.replacingOccurrences(of: #"\[([^\]]*)\]\(\{equipo\}\)"#, with: "$1", options: .regularExpression) } let options = AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace) guard var out = try? AttributedString(markdown: text, options: options) else { return AttributedString(text.replacingOccurrences(of: #"[\[\]]|\(<[^>]*>\)"#, with: "", options: .regularExpression)) } for run in out.runs where run.link != nil { out[run.range].underlineStyle = .single } return out } @ViewBuilder private func note(_ p: Palette) -> some View { VStack(alignment: .leading, spacing: 14) { if model.phase == .loading { ProgressView().controlSize(.small) } else { let note = AskActions.note(model.phase) lede(note.lede, p) switch note.way { case .here(let key)?: button(key, actions.setUp) case .web(let key)?: button(key, actions.setUpOnWeb) case nil: EmptyView() } } } .padding(.vertical, 22).padding(.horizontal, 20) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) } private func lede(_ key: String, _ p: Palette) -> some View { Text(BaseText.text(key)).font(BaseFont.body(16)).foregroundStyle(p[.haze]).fixedSize(horizontal: false, vertical: true) } private func button(_ key: String, _ action: @escaping () -> Void) -> some View { Button(action: action) { Text(BaseText.text(key)) }.buttonStyle(BaseButtonStyle(.primary)) } }