Show desktop dice previews and mobile chat journal tabs

This commit is contained in:
coyotte508 2026-09-11 20:46:02 +02:00
commit 5b4614371e
8 changed files with 204 additions and 42 deletions

View file

@ -10,7 +10,7 @@
"fmt": "oxfmt",
"fmt:check": "oxfmt --check",
"simulate": "pnpm --filter fuji-engine build && node scripts/simulate.mjs",
"test:browser": "node tests/browser-smoke.mjs && node tests/bgs-bundle-smoke.mjs && node tests/presentation-smoke.mjs && node tests/chat-smoke.mjs && node tests/mobile-readiness-smoke.mjs"
"test:browser": "node tests/browser-smoke.mjs && node tests/bgs-bundle-smoke.mjs && node tests/presentation-smoke.mjs && node tests/chat-smoke.mjs && node tests/mobile-readiness-smoke.mjs && node tests/chat-tabs-smoke.mjs"
},
"devDependencies": {
"oxfmt": "0.61.0",

View file

@ -66,6 +66,17 @@
let drop = $state("");
let help = $state(false);
let journal = $state(true);
let socialTab = $state("chat");
$effect(() => {
store.chat.playerIndex = seat;
});
function openChat() {
socialTab = "chat";
store.chat.open = true;
requestAnimationFrame(() =>
document.querySelector(".expedition-chat")?.scrollIntoView({ behavior: "smooth", block: "center" })
);
}
const journalEntries = $derived(
store.journal
.filter((entry) => !entry.detail)
@ -378,6 +389,37 @@
);
</script>
{#snippet journalPanel(location: string)}
<section class="journal inline-journal" aria-label="Expedition journal">
<header class="journal-header">
<h2>Expedition journal</h2>
<button
class="text-button"
aria-label={journal ? "Collapse journal" : "Expand journal"}
aria-expanded={journal}
aria-controls={`journal-entries-${location}`}
onclick={() => (journal = !journal)}>{journal ? "−" : "+"}</button
>
</header>
{#if journal}
<ol id={`journal-entries-${location}`} tabindex="0" aria-label="Journal entries">
{#each journalEntries as e, i}
{#if i === 0 || journalEntries[i - 1]!.round !== e.round}
<li class="journal-divider">
<h3>{e.round === 0 ? "Preparation" : `Round ${String(e.round).padStart(2, "0")}`}</h3>
</li>
{/if}
<li class:latest={i === 0} class:journal-phase={e.type === "phase"}>
{#if e.type === "phase"}
<h4><span aria-hidden="true">◇</span>{e.text}</h4>
{:else}<div class="journal-entry">
<JournalEntry entry={e} colorblind={store.colorblind} />
</div>{/if}
</li>{/each}
</ol>{/if}
</section>
{/snippet}
{#if s}
<main class="expedition">
<div class="game-tools">
@ -394,22 +436,21 @@
: s.phase}</span
>
</div>
{#if store.chat.enabled}<button
class="text-button"
onclick={() => {
store.chat.open = true;
requestAnimationFrame(() =>
document.querySelector(".expedition-chat")?.scrollIntoView({ behavior: "smooth", block: "center" })
);
}}>Chat</button
{#if store.chat.enabled}<button class="text-button" onclick={openChat}
>Chat{store.chat.unread ? ` · ${store.chat.unread}` : ""}</button
>{/if}
<button class="text-button" onclick={() => (help = true)} aria-label="Open playing guide">Help</button>
<button
class="text-button"
onclick={() => {
journal = true;
socialTab = "journal";
requestAnimationFrame(() =>
document.querySelector(".inline-journal")?.scrollIntoView({ behavior: "smooth", block: "nearest" })
document
.querySelector(
window.innerWidth <= 850 && store.chat.enabled ? ".mobile-journal .inline-journal" : ".inline-journal"
)
?.scrollIntoView({ behavior: "smooth", block: "nearest" })
);
}}>Journal</button
>
@ -502,6 +543,9 @@
(s.phase === "equipment" && me.ready && !canReopenChoice(s, seat!)) ||
(s.phase === "eruption" && seat !== 0)}>{mobileLabel} →</button
>
{#if store.chat.enabled}<button class="dock-chat" onclick={openChat}
>Chat{store.chat.unread ? ` · ${store.chat.unread}` : ""}</button
>{/if}
</div>{/if}
<section class="team" aria-label="Your expedition">
{#each s.players as p, i}
@ -740,37 +784,29 @@
</div>{/if}
{/if}
</section>
<section class="journal inline-journal" aria-label="Expedition journal">
<header class="journal-header">
<h2>Expedition journal</h2>
<button
class="text-button"
aria-label={journal ? "Collapse journal" : "Expand journal"}
aria-expanded={journal}
aria-controls="journal-entries"
onclick={() => (journal = !journal)}>{journal ? "−" : "+"}</button
>
</header>
{#if journal}
<ol id="journal-entries" tabindex="0" aria-label="Journal entries">
{#each journalEntries as e, i}
{#if i === 0 || journalEntries[i - 1]!.round !== e.round}
<li class="journal-divider">
<h3>{e.round === 0 ? "Preparation" : `Round ${String(e.round).padStart(2, "0")}`}</h3>
</li>
{/if}
<li class:latest={i === 0} class:journal-phase={e.type === "phase"}>
{#if e.type === "phase"}
<h4><span aria-hidden="true">◇</span>{e.text}</h4>
{:else}<div class="journal-entry">
<JournalEntry entry={e} colorblind={store.colorblind} />
</div>{/if}
</li>{/each}
</ol>{/if}
</section>
<div class:desktop-journal={store.chat.enabled}>{@render journalPanel("desktop")}</div>
</div>
<aside class="journey">
<ChatPanel {store} />
<div class="social-panel" data-tab={socialTab}>
{#if store.chat.enabled}<nav class="social-tabs" aria-label="Chat and journal">
<button
class:active={socialTab === "chat"}
onclick={() => {
socialTab = "chat";
store.chat.open = true;
}}>Chat{store.chat.unread ? ` · ${store.chat.unread}` : ""}</button
>
<button
class:active={socialTab === "journal"}
onclick={() => {
socialTab = "journal";
journal = true;
}}>Journal</button
>
</nav>{/if}
<ChatPanel {store} />
{#if store.chat.enabled}<div class="mobile-journal">{@render journalPanel("mobile")}</div>{/if}
</div>
<div class="journey-actions">
<h2 class="sr-only">{nextAction}</h2>
<nav class="phase-track" aria-label="Round phases">

View file

@ -22,6 +22,7 @@
function visibleRead() {
if (!list || !chat.open || document.visibilityState !== "visible" || !document.hasFocus()) return;
const bounds = list.getBoundingClientRect();
if (!bounds.width || !bounds.height) return;
const rows = [...list.querySelectorAll<HTMLElement>("[data-message-id]")];
const latest = rows.reverse().find((row) => {
const r = row.getBoundingClientRect();

View file

@ -319,7 +319,7 @@
<style>
.dice-preview {
display: none;
display: block;
pointer-events: none;
}
.dice-preview rect {

View file

@ -24,8 +24,18 @@ export class Chat {
send: (data: { text: string; requestId: string }) => void = () => {};
read: (messageId: string) => void = () => {};
private newestRead = "";
unreadIds: string[] = $state([]);
playerIndex: number | undefined;
get unread() {
return this.unreadIds.filter((id) => this.messages.some((m) => m._id === id)).length;
}
private readTimer: ReturnType<typeof setTimeout> | undefined;
markRead(messageId: string) {
const index = this.messages.findIndex((m) => m._id === messageId);
if (index >= 0) {
const seen = new Set(this.messages.slice(0, index + 1).map((m) => m._id));
this.unreadIds = this.unreadIds.filter((id) => !seen.has(id));
}
// BGS message IDs are time-ordered ObjectIds. Keep the watermark even when
// older messages are deleted, history is replaced, or the panel is collapsed.
if (!/^[0-9a-f]{24}$/i.test(messageId)) return;
@ -42,8 +52,20 @@ export class Chat {
replace(messages: ChatMessage[]) {
this.enabled = true;
this.messages = messages;
this.unreadIds = [];
}
append(messages: ChatMessage[]) {
this.unreadIds = [
...this.unreadIds,
...messages
.filter(
(m) =>
m._id &&
!this.messages.some((old) => old._id === m._id) &&
(m.playerIndex === undefined || m.playerIndex !== this.playerIndex)
)
.map((m) => m._id!),
];
this.messages = [
...this.messages,
...messages.filter((m) => !m._id || !this.messages.some((old) => old._id === m._id)),

View file

@ -918,7 +918,7 @@ h3 {
max-height: none;
display: contents;
}
.journey > .expedition-chat {
.journey > .social-panel {
grid-row: 1;
}
.world-column {
@ -1706,3 +1706,43 @@ button.equipment-chip {
min-height: 40px;
}
}
.social-tabs,
.mobile-journal {
display: none;
}
@media (max-width: 850px) {
.desktop-journal {
display: none;
}
.social-tabs {
display: flex;
gap: 6px;
border-bottom: 1px solid var(--line);
padding-bottom: 6px;
}
.social-tabs button {
background: transparent;
color: var(--text);
border: 1px solid #58756a;
padding: 6px 14px;
}
.social-tabs button.active {
color: #f1d584;
border-color: #f1d584;
}
.social-panel[data-tab="journal"] .expedition-chat {
display: none;
}
.social-panel[data-tab="journal"] .mobile-journal {
display: block;
}
.mobile-journal #journal-entries-mobile {
max-height: 280px;
overflow-y: auto;
}
.mobile-dock .dock-chat {
flex-shrink: 0;
padding: 8px;
}
}

63
tests/chat-tabs-smoke.mjs Normal file
View file

@ -0,0 +1,63 @@
import { chromium } from "playwright";
import assert from "node:assert/strict";
const root = process.cwd() + "/";
const { initGame, applyMove, stripSecret, total, cell } = await import(root + "packages/engine/dist/index.js");
let state = initGame(3, { skillAssignment: "fixed" }, "mobile-readiness");
for (let i = 0; i < 3; i++)
if (!state.players[i].setupDone)
state = applyMove(
state,
{ action: "setup", keep: state.players[i].cards.slice(0, i === 2 ? 2 : 1).map((c) => c.id) },
i
);
const browser = await chromium.launch({ executablePath: process.env.FUJI_CHROMIUM_EXECUTABLE, headless: true });
const page = await browser.newPage({ viewport: { width: 390, height: 740 }, isMobile: true, hasTouch: true });
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.setContent('<meta name="viewport" content="width=device-width, initial-scale=1"><div id="app"></div>');
await page.addStyleTag({ path: root + "packages/viewer/dist/fuji-viewer.css" });
await page.addScriptTag({ path: root + "packages/viewer/dist/fuji-viewer.iife.js" });
await page.evaluate(
(s) => {
window.events = [];
window.bridge = fuji.launch("#app");
bridge.on("player:clicked", (p) => events.push(["player", p]));
bridge.on("move", (p) => events.push(["move", p]));
bridge.emit("player", { index: 0 });
bridge.emit("state", s);
},
stripSecret(state, 0)
);
const previews = page.locator(".dice-preview");
await previews.first().waitFor({ state: "visible" });
for (const preview of await previews.all()) {
const tileId = await preview.getAttribute("data-location");
const value = Number(await preview.locator("text").textContent());
assert.equal(value, total(state.players[0], cell(state, tileId).terrain) + state.players[0].bonus);
}
await page.evaluate(() => bridge.emit("chat:messages", []));
await page.getByRole("button", { name: "Journal", exact: true }).last().click();
await page.evaluate(() =>
bridge.emit("chat:appended", [
{ _id: "abcdef000000000000000001", author: "Teammate", playerIndex: 1, text: "Try the bridge", type: "text" },
])
);
await page.waitForTimeout(100);
assert(
await page
.locator(".dock-chat")
.textContent()
.then((t) => t.includes("1"))
);
await page.locator(".dock-chat").click();
await page.waitForTimeout(700);
assert.equal(await page.locator(".dock-chat").textContent(), "Chat");
await page.setViewportSize({ width: 1440, height: 1000 });
assert(await page.locator(".dice-preview").first().isVisible());
assert(await page.locator(".desktop-journal").isVisible());
assert(!(await page.locator(".social-tabs").isVisible()));
assert.deepEqual(errors, []);
console.log("Fuji: mobile tabs, unread, desktop previews passed");
await browser.close();

View file

@ -50,7 +50,7 @@ await page.evaluate(() => scrollTo(0, 600));
const rect = await page.locator(".mobile-dock").boundingBox();
assert(rect && rect.y >= -1 && rect.y < 5);
await page.locator(".mobile-dock button").click();
await page.locator(".mobile-dock button").first().click();
assert.equal((await page.evaluate(() => events)).at(-1)[1].action, "ready");
assert.deepEqual(errors, []);
console.log(