bugger/Sources/Views/OAuth/OAuthSetupView.swift

77 lines
2.4 KiB
Swift

import AppKit
import SwiftUI
struct OAuthSetupView: View {
@State private var tokenManager = TokenManager.shared
@State private var statusMessage: String?
var body: some View {
VStack(spacing: 16) {
Image(systemName: "link.circle")
.font(.system(size: 40))
.foregroundStyle(.blue)
Text("Connect Feishu")
.font(.headline)
Text("Authorize Bugger to read bugs assigned to you from your Feishu Bitable.")
.font(.caption)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
if tokenManager.authorizeURL == nil {
Text("Set FEISHU_APP_ID and FEISHU_APP_SECRET in the Xcode build settings before connecting.")
.font(.caption)
.foregroundStyle(.orange)
.multilineTextAlignment(.center)
} else {
Button("Connect Feishu") {
connectFeishu()
}
.buttonStyle(.borderedProminent)
}
if case .authenticating = tokenManager.state {
ProgressView("Completing sign-in...")
}
if case .error(let message) = tokenManager.state {
Text(message)
.font(.caption)
.foregroundStyle(.red)
.multilineTextAlignment(.center)
}
if let statusMessage {
Text(statusMessage)
.font(.caption)
.foregroundStyle(.secondary)
}
}
.padding(24)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
private func connectFeishu() {
guard let url = tokenManager.authorizeURL else { return }
statusMessage = "Complete authorization in your browser."
NSWorkspace.shared.open(url)
}
}
enum OAuthCallbackHandler {
static func handle(_ url: URL) {
Task {
do {
try await TokenManager.shared.handleCallback(url: url)
await PollerService.shared.startIfConfigured()
await PollerService.shared.fetchNow()
} catch {
await MainActor.run {
TokenManager.shared.setError(error.localizedDescription)
}
}
}
}
}