Files
jobboard/app/static/role.js
T
WolfandClaude Fable 5.1 09c5dde113 Roles as a feed, a read page, votes that move the rank, unstar
The Roles tab is news's feed row: the title opens /roles/{uid}, company,
place and age under it, the badges, a one-line excerpt (summary, else the
description's opening), and ▲ ● ▼ ★ on the right, under the text at 640px.
The company affinity control leaves the row. A passed role and a hidden
company's are dimmed and stay. The filter bar is one row: order, search and
a Filters <details> holding the rest, with a judged select and "Show hidden
companies". The list loads 60 and appends on an IntersectionObserver
sentinel; back from a read page restores the filters, rows and scroll.

GET /api/roles takes offset, defaults to 60 and answers {items, total}.
judged= is a WHERE on roles.vote. Under a ranking the served score and the
order are the stored score moved by the vote (x0.5 pass, x1.2 like, capped
at 1), so a vote shows at once without a rescore. POST and DELETE
/api/roles/{uid}/vote set and clear it; pressing the pressed stance clears.

★ saves; pressed again it calls DELETE /api/applications/{id}, which drops
the application while it is still saved and answers 409 after. save_role no
longer writes the placeholder next step.

The read page (role.html, role.js) is news's reader: a bar with back,
▲ ● ▼ ★ and ↗, the company with its affinity control, place, badges, salary,
summary, the text (fetched posting, else description) and the link out.
It offers Fetch the posting for a teaser and Summarise without a summary,
against the /text and /summarise routes B4 adds. The vote and star buttons
are judge.js, shared by the feed and the reader.

Applications: titles open the read page, the Next column is gone, a Saved
column instead, and the lede says what the page is for.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-25 21:51:48 +02:00

201 lines
7.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 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"));
});
})();