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>
239 lines
9.8 KiB
JavaScript
239 lines
9.8 KiB
JavaScript
/* The one way this app talks to its own API.
|
|
*
|
|
* const kinds = await api("/api/jobs/kinds");
|
|
* await api("/api/jobs", { method: "POST", body: { kind: "tick" } });
|
|
*
|
|
* Every button on every tab goes through `api`, so a failure is always one
|
|
* toast carrying the server's own sentence. That is the other half of the
|
|
* bargain the exception handler in `app/main.py` makes: it promises every
|
|
* error body is `{"detail": "<one readable sentence>"}`, and this is what
|
|
* cashes the promise. Wording a failure in the browser as well would be two
|
|
* descriptions of one thing, and the client's would be the one that was wrong.
|
|
*
|
|
* `body` may be a plain object; it is JSON-encoded with the header set, since
|
|
* forgetting the header is the failure that looks like a server bug.
|
|
*
|
|
* Beside `api` on `window`, the few helpers more than one page script uses:
|
|
* `count`, `cell`, `debounce`, `safeUrl`, and the company list with its
|
|
* affinity control (`loadCompanies`, `companies`, `affinityControl`,
|
|
* `setAffinity`) and the All roles tag beside it (`allRolesControl`). One
|
|
* copy each, since two copies of a helper is one of them
|
|
* being fixed.
|
|
*
|
|
* Affinity is per search agent. The three company helpers take the agent's
|
|
* id as an optional last argument and send it along; a caller that passes
|
|
* none gets the server's default agent, the enabled one with the lowest id.
|
|
*/
|
|
|
|
(function () {
|
|
"use strict";
|
|
|
|
async function api(path, options) {
|
|
const o = Object.assign({}, options);
|
|
const headers = Object.assign({ Accept: "application/json" }, o.headers);
|
|
if (o.body !== undefined && typeof o.body !== "string") {
|
|
headers["Content-Type"] = "application/json";
|
|
o.body = JSON.stringify(o.body);
|
|
}
|
|
o.headers = headers;
|
|
|
|
let response;
|
|
try {
|
|
response = await fetch(path, o);
|
|
} catch (e) {
|
|
// The network, not the server: there is no `detail` to read.
|
|
window.toast.error("Could not reach the server. Is it still running?");
|
|
throw e;
|
|
}
|
|
const text = await response.text();
|
|
let data = null;
|
|
if (text) {
|
|
try { data = JSON.parse(text); } catch (e) { data = null; }
|
|
}
|
|
if (!response.ok) {
|
|
const detail = (data && data.detail)
|
|
|| `The server answered ${response.status} and said nothing about why.`;
|
|
window.toast.error(String(detail));
|
|
const error = new Error(detail);
|
|
error.status = response.status;
|
|
error.detail = detail;
|
|
throw error;
|
|
}
|
|
return data;
|
|
}
|
|
|
|
/* `window.esc` is toasts.js's — it loads first and had to have one anyway,
|
|
and two copies of an escaper is one of them being fixed. See base.html
|
|
for the load order that makes this safe. */
|
|
|
|
/* The queue's own elements do their own fetching, so their failures never
|
|
pass through `api()`. They raise an event carrying the server's `detail`
|
|
instead, and this is the same bargain kept one level out: one toast, the
|
|
server's sentence, nothing reworded here.
|
|
|
|
`desk-jobs:notice` is a notice arriving on the element's socket, which
|
|
live.js already delivers as `app:notice` — so it is deliberately not
|
|
listened for. Two toasts for one failure is the bug that listening would
|
|
buy. */
|
|
document.addEventListener("desk-schedules:error", (e) => {
|
|
const detail = (e.detail && e.detail.detail) || "That change was refused.";
|
|
window.toast.error(String(detail));
|
|
});
|
|
|
|
// ----------------------------------------------------------------- helpers
|
|
|
|
/** `count(3, "role")` is "3 roles"; `many` for an irregular plural. */
|
|
function count(n, one, many) {
|
|
return `${n} ${n === 1 ? one : many || `${one}s`}`;
|
|
}
|
|
|
|
/** A `<td>` with its column name in `data-label`, which the phone layout
|
|
prints above the value once the table's head is hidden. */
|
|
function cell(tr, label, content) {
|
|
const td = document.createElement("td");
|
|
td.dataset.label = label;
|
|
if (typeof content === "string") td.textContent = content;
|
|
else td.appendChild(content);
|
|
tr.appendChild(td);
|
|
return td;
|
|
}
|
|
|
|
function debounce(fn, ms) {
|
|
let timer;
|
|
return (...args) => {
|
|
clearTimeout(timer);
|
|
timer = setTimeout(() => fn(...args), ms);
|
|
};
|
|
}
|
|
|
|
/** An `http(s)` URL, or "" for anything else — `javascript:`, a bare path,
|
|
empty. Every link whose address came from a feed, a board, discovery or
|
|
a person (a search result's `url`, a company's `careers_url`, a saved
|
|
application's `url`, a role's `original_url`) goes through this before
|
|
it lands in an `href`, so a board that wrote something other than a URL
|
|
cannot make this page navigate to it. Moved here from roles.js, which
|
|
first needed it for the reader's outbound links. */
|
|
function safeUrl(u) {
|
|
return /^https?:\/\//i.test(u || "") ? u : "";
|
|
}
|
|
|
|
// --------------------------------------------------------------- companies
|
|
|
|
/** @type {Map<string, object>} company_key -> row from /api/companies. One
|
|
Map, refilled in place, so a script holding it sees every reload. */
|
|
const companies = new Map();
|
|
|
|
async function loadCompanies(agent) {
|
|
const query = agent == null ? "" : `?agent=${encodeURIComponent(agent)}`;
|
|
const rows = await api(`/api/companies${query}`);
|
|
companies.clear();
|
|
for (const c of rows) companies.set(c.key, c);
|
|
return companies;
|
|
}
|
|
|
|
/* Three icon toggles, not four: there is no button for neutral, since
|
|
pressing the one that is already pressed is how a company gets back to
|
|
it. "Hidden" is the word for -1 everywhere the UI names it, even though
|
|
the code and the API keep `mute`/`hide_muted` — a rename in the store
|
|
and CLI is a separate, larger change than this control. */
|
|
const AFFINITY_LEVELS = [
|
|
{ value: 2, icon: "★", label: "Favourite" }, // ★
|
|
{ value: 1, icon: "▲", label: "Like" }, // ▲
|
|
{ value: -1, icon: "⦸", label: "Hidden" }, // ⊘
|
|
];
|
|
|
|
/* `onChange` runs after the server accepted the change, so each page
|
|
decides what a change means for it: the role list refetches, since a
|
|
hidden company's roles come out of it, and the company list redraws.
|
|
It is given the route's answer, the company with `score_job`, the id
|
|
of the `score` that carries the change, for a page that waits on it. */
|
|
function affinityControl(companyKey, current, onChange, agent) {
|
|
const span = document.createElement("span");
|
|
span.className = "affinity";
|
|
span.setAttribute("role", "group");
|
|
span.setAttribute("aria-label", "Company affinity");
|
|
for (const level of AFFINITY_LEVELS) {
|
|
const pressed = level.value === current;
|
|
const button = document.createElement("button");
|
|
button.type = "button";
|
|
button.dataset.value = String(level.value);
|
|
button.textContent = level.icon;
|
|
button.title = level.label;
|
|
button.setAttribute("aria-label", level.label);
|
|
button.setAttribute("aria-pressed", String(pressed));
|
|
button.addEventListener("click", () =>
|
|
setAffinity(companyKey, pressed ? 0 : level.value, onChange, agent));
|
|
span.appendChild(button);
|
|
}
|
|
return span;
|
|
}
|
|
|
|
async function setAffinity(companyKey, affinity, onChange, agent) {
|
|
if (!companyKey) return;
|
|
const body = agent == null ? { affinity } : { affinity, agent };
|
|
let updated;
|
|
try {
|
|
updated = await api(`/api/companies/${encodeURIComponent(companyKey)}/affinity`, {
|
|
method: "POST", body,
|
|
});
|
|
} catch (err) {
|
|
return;
|
|
}
|
|
const company = companies.get(companyKey);
|
|
if (company) company.affinity = updated.affinity;
|
|
if (onChange) await onChange(updated);
|
|
}
|
|
|
|
/* The All roles tag, one more toggle in the affinity buttons' fragment
|
|
beside them, in a group of its own since it is global rather than an
|
|
agent's stance: every board of the company is read with the agents'
|
|
wide reach, Europe today, rather than their places (app/jobs/reach.py). On queues a pull on the server, as the other
|
|
reopening switches do; off asks nothing, since no role closes until the
|
|
next pull reads the board with its filter again. `onChange` runs after
|
|
the server accepted it, with the company it answered. */
|
|
function allRolesControl(companyKey, current, onChange) {
|
|
const span = document.createElement("span");
|
|
span.className = "affinity";
|
|
span.setAttribute("role", "group");
|
|
span.setAttribute("aria-label", "Company tag");
|
|
const button = document.createElement("button");
|
|
button.type = "button";
|
|
button.dataset.value = "all";
|
|
button.textContent = "All";
|
|
button.title = current
|
|
? "All roles within the agents' wide reach (Europe), not only their places. Turning it "
|
|
+ "off asks nothing first: no role closes until the next pull reads the board again."
|
|
: "All roles: read this company's boards for all roles within the agents' wide reach "
|
|
+ "(Europe), not only their places. Turning it on queues a pull.";
|
|
button.setAttribute("aria-label", "All roles");
|
|
button.setAttribute("aria-pressed", String(Boolean(current)));
|
|
button.addEventListener("click", () => setAllRoles(companyKey, !current, onChange));
|
|
span.appendChild(button);
|
|
return span;
|
|
}
|
|
|
|
async function setAllRoles(companyKey, on, onChange) {
|
|
if (!companyKey) return;
|
|
let updated;
|
|
try {
|
|
updated = await api(`/api/companies/${encodeURIComponent(companyKey)}/all-roles`, {
|
|
method: "POST", body: { on },
|
|
});
|
|
} catch (err) {
|
|
return;
|
|
}
|
|
const company = companies.get(companyKey);
|
|
if (company) company.all_roles = Boolean(updated.all_roles);
|
|
window.toast.info(on
|
|
? `${updated.name}: all roles within the agents' wide reach. `
|
|
+ "Reading now, or with the pull right after."
|
|
: `${updated.name}: back to its reach from the next pull.`);
|
|
if (onChange) await onChange(updated);
|
|
}
|
|
|
|
Object.assign(window, {
|
|
api, count, cell, debounce, safeUrl, companies, loadCompanies, affinityControl, setAffinity,
|
|
allRolesControl,
|
|
});
|
|
})();
|