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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13599-the-feed-says-n-newer-posts-as-a-plain-l → main 8 commits fork

  • Merged. scout/13599-the-feed-says-n-newer-posts-as-a-plain-l was merged into main as fd2db04.

Review

Sign in to review this change.
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
190190 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.
191191
192192 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 @@
66 - 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`.
77 - `.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`.
88 - `.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
66 chips left out), up to 99+. Opening the feed moves the mark on the next request, so the first
77 paint still shows the count. A first visit, or nothing new, says nothing. The terminal feed
88 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";
3535 import { draftJs } from "../draft.ts";
3636 import { outboxJs } from "../outbox.ts";
3737 import { avatar, btn, card, disclosure, empty, hintAttr, nameMark, row, stateBadge, time } from "../ui.ts";
38 import { newerLabel } from "../../feed/newer_label.ts";
3839 import { heldPostsJs } from "./held_posts.ts";
3940 import { stepLink } from "./step_link.ts";
4041 import { writableAppsOf } from "./asks.ts";
@@ -302,13 +303,22 @@ export function composer(ctx: Ctx, user: User): Html {
302303 return repos.length === 0 ? html`${form}${noApps}` : form;
303304 }
304305
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
305315 /**
306316 * One line above the feed, empty unless there is something to say: the feed
307317 * on screen is old (the service worker served it with no network), or acts
308318 * are waiting in the outbox. The script fills it; `hidden` until then.
309319 */
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
312322 id="feed-note"
313323 class="feed-note small muted"
314324 role="status"
@@ -739,6 +749,7 @@ ${heldPostsJs}
739749 var slot = document.getElementById('new-posts');
740750 var slotBtn = slot && slot.querySelector('button');
741751 var held = null;
752 var newer = document.getElementById('feed-newer') && document.getElementById('feed-newer').querySelector('a');
742753 function ids(root) {
743754 return Array.prototype.map.call(root.querySelectorAll('[data-item]'), function (c) { return c.getAttribute('data-item'); });
744755 }
@@ -749,6 +760,8 @@ ${heldPostsJs}
749760 var n = newPostIds(ids(feed), ids(holder.content)).length;
750761 if (n < 1) { held = null; slotBtn.hidden = true; return; }
751762 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;
752765 slotBtn.textContent = newPostsLabel(n);
753766 slotBtn.hidden = false;
754767 });
src/web/studio/feed_style.ts +2
@@ -8,6 +8,8 @@ export const feedCss = /* css */ `
88 .new-posts { position: sticky; top: calc(var(--topbar-h) + 8px); z-index: 9; margin: 0; display: flex; justify-content: center; pointer-events: none; }
99 .new-posts .btn { pointer-events: auto; min-height: 44px; box-shadow: var(--shadow-2); }
1010 .new-posts .btn[hidden] { display: none; }
11 .feed-newer { margin: 0; min-height: 24px; line-height: 24px; }
12 .feed-newer a[hidden] { display: none; }
1113 .feed > .card[hidden] { display: none; }
1214 /* The Like form sits in the card's flow; its count has a fixed width so a press moves nothing. */
1315 .feed .like { display: inline-block; margin: 0; }
src/web/studio/held_posts.ts +7 −4
@@ -4,6 +4,9 @@
44 * is the same two functions as plain JS for the page.
55 */
66
7 import { NEW_COUNT_CAP } from "../../feed/last_seen.ts";
8 import { newerLabelJs } from "../../feed/newer_label.ts";
9
710 /** The ids in `fresh` not in `shown`, in `fresh`'s order, each once. */
811 export function newPostIds(shown: readonly string[], fresh: readonly string[]): string[] {
912 const seen = new Set(shown);
@@ -16,13 +19,13 @@ export function newPostIds(shown: readonly string[], fresh: readonly string[]):
1619 return out;
1720 }
1821
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. */
2023 export function newPostsLabel(n: number): string {
2124 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`;
2326 }
2427
25 export const heldPostsJs = /* js */ `
28 export const heldPostsJs = /* js */ `${newerLabelJs}
2629 function newPostIds(shown, fresh) {
2730 var seen = {}, out = [], i;
2831 for (i = 0; i < shown.length; i++) seen['$' + shown[i]] = 1;
@@ -34,6 +37,6 @@ function newPostIds(shown, fresh) {
3437 return out;
3538 }
3639 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';
3841 }
3942 `;
src/web/studio/home.ts +3 −2
@@ -357,7 +357,8 @@ export async function feedPage(ctx: Ctx): Promise<Response> {
357357 // The feed reads first: the count below then reuses its cached candidates instead of warming
358358 // them.
359359 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);
361362 // One sentence, once: the count rides in the digest's head; with no digest it stands alone.
362363 const since = timePhase(ctx.request, "since", () => sinceYouWere(ctx.app.db, user, before, visitedAt, fresh));
363364 // 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> {
373374 <div class="stack">
374375 ${composer(ctx, user)}
375376 ${stalled}
376 ${feedNote()}
377 ${feedNote(newer)}
377378 ${feedRegion}
378379 </div>
379380 <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
7070 expect(form).toBeGreaterThan(-1);
7171 expect(form).toBeLessThan(region);
7272 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) };
7474 };
7575 const before = split(emptyPage);
7676 expect(before.inside).toContain('class="empty"');
7777 const after = split(await (await fetch(`${servers.url}/feed`, { headers: { cookie } })).text());
7878 expect(after.inside).toContain("make the jump higher");
7979 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.
8182 expect(after.above).toBe(before.above);
8283 });
test/feed-new-posts.test.ts +17 −2
@@ -62,7 +62,8 @@ function drive(shown: string[]) {
6262 };
6363 const button: Any = { hidden: true, textContent: "New posts", addEventListener: (_t: string, f: () => void) => clicks.push(f) };
6464 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 } };
6667 const doc: Any = {
6768 getElementById: (id: string) => els[id] ?? null,
6869 createElement: () => {
@@ -76,7 +77,7 @@ function drive(shown: string[]) {
7677 new Function("document", "window", "fetch", "CustomEvent", "location", "navigator", "setTimeout", feedJs)(
7778 doc, win, async () => ({ ok: true }), CE, { pathname: "/" }, {}, () => 1,
7879 );
79 return { focused, feed, slot, button, listeners, clicks, events, scrolls };
80 return { focused, feed, slot, button, link, listeners, clicks, events, scrolls };
8081 }
8182
8283 describe("new posts, in the browser", () => {
@@ -95,6 +96,20 @@ describe("new posts, in the browser", () => {
9596 d.clicks[0]!();
9697 expect(d.events).toEqual(["forge:swapped"]);
9798 });
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 });
98113 test("a later hold replaces the held markup", () => {
99114 const d = drive(["a"]);
100115 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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs