/* The Watchlist tab: the companies the app cannot read a board of, for * going through their careers pages by hand. * * The list is app.js's shared company list (`/api/companies`), narrowed to * the rule the "Not readable yet" filter of Company boards uses (no board * subscription, not from a search), plus every company whose board is * failing. The rating (`watch`: null unrated, 1 yes, 0 no) is global, not a * search agent's affinity, and is set with `POST .../watch`. Opening the * careers page stamps `careers_opened_at` (`POST .../opened`), so the row can * say when, and the On the list filter sorts what was opened longest ago * first. The filter is remembered per browser, and opens on Unrated. */ (function () { "use strict"; const { api, count, debounce, safeUrl, companies, loadCompanies } = window; const $ = (sel, root = document) => root.querySelector(sel); const FILTER = "roledesk.watchlist"; const FILTERS = [ ["unrated", "Unrated"], ["yes", "On the list"], ["no", "Not on the list"], ["all", "All"], ]; const DAY = 24 * 60 * 60 * 1000; /** The key of the row whose careers link editor is open, or null. */ let editing = null; function savedFilter() { try { const value = localStorage.getItem(FILTER); return FILTERS.some(([f]) => f === value) ? value : "unrated"; } catch (err) { return "unrated"; } } function saveFilter(value) { try { localStorage.setItem(FILTER, value); } catch (err) { /* not remembered */ } } /** The companies the tab is about, before any filter. */ function watched() { return [...companies.values()].filter( (c) => (c.subscription_id == null && !c.from_search) || c.failing_since); } function matchesFilter(company, filter) { switch (filter) { case "unrated": return company.watch == null; case "yes": return company.watch === 1; case "no": return company.watch === 0; default: return true; } } function ago(iso) { if (!iso) return "not opened yet"; const days = Math.floor((Date.now() - new Date(iso).getTime()) / DAY); if (Number.isNaN(days)) return "not opened yet"; if (days < 1) return "opened today"; return `opened ${count(days, "day")} ago`; } /** Where the link goes: the careers page, else the company's own site. */ function target(company) { const careers = safeUrl(company.careers_url); if (careers) return { url: careers, label: "Careers page" }; const site = company.domain ? safeUrl(`https://${company.domain}`) : ""; return site ? { url: site, label: "Website" } : null; } async function rate(company, watch, rerender) { try { const updated = await api(`/api/companies/${encodeURIComponent(company.key)}/watch`, { method: "POST", body: { watch }, }); company.watch = updated.watch; } catch (err) { return; } rerender(); } /** Yes and No, in `.affinity`'s look; pressing the pressed one clears it. */ function ratingControl(company, rerender) { const span = document.createElement("span"); span.className = "affinity"; span.setAttribute("role", "group"); span.setAttribute("aria-label", `Watch ${company.name}`); for (const [value, label, watch] of [[1, "Yes", true], [-1, "No", false]]) { const pressed = company.watch === (watch ? 1 : 0); const button = document.createElement("button"); button.type = "button"; button.dataset.value = String(value); button.textContent = label; button.setAttribute("aria-pressed", String(pressed)); button.addEventListener("click", () => rate(company, pressed ? null : watch, rerender)); span.appendChild(button); } return span; } function subLine(company, rerender) { const sub = document.createElement("span"); sub.className = "sub"; const badge = document.createElement("span"); badge.className = "badge"; badge.textContent = company.board || company.ats || "board not found"; sub.appendChild(badge); const place = [company.city, (company.country || "").toUpperCase()].filter(Boolean).join(", "); if (place) sub.append(` ${place}`); if (company.failing_since) { const failing = document.createElement("span"); failing.className = "failing"; failing.textContent = `board failing since ${company.failing_since.slice(0, 10)}`; sub.append(" · "); sub.appendChild(failing); } sub.append(" · "); const where = target(company); if (where) { const link = document.createElement("a"); link.href = where.url; link.target = "_blank"; link.rel = "noopener noreferrer"; link.textContent = where.label; link.addEventListener("click", () => opened(company, rerender)); sub.appendChild(link); sub.append(` · ${ago(company.careers_opened_at)} `); } const edit = document.createElement("button"); edit.type = "button"; edit.className = "linky"; edit.textContent = company.careers_url ? "Edit link" : "Add careers page"; 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 link was followed: stamp it. The new tab is already open, so this only updates the row, and only the stamp, so the row does not jump. */ async function opened(company, rerender) { try { const updated = await api(`/api/companies/${encodeURIComponent(company.key)}/opened`, { method: "POST" }); company.careers_opened_at = updated.careers_opened_at; } catch (err) { return; } rerender(); } function linkEditor(company, rerender) { const form = document.createElement("form"); form.className = "board-edit"; form.autocomplete = "off"; const input = document.createElement("input"); input.type = "url"; input.spellcheck = false; input.placeholder = "https://"; input.value = company.careers_url || ""; input.setAttribute("aria-label", `${company.name}'s careers page`); const save = document.createElement("button"); save.type = "submit"; save.textContent = "Save"; const cancel = document.createElement("button"); cancel.type = "button"; cancel.className = "linky"; cancel.textContent = "Cancel"; cancel.addEventListener("click", () => { editing = null; rerender(); }); form.append(input, save, cancel); form.addEventListener("submit", async (event) => { event.preventDefault(); save.disabled = true; try { const updated = await api(`/api/companies/${encodeURIComponent(company.key)}/careers`, { method: "POST", body: { careers_url: input.value.trim() } }); company.careers_url = updated.careers_url; editing = null; rerender(); } catch (err) { save.disabled = false; } }); return form; } /** The filter's options with their counts, kept on the chosen one. */ function drawFilter(all) { const select = $("#w-filter"); const chosen = select.value || savedFilter(); select.replaceChildren(); for (const [value, label] of FILTERS) { const option = document.createElement("option"); option.value = value; option.textContent = `${label} (${all.filter((c) => matchesFilter(c, value)).length})`; select.appendChild(option); } select.value = chosen; } function render() { const all = watched(); drawFilter(all); const q = $("#w-q").value.trim().toLowerCase(); const filter = $("#w-filter").value; const rows = all .filter((c) => !q || c.name.toLowerCase().includes(q) || (c.board || c.ats || "").includes(q) || (c.country || "").toLowerCase() === q) .filter((c) => matchesFilter(c, filter)); // Overdue first on the list, never opened on top (the empty stamp sorts // before any date); every other filter is by name. rows.sort((a, b) => (filter === "yes" ? a.careers_opened_at.localeCompare(b.careers_opened_at) : 0) || a.name.localeCompare(b.name)); const body = $("#watchlist-list"); body.replaceChildren(); for (const company of rows) { const row = document.createElement("div"); row.className = "crow"; const name = document.createElement("span"); name.className = "name"; name.textContent = company.name; row.appendChild(name); row.appendChild(ratingControl(company, render)); row.appendChild(subLine(company, render)); if (editing === company.key) row.appendChild(linkEditor(company, render)); body.appendChild(row); } $("#watchlist-count").textContent = count(rows.length, "company", "companies"); } document.addEventListener("DOMContentLoaded", async () => { if (!$("#watchlist-list")) return; try { await loadCompanies(); } catch (err) { return; } $("#w-filter").value = savedFilter(); render(); $("#w-q").addEventListener("input", debounce(render, 150)); $("#w-filter").addEventListener("change", () => { saveFilter($("#w-filter").value); render(); }); }); })();