import SwiftUI import ShotdeckCore /// The five review-detail fields of a session, in one compact grid. /// /// Values are held locally while typing and written through to the session manifest /// on every change, so closing the panel cannot lose them. The local copy is re-seeded /// whenever the open session changes (a new session inherits the previous details). struct SessionContextEditor: View { @Environment(AppModel.self) private var model @State private var lane = "" @State private var deployedVersion = "" @State private var commitHash = "" @State private var reviewer = "" @State private var note = "" @State private var seededSessionID: UUID? /// What this view last wrote (or seeded). Lets an out-of-band change — a context /// inherited by a new session, for example — refresh the fields without fighting /// the user's own keystrokes. @State private var lastKnownContext: ReviewContext = .empty /// The pending debounced write and its value. Typing replaces it, so one short /// pause writes once instead of once per keystroke, and an older write can never /// land after a newer one. @State private var pendingWrite: Task? @State private var lastScheduledWrite: ReviewContext = .empty private let labelWidth: CGFloat = 84 var body: some View { Grid(alignment: .leading, horizontalSpacing: 8, verticalSpacing: 4) { row("Lane", text: $lane, prompt: "dev03, customer test lane…") row("Deployed", text: $deployedVersion, prompt: "build that is live") row("Commit", text: $commitHash, prompt: "git SHA") row("Reviewer", text: $reviewer, prompt: "who is reviewing") row("Note", text: $note, prompt: "what this session covers") } .onAppear { seedIfNeeded() } .onChange(of: model.session.id) { _, _ in seedIfNeeded() } .onChange(of: model.session.context) { _, newValue in guard newValue != lastKnownContext else { return } seed(from: newValue) } .onChange(of: lane) { _, _ in commit() } .onChange(of: deployedVersion) { _, _ in commit() } .onChange(of: commitHash) { _, _ in commit() } .onChange(of: reviewer) { _, _ in commit() } .onChange(of: note) { _, _ in commit() } } private func row(_ label: String, text: Binding, prompt: String) -> some View { GridRow(alignment: .center) { Text(label) .font(.caption) .foregroundStyle(.secondary) .frame(width: labelWidth, alignment: .trailing) TextField(prompt, text: text) .textFieldStyle(.roundedBorder) .font(.caption) .onSubmit { commit() } } } private func seedIfNeeded() { guard seededSessionID != model.session.id else { return } seededSessionID = model.session.id seed(from: model.session.context) } private func seed(from context: ReviewContext) { lane = context.lane deployedVersion = context.deployedVersion commitHash = context.commit reviewer = context.reviewer note = context.note lastKnownContext = context } private func commit() { let context = ReviewContext( lane: lane, deployedVersion: deployedVersion, commit: commitHash, reviewer: reviewer, note: note ) guard context != model.session.context else { return } guard context != lastScheduledWrite else { return } lastKnownContext = context lastScheduledWrite = context pendingWrite?.cancel() pendingWrite = Task { try? await Task.sleep(for: .milliseconds(150)) guard !Task.isCancelled else { return } await model.setContextField(context) } } }