Loading…
${esc(text)}
` : ""} +No open role fits these filters. + and start again.
` + : `Nothing has been pulled yet. Pulls runs one; + Searches and + Companies decide what it brings in.
`; + const clear = empty.querySelector("[data-clear]"); + if (clear) clear.onclick = () => { state.q = ""; $("#search").value = ""; clearFilters(); }; + } + + // The end of the list says which end it is: a list that has run out and + // one that is still loading look the same otherwise. + const end = $("#feed-end"); + const n = state.items.length; + end.hidden = !state.loaded || !n; + end.textContent = n < state.total + ? "Loading more…" + : `${number.format(n)} ${n === 1 ? "role" : "roles"} — that is all of them.`; + } + + // ----------------------------------------------------------- the reader + + // Reading happens in a third column rather than on a page of its own, so + // voting stays one keystroke away from the text being judged. + function openReader(uid) { + if (!uid) return; + const reading = Boolean(state.readingUid); + navigate(pathFor(uid), { replace: reading }); + if (!reading) state.pushedReader = true; + } + + // Every way of closing goes through the back button rather than around it, + // so the entry `openReader` pushed is popped by the close that matches it. A + // role arrived at directly (a link, a reload) has no entry of ours under it, + // so its list replaces it instead of leaving the app. + function closeReader() { + if (state.pushedReader) history.back(); + else navigate(pathFor(null), { replace: true }); + } + + // What `route` does when the path names a role: the pane, and the fetch. + async function showReader(uid) { + if (!state.readingUid) state.listY = window.scrollY; + // Drop the last role first, or the loading render paints its text for as + // long as the fetch takes. + state.reading = null; + state.readingUid = uid; + const idx = state.items.findIndex((r) => r.uid === uid); + if (idx >= 0) state.selected = idx; + // Three columns need room. Below that the rail yields, since choosing + // what to read is done the moment reading starts. + if (state.railOpen && window.innerWidth < 1500) { + state.railOpen = false; + state.railAutoShut = true; + renderRail(); + } + renderReader({ loading: true }); + renderFeed(); + rowAt(state.selected)?.scrollIntoView({ block: "nearest" }); + markRead(uid); + try { + const role = await api(`/api/roles/${encodeURIComponent(uid)}`); + if (state.readingUid !== uid) return; // moved on before this landed + state.reading = role; + renderReader(); + } catch (err) { + if (state.readingUid === uid) renderReader({ error: err.detail || String(err) }); + } + } + + // What `route` does when the path no longer names a role. + function shutReader() { + state.pushedReader = false; + state.reading = null; + state.readingUid = null; + state.summarising = null; + if (state.railAutoShut) { state.railOpen = true; state.railAutoShut = false; } + renderReader(); + renderFeed(); + renderRail(); + document.title = "Roles — roledesk"; + // The feed was not on screen while the reader was (narrow), or was a + // pane of its own that shortened the page (wide), and the page lost its + // place with it. Back to where it was, then as far as the row the keys + // moved to. + window.scrollTo(0, state.listY); + rowAt(state.selected)?.scrollIntoView({ block: "nearest" }); + } + + async function markRead(uid) { + const item = state.items.find((r) => r.uid === uid); + if (item && item.read_at) return; + if (item) { item.read_at = new Date().toISOString(); renderFeed(); } + try { + const role = await api(`/api/roles/${encodeURIComponent(uid)}/read`, { method: "POST" }); + if (item && role) item.read_at = role.read_at; + } catch (err) { /* toasted by api() */ } + } + + function salary(r) { + const n = (v) => number.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) { + return text.split(/\n+/).map((p) => p.trim()).filter(Boolean) + .map((p) => `${esc(p)}
`).join(""); + } + + function renderReader({ loading = false, error = "" } = {}) { + const el = $("#reader"); + const open = Boolean(state.readingUid); + el.hidden = !open; + const want = open ? "open" : "shut"; + if (document.body.dataset.reader !== want) { + document.body.dataset.reader = want; + renderRail(); + } + if (!open) { el.replaceChildren(); return; } + + // The list row is already in memory, and a list row is the whole role, so + // the pane paints from it at once and the fetch only brings it up to date. + const stub = state.items.find((r) => r.uid === state.readingUid); + const r = state.reading || stub || {}; + const same = el.dataset.uid === state.readingUid; + const scrolled = same ? el.querySelector(".reader-body")?.scrollTop || 0 : 0; + el.dataset.uid = state.readingUid; + if (r.title) document.title = `${r.title} — roledesk`; + + const out = safeUrl(r.original_url); + const text = (r.article_text || r.description || "").trim(); + const body = error + ? `This role could not be loaded: ${esc(error)}
` + : !r.uid + ? `Loading…
` + : text ? paragraphs(text) + : `The board gave no description for this one.
`; + const pay = salary(r); + const waiting = state.summarising === state.readingUid; + const teaser = r.uid && !r.article_text && out && (r.description || "").length < TEASER; + + el.innerHTML = ` + +${esc(pay)}
` : ""} + ${r.summary ? `${esc(r.summary)}
Loading…