The company control follows as ⊘ ▲ ★, so like and favourite sit together. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
6.7 KiB
JavaScript
174 lines
6.7 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. A save also casts ▲ for
|
|
* the agent, unless it is cast already, since a starred role is a liked one;
|
|
* an unsave leaves the vote alone. ▲ sits next to ★ for that reason.
|
|
*
|
|
* 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: "down", glyph: "▼", said: "Pass" },
|
|
{ value: 0, cls: "mid", glyph: "●", said: "Neither way" },
|
|
{ value: 1, cls: "up", glyph: "▲", said: "Like" },
|
|
];
|
|
|
|
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" },
|
|
}));
|
|
if (role.vote !== 1) vote(1);
|
|
}
|
|
} 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 });
|
|
})();
|