Files

244 lines
9.3 KiB
Swift
Raw Permalink Normal View History

import ByreAPI
2026-10-17 07:26:18 -07:00
import SwiftUI
import UserNotifications
typealias NotificationPrefs = Components.Schemas.NotificationPrefs
@MainActor
private let kinds: [(key: WritableKeyPath<NotificationPrefs, Bool>, label: String)] = [
(\.questions, "Questions"),
(\.ready, "Ready to merge"),
(\.discard, "Suggests discard"),
(\.mergeProblems, "Merge problems"),
(\.workflows, "Workflow finished"),
2026-10-17 07:26:18 -07:00
(\.errors, "Session errors"),
(\.rateLimits, "Rate limits"),
2026-10-19 19:56:24 -07:00
(\.reports, "Turn summaries"),
2026-10-17 07:26:18 -07:00
]
struct SettingsView: View {
@Environment(Store.self) private var store
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
heading("Window colour")
Text("Windows for this server have a border in this colour, on every Mac you use.")
.foregroundStyle(Palette.muted)
.fixedSize(horizontal: false, vertical: true)
ServerColorPicker()
2026-10-17 07:26:18 -07:00
heading("Notifications")
DeviceNotifications()
PrefsBox()
heading("Default model")
Text("New tasks you start from byre run on this model.")
.foregroundStyle(Palette.muted)
.fixedSize(horizontal: false, vertical: true)
2026-10-18 22:56:33 -07:00
ModelMenu(current: store.defaultModel, help: "Model for new tasks") { m in
try await store.api.client.updateDefaultModel(body: .json(.init(model: m))).ok.body.json
}
if let me = store.me {
heading("Review models")
Text("The first model reviews your tasks' work. The second reviews work the first model wrote.")
.foregroundStyle(Palette.muted)
.fixedSize(horizontal: false, vertical: true)
ModelMenu(current: me.reviewModel, help: "Model that reviews your tasks") { m in
try await store.api.client.updateReviewModels(body: .json(.init(reviewModel: m, secondReviewModel: me.secondReviewModel))).ok.body.json
}
ModelMenu(current: me.secondReviewModel, help: "Model that reviews work the first review model wrote") { m in
try await store.api.client.updateReviewModels(body: .json(.init(reviewModel: me.reviewModel, secondReviewModel: m))).ok.body.json
}
}
heading("iCloud sync")
SyncState()
2026-10-18 22:17:54 -07:00
heading("This Mac's permissions")
Text("The macOS privacy grants byre's app needs for agents to see and control this Mac. Checking asks macOS for any that are missing.")
.foregroundStyle(Palette.muted)
.fixedSize(horizontal: false, vertical: true)
PermissionsCheck(run: checkLocalPermissions)
2026-10-17 07:26:18 -07:00
}
.padding(.horizontal, 16)
.padding(.top, 8)
.padding(.bottom, 16)
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Palette.bg)
}
private func heading(_ text: String) -> some View {
Text(text)
.font(Fonts.font(13, weight: 700))
.padding(.top, 16)
.padding(.bottom, 6)
}
}
private struct ServerColorPicker: View {
@Environment(Store.self) private var store
var body: some View {
let origin = store.api.origin.absoluteString
let current = Servers.shared.record(origin)?.color
HStack(spacing: 4) {
ForEach(taskPalette.indices, id: \.self) { i in
Button {
Servers.shared.setColor(origin, i)
} label: {
Circle()
.fill(taskPalette[i])
.frame(width: 18, height: 18)
.padding(3)
.overlay(Circle().stroke(i == current ? Palette.text : .clear, lineWidth: 1.5))
.contentShape(Circle())
}
.buttonStyle(.plain)
}
}
.padding(.top, 8)
}
}
private struct SyncState: View {
var body: some View {
TimelineView(.periodic(from: .now, by: 30)) { _ in
if let sync = Servers.shared.sync {
if sync.ok {
Text("This Mac shares its servers, window colours, drafts and task visits through iCloud. Last synced \(ago(sync.at)).")
.fixedSize(horizontal: false, vertical: true)
} else {
Text("iCloud sync is failing on this Mac, so changes made here aren't reaching your other Macs. Failed \(ago(sync.at)): \(sync.error ?? "")")
.foregroundStyle(Palette.danger)
.textSelection(.enabled)
.fixedSize(horizontal: false, vertical: true)
}
} else {
Text("No iCloud sync has finished since byre started.").foregroundStyle(Palette.muted)
}
}
}
}
2026-10-17 07:26:18 -07:00
private struct DeviceNotifications: View {
@ViewState private var status: UNAuthorizationStatus?
@ViewState private var asking = false
var body: some View {
Group {
switch status {
case .none:
EmptyView()
case .denied:
Text("Notifications are blocked for byre in System Settings.").foregroundStyle(Palette.muted)
case .authorized, .provisional:
Text("This Mac shows notifications while byre is running.")
default:
VStack(alignment: .leading, spacing: 8) {
Text("This Mac doesn't show notifications from byre.")
Button("Turn on for this Mac") {
asking = true
Task {
_ = try? await UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound])
await read()
asking = false
}
}
.buttonStyle(WebButtonStyle())
.disabled(asking)
}
}
}
.padding(.bottom, 8)
.task {
await read()
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
Task {
await read()
}
}
}
private func read() async {
status = await UNUserNotificationCenter.current().notificationSettings().authorizationStatus
}
}
private struct PrefsBox: View {
@Environment(Store.self) private var store
@Environment(Overlays.self) private var overlays
var body: some View {
if let me = store.me {
VStack(alignment: .leading, spacing: 4) {
Text("Notify me about")
.foregroundStyle(Palette.muted)
.padding(.horizontal, 4)
.background(Palette.bg)
.offset(y: -18)
.padding(.bottom, -18)
ForEach(kinds, id: \.label) { kind in
Toggle(kind.label, isOn: Binding(get: { me.notificationPrefs[keyPath: kind.key] }, set: { on in
var prefs = me.notificationPrefs
prefs[keyPath: kind.key] = on
Task {
do {
store.me = try await store.api.client.updateNotificationPrefs(body: .json(prefs)).ok.body.json
} catch {
overlays.showAlert(errorText(error))
}
}
}))
.toggleStyle(.checkbox)
.tint(Palette.accent)
}
}
.padding(.horizontal, 12)
.padding(.top, 8)
.padding(.bottom, 10)
.frame(maxWidth: .infinity, alignment: .leading)
2026-10-18 16:24:58 -07:00
.background(RoundedRectangle(cornerRadius: 8).stroke(Palette.border))
2026-10-17 07:26:18 -07:00
.padding(.top, 16)
}
}
}
2026-10-18 22:56:33 -07:00
private struct ModelMenu: View {
let current: String
let help: String
let pick: (String) async throws -> User
@Environment(Store.self) private var store
@Environment(Overlays.self) private var overlays
var body: some View {
Menu {
ForEach(models, id: \.self) { m in
Button(m) {
Task {
do {
2026-10-18 22:56:33 -07:00
store.me = try await pick(m)
} catch {
overlays.showAlert(errorText(error))
}
}
}
}
} label: {
HStack(spacing: 8) {
2026-10-18 22:56:33 -07:00
Text(current)
Image(systemName: "chevron.up.chevron.down")
.font(.system(size: 9, weight: .semibold))
}
.foregroundStyle(Palette.text)
.modifier(FieldStyle())
.contentShape(Rectangle())
}
.menuStyle(.button)
.buttonStyle(.plain)
.menuIndicator(.hidden)
.fixedSize()
2026-10-18 22:56:33 -07:00
.help(help)
.padding(.top, 8)
}
}