Branchwell

Jobley / app · jobley.ai / Apps / Mac / MacApp.swift

1import SwiftUI
2import JobleyKit
3import JobleyUI
4
5/// `Jobley --mcp` runs the MCP server on stdin/stdout instead of the app.
6@main
7enum Launcher {
8 static func main() {
9 if CommandLine.arguments.contains("--mcp") {
10 let done = DispatchSemaphore(value: 0)
11 Task.detached { await MCPServer.live().run(); done.signal() }
12 done.wait()
13 return
14 }
15 JobleyMacApp.main()
16 }
17}
18
19struct JobleyMacApp: App {
20 @StateObject private var desk = DeskModel()
21 @StateObject private var updates = AppUpdates()
22
23 var body: some Scene {
24 Window("Jobley", id: "desk") {
25 DeskView()
26 .environmentObject(desk)
27 .frame(minWidth: 1080, minHeight: 680)
28 .tint(Theme.accent)
29 }
30 .defaultSize(width: 1320, height: 840)
31 .commands {
32 CommandGroup(after: .appInfo) { CheckForUpdatesButton(updates: updates) }
33 CommandGroup(after: .toolbar) {
34 Button("Refresh") { Task { await desk.refresh() } }.keyboardShortcut("r")
35 }
36 }
37
38 Settings {
39 MacSettings(updates: updates)
40 .environmentObject(desk)
41 .tint(Theme.accent)
42 }
43 }
44}
45
46/// Everything the desk shows. The passkey sign-in (a session) comes from the Keychain.
47@MainActor
48final class DeskModel: ObservableObject {
49 enum Section: Hashable {
50 case queue(CuratorModel.Queue)
51 case candidates, moderators, board, post
52 }
53
54 let curator: CuratorModel
55 let board: BoardModel
56 let candidates: CandidatesModel
57 let moderators: ModeratorsModel
58 @Published var section: Section = .queue(.pending)
59 @Published var selectedJobID: String?
60 @Published var selectedCandidateID: String?
61 private var purged = false
62
63 init() {
64 let api = APIClient(client: .mac, token: TokenStore.load())
65 let curator = CuratorModel(api: api)
66 self.curator = curator
67 board = BoardModel(api: APIClient(client: .mac))
68 candidates = CandidatesModel { curator.api }
69 moderators = ModeratorsModel { curator.api }
70 #if DEBUG
71 if let start = ProcessInfo.processInfo.environment["JOBLEY_SECTION"] { section = Self.section(named: start) }
72 selectedJobID = ProcessInfo.processInfo.environment["JOBLEY_JOB"]
73 selectedCandidateID = ProcessInfo.processInfo.environment["JOBLEY_CANDIDATE"]
74 #endif
75 }
76
77 static func section(named name: String) -> Section {
78 switch name {
79 case "candidates": .candidates
80 case "moderators": .moderators
81 case "board": .board
82 case "post": .post
83 default: .queue(CuratorModel.Queue(rawValue: name) ?? .pending)
84 }
85 }
86
87 func start() async {
88 await board.loadMeta()
89 await curator.start()
90 await purgeOnce()
91 await refreshSection()
92 }
93
94 func refresh() async {
95 await curator.refreshAll()
96 await refreshSection()
97 }
98
99 func refreshSection() async {
100 switch section {
101 case .board:
102 await board.reload()
103 #if DEBUG
104 if ProcessInfo.processInfo.environment["JOBLEY_JOB"] == "first" { selectedJobID = board.jobs.first?.id }
105 #endif
106 case .candidates where curator.canSeeCandidates:
107 await candidates.reload()
108 await candidates.open(selectedCandidateID)
109 case .moderators where curator.me?.isCurator == true: await moderators.reload()
110 case .queue:
111 if selectedJobID == nil, case .queue(let queue) = section { selectedJobID = curator.jobs(queue).first?.id }
112 await curator.open(selectedJobID)
113 default: break
114 }
115 }
116
117 /// The privacy promise: posts off the board, candidates and applications are deleted
118 /// 12 months after their last change. The curator's desk runs it when it opens.
119 private func purgeOnce() async {
120 guard !purged, curator.me?.isCurator == true else { return }
121 purged = true
122 _ = try? await curator.api.purge()
123 }
124}