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

Parked opened this
Closed without merging

scout/forge:scout/1928-home-pressing-a-feed-chip-refreshes-the → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +66 −6
src/web/studio/feed.ts +47 −6
@@ -289,6 +289,7 @@ ${outboxJs}
289289 if (!feed || typeof fetch === 'undefined') return;
290290 var note = document.getElementById('feed-note');
291291 var outbox = null;
292 var problem = '';
292293 try { outbox = forgeOutbox(window.localStorage, function (u, o) { return fetch(u, o); }); } catch (e) {}
293294 function ago(ms) {
294295 var m = Math.max(0, Math.round((Date.now() - ms) / 60000));
@@ -304,6 +305,7 @@ ${outboxJs}
304305 parts.push('Offline: this feed is from ' + ago(Number(note.getAttribute('data-rendered'))) + '.');
305306 var n = outbox ? outbox.size() : 0;
306307 if (n > 0) parts.push(n + (n === 1 ? ' act is' : ' acts are') + ' waiting to send.');
308 if (problem) parts.push(problem);
307309 note.textContent = parts.join(' ');
308310 note.hidden = parts.length === 0;
309311 }
@@ -329,14 +331,53 @@ ${outboxJs}
329331 }
330332 // Acting is instant: the card goes, the act is kept and sent.
331333 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 }
332378 feed.addEventListener('click', function (e) {
333379 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; }
340381 var t = e.target && e.target.closest ? e.target.closest('[data-feed],[data-feed-open]') : null;
341382 var card = t && t.closest('.fcard');
342383 if (!card) return;
test/feed-chips.test.ts +19
@@ -17,6 +17,7 @@ import { serve, type Servers } from "../src/http/server.ts";
1717 import { chipHides } from "../src/feed/chips.ts";
1818 import type { FeedKind } from "../src/feed/items.ts";
1919 import { readFeed } from "../src/feed/feed.ts";
20 import { feedJs } from "../src/web/studio/feed.ts";
2021
2122 const ALL: FeedKind[] = [
2223 "run", "ask", "pull", "decision", "map", "cost", "tell", "reply", "look", "milestone", "choose", "release",
@@ -173,4 +174,22 @@ describe("the chips of a forge", () => {
173174 expect(page).toMatch(/data-chip="person"\s+data-value="alice"/);
174175 setChip(app.db, bob.id, { kind: "kind", value: "crew" }, false);
175176 });
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 });
176195 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs