import SwiftUI import JobleyKit /// Labels for the API's enum values, from /api/meta (or the built-in copy until it loads). public struct Labels: Sendable { public var meta: Meta public init(_ meta: Meta = .fallback) { self.meta = meta } public func workplace(_ v: String) -> String { meta.workplaces[v] ?? Meta.fallback.workplaces[v] ?? v } public func type(_ v: String) -> String { meta.employmentTypes[v] ?? Meta.fallback.employmentTypes[v] ?? v } public func category(_ v: String) -> String { meta.categories[v] ?? Meta.fallback.categories[v] ?? v } } public struct Chip: View { let text: String public init(_ text: String) { self.text = text } public var body: some View { Text(text) .font(.caption) .foregroundStyle(Theme.ink2) .padding(.horizontal, 9).padding(.vertical, 4) .overlay(Capsule().strokeBorder(Theme.line)) } } public struct StatusPill: View { let status: String public init(_ status: String) { self.status = status } var color: Color { switch status { case "published", "introduced": Theme.accent case "pending", "new": Theme.sealText case "rejected", "declined", "unpublished": Theme.danger default: Theme.muted } } public var body: some View { Text(JobStatus(rawValue: status)?.label ?? status.capitalized) .font(.caption.weight(.semibold)) .foregroundStyle(color) .padding(.horizontal, 8).padding(.vertical, 3) .background(color.opacity(0.12), in: Capsule()) } } /// The "Reviewed by a person" seal shown on published jobs. public struct ReviewedSeal: View { public init() {} public var body: some View { Label("Reviewed by a person", systemImage: "checkmark.seal.fill") .font(.caption.weight(.semibold)) .foregroundStyle(Theme.sealText) .symbolRenderingMode(.hierarchical) } } /// One job in a list: title, company, pay, the facts as chips. public struct JobRow: View { let job: Job let labels: Labels var showStatus = false var saved = false public init(job: Job, labels: Labels, showStatus: Bool = false, saved: Bool = false) { self.job = job; self.labels = labels; self.showStatus = showStatus; self.saved = saved } public var body: some View { VStack(alignment: .leading, spacing: 6) { HStack(alignment: .firstTextBaseline) { Text(job.title).font(Theme.display(17)).foregroundStyle(Theme.ink).lineLimit(2) Spacer(minLength: 8) if saved { Image(systemName: "bookmark.fill").foregroundStyle(Theme.accent).font(.caption) } if showStatus, let status = job.status { StatusPill(status) } } Text(job.company).font(.subheadline).foregroundStyle(Theme.ink2) Text(job.pay.text).font(Theme.mono(12)).foregroundStyle(Theme.accent) HStack(spacing: 6) { Chip(labels.workplace(job.workplace)) Chip(job.location).lineLimit(1) Chip(labels.type(job.employmentType)) } if showStatus { Text("Posted \(APIDate.short(job.createdAt))").font(.caption2).foregroundStyle(Theme.muted) } } .padding(.vertical, 6) .accessibilityElement(children: .combine) } } /// The body of a job as people read it: facts, pay, description, links. public struct JobBody: View { let job: Job let labels: Labels public init(job: Job, labels: Labels) { self.job = job; self.labels = labels } public var body: some View { VStack(alignment: .leading, spacing: 14) { VStack(alignment: .leading, spacing: 6) { Text(job.title).font(Theme.display(28)).foregroundStyle(Theme.ink).fixedSize(horizontal: false, vertical: true) HStack(spacing: 6) { Text(job.company).font(.title3).foregroundStyle(Theme.ink2) if let link = job.companyURL.flatMap(URL.init(string:)) { Link(destination: link) { Image(systemName: "arrow.up.right.square") }.foregroundStyle(Theme.accent) } } } Text(job.pay.text).font(Theme.mono(15).weight(.semibold)).foregroundStyle(Theme.accent) FlowChips(items: [labels.workplace(job.workplace), job.location, labels.type(job.employmentType), labels.category(job.category)]) if job.status == nil || job.status == "published" { HStack(spacing: 10) { ReviewedSeal() if let published = job.publishedAt { Text("Published \(APIDate.short(published))").font(.caption).foregroundStyle(Theme.muted) } } } Divider() Text(job.description) .font(.body).foregroundStyle(Theme.ink) .lineSpacing(3) .textSelection(.enabled) .fixedSize(horizontal: false, vertical: true) if let apply = job.applyURL.flatMap(URL.init(string:)) { Link(destination: apply) { Label("The company's own job page", systemImage: "arrow.up.right") } .font(.subheadline).foregroundStyle(Theme.accent) } } } } /// Chips that wrap onto more lines. public struct FlowChips: View { let items: [String] public init(items: [String]) { self.items = items } public var body: some View { FlowLayout(spacing: 6) { ForEach(items, id: \.self) { Chip($0) } } } } public struct FlowLayout: Layout { var spacing: CGFloat public init(spacing: CGFloat = 6) { self.spacing = spacing } public func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { let width = proposal.width ?? .infinity var x: CGFloat = 0, y: CGFloat = 0, rowHeight: CGFloat = 0, widest: CGFloat = 0 for view in subviews { let size = view.sizeThatFits(.unspecified) if x > 0 && x + size.width > width { x = 0; y += rowHeight + spacing; rowHeight = 0 } x += size.width + spacing; rowHeight = max(rowHeight, size.height); widest = max(widest, x - spacing) } return CGSize(width: min(widest, width), height: y + rowHeight) } public func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { var x = bounds.minX, y = bounds.minY, rowHeight: CGFloat = 0 for view in subviews { let size = view.sizeThatFits(.unspecified) if x > bounds.minX && x + size.width > bounds.maxX { x = bounds.minX; y += rowHeight + spacing; rowHeight = 0 } view.place(at: CGPoint(x: x, y: y), proposal: .unspecified) x += size.width + spacing; rowHeight = max(rowHeight, size.height) } } } /// A plain message for empty, loading and error states. public struct StateMessage: View { let title: String, detail: String, systemImage: String var retry: (() -> Void)? public init(_ title: String, detail: String, systemImage: String, retry: (() -> Void)? = nil) { self.title = title; self.detail = detail; self.systemImage = systemImage; self.retry = retry } public var body: some View { ContentUnavailableView { Label(title, systemImage: systemImage) } description: { Text(detail) } actions: { if let retry { Button("Try again", action: retry).buttonStyle(.bordered) } } } } public struct PrimaryButtonStyle: ButtonStyle { var role: ButtonRole? public init(role: ButtonRole? = nil) { self.role = role } public func makeBody(configuration: Configuration) -> some View { configuration.label .font(.body.weight(.semibold)) .foregroundStyle(.white) .padding(.horizontal, 16).padding(.vertical, 9) .background(role == .destructive ? Theme.danger : Theme.fill, in: Capsule()) .opacity(configuration.isPressed ? 0.8 : 1) } } public struct SecondaryButtonStyle: ButtonStyle { public init() {} public func makeBody(configuration: Configuration) -> some View { configuration.label .font(.body.weight(.medium)) .foregroundStyle(Theme.ink) .padding(.horizontal, 14).padding(.vertical, 8) .overlay(Capsule().strokeBorder(Theme.line, lineWidth: 1.2)) .contentShape(Capsule()) .opacity(configuration.isPressed ? 0.7 : 1) } } /// Opens a mail draft for the person to read and send (the API sends no email in v1). public enum MailDraft { public static func url(to: [String], subject: String, body: String) -> URL? { var components = URLComponents() components.scheme = "mailto" components.path = to.joined(separator: ",") components.queryItems = [.init(name: "subject", value: subject), .init(name: "body", value: body)] return components.url } public static func sendBack(_ job: Job, reason: String) -> URL? { guard let contact = job.contact else { return nil } let body = """ Hi \(contact.name), 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: \(reason) If you can fix that, post it again at https://jobley.ai/post/ and we'll read it again. The Jobley team """ return url(to: [contact.email], subject: "Your Jobley post: \(job.title)", body: body) } public static func introduction(candidate: Candidate, job: Job, note: String) -> URL? { guard let contact = job.contact else { return nil } let body = """ Hi \(contact.name) and \(candidate.name), \(note) \(candidate.name): \(candidate.headline)\(candidate.links.isEmpty ? "" : "\n" + candidate.links.joined(separator: "\n"))\(candidate.resumeURL.map { "\nResume: \($0)" } ?? "") \(contact.name) is hiring for “\(job.title)” at \(job.company): \(job.url) We'll leave you both to it. The Jobley team """ return url(to: [contact.email, candidate.email], subject: "Introduction: \(candidate.name) for \(job.title)", body: body) } }