Migration 21 adds companies.all_roles, tags every favourite of any agent and each company whose board had params {"reach": "all"} (logged per board, the param removed). reach.plan reads a tagged company's boards with the union of the enabled agents' require reach and the base reach; favourite no longer widens the fetch. POST /api/companies/{key}/all-roles sets it and queues a pull when on. The Companies list, the reader and the company strip draw the toggle beside the affinity buttons, the list filters on it, and the board's reach editor drops its All roles option.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
425 lines
17 KiB
JavaScript
425 lines
17 KiB
JavaScript
/* The Company boards section of Sources: every company, its board, its open
|
|
* roles, its affinity, and following a new one.
|
|
*
|
|
* The list is app.js's shared company list (`/api/companies`), already
|
|
* joined with each company's board subscription — its id and whether it is
|
|
* on — so nothing here fetches `/api/sources` as well. The affinity buttons
|
|
* are `affinityControl`, the same ones the Roles tab and the read page draw,
|
|
* and beside them `allRolesControl`, the All roles tag, which is global and
|
|
* drawn with or without an agent.
|
|
*
|
|
* Liked and hidden are per search agent, so the `#c-agent` select above the
|
|
* list says whose the list shows and whose the buttons set. It opens on the
|
|
* server's default, the enabled agent with the lowest id; with no agent at
|
|
* all there is nothing to like, and the section says so instead of drawing
|
|
* the buttons.
|
|
*
|
|
* Each row's "Set board" (no board yet) or "Change" (a board) opens a small
|
|
* editor in the row: the host select, with the options the server rendered
|
|
* into the Follow form's `#b-host`, and a slug. Saving posts to
|
|
* `/api/companies/{key}/board`, which reads the board once before keeping
|
|
* it; Detach stops reading it and keeps the company.
|
|
*
|
|
* A row with a board says what a pull asks it for ("reads DK, SE · agents",
|
|
* `reach` from app/jobs/reach.py), and its editor has a second line that
|
|
* sets the board's own reach: the agents', or its own countries and remote,
|
|
* posted to `/api/sources/{id}/reach`. All roles within the agents' wide
|
|
* reach (Europe) is the company's All roles tag instead, which the board's
|
|
* own countries win over.
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
const {
|
|
api, count, debounce, safeUrl, companies, loadCompanies, affinityControl, allRolesControl,
|
|
} = window;
|
|
const $ = (sel, root = document) => root.querySelector(sel);
|
|
|
|
/** @type {object[]} the search agents, by id */
|
|
let agents = [];
|
|
|
|
/** The key of the row whose board editor is open, or null. One at a time. */
|
|
let editing = null;
|
|
|
|
/** The id in `#c-agent`, or null while there is no agent to pick. */
|
|
function agentId() {
|
|
const value = $("#c-agent").value;
|
|
return value ? Number(value) : null;
|
|
}
|
|
|
|
async function loadAgents() {
|
|
agents = (await api("/api/agents")).sort((a, b) => a.id - b.id);
|
|
const select = $("#c-agent");
|
|
select.replaceChildren();
|
|
for (const a of agents) {
|
|
const option = document.createElement("option");
|
|
option.value = String(a.id);
|
|
option.textContent = a.name;
|
|
select.appendChild(option);
|
|
}
|
|
const first = agents.find((a) => a.enabled) || agents[0];
|
|
if (first) select.value = String(first.id);
|
|
select.closest("label").hidden = !first;
|
|
$("#c-no-agent").hidden = Boolean(first);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- the list
|
|
|
|
function subLine(company, rerender) {
|
|
const sub = document.createElement("span");
|
|
sub.className = "sub";
|
|
if (company.board) {
|
|
const badge = document.createElement("span");
|
|
badge.className = "badge";
|
|
badge.textContent = company.board;
|
|
sub.appendChild(badge);
|
|
} else if (company.ats || company.careers_url) {
|
|
// Followed, but its careers site runs on nothing the app reads yet.
|
|
const badge = document.createElement("span");
|
|
badge.className = "badge";
|
|
badge.textContent = company.ats
|
|
? (company.ats_slug ? `${company.ats}:${company.ats_slug}` : company.ats)
|
|
: "own site";
|
|
sub.appendChild(badge);
|
|
sub.append(" · not readable yet");
|
|
} else if (company.from_search) {
|
|
// Nobody followed it: its roles came from a search, and it was judged.
|
|
sub.append("from job boards");
|
|
} else {
|
|
// Followed by the seed, Follow or discover, and none of them found
|
|
// where this company posts its roles.
|
|
sub.append("board not found");
|
|
}
|
|
const careersUrl = safeUrl(company.careers_url);
|
|
if (careersUrl && !company.board) {
|
|
const link = document.createElement("a");
|
|
link.href = careersUrl;
|
|
link.target = "_blank";
|
|
link.rel = "noopener";
|
|
link.textContent = "careers page";
|
|
sub.append(" · ");
|
|
sub.appendChild(link);
|
|
}
|
|
sub.append(` · ${count(company.open_roles, "open role")}`);
|
|
if (company.reach) sub.append(` · reads ${company.reach}`);
|
|
if (company.failing_since) {
|
|
// The board has failed on every pull since then; after 21 days its
|
|
// roles close.
|
|
const failing = document.createElement("span");
|
|
failing.className = "failing";
|
|
failing.textContent = `not answering since ${company.failing_since.slice(0, 10)}`
|
|
+ (company.failing_reason ? `: ${company.failing_reason}` : "");
|
|
sub.append(" · ");
|
|
sub.appendChild(failing);
|
|
}
|
|
sub.append(" ");
|
|
const edit = document.createElement("button");
|
|
edit.type = "button";
|
|
edit.className = "linky";
|
|
edit.textContent = company.subscription_id == null ? "Set board" : "Change";
|
|
edit.setAttribute("aria-expanded", String(editing === company.key));
|
|
edit.addEventListener("click", () => {
|
|
editing = editing === company.key ? null : company.key;
|
|
rerender();
|
|
});
|
|
sub.appendChild(edit);
|
|
return sub;
|
|
}
|
|
|
|
/** The open row's board editor: host and slug, Save, Cancel, and Detach
|
|
for a row that has a board. */
|
|
function boardEditor(company, rerender) {
|
|
const form = document.createElement("form");
|
|
form.className = "board-edit";
|
|
form.autocomplete = "off";
|
|
const host = document.createElement("select");
|
|
host.setAttribute("aria-label", `${company.name}'s board host`);
|
|
for (const option of $("#b-host").options) host.appendChild(option.cloneNode(true));
|
|
const known = [...host.options].map((o) => o.value);
|
|
const current = company.board || company.ats;
|
|
if (known.includes(current)) host.value = current;
|
|
const slug = document.createElement("input");
|
|
slug.type = "text";
|
|
slug.required = true;
|
|
slug.spellcheck = false;
|
|
slug.placeholder = "slug";
|
|
slug.value = company.ats_slug || "";
|
|
slug.setAttribute("aria-label", `${company.name}'s slug on that host`);
|
|
const save = document.createElement("button");
|
|
save.type = "submit";
|
|
save.textContent = company.subscription_id == null ? "Set board" : "Change board";
|
|
const cancel = document.createElement("button");
|
|
cancel.type = "button";
|
|
cancel.className = "linky";
|
|
cancel.textContent = "Cancel";
|
|
cancel.addEventListener("click", () => { editing = null; rerender(); });
|
|
form.append(host, slug, save, cancel);
|
|
|
|
async function done() {
|
|
editing = null;
|
|
await loadCompanies(agentId());
|
|
rerender();
|
|
}
|
|
|
|
form.addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
const board = `${host.value}:${slug.value.trim()}`;
|
|
// Replacing a different board closes its roles (see set_board in
|
|
// app/jobs/sources.py); ask first, naming both boards and the count,
|
|
// the same as Detach and a removed search already do.
|
|
if (company.board && company.board !== board
|
|
&& !window.confirm(`Replace ${company.name}'s board (${company.board}) `
|
|
+ `with ${board}? Its ${count(company.open_roles, "open role")} close.`)) {
|
|
return;
|
|
}
|
|
save.disabled = true;
|
|
try {
|
|
const result = await api(`/api/companies/${encodeURIComponent(company.key)}/board`,
|
|
{ method: "POST", body: { board } });
|
|
const closed = result.closed ? `, ${count(result.closed, "old role")} closed` : "";
|
|
window.toast.info(`${company.name}: ${result.source.target} listed `
|
|
+ `${count(result.found, "role")}${closed}. Reading now, or with the pull right after.`);
|
|
await done();
|
|
} catch (err) {
|
|
save.disabled = false;
|
|
}
|
|
});
|
|
if (company.subscription_id != null) {
|
|
const detach = document.createElement("button");
|
|
detach.type = "button";
|
|
detach.className = "linky";
|
|
detach.textContent = "Detach";
|
|
detach.addEventListener("click", async () => {
|
|
if (company.open_roles
|
|
&& !window.confirm(`Detach ${company.name}'s board (${company.board})? `
|
|
+ `Its ${count(company.open_roles, "open role")} close.`)) {
|
|
return;
|
|
}
|
|
detach.disabled = true;
|
|
try {
|
|
const result = await api(`/api/companies/${encodeURIComponent(company.key)}/board`,
|
|
{ method: "DELETE" });
|
|
window.toast.info(`${company.name}: board detached, `
|
|
+ `${count(result.closed, "open role")} closed.`);
|
|
await done();
|
|
} catch (err) {
|
|
detach.disabled = false;
|
|
}
|
|
});
|
|
form.append(detach);
|
|
}
|
|
return form;
|
|
}
|
|
|
|
/** The open row's reach editor, for a row with a board: the agents' reach
|
|
or the board's own countries and remote. The wide reach is the
|
|
company's All roles tag, which the editor points to. */
|
|
function reachEditor(company, rerender) {
|
|
if (company.subscription_id == null) return null;
|
|
const own = company.reach_params || {};
|
|
const form = document.createElement("form");
|
|
form.className = "board-edit";
|
|
form.autocomplete = "off";
|
|
const mode = document.createElement("select");
|
|
mode.setAttribute("aria-label", `What ${company.name}'s board is asked for`);
|
|
for (const [value, label] of [["agents", "Agents' reach"], ["own", "These countries"]]) {
|
|
mode.appendChild(new Option(label, value));
|
|
}
|
|
mode.value = own.countries || own.remote != null ? "own" : "agents";
|
|
const codes = document.createElement("input");
|
|
codes.type = "text";
|
|
codes.spellcheck = false;
|
|
codes.placeholder = "DK SE";
|
|
codes.value = (own.countries || []).join(" ");
|
|
codes.setAttribute("aria-label", "Country codes");
|
|
const remote = document.createElement("select");
|
|
remote.setAttribute("aria-label", "Remote roles");
|
|
remote.append(new Option("and remote", "yes"), new Option("no remote", "no"));
|
|
remote.value = own.remote ? "yes" : "no";
|
|
const save = document.createElement("button");
|
|
save.type = "submit";
|
|
save.textContent = "Set reach";
|
|
const shown = () => { codes.hidden = remote.hidden = mode.value !== "own"; };
|
|
mode.addEventListener("change", shown);
|
|
shown();
|
|
const note = document.createElement("span");
|
|
note.className = "muted small";
|
|
note.textContent = company.all_roles
|
|
? "The All roles tag is on; a reach set here wins over it."
|
|
: "All roles within the agents' wide reach (Europe): the All tag beside the stars.";
|
|
form.append(mode, codes, remote, save, note);
|
|
form.addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
save.disabled = true;
|
|
try {
|
|
const result = await api(`/api/sources/${company.subscription_id}/reach`, {
|
|
method: "POST",
|
|
body: { mode: mode.value, countries: codes.value.split(/[\s,]+/).filter(Boolean),
|
|
remote: remote.value === "yes" },
|
|
});
|
|
window.toast.info(`${company.name}: reads ${result.reach} from the next pull.`);
|
|
editing = null;
|
|
await loadCompanies(agentId());
|
|
rerender();
|
|
} catch (err) {
|
|
save.disabled = false;
|
|
}
|
|
});
|
|
return form;
|
|
}
|
|
|
|
/** The board switch, or nothing for a company with no board:
|
|
there is no subscription to switch. */
|
|
function onSwitch(company, rerender) {
|
|
if (company.subscription_id == null) return null;
|
|
const box = document.createElement("input");
|
|
box.type = "checkbox";
|
|
box.checked = Boolean(company.on);
|
|
box.setAttribute("aria-label", `Read ${company.name}'s board`);
|
|
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 && company.open_roles
|
|
&& !window.confirm(`Switch off ${company.name}'s board? `
|
|
+ `Its ${count(company.open_roles, "open role")} close.`)) {
|
|
box.checked = true;
|
|
return;
|
|
}
|
|
box.disabled = true;
|
|
try {
|
|
const result = await api(`/api/sources/${company.subscription_id}/switch`,
|
|
{ method: "POST", body: { on: box.checked } });
|
|
company.on = result.source.on;
|
|
if (!box.checked && result.closed) {
|
|
window.toast.info(`${company.name}: ${count(result.closed, "open role")} closed.`);
|
|
} else if (box.checked) {
|
|
window.toast.info(`${company.name}: reading now, or with the pull right after.`);
|
|
}
|
|
} catch (err) {
|
|
box.checked = !box.checked;
|
|
}
|
|
box.disabled = false;
|
|
rerender();
|
|
});
|
|
return box;
|
|
}
|
|
|
|
function matchesFilter(company, filter) {
|
|
switch (filter) {
|
|
case "favourite": return company.affinity === 2;
|
|
case "liked": return company.affinity === 1;
|
|
case "hidden": return company.affinity === -1;
|
|
case "all_roles": return company.all_roles === true;
|
|
case "on": return company.on === true;
|
|
case "off": return company.on === false;
|
|
case "unreadable": return company.subscription_id == null && !company.from_search;
|
|
default: return true;
|
|
}
|
|
}
|
|
|
|
function render() {
|
|
const q = $("#c-q").value.trim().toLowerCase();
|
|
const filter = $("#c-filter").value;
|
|
const rows = [...companies.values()]
|
|
.filter((c) => !q || c.name.toLowerCase().includes(q) || c.board.includes(q)
|
|
|| (c.ats || "").includes(q) || (c.country || "").toLowerCase() === q)
|
|
.filter((c) => matchesFilter(c, filter))
|
|
.sort((a, b) => b.open_roles - a.open_roles || a.name.localeCompare(b.name));
|
|
const body = $("#companies-list");
|
|
body.replaceChildren();
|
|
for (const company of rows) {
|
|
const row = document.createElement("div");
|
|
row.className = "crow";
|
|
if (company.affinity === -1) row.dataset.hidden = "true";
|
|
const link = document.createElement("a");
|
|
link.className = "name";
|
|
link.href = `/?company=${encodeURIComponent(company.name)}`;
|
|
link.title = "This company's open roles";
|
|
link.textContent = company.name;
|
|
row.appendChild(link);
|
|
const switchBox = onSwitch(company, render);
|
|
if (switchBox) row.appendChild(switchBox);
|
|
const agent = agentId();
|
|
if (agent != null) {
|
|
row.appendChild(affinityControl(company.key, company.affinity, async () => render(), agent));
|
|
}
|
|
row.appendChild(allRolesControl(company.key, company.all_roles, async () => {
|
|
await loadCompanies(agentId());
|
|
render();
|
|
}));
|
|
row.appendChild(subLine(company, render));
|
|
if (editing === company.key) {
|
|
row.appendChild(boardEditor(company, render));
|
|
const reach = reachEditor(company, render);
|
|
if (reach) row.appendChild(reach);
|
|
}
|
|
body.appendChild(row);
|
|
}
|
|
const open = rows.reduce((sum, c) => sum + c.open_roles, 0);
|
|
$("#companies-count").textContent =
|
|
`${count(rows.length, "company", "companies")}, ${count(open, "open role")}`;
|
|
}
|
|
|
|
// -------------------------------------------------------------------- follow
|
|
|
|
async function addBoard(event) {
|
|
event.preventDefault();
|
|
const status = $("#board-status");
|
|
const button = $("#board-form button[type=submit]");
|
|
const name = $("#b-name").value.trim();
|
|
const board = `${$("#b-host").value}:${$("#b-slug").value.trim()}`;
|
|
// This name may already be a company with a different board: `set_board`
|
|
// would replace it and close its roles, the same as the row's own
|
|
// editor does, so it asks first too.
|
|
const existing = [...companies.values()]
|
|
.find((c) => c.name.toLowerCase() === name.toLowerCase());
|
|
if (existing && existing.board && existing.board !== board
|
|
&& !window.confirm(`Replace ${existing.name}'s board (${existing.board}) `
|
|
+ `with ${board}? Its ${count(existing.open_roles, "open role")} close.`)) {
|
|
return;
|
|
}
|
|
button.disabled = true;
|
|
status.textContent = "Reading the board once before following it…";
|
|
try {
|
|
const result = await api("/api/boards", {
|
|
method: "POST",
|
|
body: { board, name },
|
|
});
|
|
await loadCompanies(agentId());
|
|
render();
|
|
$("#board-form").reset();
|
|
status.textContent = `Following ${result.source.label}: `
|
|
+ `${count(result.found, "role")} listed. Reading now, or with the pull right after.`;
|
|
} catch (err) {
|
|
status.textContent = "";
|
|
}
|
|
button.disabled = false;
|
|
}
|
|
|
|
// -------------------------------------------------------------------- init
|
|
|
|
async function switchAgent() {
|
|
try {
|
|
await loadCompanies(agentId());
|
|
} catch (err) {
|
|
return;
|
|
}
|
|
render();
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
if (!$("#companies-list")) return;
|
|
try {
|
|
await loadAgents();
|
|
await loadCompanies(agentId());
|
|
} catch (err) {
|
|
return;
|
|
}
|
|
render();
|
|
$("#c-q").addEventListener("input", debounce(render, 150));
|
|
$("#c-filter").addEventListener("change", render);
|
|
$("#c-agent").addEventListener("change", switchAgent);
|
|
$("#board-form").addEventListener("submit", addBoard);
|
|
});
|
|
})();
|