Branchwell

Jobley / app / Sources / JobleyUI / Components.swift

1import SwiftUI
2import JobleyKit
3
4/// Labels for the API's enum values, from /api/meta (or the built-in copy until it loads).
5public struct Labels: Sendable {
6 public var meta: Meta
7 public init(_ meta: Meta = .fallback) { self.meta = meta }
8 public func workplace(_ v: String) -> String { meta.workplaces[v] ?? Meta.fallback.workplaces[v] ?? v }
9 public func type(_ v: String) -> String { meta.employmentTypes[v] ?? Meta.fallback.employmentTypes[v] ?? v }
10 public func category(_ v: String) -> String { meta.categories[v] ?? Meta.fallback.categories[v] ?? v }
11}
12
13public struct Chip: View {
14 let text: String
15 public init(_ text: String) { self.text = text }
16 public var body: some View {
17 Text(text)
18 .font(.caption)
19 .foregroundStyle(Theme.ink2)
20 .padding(.horizontal, 9).padding(.vertical, 4)
21 .overlay(Capsule().strokeBorder(Theme.line))
22 }
23}
24
25public struct StatusPill: View {
26 let status: String
27 public init(_ status: String) { self.status = status }
28 var color: Color {
29 switch status {
30 case "published", "introduced": Theme.accent
31 case "pending", "new": Theme.sealText
32 case "rejected", "declined", "unpublished": Theme.danger
33 default: Theme.muted
34 }
35 }
36 public var body: some View {
37 Text(JobStatus(rawValue: status)?.label ?? status.capitalized)
38 .font(.caption.weight(.semibold))
39 .foregroundStyle(color)
40 .padding(.horizontal, 8).padding(.vertical, 3)
41 .background(color.opacity(0.12), in: Capsule())
42 }
43}
44
45/// The "Reviewed by a person" seal shown on published jobs.
46public struct ReviewedSeal: View {
47 public init() {}
48 public var body: some View {
49 Label("Reviewed by a person", systemImage: "checkmark.seal.fill")
50 .font(.caption.weight(.semibold))
51 .foregroundStyle(Theme.sealText)
52 .symbolRenderingMode(.hierarchical)
53 }
54}
55
56/// One job in a list: title, company, pay, the facts as chips.
57public struct JobRow: View {
58 let job: Job
59 let labels: Labels
60 var showStatus = false
61 var saved = false
62
63 public init(job: Job, labels: Labels, showStatus: Bool = false, saved: Bool = false) {
64 self.job = job; self.labels = labels; self.showStatus = showStatus; self.saved = saved
65 }
66
67 public var body: some View {
68 VStack(alignment: .leading, spacing: 6) {
69 HStack(alignment: .firstTextBaseline) {
70 Text(job.title).font(Theme.display(17)).foregroundStyle(Theme.ink).lineLimit(2)
71 Spacer(minLength: 8)
72 if saved { Image(systemName: "bookmark.fill").foregroundStyle(Theme.accent).font(.caption) }
73 if showStatus, let status = job.status { StatusPill(status) }
74 }
75 Text(job.company).font(.subheadline).foregroundStyle(Theme.ink2)
76 Text(job.pay.text).font(Theme.mono(12)).foregroundStyle(Theme.accent)
77 HStack(spacing: 6) {
78 Chip(labels.workplace(job.workplace))
79 Chip(job.location).lineLimit(1)
80 Chip(labels.type(job.employmentType))
81 }
82 if showStatus {
83 Text("Posted \(APIDate.short(job.createdAt))").font(.caption2).foregroundStyle(Theme.muted)
84 }
85 }
86 .padding(.vertical, 6)
87 .accessibilityElement(children: .combine)
88 }
89}
90
91/// The body of a job as people read it: facts, pay, description, links.
92public struct JobBody: View {
93 let job: Job
94 let labels: Labels
95 public init(job: Job, labels: Labels) { self.job = job; self.labels = labels }
96
97 public var body: some View {
98 VStack(alignment: .leading, spacing: 14) {
99 VStack(alignment: .leading, spacing: 6) {
100 Text(job.title).font(Theme.display(28)).foregroundStyle(Theme.ink).fixedSize(horizontal: false, vertical: true)
101 HStack(spacing: 6) {
102 Text(job.company).font(.title3).foregroundStyle(Theme.ink2)
103 if let link = job.companyURL.flatMap(URL.init(string:)) {
104 Link(destination: link) { Image(systemName: "arrow.up.right.square") }.foregroundStyle(Theme.accent)
105 }
106 }
107 }
108 Text(job.pay.text).font(Theme.mono(15).weight(.semibold)).foregroundStyle(Theme.accent)
109 FlowChips(items: [labels.workplace(job.workplace), job.location, labels.type(job.employmentType), labels.category(job.category)])
110 if job.status == nil || job.status == "published" {
111 HStack(spacing: 10) {
112 ReviewedSeal()
113 if let published = job.publishedAt { Text("Published \(APIDate.short(published))").font(.caption).foregroundStyle(Theme.muted) }
114 }
115 }
116 Divider()
117 Text(job.description)
118 .font(.body).foregroundStyle(Theme.ink)
119 .lineSpacing(3)
120 .textSelection(.enabled)
121 .fixedSize(horizontal: false, vertical: true)
122 if let apply = job.applyURL.flatMap(URL.init(string:)) {
123 Link(destination: apply) { Label("The company's own job page", systemImage: "arrow.up.right") }
124 .font(.subheadline).foregroundStyle(Theme.accent)
125 }
126 }
127 }
128}
129
130/// Chips that wrap onto more lines.
131public struct FlowChips: View {
132 let items: [String]
133 public init(items: [String]) { self.items = items }
134 public var body: some View {
135 FlowLayout(spacing: 6) { ForEach(items, id: \.self) { Chip($0) } }
136 }
137}
138
139public struct FlowLayout: Layout {
140 var spacing: CGFloat
141 public init(spacing: CGFloat = 6) { self.spacing = spacing }
142
143 public func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
144 let width = proposal.width ?? .infinity
145 var x: CGFloat = 0, y: CGFloat = 0, rowHeight: CGFloat = 0, widest: CGFloat = 0
146 for view in subviews {
147 let size = view.sizeThatFits(.unspecified)
148 if x > 0 && x + size.width > width { x = 0; y += rowHeight + spacing; rowHeight = 0 }
149 x += size.width + spacing; rowHeight = max(rowHeight, size.height); widest = max(widest, x - spacing)
150 }
151 return CGSize(width: min(widest, width), height: y + rowHeight)
152 }
153
154 public func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
155 var x = bounds.minX, y = bounds.minY, rowHeight: CGFloat = 0
156 for view in subviews {
157 let size = view.sizeThatFits(.unspecified)
158 if x > bounds.minX && x + size.width > bounds.maxX { x = bounds.minX; y += rowHeight + spacing; rowHeight = 0 }
159 view.place(at: CGPoint(x: x, y: y), proposal: .unspecified)
160 x += size.width + spacing; rowHeight = max(rowHeight, size.height)
161 }
162 }
163}
164
165/// A plain message for empty, loading and error states.
166public struct StateMessage: View {
167 let title: String, detail: String, systemImage: String
168 var retry: (() -> Void)?
169 public init(_ title: String, detail: String, systemImage: String, retry: (() -> Void)? = nil) {
170 self.title = title; self.detail = detail; self.systemImage = systemImage; self.retry = retry
171 }
172 public var body: some View {
173 ContentUnavailableView {
174 Label(title, systemImage: systemImage)
175 } description: {
176 Text(detail)
177 } actions: {
178 if let retry { Button("Try again", action: retry).buttonStyle(.bordered) }
179 }
180 }
181}
182
183public struct PrimaryButtonStyle: ButtonStyle {
184 var role: ButtonRole?
185 public init(role: ButtonRole? = nil) { self.role = role }
186 public func makeBody(configuration: Configuration) -> some View {
187 configuration.label
188 .font(.body.weight(.semibold))
189 .foregroundStyle(.white)
190 .padding(.horizontal, 16).padding(.vertical, 9)
191 .background(role == .destructive ? Theme.danger : Theme.fill, in: Capsule())
192 .opacity(configuration.isPressed ? 0.8 : 1)
193 }
194}
195
196public struct SecondaryButtonStyle: ButtonStyle {
197 public init() {}
198 public func makeBody(configuration: Configuration) -> some View {
199 configuration.label
200 .font(.body.weight(.medium))
201 .foregroundStyle(Theme.ink)
202 .padding(.horizontal, 14).padding(.vertical, 8)
203 .overlay(Capsule().strokeBorder(Theme.line, lineWidth: 1.2))
204 .contentShape(Capsule())
205 .opacity(configuration.isPressed ? 0.7 : 1)
206 }
207}
208
209/// Opens a mail draft for the person to read and send (the API sends no email in v1).
210public enum MailDraft {
211 public static func url(to: [String], subject: String, body: String) -> URL? {
212 var components = URLComponents()
213 components.scheme = "mailto"
214 components.path = to.joined(separator: ",")
215 components.queryItems = [.init(name: "subject", value: subject), .init(name: "body", value: body)]
216 return components.url
217 }
218
219 public static func sendBack(_ job: Job, reason: String) -> URL? {
220 guard let contact = job.contact else { return nil }
221 let body = """
222 Hi \(contact.name),
223
224 Thank you for posting “\(job.title)” on Jobley. We read every post before it goes on the board, and we couldn't publish this one yet:
225
226 \(reason)
227
228 If you can fix that, post it again at https://jobley.ai/post/ and we'll read it again.
229
230 The Jobley team
231 """
232 return url(to: [contact.email], subject: "Your Jobley post: \(job.title)", body: body)
233 }
234
235 public static func introduction(candidate: Candidate, job: Job, note: String) -> URL? {
236 guard let contact = job.contact else { return nil }
237 let body = """
238 Hi \(contact.name) and \(candidate.name),
239
240 \(note)
241
242 \(candidate.name): \(candidate.headline)\(candidate.links.isEmpty ? "" : "\n" + candidate.links.joined(separator: "\n"))\(candidate.resumeURL.map { "\nResume: \($0)" } ?? "")
243
244 \(contact.name) is hiring for “\(job.title)” at \(job.company): \(job.url)
245
246 We'll leave you both to it.
247
248 The Jobley team
249 """
250 return url(to: [contact.email, candidate.email], subject: "Introduction: \(candidate.name) for \(job.title)", body: body)
251 }
252}