Files
jobboard/app/static/sources.js
T

515 lines
20 KiB
JavaScript

/* The Job boards section of Sources: one query on one board per row, on
* Jobindex, Adzuna or Platsbanken. The UI says "job board" for what the API and the CLI
* call a search (`/api/searches`, `app searches`); company boards are
* companies.js's section, since a board is a company's own, not a query's.
*
* Edit turns a row into the form from the page's `<template>`, in place, and
* New job board puts a blank form on top of the list, as agents.js does for
* its cards. One form is open at a time, so its ids stay unique; Save and
* Cancel put it away.
*
* The list comes from `/api/sources`, and every action is a fetch through
* `api()` from app.js, so a refusal (a query that finds nothing) is one
* toast with the server's own sentence. The catch blocks here only put a
* control back.
*
* It is also the page's first script, so it shows the one section of the
* four the URL fragment names and marks it in the settings rail.
*
* Under the Follow form of Company boards, a fold lists what each reader
* declares (`/api/readers`): how its list arrives, where the text is, what
* it filters at the source, how it states places, how it knows a read is
* complete, its page size and ceiling, which fields its rows state, and the
* robots.txt decision taken when it was built.
*
* The Unmapped section lists what prep could not place, most roles first:
* the place texts no place resolves (`/api/prep/unlinked`), with the
* countries and companies of their roles, and the host words no table in
* `app/prep/hosts` maps (`/api/prep/unmapped`), per field and host. Copy
* puts both on the clipboard as text for Claude, or, where there is no
* clipboard, on the page selected.
*/
(function () {
"use strict";
const { api, count, debounce, safeUrl } = window;
const $ = (sel, root = document) => root.querySelector(sel);
/** @type {{searches: object[], adzuna_key: boolean}} */
let data = { searches: [], adzuna_key: false };
const NEW = "new";
/** @type {number | "new" | null} the row in edit mode: a source's id, NEW
* for the blank form on top, null for none */
let editing = null;
/** @type {HTMLFormElement | null} that form, kept across renders so a
* switch flipped on another row does not wipe what is being typed */
let editor = null;
/** "DK 120 · EU 4 · unknown 2", the three biggest. */
function countries(source) {
const shown = source.countries.slice(0, 3)
.map(([code, n]) => `${code || "unknown"} ${n}`);
if (source.countries.length > 3) shown.push("…");
return shown.join(" · ");
}
function onSwitch(source, rerender) {
const box = document.createElement("input");
box.type = "checkbox";
box.checked = source.on;
box.setAttribute("aria-label", `Run ${source.label}`);
box.addEventListener("change", async () => {
// Switching off closes every open role in one tap; ask first, naming
// the count, as removing a search and deleting an agent already do.
if (!box.checked && source.open_roles
&& !window.confirm(`Switch off ${source.label}? `
+ `Its ${count(source.open_roles, "open role")} close.`)) {
box.checked = true;
return;
}
box.disabled = true;
try {
const result = await api(`/api/sources/${source.id}/switch`,
{ method: "POST", body: { on: box.checked } });
Object.assign(source, result.source);
if (!box.checked && result.closed) {
window.toast.info(`${source.label}: ${count(result.closed, "open role")} closed.`);
} else if (box.checked) {
window.toast.info(`${source.label}: reading now, or with the pull right after.`);
}
} catch (err) {
box.checked = !box.checked;
}
box.disabled = false;
rerender();
});
return box;
}
async function load() {
data = await api("/api/sources");
}
// ---------------------------------------------------------------- searches
function renderSearches() {
const body = $("#searches-list");
body.replaceChildren();
if (editing === NEW) body.appendChild(editor);
for (const source of data.searches) {
if (editing === source.id) {
body.appendChild(editor);
continue;
}
const row = document.createElement("div");
row.className = "srow";
if (!source.on) row.dataset.muted = "true";
const name = document.createElement("span");
name.className = "name";
const label = document.createElement("span");
label.textContent = source.label;
const provider = document.createElement("span");
provider.className = "badge";
provider.textContent = source.provider;
name.append(label, provider);
row.appendChild(name);
row.appendChild(onSwitch(source, renderSearches));
const q = (source.params && source.params.q) || "";
const sub = document.createElement("span");
sub.className = "sub";
const parts = [q, `${source.open_roles} open`];
if (source.countries.length) parts.push(countries(source));
sub.append(parts.join(" · "), " ");
if (source.failing_since) {
// The search has failed on every pull since then; after 21 days its
// roles close, as a board's do (app/jobs/pull.py).
const failing = document.createElement("span");
failing.className = "failing";
failing.textContent = `not answering since ${source.failing_since.slice(0, 10)}`
+ (source.failing_reason ? `: ${source.failing_reason}` : "");
sub.append("· ", failing, " ");
}
const edit = document.createElement("button");
edit.type = "button";
edit.className = "linky";
edit.textContent = "Edit";
edit.addEventListener("click", () => openForm(source));
const remove = document.createElement("button");
remove.type = "button";
remove.className = "linky";
remove.textContent = "Remove";
remove.addEventListener("click", () => removeSearch(source));
sub.append(edit, " ", remove);
row.appendChild(sub);
body.appendChild(row);
}
const open = data.searches.reduce((sum, s) => sum + s.open_roles, 0);
$("#searches-count").textContent = data.searches.length
? `${count(data.searches.length, "job board")}, ${count(open, "open role")}`
: "No job boards yet. New job board adds one; the starter ones in config.yaml import on the first pull.";
}
function showProvider(f) {
const provider = $("#s-provider", f).value;
for (const node of f.querySelectorAll("[data-provider]")) {
node.hidden = node.dataset.provider !== provider;
}
$("#adzuna-key", f).hidden = provider !== "adzuna" || data.adzuna_key;
}
function form(source) {
const f = $("#search-form-template").content.querySelector("form").cloneNode(true);
const p = (source && source.params) || {};
$("#search-form-title", f).textContent = source ? `Edit ${source.label}` : "New job board";
$("#s-provider", f).value = source ? source.provider : "jobindex";
$("#s-name", f).value = source ? source.label : "";
$("#s-name", f).readOnly = Boolean(source);
$("#s-provider", f).disabled = Boolean(source);
$("#s-q", f).value = p.q || "";
$("#s-area", f).value = String(p.area || (source ? 0 : 1));
$("#s-country", f).value = p.country || "de";
$("#s-where", f).value = p.where || "";
$("#s-region", f).value = p.region || "";
$("#s-pages", f).value = String(p.pages || 10);
showProvider(f);
f.addEventListener("input", () => { f.dataset.dirty = "true"; });
$("#s-provider", f).addEventListener("change", () => showProvider(f));
$("#s-try", f).addEventListener("click", () => trySearch(f));
$("#s-cancel", f).addEventListener("click", () => { if (close()) renderSearches(); });
f.addEventListener("submit", saveSearch);
return f;
}
// Put the open form away, asking first if it holds changes. False when the
// answer was to keep them.
function close() {
if (editor && editor.dataset.dirty
&& !window.confirm("Discard the changes to this job board?")) {
return false;
}
editing = null;
editor = null;
return true;
}
// Edit `source` in place of its row, or a new job board on top of the list.
function openForm(source) {
const target = source ? source.id : NEW;
if (editing !== target) {
if (!close()) return;
editing = target;
editor = form(source);
$("#searches-answer").textContent = "";
renderSearches();
}
editor.scrollIntoView({ behavior: "smooth", block: "nearest" });
$("#s-q", editor).focus({ preventScroll: true });
}
function searchBody(f) {
return {
provider: $("#s-provider", f).value,
name: $("#s-name", f).value.trim(),
q: $("#s-q", f).value.trim(),
area: Number($("#s-area", f).value) || 0,
country: $("#s-country", f).value,
where: $("#s-where", f).value.trim(),
region: $("#s-region", f).value,
pages: Number($("#s-pages", f).value) || 0,
};
}
function found(result) {
const all = result.complete ? "every match" : "the page limit cut it short";
return `Found ${count(result.found, "role")} (${all}).`;
}
async function trySearch(f) {
const status = $("#search-status", f);
const button = $("#s-try", f);
button.disabled = true;
status.textContent = "Searching…";
$("#search-sample", f).replaceChildren();
try {
const result = await api("/api/searches/try", { method: "POST", body: searchBody(f) });
status.textContent = `${found(result)} The newest ${Math.min(result.found, 10)}:`;
const list = $("#search-sample", f);
for (const row of result.sample) {
const li = document.createElement("li");
const url = safeUrl(row.url);
if (url) {
const link = document.createElement("a");
link.href = url;
link.target = "_blank";
link.rel = "noopener";
link.textContent = row.title;
li.appendChild(link);
} else {
li.append(row.title);
}
li.append(` — ${[row.company, row.place].filter(Boolean).join(", ")}`);
list.appendChild(li);
}
} catch (err) {
status.textContent = "";
}
button.disabled = false;
}
async function saveSearch(event) {
event.preventDefault();
const f = event.target;
const status = $("#search-status", f);
const button = $("button[type=submit]", f);
button.disabled = true;
status.textContent = "Running it once before saving…";
let result;
try {
result = await api("/api/searches", { method: "POST", body: searchBody(f) });
await load();
} catch (err) {
status.textContent = "";
button.disabled = false;
return;
}
editing = null;
editor = null;
renderSearches();
$("#searches-answer").textContent = `Saved ${result.source.label}. ${found(result)} `
+ "Reading now, or with the pull right after.";
}
async function removeSearch(source) {
if (!window.confirm(`Remove ${source.label}? Its ${count(source.open_roles, "open role")} close.`)) {
return;
}
try {
await api(`/api/searches/${source.id}`, { method: "DELETE" });
data.searches = data.searches.filter((s) => s.id !== source.id);
if (editing === source.id) {
editing = null;
editor = null;
}
renderSearches();
} catch (err) {
/* already toasted */
}
}
// ----------------------------------------------------------------- readers
/** "country one, remote ids", or "none". */
function filters(reader) {
const shown = Object.entries(reader.filters).map(([name, how]) => `${name} ${how}`);
return shown.length ? shown.join(", ") : "none";
}
/** What the reader declares of its limits, short: how completeness is known,
the page size and ceiling, a teaser beside the detail text, an empty list
that may be an unknown slug, and what its rows state. */
function limits(reader) {
const out = [`complete ${reader.complete}`];
if (reader.page_size) out.push(`${reader.page_size} a page`);
if (reader.ceiling) out.push(`ceiling ${reader.ceiling}`);
if (reader.teaser) out.push("teaser in the list");
if (reader.empty === "suspect") out.push("empty may be an unknown slug");
const fields = Object.entries(reader.fields || {}).map(([name, how]) => `${name} ${how}`);
if (fields.length) out.push(`states ${fields.join(", ")}`);
return out;
}
function renderReaders(readers) {
const fold = document.createElement("details");
fold.className = "prune";
fold.id = "readers";
const summary = document.createElement("summary");
summary.textContent = `What each reader does (${readers.length})`;
const list = document.createElement("div");
list.className = "srows";
for (const reader of readers) {
const row = document.createElement("div");
row.className = "srow";
const name = document.createElement("span");
name.className = "name";
const label = document.createElement("span");
label.textContent = reader.label;
const scope = document.createElement("span");
scope.className = "badge";
scope.textContent = reader.scope === "search" ? "job board" : "company board";
name.append(label, scope);
const sub = document.createElement("span");
sub.className = "sub";
sub.textContent = [`list ${reader.listing}`, `text ${reader.text}`,
`filters ${filters(reader)}`, `places ${reader.places}`, ...limits(reader),
reader.robots].join(" · ");
row.append(name, sub);
list.appendChild(row);
}
fold.append(summary, list);
$("#board-form").after(fold);
}
// ------------------------------------------------------------ unmapped
/** Rows shown before Show all; the copy always holds every one. */
const UNLINKED_SHOWN = 40;
/** @type {{places: object[], words: object[]}} */
const unmapped = { places: [], words: [] };
/** "DE 5 · EU 5 · none 1", the three biggest. */
function where(place) {
const shown = place.countries.slice(0, 3).map(([code, n]) => `${code || "none"} ${n}`);
if (place.countries.length > 3) shown.push("…");
return shown.join(" · ");
}
function srow(name, right, sub) {
const row = document.createElement("div");
row.className = "srow";
const label = document.createElement("span");
label.className = "name";
label.textContent = name;
const n = document.createElement("span");
n.className = "count";
n.textContent = right;
const line = document.createElement("span");
line.className = "sub";
line.textContent = sub;
row.append(label, n, line);
return row;
}
function renderUnlinked(all) {
const list = $("#unlinked-list");
const shown = all ? unmapped.places : unmapped.places.slice(0, UNLINKED_SHOWN);
list.replaceChildren(...shown.map((p) => srow(p.text, count(p.roles, "role"),
[where(p), p.companies.join(", ")].filter(Boolean).join(" · "))));
$("#unlinked-count").textContent = unmapped.places.length
? `${count(unmapped.places.length, "place text")} on open roles no place resolves.`
: "Every place text of an open role resolves.";
if (shown.length < unmapped.places.length) {
const more = document.createElement("button");
more.type = "button";
more.className = "linky";
more.textContent = `Show all ${unmapped.places.length}`;
more.addEventListener("click", () => renderUnlinked(true));
list.appendChild(more);
}
}
function renderWords() {
const groups = new Map();
for (const w of unmapped.words) {
const key = `${w.field} · ${w.host || "no host"}`;
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(w);
}
$("#words-list").replaceChildren(...[...groups].map(([key, found]) =>
srow(key, count(found.reduce((n, w) => n + w.roles, 0), "role"),
found.map((w) => `${w.word} ${w.roles}`).join(" · "))));
$("#words-count").textContent = unmapped.words.length
? `${count(unmapped.words.length, "host word")} no table maps.`
: "Every host word is mapped.";
}
/** Both lists as text, one line an entry, for pasting to Claude. */
function unmappedText() {
const lines = ["Unlinked places (roles, their countries, companies):"];
for (const p of unmapped.places) {
lines.push(`${p.roles} ${p.text} [${where(p)}] ${p.companies.join(", ")}`);
}
lines.push("", "Unmapped host words (field, host, roles, word):");
for (const w of unmapped.words) {
lines.push(`${w.field} ${w.host || "-"} ${w.roles} ${w.word}`);
}
return lines.join("\n");
}
// The clipboard needs a secure context; the server reached at a LAN
// address is not one. As news's copyLink, the text is then put on the
// page and selected, for the device's own Copy.
async function copyUnmapped() {
const text = unmappedText();
try {
await navigator.clipboard.writeText(text);
window.toast.good("Copied the unmapped list.");
return;
} catch { /* no clipboard here: show the text instead */ }
const holder = $("#unmapped-text");
holder.textContent = text;
holder.hidden = false;
const range = document.createRange();
range.selectNodeContents(holder);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
window.toast.info("The list is selected: use your device's Copy.");
}
async function loadUnmapped() {
[unmapped.places, unmapped.words] = await Promise.all(
[api("/api/prep/unlinked"), api("/api/prep/unmapped")]);
renderUnlinked(false);
renderWords();
}
// ------------------------------------------------------ one section shown
// Sources shows one section: the one the fragment names or contains, or the
// first when there is none or it names none, so `/settings/sources#runs` (a
// rail link, or the Search agents page's "follow it under Runs") opens
// Runs, and so does `#messages`, the log at its end. The rest
// are hidden, not removed, so their scripts still find their roots. It runs
// as the script loads rather than on DOMContentLoaded, so the page never
// paints all three first.
function showSection() {
const sections = [...document.querySelectorAll("section[data-section]")];
if (!sections.length) return;
const want = decodeURIComponent(location.hash.slice(1));
const target = want && document.getElementById(want);
const shown = sections.find((s) => target && s.contains(target)) || sections[0];
for (const section of sections) section.hidden = section !== shown;
for (const link of document.querySelectorAll(".settings-rail a[data-section]")) {
if (link.dataset.section === shown.id) {
link.setAttribute("aria-current", "location");
link.scrollIntoView({ block: "nearest", inline: "nearest" });
} else {
link.removeAttribute("aria-current");
}
}
}
showSection();
// A switch starts the section at the top of the page, as opening it would;
// a fragment inside one (`#messages`) scrolls to itself.
window.addEventListener("hashchange", () => {
showSection();
const target = document.getElementById(decodeURIComponent(location.hash.slice(1)));
if (target && !target.matches("section[data-section]")) target.scrollIntoView();
else window.scrollTo(0, 0);
});
// -------------------------------------------------------------------- init
document.addEventListener("DOMContentLoaded", async () => {
if (!$("#searches-list")) return;
if ($("#board-form")) {
api("/api/readers").then(renderReaders, () => {});
}
if ($("#unlinked-list")) {
loadUnmapped().catch(() => {});
$("#unmapped-copy").addEventListener("click", copyUnmapped);
}
try {
await load();
} catch (err) {
return;
}
renderSearches();
$("#s-add").addEventListener("click", () => openForm(null));
});
})();