A search agent is a persona, and every like or pass belongs to the one being viewed. The rail now opens on Viewing as: a full-width chooser, the name alone with one agent, a note with none, remembered in the URL and localStorage. The vote buttons, the reader's affinity control, the feed, the tag counts and the reader fetch all carry that agent; before, votes sent none and every one landed on the default agent. Best first and Newest first are back as the segmented control under Order, independent of the agent. The rail selects no longer clip descenders: Chrome ignores line-height on a select, so the fixed box is gone in favour of padding and a min-height. Settings render inside a sticky rail (Search agents; Sources with Job boards, Company boards, Runs, Messages; Account), a strip under the header on a phone, so Runs is one click away however long the company list gets. Sources shows one section at a time, chosen by the fragment, so the old redirects still land. Search agents are cards, newest first: name, an On/Off switch with its word, the intent in full, the weights, Edit in place, New agent on top, and a link to Roles as that agent. Switching one on rescores it. replace_scores writes nothing for an agent deleted while score was running, instead of failing the run on the foreign key. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
171 lines
6.4 KiB
JavaScript
171 lines
6.4 KiB
JavaScript
/* ▲ ● ▼ ★ for one role: the feed row and the reader bar share them.
|
|
*
|
|
* news's `voteButtons`, `starButton` and `vote()`, adapted. Three stances and
|
|
* no stance is four states from three buttons; pressing the stance a role
|
|
* already has clears it, which is the only way back to unjudged, since ● is
|
|
* an answer ("neither way") and not the absence of one. A vote never hides a
|
|
* role: a stray tap stays on screen, pressed, and one more tap undoes it.
|
|
*
|
|
* ★ is Save. It files the role under Applications (`POST /api/applications`)
|
|
* and, pressed again, takes it back out (`DELETE /api/applications/{id}`),
|
|
* which the server allows only while the application is still at `saved`; a
|
|
* 409 is toasted by `api()` and the star stays lit.
|
|
*
|
|
* Both are optimistic: the button changes before the request and changes back
|
|
* if it fails. `api()` has already toasted the failure by then.
|
|
*
|
|
* A vote is a search agent's: `{agent}` names whose, and is sent with the POST
|
|
* and the DELETE. Without it the server's default agent takes the vote, which
|
|
* is right on a page that picks none. Save is not an agent's; it is the same
|
|
* application whoever is viewing.
|
|
*
|
|
* One role can have two sets of these on screen at once, its feed row's and
|
|
* the reader bar's, each bound to its own copy of the role. A change in one is
|
|
* echoed to every other set for the same uid (`echo`), which copies the vote
|
|
* and the read mark across and repaints it, so the row never shows a stance
|
|
* the reader has taken back. Only sets for the same agent are echoed to,
|
|
* since another agent can hold a different stance on the same role. A vote
|
|
* marks the role read on the server, and the answer's `read_at` is copied onto
|
|
* the role with the vote.
|
|
*
|
|
* await loadTracked();
|
|
* container.append(judgeButtons(role, (role) => row.classList.toggle(...), { agent: 2 }));
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
const { api } = window;
|
|
|
|
/** @type {Map<string, object>} role_uid -> application, for roles saved */
|
|
const tracked = new Map();
|
|
|
|
async function loadTracked() {
|
|
const applications = await api("/api/applications");
|
|
tracked.clear();
|
|
for (const a of applications) if (a.role_uid) tracked.set(a.role_uid, a);
|
|
return tracked;
|
|
}
|
|
|
|
/** Every set of buttons made, so a change in one repaints the others for
|
|
the same role. A set whose buttons have left the document is dropped the
|
|
next time a change is echoed. */
|
|
const groups = new Set();
|
|
|
|
function echo(from, agent) {
|
|
for (const group of groups) {
|
|
if (!group.star.isConnected) { groups.delete(group); continue; }
|
|
if (group.role === from || group.role.uid !== from.uid || group.agent !== agent) continue;
|
|
group.role.vote = from.vote;
|
|
group.role.voted_at = from.voted_at;
|
|
if (from.read_at !== undefined) group.role.read_at = from.read_at;
|
|
group.paint();
|
|
}
|
|
}
|
|
|
|
const STANCES = [
|
|
{ value: 1, cls: "up", glyph: "▲", said: "Like" },
|
|
{ value: 0, cls: "mid", glyph: "●", said: "Neither way" },
|
|
{ value: -1, cls: "down", glyph: "▼", said: "Pass" },
|
|
];
|
|
|
|
function button(cls, glyph) {
|
|
const b = document.createElement("button");
|
|
b.type = "button";
|
|
b.className = cls;
|
|
b.textContent = glyph;
|
|
return b;
|
|
}
|
|
|
|
/* The four buttons as a fragment, bound to `role`, which they read and
|
|
write in place (`role.vote`), so the caller's copy is always current.
|
|
`onChange(role)` runs after every change, optimistic or reverted: the
|
|
feed dims a passed row with it. `agent` is whose vote, the server's
|
|
default agent when absent. */
|
|
function judgeButtons(role, onChange, { agent = null } = {}) {
|
|
const fragment = document.createDocumentFragment();
|
|
const votes = STANCES.map((s) => {
|
|
const b = button(s.cls, s.glyph);
|
|
b.addEventListener("click", () => vote(s.value));
|
|
fragment.appendChild(b);
|
|
return b;
|
|
});
|
|
const star = button("star", "★");
|
|
star.addEventListener("click", () => toggleSave());
|
|
fragment.appendChild(star);
|
|
|
|
function paint() {
|
|
STANCES.forEach((s, i) => {
|
|
const on = role.vote === s.value;
|
|
const said = on ? `${s.said} — tap again to undo` : s.said;
|
|
votes[i].setAttribute("aria-pressed", String(on));
|
|
votes[i].title = said;
|
|
votes[i].setAttribute("aria-label", said);
|
|
});
|
|
const saved = tracked.has(role.uid);
|
|
const said = saved ? "Saved to Applications — tap to unsave" : "Save to Applications";
|
|
star.setAttribute("aria-pressed", String(saved));
|
|
star.title = said;
|
|
star.setAttribute("aria-label", said);
|
|
if (onChange) onChange(role);
|
|
}
|
|
|
|
async function vote(value) {
|
|
const before = role.vote ?? null;
|
|
const clearing = before === value;
|
|
role.vote = clearing ? null : value;
|
|
paint();
|
|
echo(role, agent);
|
|
const path = `/api/roles/${encodeURIComponent(role.uid)}/vote`;
|
|
const byDefault = agent == null;
|
|
try {
|
|
const updated = clearing
|
|
? await api(byDefault ? path : `${path}?agent=${encodeURIComponent(agent)}`,
|
|
{ method: "DELETE" })
|
|
: await api(path, { method: "POST", body: byDefault ? { value } : { value, agent } });
|
|
role.vote = updated.vote;
|
|
role.voted_at = updated.voted_at;
|
|
if (updated.read_at !== undefined) role.read_at = updated.read_at;
|
|
} catch (err) {
|
|
role.vote = before;
|
|
}
|
|
paint();
|
|
echo(role, agent);
|
|
}
|
|
|
|
// One request at a time: a second tap while the first is in flight would
|
|
// unsave an application whose id has not come back yet.
|
|
async function toggleSave() {
|
|
if (star.disabled) return;
|
|
star.disabled = true;
|
|
const before = tracked.get(role.uid);
|
|
try {
|
|
if (before) {
|
|
tracked.delete(role.uid);
|
|
paint();
|
|
await api(`/api/applications/${encodeURIComponent(before.id)}`,
|
|
{ method: "DELETE" });
|
|
} else {
|
|
tracked.set(role.uid, { role_uid: role.uid });
|
|
paint();
|
|
tracked.set(role.uid, await api("/api/applications", {
|
|
method: "POST", body: { role_uid: role.uid, status: "saved" },
|
|
}));
|
|
}
|
|
} catch (err) {
|
|
if (before) tracked.set(role.uid, before);
|
|
else tracked.delete(role.uid);
|
|
} finally {
|
|
star.disabled = false;
|
|
}
|
|
paint();
|
|
echo(role, agent);
|
|
}
|
|
|
|
groups.add({ role, star, paint, agent });
|
|
paint();
|
|
return fragment;
|
|
}
|
|
|
|
Object.assign(window, { judgeButtons, loadTracked });
|
|
})();
|