Public: anyone can read this; the crew can change it.
Home: pressing a feed chip refreshes the feed region in place and says when it failed #1953
scout/forge:scout/1928-home-pressing-a-feed-chip-refreshes-the → main 1 commit fork
2 files
changed +66
−6
src/web/studio/feed.ts +47 −6
| @@ -289,6 +289,7 @@ ${outboxJs} | ||
| 289 | 289 | if (!feed || typeof fetch === 'undefined') return; |
| 290 | 290 | var note = document.getElementById('feed-note'); |
| 291 | 291 | var outbox = null; |
| 292 | var problem = ''; | |
| 292 | 293 | try { outbox = forgeOutbox(window.localStorage, function (u, o) { return fetch(u, o); }); } catch (e) {} |
| 293 | 294 | function ago(ms) { |
| 294 | 295 | var m = Math.max(0, Math.round((Date.now() - ms) / 60000)); |
| @@ -304,6 +305,7 @@ ${outboxJs} | ||
| 304 | 305 | parts.push('Offline: this feed is from ' + ago(Number(note.getAttribute('data-rendered'))) + '.'); |
| 305 | 306 | var n = outbox ? outbox.size() : 0; |
| 306 | 307 | if (n > 0) parts.push(n + (n === 1 ? ' act is' : ' acts are') + ' waiting to send.'); |
| 308 | if (problem) parts.push(problem); | |
| 307 | 309 | note.textContent = parts.join(' '); |
| 308 | 310 | note.hidden = parts.length === 0; |
| 309 | 311 | } |
| @@ -329,14 +331,53 @@ ${outboxJs} | ||
| 329 | 331 | } |
| 330 | 332 | // Acting is instant: the card goes, the act is kept and sent. |
| 331 | 333 | function act(card, kind) { if (send(card, kind)) gone(card); } |
| 334 | // A chip flips at once; the saved filter then redraws the region in place | |
| 335 | // (the same fragment the live region pulls), keeping scroll and focus. A | |
| 336 | // refusal puts the chip back and says why in the note. | |
| 337 | function fail(c, was, why) { | |
| 338 | c.setAttribute('aria-pressed', was); | |
| 339 | problem = why; | |
| 340 | paint(); | |
| 341 | } | |
| 342 | function chip(c) { | |
| 343 | var was = c.getAttribute('aria-pressed'); | |
| 344 | var kind = c.getAttribute('data-chip'); | |
| 345 | var value = c.getAttribute('data-value'); | |
| 346 | c.setAttribute('aria-pressed', was === 'true' ? 'false' : 'true'); | |
| 347 | problem = ''; | |
| 348 | paint(); | |
| 349 | fetch('/api/v1/feed/chips', { method: 'POST', credentials: 'same-origin', headers: { 'content-type': 'application/json' }, | |
| 350 | body: JSON.stringify({ kind: kind, value: value, hidden: was === 'true' }) }) | |
| 351 | .then(function (r) { | |
| 352 | if (r.ok) return refetch(kind, value); | |
| 353 | return r.json().catch(function () { return {}; }).then(function (b) { | |
| 354 | fail(c, was, (b && (b.message || b.error)) || 'The filter was not saved (' + r.status + ').'); | |
| 355 | }); | |
| 356 | }) | |
| 357 | .catch(function () { fail(c, was, 'The filter was not saved: no connection.'); }); | |
| 358 | } | |
| 359 | function refetch(kind, value) { | |
| 360 | return fetch('/live/feed', { headers: { accept: 'text/html' }, credentials: 'same-origin' }) | |
| 361 | .then(function (r) { if (!r.ok) throw r; return r.text(); }) | |
| 362 | .then(function (text) { | |
| 363 | var holder = document.createElement('template'); | |
| 364 | holder.innerHTML = text; | |
| 365 | var fresh = holder.content.querySelector('#live-feed'); | |
| 366 | if (!fresh) return; | |
| 367 | var had = document.activeElement && document.activeElement.closest && document.activeElement.closest('[data-chip]'); | |
| 368 | feed.innerHTML = fresh.innerHTML; | |
| 369 | feed.dispatchEvent(new CustomEvent('forge:swapped', { bubbles: true })); | |
| 370 | if (had) { | |
| 371 | var all = feed.querySelectorAll('[data-chip]'); | |
| 372 | for (var i = 0; i < all.length; i++) | |
| 373 | if (all[i].getAttribute('data-chip') === kind && all[i].getAttribute('data-value') === value) all[i].focus(); | |
| 374 | } | |
| 375 | }) | |
| 376 | .catch(function () { problem = 'Saved, but the feed could not be refreshed.'; paint(); }); | |
| 377 | } | |
| 332 | 378 | feed.addEventListener('click', function (e) { |
| 333 | 379 | var c = e.target && e.target.closest ? e.target.closest('[data-chip]') : null; |
| 334 | if (c) { | |
| 335 | fetch('/api/v1/feed/chips', { method: 'POST', credentials: 'same-origin', headers: { 'content-type': 'application/json' }, | |
| 336 | body: JSON.stringify({ kind: c.getAttribute('data-chip'), value: c.getAttribute('data-value'), hidden: c.getAttribute('aria-pressed') === 'true' }) }) | |
| 337 | .then(function (r) { if (r.ok) location.reload(); }); | |
| 338 | return; | |
| 339 | } | |
| 380 | if (c) { chip(c); return; } | |
| 340 | 381 | var t = e.target && e.target.closest ? e.target.closest('[data-feed],[data-feed-open]') : null; |
| 341 | 382 | var card = t && t.closest('.fcard'); |
| 342 | 383 | if (!card) return; |
test/feed-chips.test.ts +19
| @@ -17,6 +17,7 @@ import { serve, type Servers } from "../src/http/server.ts"; | ||
| 17 | 17 | import { chipHides } from "../src/feed/chips.ts"; |
| 18 | 18 | import type { FeedKind } from "../src/feed/items.ts"; |
| 19 | 19 | import { readFeed } from "../src/feed/feed.ts"; |
| 20 | import { feedJs } from "../src/web/studio/feed.ts"; | |
| 20 | 21 | |
| 21 | 22 | const ALL: FeedKind[] = [ |
| 22 | 23 | "run", "ask", "pull", "decision", "map", "cost", "tell", "reply", "look", "milestone", "choose", "release", |
| @@ -173,4 +174,22 @@ describe("the chips of a forge", () => { | ||
| 173 | 174 | expect(page).toMatch(/data-chip="person"\s+data-value="alice"/); |
| 174 | 175 | setChip(app.db, bob.id, { kind: "kind", value: "crew" }, false); |
| 175 | 176 | }); |
| 177 | ||
| 178 | test("a pressed chip refreshes the region in place and says when it failed", async () => { | |
| 179 | const handler = feedJs.slice(feedJs.indexOf("function chip(c)"), feedJs.indexOf("feed.addEventListener('click'")); | |
| 180 | expect(handler).not.toContain("location.reload"); | |
| 181 | expect(handler).toContain("/live/feed"); | |
| 182 | expect(handler).toContain("aria-pressed"); | |
| 183 | expect(feedJs).toContain("problem"); | |
| 184 | const session = createSession(app.db, bob.id); | |
| 185 | const cookie = { cookie: `forge_session=${session.token}` }; | |
| 186 | await call(bobToken, "POST", "/api/v1/feed/chips", { kind: "kind", value: "crew", hidden: true }); | |
| 187 | const region = await (await fetch(`${servers.url}/live/feed`, { headers: cookie })).text(); | |
| 188 | expect(region).toMatch(/data-chip="kind"\s+data-value="crew"\s+aria-pressed="false"/); | |
| 189 | expect(region).not.toContain('data-kind="look"'); | |
| 190 | await call(bobToken, "POST", "/api/v1/feed/chips", { kind: "kind", value: "crew", hidden: false }); | |
| 191 | const back = await (await fetch(`${servers.url}/live/feed`, { headers: cookie })).text(); | |
| 192 | expect(back).toMatch(/data-chip="kind"\s+data-value="crew"\s+aria-pressed="true"/); | |
| 193 | expect(back).toContain('data-kind="look"'); | |
| 194 | }); | |
| 176 | 195 | }); |
new above
offline