Branchwell

Jobley / app / Sources / JobleyUI / ReviewViews.swift

1import SwiftUI
2import JobleyKit
3
4/// One post under review: the post in full, the rules as a checklist ticked by hand, the decisions,
5/// notes and history. Used by the Mac desk and the iPhone Review tab.
6public struct ReviewDetail: View {
7 @ObservedObject var model: CuratorModel
8 let item: JobReviewItem
9 let labels: Labels
10 var allowsEdit = true
11
12 @State private var ticked: Set<String> = []
13 @State private var sheet: Sheet?
14 @State private var note = ""
15 @Environment(\.openURL) private var openURL
16
17 enum Sheet: String, Identifiable { case sendBack, unpublish, edit; var id: String { rawValue } }
18
19 public init(model: CuratorModel, item: JobReviewItem, labels: Labels, allowsEdit: Bool = true) {
20 self.model = model; self.item = item; self.labels = labels; self.allowsEdit = allowsEdit
21 }
22
23 var job: Job { item.job }
24 var status: String { job.status ?? "pending" }
25
26 public var body: some View {
27 ScrollView {
28 VStack(alignment: .leading, spacing: 22) {
29 HStack {
30 StatusPill(status)
31 Text("Posted \(APIDate.full(job.createdAt))").font(.caption).foregroundStyle(Theme.muted)
32 Spacer()
33 if item.candidateCount > 0 {
34 Label("\(item.candidateCount) asked to be put forward", systemImage: "person.2").font(.caption).foregroundStyle(Theme.ink2)
35 }
36 }
37 actions
38 if let reason = job.review?.reason, status == "rejected" || status == "unpublished" {
39 Label("Reason: \(reason)", systemImage: "text.quote").font(.callout).foregroundStyle(Theme.danger)
40 }
41 JobBody(job: job, labels: labels)
42 if let contact = job.contact {
43 GroupBox {
44 VStack(alignment: .leading, spacing: 4) {
45 Text(contact.name).font(.body.weight(.semibold))
46 Text(contact.email).font(.callout).textSelection(.enabled).foregroundStyle(Theme.ink2)
47 }.frame(maxWidth: .infinity, alignment: .leading)
48 } label: {
49 Label("Poster (private, never shown on the site)", systemImage: "lock").font(.caption.weight(.semibold)).foregroundStyle(Theme.muted)
50 }
51 }
52 rules
53 notes
54 history
55 }
56 .padding(24)
57 .frame(maxWidth: 760, alignment: .leading)
58 .frame(maxWidth: .infinity, alignment: .leading)
59 }
60 .background(Theme.background)
61 .disabled(model.busy)
62 .sheet(item: $sheet) { which in
63 switch which {
64 case .sendBack:
65 ReasonSheet(title: "Send back “\(job.title)”", detail: "Say which rule it misses, in plain words. We'll open a mail draft to the poster with it; you send it.",
66 action: "Send back", required: true) { reason in
67 if await model.sendBack(job, reason: reason) {
68 if let mail = MailDraft.sendBack(job, reason: reason) { openURL(mail) }
69 return true
70 }
71 return false
72 }
73 case .unpublish:
74 ReasonSheet(title: "Unpublish “\(job.title)”", detail: "It comes off the board now. A reason helps the next person who reads it.",
75 action: "Unpublish", required: false) { reason in await model.unpublish(job, reason: reason) }
76 case .edit:
77 EditJobSheet(job: job, labels: labels) { edit in await model.save(job, edit: edit) }
78 }
79 }
80 }
81
82 @ViewBuilder var actions: some View {
83 FlowLayout(spacing: 10) {
84 switch status {
85 case "pending", "rejected":
86 Button { Task { _ = await model.publishNow(job) } } label: { Label("Publish", systemImage: "checkmark.seal") }
87 .buttonStyle(PrimaryButtonStyle()).accessibilityIdentifier("reviewPublish")
88 if status == "pending" {
89 Button { sheet = .sendBack } label: { Label("Send back", systemImage: "arrow.uturn.backward") }
90 .buttonStyle(SecondaryButtonStyle()).accessibilityIdentifier("reviewSendBack")
91 }
92 Button { Task { _ = await model.approveOnly(job) } } label: { Text("Approve, publish later") }
93 .buttonStyle(SecondaryButtonStyle())
94 case "approved":
95 Button { Task { _ = await model.publish(job) } } label: { Label("Publish", systemImage: "megaphone") }
96 .buttonStyle(PrimaryButtonStyle())
97 Button { sheet = .sendBack } label: { Label("Send back", systemImage: "arrow.uturn.backward") }
98 .buttonStyle(SecondaryButtonStyle())
99 case "published":
100 Button { sheet = .unpublish } label: { Label("Unpublish", systemImage: "eye.slash") }
101 .buttonStyle(SecondaryButtonStyle())
102 Link(destination: URL(string: job.url) ?? AppLinks.website) { Label("Open on the site", systemImage: "safari") }
103 .buttonStyle(SecondaryButtonStyle())
104 case "unpublished":
105 Button { Task { _ = await model.publish(job) } } label: { Label("Publish again", systemImage: "megaphone") }
106 .buttonStyle(PrimaryButtonStyle())
107 default:
108 EmptyView()
109 }
110 if allowsEdit && status != "closed" {
111 Button { sheet = .edit } label: { Label("Edit wording", systemImage: "pencil") }
112 .buttonStyle(SecondaryButtonStyle()).accessibilityIdentifier("reviewEdit")
113 }
114 }
115 }
116
117 var rules: some View {
118 VStack(alignment: .leading, spacing: 10) {
119 HStack {
120 Text("The rules").font(Theme.display(19)).foregroundStyle(Theme.ink)
121 Spacer()
122 Text("\(ticked.count) of \(HouseRules.all.count) ticked").font(Theme.mono(11)).foregroundStyle(Theme.muted)
123 }
124 Text("Tick each one you've checked yourself. Nothing here is checked for you.").font(.caption).foregroundStyle(Theme.muted)
125 ForEach(HouseRules.all) { rule in
126 Button {
127 if ticked.contains(rule.id) { ticked.remove(rule.id) } else { ticked.insert(rule.id) }
128 } label: {
129 HStack(alignment: .top, spacing: 10) {
130 Image(systemName: ticked.contains(rule.id) ? "checkmark.square.fill" : "square")
131 .foregroundStyle(ticked.contains(rule.id) ? Theme.accent : Theme.muted).font(.title3)
132 VStack(alignment: .leading, spacing: 2) {
133 Text(rule.title).font(.callout.weight(.semibold)).foregroundStyle(Theme.ink)
134 Text(rule.detail).font(.caption).foregroundStyle(Theme.ink2).fixedSize(horizontal: false, vertical: true)
135 }
136 }
137 .frame(maxWidth: .infinity, alignment: .leading)
138 .contentShape(Rectangle())
139 }
140 .buttonStyle(.plain)
141 }
142 }
143 .padding(16)
144 .background(Theme.surface, in: RoundedRectangle(cornerRadius: 14))
145 .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(Theme.line))
146 }
147
148 var notes: some View {
149 VStack(alignment: .leading, spacing: 8) {
150 Text("Notes").font(Theme.display(19)).foregroundStyle(Theme.ink)
151 HStack(alignment: .bottom) {
152 TextField("A note for whoever reads this next", text: $note, axis: .vertical)
153 .textFieldStyle(.roundedBorder).lineLimit(1...4)
154 Button("Add") { Task { if await model.addNote(job, note: note) { note = "" } } }
155 .disabled(note.trimmingCharacters(in: .whitespaces).isEmpty)
156 }
157 }
158 }
159
160 var history: some View {
161 VStack(alignment: .leading, spacing: 8) {
162 Text("History").font(Theme.display(19)).foregroundStyle(Theme.ink)
163 if item.activity.isEmpty { Text("Nothing yet.").font(.caption).foregroundStyle(Theme.muted) }
164 ForEach(item.activity.reversed()) { entry in
165 HStack(alignment: .top, spacing: 10) {
166 Image(systemName: entry.client == "mcp" ? "cpu" : "person").foregroundStyle(Theme.muted).frame(width: 18)
167 VStack(alignment: .leading, spacing: 2) {
168 Text("\(entry.actor) · \(entry.action)\(entry.client.map { " · \($0)" } ?? "")").font(.caption.weight(.semibold)).foregroundStyle(Theme.ink2)
169 if let detail = entry.detail, !detail.isEmpty { Text(detail).font(.caption).foregroundStyle(Theme.ink2).textSelection(.enabled) }
170 Text(APIDate.full(entry.createdAt)).font(.caption2).foregroundStyle(Theme.muted)
171 }
172 }
173 }
174 }
175 }
176}
177
178/// Asks for a reason, then runs the action. A required reason must be at least three characters.
179public struct ReasonSheet: View {
180 let title: String, detail: String, action: String
181 let required: Bool
182 let run: (String) async -> Bool
183 @State private var reason = ""
184 @State private var working = false
185 @Environment(\.dismiss) private var dismiss
186
187 public init(title: String, detail: String, action: String, required: Bool, run: @escaping (String) async -> Bool) {
188 self.title = title; self.detail = detail; self.action = action; self.required = required; self.run = run
189 }
190
191 var valid: Bool { !required || reason.trimmingCharacters(in: .whitespacesAndNewlines).count >= 3 }
192
193 public var body: some View {
194 VStack(alignment: .leading, spacing: 14) {
195 Text(title).font(Theme.display(20))
196 Text(detail).font(.callout).foregroundStyle(Theme.ink2)
197 TextEditor(text: $reason)
198 .font(.body).frame(minHeight: 120)
199 .scrollContentBackground(.hidden)
200 .padding(6).background(Theme.surface2, in: RoundedRectangle(cornerRadius: 8))
201 .accessibilityIdentifier("reasonField")
202 HStack {
203 Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
204 Spacer()
205 Button(action) {
206 working = true
207 Task { if await run(reason.trimmingCharacters(in: .whitespacesAndNewlines)) { dismiss() }; working = false }
208 }
209 .buttonStyle(PrimaryButtonStyle())
210 .disabled(!valid || working)
211 }
212 }
213 .padding(22)
214 #if os(macOS)
215 .frame(width: 460)
216 #endif
217 }
218}
219
220/// Edit a post's wording before it is published. Contact details and status are never edited here.
221public struct EditJobSheet: View {
222 let job: Job
223 let labels: Labels
224 let save: (JobEdit) async -> Bool
225 @State private var edit: JobEdit
226 @State private var payMin: String
227 @State private var payMax: String
228 @Environment(\.dismiss) private var dismiss
229
230 public init(job: Job, labels: Labels, save: @escaping (JobEdit) async -> Bool) {
231 self.job = job; self.labels = labels; self.save = save
232 _edit = State(initialValue: JobEdit(job))
233 _payMin = State(initialValue: String(job.pay.min)); _payMax = State(initialValue: String(job.pay.max))
234 }
235
236 public var body: some View {
237 NavigationStack {
238 Form {
239 Section("The job") {
240 TextField("Job title", text: $edit.title)
241 TextField("Company", text: $edit.company)
242 TextField("Company website", text: $edit.companyURL)
243 TextField("Location", text: $edit.location)
244 Picker("Workplace", selection: $edit.workplace) { ForEach(Meta.workplaceOrder, id: \.self) { Text(labels.workplace($0)).tag($0) } }
245 Picker("Job type", selection: $edit.employmentType) { ForEach(Meta.employmentTypeOrder, id: \.self) { Text(labels.type($0)).tag($0) } }
246 Picker("Category", selection: $edit.category) { ForEach(Meta.categoryOrder, id: \.self) { Text(labels.category($0)).tag($0) } }
247 }
248 Section("Pay") {
249 TextField("Lowest", text: $payMin)
250 TextField("Highest", text: $payMax)
251 TextField("Currency", text: $edit.payCurrency)
252 Picker("Per", selection: $edit.payPeriod) { ForEach(Meta.payPeriodOrder, id: \.self) { Text($0).tag($0) } }
253 }
254 Section("Description") {
255 TextEditor(text: $edit.description).frame(minHeight: 200)
256 TextField("Company's own job page", text: $edit.applyURL)
257 }
258 }
259 .formStyle(.grouped)
260 .navigationTitle("Edit wording")
261 .toolbar {
262 ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } }
263 ToolbarItem(placement: .confirmationAction) {
264 Button("Save") {
265 edit.payMin = Int(payMin.filter(\.isNumber)) ?? edit.payMin
266 edit.payMax = Int(payMax.filter(\.isNumber)) ?? edit.payMax
267 Task { if await save(edit) { dismiss() } }
268 }
269 }
270 }
271 }
272 #if os(macOS)
273 .frame(width: 560, height: 680)
274 #endif
275 }
276}