Companies gets a Follow-a-company form, a combined affinity/on-off filter, and one .srow-style row per company (name, board or "from searches", open roles, on/off switch, affinity). The prune-by-region tool moves here from Searches, folded under the list in a <details>. The lede states the difference between switching a board off (stops reading it, closes its roles) and hiding a company (keeps reading it, keeps its roles out of the list and unscored). affinityControl becomes three icon toggles (favourite, like, hidden) instead of four letter buttons with a neutral one; pressing the pressed icon clears back to neutral. "Hidden" is the word everywhere the UI names affinity -1; the code keeps mute/hide_muted. CompanyOut carries each company's board subscription id and on-state, so the Companies page needs only /api/companies, not a second fetch of /api/sources. Setting a company's affinity now enqueues a score job, deduped by the queue to one run. Searches keeps only the search form and list, in the same row style; company boards and the prune tool left with it, and "Pull now" left for Pulls (package B3). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
193 lines
7.3 KiB
JavaScript
193 lines
7.3 KiB
JavaScript
/* The Companies page: every company, its board, its open roles, its
|
|
* affinity, following a new one, and pruning boards with nothing open in a
|
|
* region.
|
|
*
|
|
* 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.
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
const { api, count, debounce, companies, loadCompanies, affinityControl } = window;
|
|
const $ = (sel, root = document) => root.querySelector(sel);
|
|
|
|
// ---------------------------------------------------------------- the list
|
|
|
|
function subLine(company) {
|
|
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 {
|
|
sub.append("from searches");
|
|
}
|
|
sub.append(` · ${count(company.open_roles, "open role")}`);
|
|
return sub;
|
|
}
|
|
|
|
/** The board switch, or nothing for a company known only from a search:
|
|
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 () => {
|
|
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.`);
|
|
}
|
|
} 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 "on": return company.on === true;
|
|
case "off": return company.on === false;
|
|
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))
|
|
.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);
|
|
row.appendChild(affinityControl(company.key, company.affinity, async () => render()));
|
|
row.appendChild(subLine(company));
|
|
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]");
|
|
button.disabled = true;
|
|
status.textContent = "Reading the board once before following it…";
|
|
const board = `${$("#b-host").value}:${$("#b-slug").value.trim()}`;
|
|
try {
|
|
const result = await api("/api/boards", {
|
|
method: "POST",
|
|
body: { board, name: $("#b-name").value.trim() },
|
|
});
|
|
await loadCompanies();
|
|
render();
|
|
$("#board-form").reset();
|
|
status.textContent = `Following ${result.source.label}: `
|
|
+ `${count(result.found, "role")} listed. They arrive with the next pull.`;
|
|
} catch (err) {
|
|
status.textContent = "";
|
|
}
|
|
button.disabled = false;
|
|
}
|
|
|
|
// -------------------------------------------------------------------- prune
|
|
|
|
/** @type {object[]} the boards the last preview named. */
|
|
let pruneBoards = [];
|
|
|
|
async function previewPrune(event) {
|
|
event.preventDefault();
|
|
const status = $("#prune-status");
|
|
const list = $("#prune-list");
|
|
const region = $("#p-region").value.trim();
|
|
list.replaceChildren();
|
|
$("#p-apply").disabled = true;
|
|
$("#p-apply").textContent = "Switch off these boards";
|
|
status.textContent = "Checking every board…";
|
|
try {
|
|
const result = await api(`/api/prune?region=${encodeURIComponent(region)}`);
|
|
pruneBoards = result.boards;
|
|
const roles = pruneBoards.reduce((sum, b) => sum + b.open_roles, 0);
|
|
status.textContent = pruneBoards.length
|
|
? `${count(pruneBoards.length, "board")} with no open role in ${region}, `
|
|
+ `${count(roles, "open role")} between them:`
|
|
: `Every board that is on has a role in ${region}.`;
|
|
for (const board of pruneBoards) {
|
|
const li = document.createElement("li");
|
|
li.textContent = `${board.label} (${board.target}), ${count(board.open_roles, "open role")}`;
|
|
list.appendChild(li);
|
|
}
|
|
$("#p-apply").disabled = pruneBoards.length === 0;
|
|
$("#p-apply").textContent = `Switch off these ${count(pruneBoards.length, "board")}`;
|
|
} catch (err) {
|
|
status.textContent = "";
|
|
}
|
|
}
|
|
|
|
async function applyPrune() {
|
|
const region = $("#p-region").value.trim();
|
|
if (!window.confirm(`Switch off ${count(pruneBoards.length, "board")} and close their roles? `
|
|
+ "Each can be switched back on in the list above.")) return;
|
|
$("#p-apply").disabled = true;
|
|
try {
|
|
const result = await api("/api/prune", { method: "POST", body: { region } });
|
|
$("#prune-status").textContent = `Switched off ${count(result.pruned, "board")}, `
|
|
+ `closed ${count(result.roles_closed, "role")}. ${result.boards_left} boards are on.`;
|
|
$("#prune-list").replaceChildren();
|
|
$("#p-apply").textContent = "Switch off these boards";
|
|
await loadCompanies();
|
|
render();
|
|
} catch (err) {
|
|
$("#p-apply").disabled = false;
|
|
}
|
|
}
|
|
|
|
// -------------------------------------------------------------------- init
|
|
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
try {
|
|
await loadCompanies();
|
|
} catch (err) {
|
|
return;
|
|
}
|
|
render();
|
|
$("#c-q").addEventListener("input", debounce(render, 150));
|
|
$("#c-filter").addEventListener("change", render);
|
|
$("#board-form").addEventListener("submit", addBoard);
|
|
$("#prune-form").addEventListener("submit", previewPrune);
|
|
$("#p-apply").addEventListener("click", applyPrune);
|
|
});
|
|
})();
|