Public: anyone can read this; the crew can change it.
The feed says 'N newer posts' as a plain link when some arrived, without moving what is on screen #13618
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13599-the-feed-says-n-newer-posts-as-a-plain-l → main 8 commits fork
11 files
changed +122
−12
docs/FEED.md +4
| @@ -190,3 +190,7 @@ To read it back: `GET /api/v1/feed/likes?item=event:…` (or `feed_like` with `a | ||
| 190 | 190 | In the terminal, the Feed screen (`ssh` in, then Ctrl-K and "Feed") marks a liked post `♥ n` and one others like `♡ n`; `l` on the post under the cursor likes it and a second `l` takes it back. It runs the same `readInteraction` check as the page and writes through `setLike` (`toggleLike` only picks the act from the current state), and the write moves the forge's write clock, so web viewers see it on their next live refresh. `h` on a post lists its ledger there (who, act, when, newest first; `m` for older rows), behind the same check. |
| 191 | 191 | |
| 192 | 192 | The how-to, the decisions and what was left out: `docs/features/feed-like-ledger.md`. |
| 193 | ||
| 194 | ## Newer posts line (#13599) | |
| 195 | ||
| 196 | The head's reserved `#feed-newer` line reads `N newer posts` as a link to the top of the feed, from the last-visit count on load and and on a poll the sticky "N new posts" button takes over the count while the link steps aside (one control at a time). See `docs/features/feed-new-count.md`. |
docs/design/areas/feed.md +1
| @@ -6,3 +6,4 @@ | ||
| 6 | 6 | - Folded burst lines — no new class: under a folded post (`planned N issues in owner/name`) `feedCard` draws the §5 `ul.list` of `row()`s, one linked row per title and a last unlinked row `and N more`. |
| 7 | 7 | - `.like` — the Like form under a post, drawn by `likeBar` in `src/web/studio/feed.ts`: an inline form around one §5 ghost `.btn` (`aria-pressed` is the only state signal; the label is always "Like"). Its count `[data-likes]` is `inline-block` at `min-width: 3ch`, right-aligned with tabular numerals, so going from 9 to 10 or pressing moves nothing. Styled in `src/web/studio/feed_style.ts`. |
| 8 | 8 | - `.new-posts` — the "N new posts" button's slot above the home timeline, drawn by `feedNote` in `src/web/studio/feed.ts`: a `role="status"` `aria-live="polite"` paragraph, sticky under the topbar, zero-high while its button is `hidden`, so nothing shifts when it appears. The button is a §5 primary small `.btn` at 44 px; its text is its accessible name and carries the count. Pressing it swaps the held feed, scrolls to the top instantly and moves focus to `#live-feed`. Styled in `src/web/studio/feed_style.ts`. |
| 9 | - `.feed-newer` — the "N newer posts" line in the feed head, drawn by `newerLine` in `src/web/studio/feed.ts`: a `role="status"` paragraph at a fixed 24 px that always exists, holding a plain `#live-feed` link that is `hidden` when nothing is newer, so showing it shifts nothing. Styled in `src/web/studio/feed_style.ts`. |
docs/features/feed-new-count.md +10
| @@ -6,3 +6,13 @@ JavaScript, and counts what the feed would show you (muted apps, dismissed posts | ||
| 6 | 6 | chips left out), up to 99+. Opening the feed moves the mark on the next request, so the first |
| 7 | 7 | paint still shows the count. A first visit, or nothing new, says nothing. The terminal feed |
| 8 | 8 | shows the same line as a toast and never moves the mark. Code: `src/feed/last_seen.ts`. |
| 9 | ||
| 10 | ## "N newer posts" (#13599) | |
| 11 | ||
| 12 | Under the head sits one reserved line, `#feed-newer`, 24 px high with or without text, so nothing | |
| 13 | shifts. With posts newer than your last visit it reads `N newer posts` (`1 newer post`, `99+`) as a | |
| 14 | plain link to `#live-feed`, the top of the feed; with none the link is `hidden`. A poll that holds | |
| 15 | new posts (the existing `forge:held` event, no new timer) shows the sticky "N new posts" button and hides the link, so one control carries the count; it | |
| 16 | never moves what is on screen. The terminal's toast says the line once. | |
| 17 | Code: `newerLine` in `src/web/studio/feed.ts`, `newerLabel` in `src/feed/newer_label.ts`; | |
| 18 | `test/feed-newer.test.ts`. |
src/feed/newer_label.ts added +20
| @@ -0,0 +1,20 @@ | ||
| 1 | /** | |
| 2 | * The words for posts newer than what is on screen, one place for both doors: `1 newer post`, | |
| 3 | * `7 newer posts`, `99+ newer posts` past the cap, nothing for none. `newerLabelJs` is the same | |
| 4 | * function as plain JS for the page's poll. | |
| 5 | */ | |
| 6 | ||
| 7 | import { NEW_COUNT_CAP } from "./last_seen.ts"; | |
| 8 | ||
| 9 | export function newerLabel(n: number): string { | |
| 10 | if (!(n >= 1)) return ""; | |
| 11 | if (n === 1) return "1 newer post"; | |
| 12 | return `${n > NEW_COUNT_CAP ? `${NEW_COUNT_CAP}+` : n} newer posts`; | |
| 13 | } | |
| 14 | ||
| 15 | /** The page's copy; `NEW_COUNT_CAP` is spliced in so the two cannot drift. */ | |
| 16 | export const newerLabelJs = /* js */ ` | |
| 17 | function newerLabel(n) { | |
| 18 | return !(n >= 1) ? '' : n === 1 ? '1 newer post' : (n > ${NEW_COUNT_CAP} ? '${NEW_COUNT_CAP}+' : n) + ' newer posts'; | |
| 19 | } | |
| 20 | `; |
src/web/studio/feed.ts +15 −2
| @@ -35,6 +35,7 @@ import { fragment } from "../layout.ts"; | ||
| 35 | 35 | import { draftJs } from "../draft.ts"; |
| 36 | 36 | import { outboxJs } from "../outbox.ts"; |
| 37 | 37 | import { avatar, btn, card, disclosure, empty, hintAttr, nameMark, row, stateBadge, time } from "../ui.ts"; |
| 38 | import { newerLabel } from "../../feed/newer_label.ts"; | |
| 38 | 39 | import { heldPostsJs } from "./held_posts.ts"; |
| 39 | 40 | import { stepLink } from "./step_link.ts"; |
| 40 | 41 | import { writableAppsOf } from "./asks.ts"; |
| @@ -302,13 +303,22 @@ export function composer(ctx: Ctx, user: User): Html { | ||
| 302 | 303 | return repos.length === 0 ? html`${form}${noApps}` : form; |
| 303 | 304 | } |
| 304 | 305 | |
| 306 | /** | |
| 307 | * The reserved line in the feed head: a plain link to the top of the feed, `hidden` (its height | |
| 308 | * kept) with nothing newer. | |
| 309 | */ | |
| 310 | export function newerLine(n: number): Html { | |
| 311 | const label = newerLabel(n); | |
| 312 | return html`<p id="feed-newer" class="feed-newer small" role="status" aria-live="polite"><a href="#live-feed"${label === "" ? raw(" hidden") : null}>${label}</a></p>`; | |
| 313 | } | |
| 314 | ||
| 305 | 315 | /** |
| 306 | 316 | * One line above the feed, empty unless there is something to say: the feed |
| 307 | 317 | * on screen is old (the service worker served it with no network), or acts |
| 308 | 318 | * are waiting in the outbox. The script fills it; `hidden` until then. |
| 309 | 319 | */ |
| 310 | export function feedNote(): Html { | |
| 311 | return html`<p id="new-posts" class="new-posts" role="status" aria-live="polite">${raw(String(btn("New posts", { type: "button", variant: "primary", size: "sm" })).replace("<button", "<button hidden"))}</p><p | |
| 320 | export function feedNote(newer = 0): Html { | |
| 321 | return html`${newerLine(newer)}<p id="new-posts" class="new-posts" role="status" aria-live="polite">${raw(String(btn("New posts", { type: "button", variant: "primary", size: "sm" })).replace("<button", "<button hidden"))}</p><p | |
| 312 | 322 | id="feed-note" |
| 313 | 323 | class="feed-note small muted" |
| 314 | 324 | role="status" |
| @@ -739,6 +749,7 @@ ${heldPostsJs} | ||
| 739 | 749 | var slot = document.getElementById('new-posts'); |
| 740 | 750 | var slotBtn = slot && slot.querySelector('button'); |
| 741 | 751 | var held = null; |
| 752 | var newer = document.getElementById('feed-newer') && document.getElementById('feed-newer').querySelector('a'); | |
| 742 | 753 | function ids(root) { |
| 743 | 754 | return Array.prototype.map.call(root.querySelectorAll('[data-item]'), function (c) { return c.getAttribute('data-item'); }); |
| 744 | 755 | } |
| @@ -749,6 +760,8 @@ ${heldPostsJs} | ||
| 749 | 760 | var n = newPostIds(ids(feed), ids(holder.content)).length; |
| 750 | 761 | if (n < 1) { held = null; slotBtn.hidden = true; return; } |
| 751 | 762 | held = e.detail.html; |
| 763 | // One control at a time: the button carries the count, so the head's link steps aside. | |
| 764 | if (newer) newer.hidden = true; | |
| 752 | 765 | slotBtn.textContent = newPostsLabel(n); |
| 753 | 766 | slotBtn.hidden = false; |
| 754 | 767 | }); |
src/web/studio/feed_style.ts +2
| @@ -8,6 +8,8 @@ export const feedCss = /* css */ ` | ||
| 8 | 8 | .new-posts { position: sticky; top: calc(var(--topbar-h) + 8px); z-index: 9; margin: 0; display: flex; justify-content: center; pointer-events: none; } |
| 9 | 9 | .new-posts .btn { pointer-events: auto; min-height: 44px; box-shadow: var(--shadow-2); } |
| 10 | 10 | .new-posts .btn[hidden] { display: none; } |
| 11 | .feed-newer { margin: 0; min-height: 24px; line-height: 24px; } | |
| 12 | .feed-newer a[hidden] { display: none; } | |
| 11 | 13 | .feed > .card[hidden] { display: none; } |
| 12 | 14 | /* The Like form sits in the card's flow; its count has a fixed width so a press moves nothing. */ |
| 13 | 15 | .feed .like { display: inline-block; margin: 0; } |
src/web/studio/held_posts.ts +7 −4
| @@ -4,6 +4,9 @@ | ||
| 4 | 4 | * is the same two functions as plain JS for the page. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { NEW_COUNT_CAP } from "../../feed/last_seen.ts"; | |
| 8 | import { newerLabelJs } from "../../feed/newer_label.ts"; | |
| 9 | ||
| 7 | 10 | /** The ids in `fresh` not in `shown`, in `fresh`'s order, each once. */ |
| 8 | 11 | export function newPostIds(shown: readonly string[], fresh: readonly string[]): string[] { |
| 9 | 12 | const seen = new Set(shown); |
| @@ -16,13 +19,13 @@ export function newPostIds(shown: readonly string[], fresh: readonly string[]): | ||
| 16 | 19 | return out; |
| 17 | 20 | } |
| 18 | 21 | |
| 19 | /** `1 new post`, `3 new posts`, and nothing for none. */ | |
| 22 | /** `1 new post`, `3 new posts`, `99+ new posts` past the cap, and nothing for none. */ | |
| 20 | 23 | export function newPostsLabel(n: number): string { |
| 21 | 24 | if (!(n >= 1)) return ""; |
| 22 | return n === 1 ? "1 new post" : `${n} new posts`; | |
| 25 | return n === 1 ? "1 new post" : `${n > NEW_COUNT_CAP ? `${NEW_COUNT_CAP}+` : n} new posts`; | |
| 23 | 26 | } |
| 24 | 27 | |
| 25 | export const heldPostsJs = /* js */ ` | |
| 28 | export const heldPostsJs = /* js */ `${newerLabelJs} | |
| 26 | 29 | function newPostIds(shown, fresh) { |
| 27 | 30 | var seen = {}, out = [], i; |
| 28 | 31 | for (i = 0; i < shown.length; i++) seen['$' + shown[i]] = 1; |
| @@ -34,6 +37,6 @@ function newPostIds(shown, fresh) { | ||
| 34 | 37 | return out; |
| 35 | 38 | } |
| 36 | 39 | function newPostsLabel(n) { |
| 37 | return !(n >= 1) ? '' : n === 1 ? '1 new post' : n + ' new posts'; | |
| 40 | return !(n >= 1) ? '' : n === 1 ? '1 new post' : (n > ${NEW_COUNT_CAP} ? '${NEW_COUNT_CAP}+' : n) + ' new posts'; | |
| 38 | 41 | } |
| 39 | 42 | `; |
src/web/studio/home.ts +3 −2
| @@ -357,7 +357,8 @@ export async function feedPage(ctx: Ctx): Promise<Response> { | ||
| 357 | 357 | // The feed reads first: the count below then reuses its cached candidates instead of warming |
| 358 | 358 | // them. |
| 359 | 359 | const feedRegion = await regionOf("feed"); |
| 360 | const fresh = timePhase(ctx.request, "new", () => newLine(newSince(ctx.app.db, user, before, visitedAt))); | |
| 360 | const newer = timePhase(ctx.request, "new", () => newSince(ctx.app.db, user, before, visitedAt)); | |
| 361 | const fresh = newLine(newer); | |
| 361 | 362 | // One sentence, once: the count rides in the digest's head; with no digest it stands alone. |
| 362 | 363 | const since = timePhase(ctx.request, "since", () => sinceYouWere(ctx.app.db, user, before, visitedAt, fresh)); |
| 363 | 364 | // Milestones are only an input here: the feed does not draw them (#12328). |
| @@ -373,7 +374,7 @@ export async function feedPage(ctx: Ctx): Promise<Response> { | ||
| 373 | 374 | <div class="stack"> |
| 374 | 375 | ${composer(ctx, user)} |
| 375 | 376 | ${stalled} |
| 376 | ${feedNote()} | |
| 377 | ${feedNote(newer)} | |
| 377 | 378 | ${feedRegion} |
| 378 | 379 | </div> |
| 379 | 380 | <aside class="stack"> |
test/feed-empty.test.ts +3 −2
| @@ -70,13 +70,14 @@ test("the first post arrives in the region below the tell box: nothing above the | ||
| 70 | 70 | expect(form).toBeGreaterThan(-1); |
| 71 | 71 | expect(form).toBeLessThan(region); |
| 72 | 72 | const end = page.indexOf("</section>", region); |
| 73 | return { above: page.slice(form, region).replace(/data-rendered="\d+"/g, "").replace(/<script[\s\S]*?<\/script>/g, ""), inside: page.slice(region, end) }; | |
| 73 | return { above: page.slice(form, region).replace(/data-rendered="\d+"/g, "").replace(/(<p id="feed-newer"[^>]*>)<a [^>]*>[^<]*<\/a>/, "$1").replace(/<script[\s\S]*?<\/script>/g, ""), inside: page.slice(region, end) }; | |
| 74 | 74 | }; |
| 75 | 75 | const before = split(emptyPage); |
| 76 | 76 | expect(before.inside).toContain('class="empty"'); |
| 77 | 77 | const after = split(await (await fetch(`${servers.url}/feed`, { headers: { cookie } })).text()); |
| 78 | 78 | expect(after.inside).toContain("make the jump higher"); |
| 79 | 79 | expect(after.inside).not.toContain('class="empty"'); |
| 80 | // Same markup above the region in both states: the box and everything before it stay put. | |
| 80 | // Same markup above the region in both states (the "N newer posts" line is the same reserved | |
| 81 | // element, only its link text may differ): the box and everything before it stay put. | |
| 81 | 82 | expect(after.above).toBe(before.above); |
| 82 | 83 | }); |
test/feed-new-posts.test.ts +17 −2
| @@ -62,7 +62,8 @@ function drive(shown: string[]) { | ||
| 62 | 62 | }; |
| 63 | 63 | const button: Any = { hidden: true, textContent: "New posts", addEventListener: (_t: string, f: () => void) => clicks.push(f) }; |
| 64 | 64 | const slot: Any = { querySelector: () => button }; |
| 65 | const els: Record<string, Any> = { "live-feed": feed, "new-posts": slot }; | |
| 65 | const link: Any = { hidden: false, textContent: "2 newer posts" }; | |
| 66 | const els: Record<string, Any> = { "live-feed": feed, "new-posts": slot, "feed-newer": { querySelector: () => link } }; | |
| 66 | 67 | const doc: Any = { |
| 67 | 68 | getElementById: (id: string) => els[id] ?? null, |
| 68 | 69 | createElement: () => { |
| @@ -76,7 +77,7 @@ function drive(shown: string[]) { | ||
| 76 | 77 | new Function("document", "window", "fetch", "CustomEvent", "location", "navigator", "setTimeout", feedJs)( |
| 77 | 78 | doc, win, async () => ({ ok: true }), CE, { pathname: "/" }, {}, () => 1, |
| 78 | 79 | ); |
| 79 | return { focused, feed, slot, button, listeners, clicks, events, scrolls }; | |
| 80 | return { focused, feed, slot, button, link, listeners, clicks, events, scrolls }; | |
| 80 | 81 | } |
| 81 | 82 | |
| 82 | 83 | describe("new posts, in the browser", () => { |
| @@ -95,6 +96,20 @@ describe("new posts, in the browser", () => { | ||
| 95 | 96 | d.clicks[0]!(); |
| 96 | 97 | expect(d.events).toEqual(["forge:swapped"]); |
| 97 | 98 | }); |
| 99 | test("a poll shows one control: the button takes the count and the head's link steps aside", () => { | |
| 100 | const d = drive(["a"]); | |
| 101 | d.listeners["forge:held"]!({ detail: { html: "x,a" } }); | |
| 102 | expect(d.link.hidden).toBe(true); | |
| 103 | expect(d.button.hidden).toBe(false); | |
| 104 | expect(d.feed.innerHTML).toBe("a"); // nothing on screen moved | |
| 105 | expect(d.events).toEqual([]); | |
| 106 | expect(d.scrolls).toEqual([]); | |
| 107 | }); | |
| 108 | test("the poll's count is capped at 99+ as the server's is", () => { | |
| 109 | const d = drive(["a"]); | |
| 110 | d.listeners["forge:held"]!({ detail: { html: `${Array.from({ length: 150 }, (_, i) => `n${i}`).join(",")},a` } }); | |
| 111 | expect(d.button.textContent).toBe("99+ new posts"); | |
| 112 | }); | |
| 98 | 113 | test("a later hold replaces the held markup", () => { |
| 99 | 114 | const d = drive(["a"]); |
| 100 | 115 | d.listeners["forge:held"]!({ detail: { html: "x,a" } }); |
test/feed-newer.test.ts added +40
| @@ -0,0 +1,40 @@ | ||
| 1 | /** | |
| 2 | * The feed's reserved "N newer posts" line (#13599): the same height with and without it, a plain | |
| 3 | * link to the top of the feed, and the poll's held posts fill the same link. | |
| 4 | */ | |
| 5 | import { expect, test } from "bun:test"; | |
| 6 | import { newerLine } from "../src/web/studio/feed.ts"; | |
| 7 | import { newerLabel, newerLabelJs } from "../src/feed/newer_label.ts"; | |
| 8 | import { newPostsLabel, heldPostsJs } from "../src/web/studio/held_posts.ts"; | |
| 9 | import { feedCss } from "../src/web/studio/feed_style.ts"; | |
| 10 | ||
| 11 | test("zero, one and many", () => { | |
| 12 | expect(newerLabel(0)).toBe(""); | |
| 13 | expect(newerLabel(1)).toBe("1 newer post"); | |
| 14 | expect(newerLabel(7)).toBe("7 newer posts"); | |
| 15 | expect(newerLabel(99)).toBe("99 newer posts"); | |
| 16 | expect(newerLabel(150)).toBe("99+ newer posts"); | |
| 17 | }); | |
| 18 | ||
| 19 | test("the page's labels are the server's, cap included", () => { | |
| 20 | const js = new Function(`${heldPostsJs}; return [newerLabel, newPostsLabel];`)() as [(n: number) => string, (n: number) => string]; | |
| 21 | for (const n of [0, 1, 4, 99, 100, 150]) { | |
| 22 | expect(js[0](n)).toBe(newerLabel(n)); | |
| 23 | expect(js[1](n)).toBe(newPostsLabel(n)); | |
| 24 | } | |
| 25 | expect(js[0](150)).toBe("99+ newer posts"); | |
| 26 | expect(js[1](150)).toBe("99+ new posts"); | |
| 27 | expect(heldPostsJs).toContain(newerLabelJs); | |
| 28 | }); | |
| 29 | ||
| 30 | test("the line is always there; its link is hidden for none and a plain anchor for some", () => { | |
| 31 | const none = String(newerLine(0)); | |
| 32 | expect(none).toMatch(/<p id="feed-newer"[^>]*role="status"[^>]*><a href="#live-feed" hidden><\/a><\/p>/); | |
| 33 | expect(String(newerLine(1))).toMatch(/<a href="#live-feed">1 newer post<\/a>/); | |
| 34 | expect(String(newerLine(3))).toMatch(/<a href="#live-feed">3 newer posts<\/a>/); | |
| 35 | expect(String(newerLine(500))).toContain(">99+ newer posts<"); | |
| 36 | }); | |
| 37 | ||
| 38 | test("the reserved height does not depend on the link", () => { | |
| 39 | expect(feedCss).toMatch(/\.feed-newer \{[^}]*min-height: 24px/); | |
| 40 | }); |
new above
offline