oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import SwiftUI
2
3/// A question the coordinator asks: its options, answered here, bound to
4/// the revision the person saw. A card that changed underneath is shown as
5/// it is now and never answered on the person's behalf. Words of their own
6/// go in the composer, as a message.
7public struct LiveQuestionCard: View {
8 let card: ThinCard
9 let taskTitle: String?
10 let session: EquipoSession
11 private var taskHeader: (task: ThinTask, open: () -> Void)?
12 @State private var phase: Phase = .open
13 @Bindable private var draft: QuestionAnswerDraft
14
15 public init(card: ThinCard, taskTitle: String?, session: EquipoSession) {
16 self.card = card; self.taskTitle = taskTitle; self.session = session
17 draft = session.questionDraft(for: card)
18 }
19
20 init(card: ThinCard, task: ThinTask, session: EquipoSession, openTask: @escaping () -> Void) {
21 self.init(card: card, taskTitle: nil, session: session)
22 taskHeader = (task, openTask)
23 }
24
25 enum Phase: Equatable { case open, sending(String), settingAside, changed(at: Int64), offline, failed }
26
27 public var body: some View {
28 if card.isAside {
29 AsideStub(card: card, title: Feed.asideTitle(card, task: session.task(card.taskId)), session: session)
30 } else {
31 raised
32 }
33 }
34
35 private var raised: some View {
36 RaisedCard(flat: !card.isOpen) {
37 if let taskHeader {
38 TaskRowButton(task: taskHeader.task, needsYou: true, showsNeedsBadge: false, action: taskHeader.open)
39 .accessibilityIdentifier("needs-question-header-" + taskHeader.task.id)
40 }
41 if let taskTitle, !card.question.contains(taskTitle) {
42 Text(taskTitle).font(Typo.caption).foregroundStyle(Palette.secondary)
43 }
44 CardHead(text: card.question.isEmpty ? String(localized: "card.question", bundle: .module) : card.question,
45 aside: aside, settingAside: phase == .settingAside)
46 if card.isOpen {
47 if card.rememberName != nil {
48 RosterCheck(title: String(format: EquipoStrings.text("projects.remember_name"), draft.name), isOn: $draft.remembers).disabled(isSending)
49 RosterField(title: EquipoStrings.text("projects.name_to_remember"), text: $draft.name)
50 .disabled(!draft.remembers || isSending)
51 }
52 choices
53 feedback
54 } else if !card.choice.isEmpty {
55 Label { Text(String(localized: "question.answered \(card.choice)", bundle: .module)) } icon: { Image(systemName: "checkmark") }
56 .font(Typo.secondaryStrong).foregroundStyle(Palette.positive)
57 } else {
58 Text(String(localized: "question.closed", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.secondary)
59 }
60 }
61 .accessibilityElement(children: .contain)
62 .onChange(of: card.revision) { _, now in
63 if case .changed(let at) = phase, !AnswerFeedback.stillChanged(conflictAt: at, now: now) { phase = .open }
64 }
65 }
66
67 private var isSending: Bool { if case .sending = phase { return true }; return false }
68 private var aside: (() -> Void)? {
69 guard Feed.canSetAside(card), !isSending, phase != .settingAside else { return nil }
70 return { Task { await setAside() } }
71 }
72
73 @ViewBuilder private var choices: some View {
74 let options = card.options ?? []
75 if options.isEmpty {
76 RosterField(title: EquipoStrings.text(String.LocalizationValue(card.answerFieldKey)), text: $draft.typedChoice).onSubmit { Task { await submit(draft.typedChoice) } }
77 .disabled(isSending)
78 RosterButton(title: EquipoStrings.text("chat.card.answer"), primary: true, expanded: false) {
79 Task { await submit(draft.typedChoice) }
80 }.disabled(isSending || draft.typedChoice.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
81 } else {
82 ChoiceRow { optionButtons(options) }
83 }
84 }
85 @ViewBuilder private func optionButtons(_ options: [String]) -> some View {
86 ForEach(Array(options.enumerated()), id: \.offset) { index, option in
87 let busy: String? = phase == .sending(option) ? String(localized: "action.sending", bundle: .module) : nil
88 RosterButton(title: option, primary: index == 0, expanded: true, busy: busy, busyInPlace: true) {
89 Task { await submit(option) }
90 }
91 .disabled(isSending)
92 }
93 }
94
95 @ViewBuilder private var feedback: some View {
96 switch phase {
97 case .changed: Text(String(localized: "card.changed", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative)
98 case .offline: Text(String(localized: "card.offline", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative)
99 case .failed: Text(session.answerNotices[card.id] ?? String(localized: "card.failed", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative)
100 default: EmptyView()
101 }
102 }
103
104 private func submit(_ choice: String) async {
105 guard !isSending, !choice.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return }
106 phase = .sending(choice)
107 switch await draft.submit(choice, card: card, session: session) {
108 case .answered: phase = .open
109 case .changed: phase = .changed(at: session.cards.first { $0.id == card.id }?.revision ?? card.revision)
110 case .offline: phase = .offline
111 case .failed: phase = .failed
112 }
113 }
114
115 private func setAside() async {
116 if isSending || phase == .settingAside { return }
117 phase = .settingAside
118 switch await session.setAside(card, true) {
119 case .answered: phase = .open
120 case .changed: phase = .changed(at: session.cards.first { $0.id == card.id }?.revision ?? card.revision)
121 case .offline: phase = .offline
122 case .failed: phase = .failed
123 }
124 }
125}
126
127/// A decision card. Merges and "Does main ship?" are answered here, in the
128/// web's words (Feed.decision); every other decision opens the web app,
129/// where its full context is.
130struct LiveDecisionCard: View {
131 let card: ThinCard
132 let taskTitle: String?
133 let session: EquipoSession
134 @State private var phase: Phase = .open
135 @Environment(\.openURL) private var openURL
136
137 enum Phase: Equatable { case open, sending(String), settingAside, changed(at: Int64), offline, failed }
138
139 var body: some View {
140 if card.isAside {
141 AsideStub(card: card, title: Feed.asideTitle(card, task: session.task(card.taskId)), session: session)
142 } else {
143 raised
144 }
145 }
146
147 private var aside: (() -> Void)? {
148 guard Feed.canSetAside(card), phase == .open || phase == .offline || phase == .failed else { return nil }
149 return { Task { await setAside() } }
150 }
151
152 @ViewBuilder private var raised: some View {
153 let view = Feed.decision(card, task: session.task(card.taskId))
154 RaisedCard(flat: !(view?.open ?? card.isOpen)) {
155 if let taskTitle, !card.question.isEmpty, !card.question.contains(taskTitle) {
156 Text(taskTitle).font(Typo.caption).foregroundStyle(Palette.secondary)
157 }
158 if let view, view.showsOnlyNote, let note = view.note {
159 // As the web's equipo-card-done: the outcome alone.
160 Label { Text(note) } icon: { Image(systemName: "checkmark") }
161 .font(Typo.secondaryStrong).foregroundStyle(Palette.positive)
162 } else {
163 CardHead(text: card.question.isEmpty ? (taskTitle ?? String(localized: "decision.untitled", bundle: .module)) : card.question,
164 aside: aside, settingAside: phase == .settingAside)
165 if let view {
166 answerable(view)
167 } else if card.isOpen {
168 Text(String(localized: "decision.on_web", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.secondary)
169 .fixedSize(horizontal: false, vertical: true)
170 RosterButton(title: String(localized: "card.open_on_web", bundle: .module), primary: true) {
171 openURL(Feed.webURL(for: card, origin: session.origin))
172 }
173 } else {
174 QuietLink(title: String(localized: "card.open_on_web", bundle: .module), icon: "arrow.up.right") {
175 openURL(Feed.webURL(for: card, origin: session.origin))
176 }
177 }
178 // An answer's or a Set aside's outcome, on every decision card.
179 feedback
180 }
181 }
182 .accessibilityElement(children: .contain)
183 .onChange(of: card.revision) { _, now in
184 if case .changed(let at) = phase, !AnswerFeedback.stillChanged(conflictAt: at, now: now) { phase = .open }
185 }
186 }
187
188 @ViewBuilder private func answerable(_ view: DecisionView) -> some View {
189 if view.hasPreview {
190 QuietLink(title: String(localized: "chat.card.preview_link", bundle: .module), icon: "arrow.up.right") {
191 openURL(Feed.previewURL(card, origin: session.origin))
192 }
193 }
194 if view.open {
195 let sending: String? = { if case .sending(let v) = phase { return v }; return nil }()
196 ChoiceRow { buttons(view, sending: sending) }
197 }
198 }
199
200 @ViewBuilder private func buttons(_ view: DecisionView, sending: String?) -> some View {
201 ForEach(view.choices) { choice in
202 RosterButton(title: choice.label, primary: choice.primary, expanded: true,
203 busy: sending == choice.value ? String(localized: "action.sending", bundle: .module) : nil, busyInPlace: true) {
204 Task { await submit(choice.value) }
205 }
206 .disabled(sending != nil)
207 }
208 }
209
210 @ViewBuilder private var feedback: some View {
211 switch phase {
212 case .changed: Text(String(localized: "card.changed", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative)
213 case .offline: Text(String(localized: "card.offline", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative)
214 case .failed: Text(String(localized: "card.failed", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative)
215 default: EmptyView()
216 }
217 }
218
219 private func submit(_ choice: String) async {
220 if case .sending = phase { return }
221 phase = .sending(choice)
222 let outcome = await session.answer(card, choice: choice)
223 switch outcome {
224 case .answered: phase = .open
225 case .changed:
226 // The card as it is now, after the conflict: the message stays until it moves past it.
227 phase = .changed(at: session.cards.first { $0.id == card.id }?.revision ?? card.revision)
228 announce(String(localized: "card.changed", bundle: .module))
229 case .offline: phase = .offline; announce(String(localized: "card.offline", bundle: .module))
230 case .failed: phase = .failed; announce(String(localized: "card.failed", bundle: .module))
231 }
232 }
233
234 private func setAside() async {
235 if phase != .open && phase != .offline && phase != .failed { return }
236 phase = .settingAside
237 switch await session.setAside(card, true) {
238 case .answered: phase = .open
239 case .changed:
240 phase = .changed(at: session.cards.first { $0.id == card.id }?.revision ?? card.revision)
241 announce(String(localized: "card.changed", bundle: .module))
242 case .offline: phase = .offline; announce(String(localized: "card.offline", bundle: .module))
243 case .failed: phase = .failed; announce(String(localized: "card.failed", bundle: .module))
244 }
245 }
246
247 private func announce(_ text: String) {
248 AccessibilityNotification.Announcement(text).post()
249 }
250}
251
252/// A card set aside (chat.html equipo-aside-stub): one line naming it,
253/// "Set aside, still in Needs you", and Undo, which brings it back at the
254/// revision seen. It never answers, cancels or starts anything.
255struct AsideStub: View {
256 let card: ThinCard
257 let title: String
258 let session: EquipoSession
259 @State private var phase: Phase = .idle
260 enum Phase: Equatable { case idle, restoring, changed, offline, failed }
261
262 var body: some View {
263 let shape = RoundedRectangle(cornerRadius: Metric.raised - 4, style: .continuous)
264 VStack(alignment: .leading, spacing: 6) {
265 HStack(alignment: .center, spacing: 12) {
266 VStack(alignment: .leading, spacing: 2) {
267 Text(title).font(Typo.secondaryStrong).foregroundStyle(Palette.text).lineLimit(1).truncationMode(.tail)
268 Text(String(localized: "roster.aside", bundle: .module)).font(Typo.caption).foregroundStyle(Palette.secondary)
269 }
270 Spacer(minLength: 8)
271 RosterButton(title: String(localized: "roster.undo", bundle: .module),
272 busy: phase == .restoring ? String(localized: "roster.restoring", bundle: .module) : nil) {
273 Task { await undo() }
274 }
275 }
276 switch phase {
277 case .changed: Text(String(localized: "card.changed", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative)
278 case .offline: Text(String(localized: "card.offline", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative)
279 case .failed: Text(String(localized: "card.failed", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.negative)
280 default: EmptyView()
281 }
282 }
283 .padding(.horizontal, 14).padding(.vertical, 10)
284 .background(Palette.surface2, in: shape)
285 .overlay(shape.strokeBorder(Palette.border, lineWidth: 1))
286 .accessibilityElement(children: .contain)
287 }
288
289 private func undo() async {
290 if phase == .restoring { return }
291 phase = .restoring
292 let outcome = await session.setAside(card, false)
293 let said: String?
294 switch outcome {
295 case .answered: phase = .idle; said = nil
296 case .changed: phase = .changed; said = String(localized: "card.changed", bundle: .module)
297 case .offline: phase = .offline; said = String(localized: "card.offline", bundle: .module)
298 case .failed: phase = .failed; said = String(localized: "card.failed", bundle: .module)
299 }
300 if let said { AccessibilityNotification.Announcement(said).post() }
301 }
302}
303
304/// A task report on its rail: eyebrow, title, the five-stop track and the
305/// facts; one line while its decision is open beside it.
306public struct LiveReport: View {
307 let report: TaskReport
308 var session: EquipoSession?
309 var compact = false
310 var wide = false
311 var onOpen: () -> Void = {}
312 var onRunNow: ((TaskPause.RunNow) -> Void)? = nil
313 public init(report: TaskReport, session: EquipoSession? = nil, compact: Bool = false, wide: Bool = false, onRunNow: ((TaskPause.RunNow) -> Void)? = nil, onOpen: @escaping () -> Void = {}) {
314 self.report = report; self.session = session; self.compact = compact; self.wide = wide; self.onRunNow = onRunNow; self.onOpen = onOpen
315 }
316 public var body: some View {
317 VStack(alignment: .leading, spacing: Metric.gap) {
318 if compact {
319 Button(action: onOpen) {
320 FlowLayout(spacing: 8, lineSpacing: 4) {
321 Text(report.title).font(Typo.compactTitle).foregroundStyle(Palette.text)
322 Text(report.eyebrow).font(Typo.eyebrow).foregroundStyle(report.tone.eyebrow)
323 if report.needsYou { NeedsChip() }
324 }
325 .frame(maxWidth: .infinity, alignment: .leading).contentShape(Rectangle())
326 }
327 .buttonStyle(.plain)
328 .accessibilityHint(Text(String(localized: "task.open_hint", bundle: .module)))
329 } else {
330 VStack(alignment: .leading, spacing: 4) {
331 HStack(alignment: .center, spacing: 8) {
332 // A pause says it in its lead and its stop.
333 if report.pause == nil { Text(report.eyebrow).font(Typo.eyebrow).foregroundStyle(report.tone.eyebrow).lineLimit(1) }
334 Spacer(minLength: 0)
335 if report.needsYou { NeedsChip() }
336 }
337 Text(report.title).font(Typo.reportTitle).foregroundStyle(Palette.text)
338 .fixedSize(horizontal: false, vertical: true)
339 }
340 if let pause = report.pause {
341 if let session { TaskPauseActions(pause: pause, taskID: report.task.id, session: session) }
342 else { PauseLead(pause: pause, onRunNow: onRunNow) }
343 }
344 if !report.cancelled { StageTrack(track: report.track, allLabels: wide) }
345 if !report.facts.isEmpty { FactsView(facts: report.facts, wide: wide) }
346 QuietLink(title: String(localized: "task.open", bundle: .module), icon: "chevron.right", action: onOpen)
347 }
348 }
349 .padding(.leading, Metric.indent).padding(.bottom, compact ? 2 : 4)
350 .background(alignment: .topLeading) {
351 // The neutral rail hangs from the byline tile's centre.
352 Capsule().fill(Palette.border).frame(width: 2).padding(.leading, Metric.tile / 2 - 1)
353 }
354 }
355}
356
357/// A one-line receipt: a project added, where new projects go.
358struct LiveReceipt: View {
359 let card: ThinCard
360 var body: some View {
361 HStack(spacing: 0) {
362 CoordinatorMark(size: 22).frame(width: Metric.indent, alignment: .leading)
363 Text(Feed.receiptLine(card)).font(Typo.secondary).foregroundStyle(Palette.secondary)
364 .fixedSize(horizontal: false, vertical: true)
365 }
366 .accessibilityElement(children: .combine)
367 }
368}
369
370/// The coordinator at work: the mark, what it is doing, elapsed time and a
371/// quiet Stop for that turn only. A turn waiting for a connection says so,
372/// without a clock. A Stop the server did not take is offered again.
373public struct ThinkingLine: View {
374 let turn: ThinTurn
375 let activity: String?
376 let stopping: Bool
377 var onStop: () -> Void
378 public init(turn: ThinTurn, activity: String?, stopping: Bool, onStop: @escaping () -> Void) {
379 self.turn = turn; self.activity = activity; self.stopping = stopping; self.onStop = onStop
380 }
381 public var body: some View {
382 TimelineView(.periodic(from: .now, by: 1)) { context in
383 let waiting = turn.state == "waiting"
384 let seconds = max(0, Int(context.date.timeIntervalSince(turn.createdAt)))
385 let doing = waiting ? String(localized: "thinking.waiting", bundle: .module)
386 : activity ?? String(localized: "thinking.working", bundle: .module)
387 HStack(spacing: 0) {
388 CoordinatorMark(size: 22).frame(width: Metric.indent, alignment: .leading)
389 HStack(spacing: 0) {
390 Text(doing).font(Typo.secondaryStrong).foregroundStyle(Palette.text).lineLimit(1).truncationMode(.tail)
391 if !waiting {
392 Text(" · ").foregroundStyle(Palette.faint).accessibilityHidden(true)
393 Text(Duration.seconds(seconds).formatted(.units(allowed: [.minutes, .seconds], width: .narrow)))
394 .monospacedDigit().foregroundStyle(Palette.secondary).fixedSize()
395 }
396 }.font(Typo.secondary)
397 Spacer(minLength: 8)
398 QuietLink(title: stopping ? String(localized: "thinking.stopping", bundle: .module) : String(localized: "thinking.stop", bundle: .module)) {
399 if !stopping { onStop() }
400 }
401 }
402 .accessibilityElement(children: .combine)
403 }
404 }
405}
406