oficina / base Public

Bring Send a new code back on time by itself

Astra's Task 16 finding 7: the button compared the kit's
resendAvailableAt with the time of its last redraw, and nothing redrew
it when the wait ended, so it stayed disabled until the person typed or
moved focus. ResendButton now waits for the deadline in a cancellable
task and redraws when it passes. ResendTests hosts it in a window and
only waits: disabled at first, enabled once the second is up.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 8f22a80d972b7e63cdea0dfd0f2d5f70aa696dd7 parent 472d909
2 files changed, +51 −3
  • native/apple/Sources/BaseKit/SignIn/BaseOnboarding.swift +30 −3
  • native/apple/Tests/BaseKitTests/StatusPanelTests.swift +21 −0
diff --git a/native/apple/Sources/BaseKit/SignIn/BaseOnboarding.swift b/native/apple/Sources/BaseKit/SignIn/BaseOnboarding.swift
index 671ef65..ffb5f54 100644
--- a/native/apple/Sources/BaseKit/SignIn/BaseOnboarding.swift
+++ b/native/apple/Sources/BaseKit/SignIn/BaseOnboarding.swift
@@ -105,9 +105,7 @@ public struct BaseSignIn: View {
if value.filter(\.isNumber).count == 6, !model.busy { Task { await model.submitCode() } }
}
.onSubmit { Task { await model.submitCode() } }
- Button { Task { await model.resend() } } label: { Text(BaseText.text("native.signin.code.resend")) }
- .buttonStyle(BaseButtonStyle(.ghost, expands: true))
- .disabled(model.busy || (model.resendAvailableAt.map { $0 > .now } ?? false))
+ ResendButton(availableAt: model.resendAvailableAt, busy: model.busy) { Task { await model.resend() } }
case .notMember(let team):
Button { if let base = team.origin(for: "base") { openURL(base.appendingPathComponent("signin")) } } label: {
Text(BaseText.text("native.signin.unknown.button"))
@@ -179,3 +177,32 @@ struct FieldStyle: ViewModifier {
.overlay { RoundedRectangle(cornerRadius: 10, style: .continuous).strokeBorder(p[.fieldEdge], lineWidth: 1) }
}
}
+
+/// Send a new code: disabled until the kit's `resendAvailableAt`, then
+/// enabled on time by itself, with nothing else happening on screen (a
+/// cancellable wait for the deadline, as the kit's own control redraws
+/// on its timeline).
+struct ResendButton: View {
+ let availableAt: Date?
+ let busy: Bool
+ let action: () -> Void
+ /// Hears whether it is enabled, each time that changes (tests).
+ var probe: ((Bool) -> Void)?
+ @State private var now = Date()
+
+ var waiting: Bool { availableAt.map { $0 > now } ?? false }
+
+ var body: some View {
+ Button(action: action) { Text(BaseText.text("native.signin.code.resend")) }
+ .buttonStyle(BaseButtonStyle(.ghost, expands: true))
+ .disabled(busy || waiting)
+ .onChange(of: busy || waiting, initial: true) { _, off in probe?(!off) }
+ .task(id: availableAt) {
+ now = Date()
+ while let deadline = availableAt, deadline > now {
+ do { try await Task.sleep(for: .seconds(max(0.05, deadline.timeIntervalSinceNow))) } catch { return }
+ now = Date()
+ }
+ }
+ }
+}
diff --git a/native/apple/Tests/BaseKitTests/StatusPanelTests.swift b/native/apple/Tests/BaseKitTests/StatusPanelTests.swift
index 45ac68d..ab9c08e 100644
--- a/native/apple/Tests/BaseKitTests/StatusPanelTests.swift
+++ b/native/apple/Tests/BaseKitTests/StatusPanelTests.swift
@@ -62,3 +62,24 @@ import XCTest
}
}
#endif
+
+#if os(macOS)
+/// Astra's Task 16 finding 7: Send a new code comes back on time by
+/// itself, with no typing, focus or other redraw.
+@MainActor final class ResendTests: XCTestCase {
+ func testResendReenablesOnTimeByWaitingOnly() async throws {
+ _ = NSApplication.shared
+ NSApp.setActivationPolicy(.accessory)
+ var states: [Bool] = []
+ let button = ResendButton(availableAt: Date().addingTimeInterval(1), busy: false, action: {}, probe: { states.append($0) })
+ let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 320, height: 80), styleMask: [.borderless], backing: .buffered, defer: false)
+ window.contentView = NSHostingView(rootView: button.frame(width: 320))
+ window.orderFrontRegardless()
+ defer { window.orderOut(nil) }
+ try await Task.sleep(for: .milliseconds(300))
+ XCTAssertEqual(states.last, false, "disabled while the code is fresh")
+ try await Task.sleep(for: .milliseconds(1400))
+ XCTAssertEqual(states.last, true, "enabled on time, by waiting only")
+ }
+}
+#endif