import SwiftUI import JobleyKit /// One post under review: the post in full, the rules as a checklist ticked by hand, the decisions, /// notes and history. Used by the Mac desk and the iPhone Review tab. public struct ReviewDetail: View { @ObservedObject var model: CuratorModel let item: JobReviewItem let labels: Labels var allowsEdit = true @State private var ticked: Set = [] @State private var sheet: Sheet? @State private var note = "" @Environment(\.openURL) private var openURL enum Sheet: String, Identifiable { case sendBack, unpublish, edit; var id: String { rawValue } } public init(model: CuratorModel, item: JobReviewItem, labels: Labels, allowsEdit: Bool = true) { self.model = model; self.item = item; self.labels = labels; self.allowsEdit = allowsEdit } var job: Job { item.job } var status: String { job.status ?? "pending" } public var body: some View { ScrollView { VStack(alignment: .leading, spacing: 22) { HStack { StatusPill(status) Text("Posted \(APIDate.full(job.createdAt))").font(.caption).foregroundStyle(Theme.muted) Spacer() if item.candidateCount > 0 { Label("\(item.candidateCount) asked to be put forward", systemImage: "person.2").font(.caption).foregroundStyle(Theme.ink2) } } actions if let reason = job.review?.reason, status == "rejected" || status == "unpublished" { Label("Reason: \(reason)", systemImage: "text.quote").font(.callout).foregroundStyle(Theme.danger) } JobBody(job: job, labels: labels) if let contact = job.contact { GroupBox { VStack(alignment: .leading, spacing: 4) { Text(contact.name).font(.body.weight(.semibold)) Text(contact.email).font(.callout).textSelection(.enabled).foregroundStyle(Theme.ink2) }.frame(maxWidth: .infinity, alignment: .leading) } label: { Label("Poster (private, never shown on the site)", systemImage: "lock").font(.caption.weight(.semibold)).foregroundStyle(Theme.muted) } } rules notes history } .padding(24) .frame(maxWidth: 760, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading) } .background(Theme.background) .disabled(model.busy) .sheet(item: $sheet) { which in switch which { case .sendBack: 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.", action: "Send back", required: true) { reason in if await model.sendBack(job, reason: reason) { if let mail = MailDraft.sendBack(job, reason: reason) { openURL(mail) } return true } return false } case .unpublish: ReasonSheet(title: "Unpublish “\(job.title)”", detail: "It comes off the board now. A reason helps the next person who reads it.", action: "Unpublish", required: false) { reason in await model.unpublish(job, reason: reason) } case .edit: EditJobSheet(job: job, labels: labels) { edit in await model.save(job, edit: edit) } } } } @ViewBuilder var actions: some View { FlowLayout(spacing: 10) { switch status { case "pending", "rejected": Button { Task { _ = await model.publishNow(job) } } label: { Label("Publish", systemImage: "checkmark.seal") } .buttonStyle(PrimaryButtonStyle()).accessibilityIdentifier("reviewPublish") if status == "pending" { Button { sheet = .sendBack } label: { Label("Send back", systemImage: "arrow.uturn.backward") } .buttonStyle(SecondaryButtonStyle()).accessibilityIdentifier("reviewSendBack") } Button { Task { _ = await model.approveOnly(job) } } label: { Text("Approve, publish later") } .buttonStyle(SecondaryButtonStyle()) case "approved": Button { Task { _ = await model.publish(job) } } label: { Label("Publish", systemImage: "megaphone") } .buttonStyle(PrimaryButtonStyle()) Button { sheet = .sendBack } label: { Label("Send back", systemImage: "arrow.uturn.backward") } .buttonStyle(SecondaryButtonStyle()) case "published": Button { sheet = .unpublish } label: { Label("Unpublish", systemImage: "eye.slash") } .buttonStyle(SecondaryButtonStyle()) Link(destination: URL(string: job.url) ?? AppLinks.website) { Label("Open on the site", systemImage: "safari") } .buttonStyle(SecondaryButtonStyle()) case "unpublished": Button { Task { _ = await model.publish(job) } } label: { Label("Publish again", systemImage: "megaphone") } .buttonStyle(PrimaryButtonStyle()) default: EmptyView() } if allowsEdit && status != "closed" { Button { sheet = .edit } label: { Label("Edit wording", systemImage: "pencil") } .buttonStyle(SecondaryButtonStyle()).accessibilityIdentifier("reviewEdit") } } } var rules: some View { VStack(alignment: .leading, spacing: 10) { HStack { Text("The rules").font(Theme.display(19)).foregroundStyle(Theme.ink) Spacer() Text("\(ticked.count) of \(HouseRules.all.count) ticked").font(Theme.mono(11)).foregroundStyle(Theme.muted) } Text("Tick each one you've checked yourself. Nothing here is checked for you.").font(.caption).foregroundStyle(Theme.muted) ForEach(HouseRules.all) { rule in Button { if ticked.contains(rule.id) { ticked.remove(rule.id) } else { ticked.insert(rule.id) } } label: { HStack(alignment: .top, spacing: 10) { Image(systemName: ticked.contains(rule.id) ? "checkmark.square.fill" : "square") .foregroundStyle(ticked.contains(rule.id) ? Theme.accent : Theme.muted).font(.title3) VStack(alignment: .leading, spacing: 2) { Text(rule.title).font(.callout.weight(.semibold)).foregroundStyle(Theme.ink) Text(rule.detail).font(.caption).foregroundStyle(Theme.ink2).fixedSize(horizontal: false, vertical: true) } } .frame(maxWidth: .infinity, alignment: .leading) .contentShape(Rectangle()) } .buttonStyle(.plain) } } .padding(16) .background(Theme.surface, in: RoundedRectangle(cornerRadius: 14)) .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(Theme.line)) } var notes: some View { VStack(alignment: .leading, spacing: 8) { Text("Notes").font(Theme.display(19)).foregroundStyle(Theme.ink) HStack(alignment: .bottom) { TextField("A note for whoever reads this next", text: $note, axis: .vertical) .textFieldStyle(.roundedBorder).lineLimit(1...4) Button("Add") { Task { if await model.addNote(job, note: note) { note = "" } } } .disabled(note.trimmingCharacters(in: .whitespaces).isEmpty) } } } var history: some View { VStack(alignment: .leading, spacing: 8) { Text("History").font(Theme.display(19)).foregroundStyle(Theme.ink) if item.activity.isEmpty { Text("Nothing yet.").font(.caption).foregroundStyle(Theme.muted) } ForEach(item.activity.reversed()) { entry in HStack(alignment: .top, spacing: 10) { Image(systemName: entry.client == "mcp" ? "cpu" : "person").foregroundStyle(Theme.muted).frame(width: 18) VStack(alignment: .leading, spacing: 2) { Text("\(entry.actor) · \(entry.action)\(entry.client.map { " · \($0)" } ?? "")").font(.caption.weight(.semibold)).foregroundStyle(Theme.ink2) if let detail = entry.detail, !detail.isEmpty { Text(detail).font(.caption).foregroundStyle(Theme.ink2).textSelection(.enabled) } Text(APIDate.full(entry.createdAt)).font(.caption2).foregroundStyle(Theme.muted) } } } } } } /// Asks for a reason, then runs the action. A required reason must be at least three characters. public struct ReasonSheet: View { let title: String, detail: String, action: String let required: Bool let run: (String) async -> Bool @State private var reason = "" @State private var working = false @Environment(\.dismiss) private var dismiss public init(title: String, detail: String, action: String, required: Bool, run: @escaping (String) async -> Bool) { self.title = title; self.detail = detail; self.action = action; self.required = required; self.run = run } var valid: Bool { !required || reason.trimmingCharacters(in: .whitespacesAndNewlines).count >= 3 } public var body: some View { VStack(alignment: .leading, spacing: 14) { Text(title).font(Theme.display(20)) Text(detail).font(.callout).foregroundStyle(Theme.ink2) TextEditor(text: $reason) .font(.body).frame(minHeight: 120) .scrollContentBackground(.hidden) .padding(6).background(Theme.surface2, in: RoundedRectangle(cornerRadius: 8)) .accessibilityIdentifier("reasonField") HStack { Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction) Spacer() Button(action) { working = true Task { if await run(reason.trimmingCharacters(in: .whitespacesAndNewlines)) { dismiss() }; working = false } } .buttonStyle(PrimaryButtonStyle()) .disabled(!valid || working) } } .padding(22) #if os(macOS) .frame(width: 460) #endif } } /// Edit a post's wording before it is published. Contact details and status are never edited here. public struct EditJobSheet: View { let job: Job let labels: Labels let save: (JobEdit) async -> Bool @State private var edit: JobEdit @State private var payMin: String @State private var payMax: String @Environment(\.dismiss) private var dismiss public init(job: Job, labels: Labels, save: @escaping (JobEdit) async -> Bool) { self.job = job; self.labels = labels; self.save = save _edit = State(initialValue: JobEdit(job)) _payMin = State(initialValue: String(job.pay.min)); _payMax = State(initialValue: String(job.pay.max)) } public var body: some View { NavigationStack { Form { Section("The job") { TextField("Job title", text: $edit.title) TextField("Company", text: $edit.company) TextField("Company website", text: $edit.companyURL) TextField("Location", text: $edit.location) Picker("Workplace", selection: $edit.workplace) { ForEach(Meta.workplaceOrder, id: \.self) { Text(labels.workplace($0)).tag($0) } } Picker("Job type", selection: $edit.employmentType) { ForEach(Meta.employmentTypeOrder, id: \.self) { Text(labels.type($0)).tag($0) } } Picker("Category", selection: $edit.category) { ForEach(Meta.categoryOrder, id: \.self) { Text(labels.category($0)).tag($0) } } } Section("Pay") { TextField("Lowest", text: $payMin) TextField("Highest", text: $payMax) TextField("Currency", text: $edit.payCurrency) Picker("Per", selection: $edit.payPeriod) { ForEach(Meta.payPeriodOrder, id: \.self) { Text($0).tag($0) } } } Section("Description") { TextEditor(text: $edit.description).frame(minHeight: 200) TextField("Company's own job page", text: $edit.applyURL) } } .formStyle(.grouped) .navigationTitle("Edit wording") .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } } ToolbarItem(placement: .confirmationAction) { Button("Save") { edit.payMin = Int(payMin.filter(\.isNumber)) ?? edit.payMin edit.payMax = Int(payMax.filter(\.isNumber)) ?? edit.payMax Task { if await save(edit) { dismiss() } } } } } } #if os(macOS) .frame(width: 560, height: 680) #endif } }