Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
src/ui/mod-menu.js builds the moderator menu for one chat line - Remove, Mute 10m/1h/24h/forever, Ban - and the exact /live frame each sends - module and test only #409
Merged
ssmithcrew
opened this
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
Merged
smith/arm:smith/384-src-ui-mod-menu-js-builds-the-moderator → main · 1 commit ·fork ·1 thread · 1 unresolved
-
Merged. smith/384-src-ui-mod-menu-js-builds-the-moderator was merged into main as 58cbfc8.
Review
Sign in to review this change.
2 files
changed +100
−0
Software/so101/src/ui/mod-menu.js added +48
| @@ -0,0 +1,48 @@ | ||
| 1 | // The moderator's menu for one chat line: who sees it, the exact /live frame each item sends, and its buttons. | |
| 2 | ||
| 3 | /** @typedef {{ id: number, from: { id: string, name?: string, owner?: boolean, moderator?: boolean } | null }} Line */ | |
| 4 | /** @typedef {{ id: string, owner?: boolean, moderator?: boolean }} Me */ | |
| 5 | /** @typedef {{ kind: "delete" | "mute" | "ban", label: string, for?: "10m" | "1h" | "24h" | "forever" }} Item */ | |
| 6 | ||
| 7 | const MUTES = /** @type {const} */ ([["10m", "Mute 10m"], ["1h", "Mute 1h"], ["24h", "Mute 24h"], ["forever", "Mute forever"]]); | |
| 8 | ||
| 9 | /** | |
| 10 | * What this person may do to this line. Owners and moderators may remove any line; mute and ban need a person | |
| 11 | * who is neither you, the owner nor another moderator (the server checks again). | |
| 12 | * @param {Line | null | undefined} line @param {Me | null | undefined} me @returns {Item[]} | |
| 13 | */ | |
| 14 | export function menuItems(line, me) { | |
| 15 | if (!line || !me || !(me.owner || me.moderator) || !line.from) return []; | |
| 16 | if (line.from.id === me.id) return []; | |
| 17 | if (!me.owner && (line.from.owner || line.from.moderator)) return []; | |
| 18 | return [ | |
| 19 | { kind: "delete", label: "Remove" }, | |
| 20 | ...MUTES.map(([f, label]) => /** @type {Item} */ ({ kind: "mute", label, for: f })), | |
| 21 | { kind: "ban", label: "Ban" }, | |
| 22 | ]; | |
| 23 | } | |
| 24 | ||
| 25 | /** The frame /live takes for an item on a line. @param {Item} item @param {Line} line */ | |
| 26 | export function frameFor(item, line) { | |
| 27 | if (item.kind === "delete") return { type: "delete", id: line.id }; | |
| 28 | const id = line.from?.id; | |
| 29 | if (item.kind === "mute") return { type: "mute", id, on: true, for: item.for }; | |
| 30 | return { type: "ban", id, on: true }; | |
| 31 | } | |
| 32 | ||
| 33 | /** Buttons, each at least 44x44 px, text through textContent. @param {Document} doc @param {Item[]} items @param {(item: Item) => void} onPick */ | |
| 34 | export function renderMenu(doc, items, onPick) { | |
| 35 | const menu = doc.createElement("div"); | |
| 36 | menu.setAttribute("role", "menu"); | |
| 37 | for (const item of items) { | |
| 38 | const b = doc.createElement("button"); | |
| 39 | b.type = "button"; | |
| 40 | b.setAttribute("role", "menuitem"); | |
| 41 | b.textContent = item.label; | |
| 42 | b.style.minWidth = "44px"; | |
| 43 | b.style.minHeight = "44px"; | |
| 44 | b.addEventListener("click", () => onPick(item)); | |
| 45 | menu.append(b); | |
| 46 | } | |
| 47 | return menu; | |
| 48 | } |
Software/so101/src/ui/mod-menu.test.ts added +52
| @@ -0,0 +1,52 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { frameFor, menuItems, renderMenu } from "./mod-menu.js"; | |
| 3 | ||
| 4 | const line = { id: 12, from: { id: "u2", name: "Bo" } }; | |
| 5 | const owner = { id: "o", owner: true }; | |
| 6 | const mod = { id: "m", moderator: true }; | |
| 7 | ||
| 8 | describe("who sees the menu", () => { | |
| 9 | test("owner and moderator get Remove, four mutes, Ban", () => { | |
| 10 | for (const me of [owner, mod]) expect(menuItems(line, me).map((i) => i.label)).toEqual(["Remove", "Mute 10m", "Mute 1h", "Mute 24h", "Mute forever", "Ban"]); | |
| 11 | }); | |
| 12 | test("nobody else, and never your own line", () => { | |
| 13 | expect(menuItems(line, { id: "u3" })).toEqual([]); | |
| 14 | expect(menuItems(line, null)).toEqual([]); | |
| 15 | expect(menuItems({ id: 1, from: { id: "o" } }, owner)).toEqual([]); | |
| 16 | expect(menuItems({ id: 1, from: { id: "m" } }, mod)).toEqual([]); | |
| 17 | }); | |
| 18 | test("a moderator gets nothing on the owner's or a moderator's line", () => { | |
| 19 | expect(menuItems({ id: 1, from: { id: "o", owner: true } }, mod)).toEqual([]); | |
| 20 | expect(menuItems({ id: 1, from: { id: "m2", moderator: true } }, mod)).toEqual([]); | |
| 21 | expect(menuItems({ id: 1, from: { id: "m2", moderator: true } }, owner).length).toBe(6); | |
| 22 | }); | |
| 23 | }); | |
| 24 | ||
| 25 | describe("frames", () => { | |
| 26 | test("each item sends exactly its frame", () => { | |
| 27 | const f = menuItems(line, owner).map((i) => frameFor(i, line)); | |
| 28 | expect(f).toEqual([ | |
| 29 | { type: "delete", id: 12 }, | |
| 30 | { type: "mute", id: "u2", on: true, for: "10m" }, | |
| 31 | { type: "mute", id: "u2", on: true, for: "1h" }, | |
| 32 | { type: "mute", id: "u2", on: true, for: "24h" }, | |
| 33 | { type: "mute", id: "u2", on: true, for: "forever" }, | |
| 34 | { type: "ban", id: "u2", on: true }, | |
| 35 | ]); | |
| 36 | }); | |
| 37 | }); | |
| 38 | ||
| 39 | describe("buttons", () => { | |
| 40 | const fake = () => { | |
| 41 | const mk = (): any => ({ style: {}, children: [], attrs: {}, setAttribute(k: string, v: string) { this.attrs[k] = v; }, append(...c: any[]) { this.children.push(...c); }, addEventListener(_: string, fn: () => void) { this.click = fn; } }); | |
| 42 | return { createElement: mk } as any; | |
| 43 | }; | |
| 44 | test("44x44, text through textContent, click picks", () => { | |
| 45 | const picked: string[] = []; | |
| 46 | const menu = renderMenu(fake(), menuItems({ id: 1, from: { id: "<b>", name: "x" } }, owner), (i) => picked.push(i.label)); | |
| 47 | expect(menu.children.length).toBe(6); | |
| 48 | for (const b of menu.children) { expect(b.style.minWidth).toBe("44px"); expect(b.style.minHeight).toBe("44px"); } | |
| 49 | menu.children[5].click(); | |
| 50 | expect(picked).toEqual(["Ban"]); | |
| 51 | }); | |
| 52 | }); |
new above
offline
role=menu implies arrow-key navigation and a single tab stop; this gives neither. Use a plain role=group with aria-label, or implement the keys.