/* The Roles tab: the filter bar and the list of open roles. * * The template renders an empty shell; everything on screen comes from the * JSON routes in app/web/api.py, and every action (Save, an affinity click) * is a fetch from here, not a form post. The company list and its affinity * control are app.js's, shared with the Companies page. * * Every request goes through `api()` from app.js, so a failure is one toast * carrying the server's own sentence. The catch blocks here only put the * control back the way it was. */ (function () { "use strict"; const { api, count, debounce, companies, loadCompanies, affinityControl } = window; /** @type {Map} role_uid -> application, for roles already tracked */ let trackedByRole = new Map(); /** @type {object[]} the last /api/roles response, already muted-filtered by the server */ let roles = []; /** @type {object[]} the enabled rankings, from /api/agents */ let agents = []; const $ = (sel, root = document) => root.querySelector(sel); async function loadApplications() { const applications = await api("/api/applications"); trackedByRole = new Map(applications.filter((a) => a.role_uid).map((a) => [a.role_uid, a])); } async function loadAgents() { agents = (await api("/api/agents")).filter((a) => a.enabled); } function filterParams() { const params = new URLSearchParams(); for (const key of ["q", "company", "place"]) { const value = $(`#f-${key}`).value.trim(); if (value) params.set(key, value); } for (const key of ["function", "remote", "days"]) { const value = $(`#f-${key}`).value; if (value) params.set(key, value); } if ($("#f-show-muted").checked) params.set("show_muted", "true"); const agent = $("#f-agent").value; if (agent) { params.set("agent", agent); if ($("#f-gated").checked) params.set("gated", "1"); } return params; } async function loadRoles() { try { roles = await api(`/api/roles?${filterParams().toString()}`); } catch (err) { return; } renderRoles(); } function relativeAge(iso) { const then = new Date(iso); const minutes = Math.max(0, Math.floor((Date.now() - then.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 renderRoles() { const list = $("#roles-list"); const template = $("#role-row"); list.replaceChildren(); for (const role of roles) { const node = template.content.cloneNode(true); const li = node.querySelector(".role"); const company = companies.get(role.company_key); if (company && company.affinity === -1) li.classList.add("muted"); const titleLink = node.querySelector(".role-title"); titleLink.textContent = role.title || "(untitled)"; titleLink.href = role.original_url || "#"; node.querySelector(".role-company").textContent = role.company || ""; node.querySelector(".role-place").textContent = role.place || ""; node.querySelector(".role-remote").textContent = role.remote || ""; node.querySelector(".role-function").textContent = role.function || ""; node.querySelector(".role-age").textContent = relativeAge(role.first_seen); if (role.score !== null && role.score !== undefined) { const badge = node.querySelector(".role-score"); badge.textContent = role.gated_reason ? "gated" : role.score.toFixed(2); badge.title = "Match"; node.querySelector(".role-gated").textContent = role.gated_reason || ""; if (role.gated_reason) li.classList.add("gated"); } node.querySelector(".affinity").replaceWith( affinityControl(role.company_key, company ? company.affinity : 0, loadRoles)); const saveButton = node.querySelector(".save"); setSaveButton(saveButton, role.uid); saveButton.addEventListener("click", () => saveRole(role.uid, saveButton)); list.appendChild(node); } $("#roles-count").textContent = count(roles.length, "role"); } function setSaveButton(button, uid) { const saved = trackedByRole.has(uid); button.textContent = saved ? "Saved" : "Save"; button.disabled = saved; } async function saveRole(uid, button) { button.disabled = true; button.textContent = "Saving…"; try { const application = await api("/api/applications", { method: "POST", body: { role_uid: uid, status: "saved" }, }); trackedByRole.set(uid, application); setSaveButton(button, uid); } catch (err) { button.disabled = false; button.textContent = "Save"; } } /* The filter bar's order: Newest, or a ranking's score. One ranking is the ordinary case and reads "Best match"; more than one only comes from the CLI or the config, and then each is listed by name so none is lost. Keeps the current choice when the list is refreshed, unless that one is gone. */ function renderAgentSelect() { const select = $("#f-agent"); const current = select.value; select.replaceChildren(new Option("Newest", "")); for (const agent of agents) { const label = agents.length === 1 ? "Best match" : agent.name; select.appendChild(new Option(label, String(agent.id))); } select.value = agents.some((a) => String(a.id) === current) ? current : ""; $("#show-gated").hidden = !select.value; } /* `?q=`, `?company=`, `?place=` and `?agent=` prefill the filter bar, which is how the Companies and Ranking pages link into this one. */ function prefill() { const query = new URLSearchParams(location.search); for (const key of ["q", "company", "place"]) { if (query.get(key)) $(`#f-${key}`).value = query.get(key); } const agent = query.get("agent"); if (agent && agents.some((a) => String(a.id) === agent)) { $("#f-agent").value = agent; $("#show-gated").hidden = false; } } document.addEventListener("DOMContentLoaded", async () => { try { await Promise.all([loadCompanies(), loadApplications(), loadAgents()]); } catch (err) { return; } renderAgentSelect(); prefill(); await loadRoles(); const refetch = debounce(loadRoles, 300); for (const key of ["q", "company", "place"]) { $(`#f-${key}`).addEventListener("input", refetch); } for (const sel of ["#f-function", "#f-remote", "#f-days", "#f-show-muted", "#f-gated"]) { $(sel).addEventListener("change", loadRoles); } $("#f-agent").addEventListener("change", () => { $("#show-gated").hidden = !$("#f-agent").value; loadRoles(); }); $("#filters").addEventListener("submit", (e) => e.preventDefault()); }); })();