/* The read page of one role, news's reader. * * The bar: back, ▲ ● ▼ ★ (judge.js, the same buttons as the feed row) and ↗ * to the posting. Then the company with its ★ ▲ ⊘ control, the one place a * company is judged outside the Companies page; place, badges, salary when * stated; the summary when there is one; the text, which is the fetched * posting when there is one and the board's description otherwise; and the * way out once more at the end. * * A search role's description is a teaser of a couple of hundred characters, * so under 500 of them, with nothing fetched yet, the page offers "Fetch the * posting" (`POST /api/roles/{uid}/text`). "Summarise" is offered while there * is no summary (`POST /api/roles/{uid}/summarise`). Both answer with the * role, and the page is drawn again from it. A summary made by the queue * rather than at once is waited for by asking for the role again. */ (function () { "use strict"; const { api, companies, loadCompanies, affinityControl, judgeButtons, loadTracked } = window; const $ = (sel, root = document) => root.querySelector(sel); const uid = $("#role").dataset.uid; const path = `/api/roles/${encodeURIComponent(uid)}`; /** Shorter than this and not fetched: the description is a teaser. */ const TEASER = 500; /** @type {object|null} the role on screen, updated in place */ let role = null; function relativeAge(iso) { const minutes = Math.max(0, Math.floor((Date.now() - new Date(iso).getTime()) / 60000)); if (minutes < 60) return `${minutes} min ago`; const hours = Math.floor(minutes / 60); if (hours < 24) return `${hours} h ago`; return `${Math.floor(hours / 24)} d ago`; } function salary(r) { const n = (v) => new Intl.NumberFormat("en").format(v); const unit = r.currency ? `${r.currency} ` : ""; if (r.salary_min && r.salary_max && r.salary_min !== r.salary_max) { return `${unit}${n(r.salary_min)}–${n(r.salary_max)}`; } if (r.salary_min || r.salary_max) { const one = r.salary_min || r.salary_max; return r.salary_min ? `From ${unit}${n(one)}` : `Up to ${unit}${n(one)}`; } return ""; } function host(url) { try { return new URL(url).host.replace(/^www\./, ""); } catch (e) { return ""; } } /* Plain text in paragraphs: a fetched page keeps its line breaks, and a board's description is one long line, which stays one paragraph. */ function paragraphs(text) { const box = document.createDocumentFragment(); for (const part of text.split(/\n\s*\n|\n/).map((p) => p.trim()).filter(Boolean)) { const p = document.createElement("p"); p.textContent = part; box.appendChild(p); } return box; } function badge(text) { const span = document.createElement("span"); span.className = "badge"; span.textContent = text; return span; } function renderAffinity() { const slot = $("#role-affinity"); const company = companies.get(role.company_key); // A company seen only through a search has no row of its own to judge. if (!company) { slot.replaceChildren(); return; } slot.replaceChildren(affinityControl(role.company_key, company.affinity, renderAffinity)); } function render() { document.title = `${role.title || "Role"} — roledesk`; $("#role").removeAttribute("aria-busy"); const out = role.original_url || ""; $("#role-out").hidden = !out; if (out) $("#role-out").href = out; const end = $("#role-out-end"); end.hidden = !out; if (out) { const a = end.querySelector("a"); a.href = out; a.textContent = `Read it on ${host(out) || "the board"} ↗`; } $("#role-company").textContent = role.company || ""; renderAffinity(); $("#role-place").textContent = role.place || ""; $("#role-age").textContent = role.first_seen ? relativeAge(role.first_seen) : ""; $("#role-title").textContent = role.title || "(untitled)"; const badges = [role.remote, role.function, role.seniority].filter(Boolean).map(badge); if (role.closed_at) badges.unshift(badge("closed")); $("#role-badges").replaceChildren(...badges); $("#role-badges").hidden = !badges.length; const pay = salary(role); $("#role-salary").hidden = !pay; $("#role-salary").textContent = pay; const summary = $("#role-summary"); summary.hidden = !role.summary; summary.querySelector("p").textContent = role.summary || ""; const text = (role.article_text || role.description || "").trim(); $("#role-text").replaceChildren(text ? paragraphs(text) : Object.assign(document.createElement("p"), { className: "muted", textContent: "The board gave no description for this one.", })); $("#role-summarise").hidden = Boolean(role.summary); $("#role-fetch").hidden = Boolean(role.article_text) || (role.description || "").length >= TEASER; } function merge(updated) { if (updated && updated.uid === role.uid) Object.assign(role, updated); render(); } /* Press, wait, draw again from the answer. A failure is toasted by `api()` (a 404 included, while the route is not there yet) and the button comes back as it was. */ async function ask(button, busy, route) { const label = button.textContent; button.disabled = true; button.textContent = busy; try { merge(await api(`${path}/${route}`, { method: "POST" })); return true; } catch (err) { return false; } finally { button.disabled = false; button.textContent = label; } } async function summarise() { if (!(await ask($("#role-summarise"), "Summarising…", "summarise"))) return; if (role.summary) return; // Queued rather than done: ask again until it is there, for two minutes. const note = $("#role-note"); note.textContent = "The summary appears here when it is ready."; $("#role-summarise").hidden = true; for (let i = 0; i < 30 && !role.summary; i++) { await new Promise((resolve) => setTimeout(resolve, 4000)); try { merge(await api(path)); } catch (err) { break; } } note.textContent = ""; render(); } /* Back is back when this page was opened from one of this app's lists, so the list keeps its place; a link from anywhere else goes to the roles. */ function back(e) { let from = null; try { from = document.referrer ? new URL(document.referrer) : null; } catch (err) { /* */ } if (from && from.origin === location.origin && history.length > 1) { e.preventDefault(); history.back(); } } /* The bar sticks under the top bar, whose height is the phone's or the desktop's, and the notch's on a standalone phone. */ function measureHeader() { const top = document.querySelector("header.top"); if (top) document.documentElement.style.setProperty("--header-h", `${top.offsetHeight}px`); } document.addEventListener("DOMContentLoaded", async () => { measureHeader(); window.addEventListener("resize", measureHeader); $("#role-back").addEventListener("click", back); try { [role] = await Promise.all([api(path), loadCompanies(), loadTracked()]); } catch (err) { $("#role-title").textContent = "This role could not be loaded."; return; } $("#role-judge").appendChild(judgeButtons(role)); render(); $("#role-summarise").addEventListener("click", summarise); $("#role-fetch").addEventListener("click", () => ask($("#role-fetch"), "Fetching…", "text")); }); })();