Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 233 additions & 0 deletions apps/web/src/fork/whiteboard/WhiteboardPanel.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
import { describe, expect, it, vi } from "vite-plus/test";

import { render } from "../../../test/render";
import { useRightPanelStore } from "../../rightPanelStore";
import { postWhiteboardHover, showBoardRef } from "./boardChannel";
import { WhiteboardPanel } from "./WhiteboardPanel";

const role = vi.hoisted(() => ({ projectRoot: "/project" }));

vi.mock("./role", () => ({
useBoardHostRole: () => ({ menu: {} }),
isBoardHost: () => true,
isTopWindow: () => true,
boardOfferedFor: () => true,
useBoardOfferedFor: () => true,
readThreadProjectRoot: () => role.projectRoot,
useThreadProjectRoot: () => role.projectRoot,
}));

type BoardWindow = Window & { kept?: boolean; received?: unknown[] };

// Each test has its own thread and project, so boards a test leaves loaded never meet another test.
function setup(name: string) {
role.projectRoot = `/project-${name}`;
const threadRef = scopeThreadRef(EnvironmentId.make("env"), ThreadId.make(`thread-${name}`));
useRightPanelStore.getState().open(threadRef, "whiteboard");
return threadRef;
}

function Panes(props: {
readonly at: "left" | "right" | null;
readonly threadRef: ReturnType<typeof setup>;
readonly sheet?: boolean;
}) {
const pane = (side: "left" | "right") => (
<div data-pane={side} style={{ position: "relative", display: "flex", width: 400 }}>
{props.at === side ? <WhiteboardPanel threadRef={props.threadRef} /> : null}
</div>
);
return (
<div
data-slot={props.sheet ? "sheet-popup" : undefined}
style={{ display: "flex", height: 300 }}
>
{pane("left")}
{pane("right")}
</div>
);
}

const shownFrame = (container: HTMLElement) =>
container.querySelector<HTMLIFrameElement>('iframe[title="Whiteboard"]');

// The board page's own `wb-ready`, posted from inside the frame so the frame is its source.
async function loadedBoard(container: HTMLElement): Promise<BoardWindow> {
const board = await vi.waitFor(() => {
const window = shownFrame(container)?.contentWindow as BoardWindow | null | undefined;
expect(window?.location.search).toContain("embed=panel");
expect(window?.document.readyState).toBe("complete");
return window!;
});
board.kept = true;
board.received = [];
board.addEventListener("message", (event) => board.received!.push(event.data));
(board as Window & { eval: (code: string) => void }).eval(
'parent.postMessage({ type: "wb-ready" }, location.origin)',
);
return board;
}

// Hover with no reference only clears a mark, which is harmless at any time.
const isNotAHoverEnd = (message: unknown) => {
const { type, ref } = message as { type?: unknown; ref?: unknown };
return type !== "wb-hover" || ref !== null;
};

function closeWhiteboardTab(threadRef: ReturnType<typeof setup>) {
const surfaces = useRightPanelStore.getState().byThreadKey[scopedThreadKey(threadRef)]?.surfaces;
for (const surface of surfaces ?? [])
if (surface.kind === "whiteboard")
useRightPanelStore.getState().closeSurface(threadRef, surface.id);
}

describe("WhiteboardPanel", () => {
it("keeps its board page loaded while hidden and when it moves to the other pane", async () => {
const threadRef = setup("keep");
await using screen = await render(<Panes at="left" threadRef={threadRef} />, { router: false });
const board = await loadedBoard(screen.container);

for (const at of [null, "left", null, "right", "left"] as const) {
await screen.rerender(<Panes at={at} threadRef={threadRef} />);
if (at === null) continue;
expect(shownFrame(screen.container)?.closest("[data-pane]")?.getAttribute("data-pane")).toBe(
at,
);
expect((shownFrame(screen.container)?.contentWindow as BoardWindow | null)?.kept).toBe(true);
}
expect(shownFrame(screen.container)?.contentWindow).toBe(board);
closeWhiteboardTab(threadRef);
});

it("highlights a link clicked while hidden in the loaded board, once, and pauses it while hidden", async () => {
const threadRef = setup("link");
await using screen = await render(<Panes at="left" threadRef={threadRef} />, { router: false });
const board = await loadedBoard(screen.container);
await screen.rerender(<Panes at={null} threadRef={threadRef} />);

postWhiteboardHover("board://r/1,2,3,4", document.createElement("a"));
showBoardRef("board://r/5,6,7,8", threadRef);
await screen.rerender(<Panes at="left" threadRef={threadRef} />);

await vi.waitFor(() =>
expect(board.received).toContainEqual({ type: "wb-highlight", ref: "board://r/5,6,7,8" }),
);
expect(shownFrame(screen.container)?.contentWindow).toBe(board);
expect(board.received!.filter(isNotAHoverEnd)).toEqual([
{ type: "wb-visible", visible: false },
{ type: "wb-visible", visible: true },
{ type: "wb-highlight", ref: "board://r/5,6,7,8" },
]);
closeWhiteboardTab(threadRef);
});

it("keeps a click that opened the board while it moves to the other pane before it is ready", async () => {
const threadRef = setup("move");
await using screen = await render(<Panes at="left" threadRef={threadRef} />, { router: false });
showBoardRef("board://r/5,6,7,8", threadRef);
await screen.rerender(<Panes at="right" threadRef={threadRef} />);

const board = await loadedBoard(screen.container);
await vi.waitFor(() =>
expect(board.received).toEqual([{ type: "wb-highlight", ref: "board://r/5,6,7,8" }]),
);
closeWhiteboardTab(threadRef);
});

it.each([
[
"closes",
"close",
(ref: ReturnType<typeof setup>) => useRightPanelStore.getState().close(ref),
],
[
"maximizes",
"maximize",
(ref: ReturnType<typeof setup>) => useRightPanelStore.getState().setMaximized(ref, true),
],
] as const)(
"keeps its board page loaded when its sheet %s and its portal node leaves first",
async (_, name, sheetGoes) => {
const threadRef = setup(name);
await using screen = await render(<Panes at="left" threadRef={threadRef} sheet />, {
router: false,
});
const board = await loadedBoard(screen.container);
sheetGoes(threadRef);
// The sheet's portal node leaves the document before React detaches the refs inside it.
screen.container.remove();
await screen.unmount();

useRightPanelStore.getState().open(threadRef, "whiteboard");
await using reopened = await render(<Panes at="left" threadRef={threadRef} />, {
router: false,
});
expect(shownFrame(reopened.container)?.contentWindow).toBe(board);
expect(board.kept).toBe(true);
closeWhiteboardTab(threadRef);
},
);

it("shows its board again when its sheet reopens before it is removed", async () => {
const threadRef = setup("reopen");
await using screen = await render(<Panes at="left" threadRef={threadRef} sheet />, {
router: false,
});
const board = await loadedBoard(screen.container);
useRightPanelStore.getState().close(threadRef);
expect(shownFrame(screen.container)).toBeNull();
useRightPanelStore.getState().open(threadRef, "whiteboard");
expect(shownFrame(screen.container)?.contentWindow).toBe(board);
closeWhiteboardTab(threadRef);
});

it("leaves its board unloaded when the sheet opens again after its last tab closed", async () => {
const threadRef = setup("evicted");
await using screen = await render(<Panes at="left" threadRef={threadRef} sheet />, {
router: false,
});
const frame = shownFrame(screen.container)!;
await loadedBoard(screen.container);
closeWhiteboardTab(threadRef);
useRightPanelStore.getState().toggleVisibility(threadRef);
expect(frame.isConnected).toBe(false);
});

it("holds a link for a board page that reloads until that page is ready again", async () => {
const threadRef = setup("reload");
await using screen = await render(<Panes at="left" threadRef={threadRef} />, { router: false });
const board = await loadedBoard(screen.container);
await expect.element(screen.getByText("Loading board...")).not.toBeInTheDocument();
(board as Window & { eval: (code: string) => void }).eval(
'parent.postMessage({ type: "wb-unload" }, location.origin)',
);
await expect.element(screen.getByText("Loading board...")).toBeInTheDocument();
showBoardRef("board://r/5,6,7,8", threadRef);
// Messages from one window arrive in order, so a highlight sent now would arrive before this.
board.postMessage({ type: "probe" }, location.origin);
await vi.waitFor(() => expect(board.received).toContainEqual({ type: "probe" }));
expect(board.received!.filter(isNotAHoverEnd)).toEqual([{ type: "probe" }]);
board.received = [];

(board as Window & { eval: (code: string) => void }).eval(
'parent.postMessage({ type: "wb-ready" }, location.origin)',
);
await vi.waitFor(() =>
expect(board.received).toEqual([{ type: "wb-highlight", ref: "board://r/5,6,7,8" }]),
);
closeWhiteboardTab(threadRef);
});

it("unloads its board when the Whiteboard tab closes", async () => {
const threadRef = setup("close");
await using screen = await render(<Panes at="left" threadRef={threadRef} />, { router: false });
const frame = shownFrame(screen.container)!;
await loadedBoard(screen.container);
await screen.rerender(<Panes at={null} threadRef={threadRef} />);

closeWhiteboardTab(threadRef);
expect(frame.isConnected).toBe(false);
});
});
27 changes: 10 additions & 17 deletions apps/web/src/fork/whiteboard/WhiteboardPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { scopedThreadKey } from "@t3tools/client-runtime/environment";
import type { ScopedThreadRef } from "@t3tools/contracts";
import { useState } from "react";

import { registerBoardFrame } from "./boardChannel";
import { boardUrl, showBoard } from "./boardChannel";
import { useBoardHostRole, useBoardOfferedFor, useThreadProjectRoot } from "./role";

/** The right panel's Whiteboard tab: the project's board, framed from the board page. */
/** The right panel's Whiteboard tab: the project's board, which stays loaded while the tab is hidden. */
export function WhiteboardPanel(props: { readonly threadRef: ScopedThreadRef }) {
const role = useBoardHostRole();
const projectRoot = useThreadProjectRoot(props.threadRef);
Expand All @@ -19,36 +19,29 @@ export function WhiteboardPanel(props: { readonly threadRef: ScopedThreadRef })
);
}
return (
<BoardFrame
<BoardSlot
key={projectRoot}
url={boardUrl(projectRoot)}
threadKey={scopedThreadKey(props.threadRef)}
projectRoot={projectRoot}
/>
);
}

function BoardFrame(props: { readonly threadKey: string; readonly projectRoot: string }) {
function BoardSlot(props: { readonly url: string; readonly threadKey: string }) {
const [ready, setReady] = useState(false);
// The React Compiler keeps this callback stable, so a re-render does not unregister the frame
// (which drops a click still waiting for it).
const registerFrame = (element: HTMLIFrameElement | null) =>
element === null
// The React Compiler keeps this callback stable, so a re-render does not park and show the board.
const attachBoard = (slot: HTMLDivElement | null) =>
slot === null
? undefined
: registerBoardFrame(element, props.threadKey, () => setReady(true));
: showBoard(slot, { url: props.url, threadKey: props.threadKey, onReady: setReady });
return (
<div className="relative flex min-h-0 flex-1">
<div ref={attachBoard} className="absolute inset-0" />
{ready ? null : (
<p className="absolute inset-x-0 top-1/3 text-center text-muted-foreground text-sm">
Loading board...
</p>
)}
{/* oxlint-disable-next-line react/iframe-missing-sandbox -- the board is this origin's own page; it needs scripts and same-origin storage, which together make a sandbox moot. */}
<iframe
ref={registerFrame}
title="Whiteboard"
src={`/?embed=panel&room=${encodeURIComponent(props.projectRoot)}`}
className="size-full border-0"
/>
</div>
);
}
35 changes: 35 additions & 0 deletions apps/web/src/fork/whiteboard/boardChannel.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isBoardOffered,
isFromBoardFrame,
keepPendingFor,
liveBoardsToKeep,
} from "./boardChannel.logic";

const OWN = "https://board.example.com";
Expand Down Expand Up @@ -103,3 +104,37 @@ describe("hoverOriginInFrame", () => {
).toEqual({ x: -580, y: 362 });
});
});

describe("liveBoardsToKeep", () => {
const board = (id: string, shown: boolean, listed: boolean) => ({ id, shown, listed });

it.each([
{
name: "the shown board, even when its tab is no longer listed",
boards: [board("a", true, false)],
kept: ["a"],
},
{
name: "a hidden board while its tab is listed",
boards: [board("a", true, true), board("b", false, true)],
kept: ["a", "b"],
},
{
name: "not a hidden board whose tab closed",
boards: [board("a", true, true), board("b", false, false)],
kept: ["a"],
},
{
name: "the most recently shown boards up to the bound",
boards: [board("a", true, true), board("b", false, true), board("c", false, true)],
kept: ["a", "b"],
},
{
name: "two hidden boards when none is shown",
boards: [board("a", false, true), board("b", false, true), board("c", false, true)],
kept: ["a", "b"],
},
])("keeps $name", ({ boards, kept }) => {
expect(liveBoardsToKeep(boards, 2).map((entry) => entry.id)).toEqual(kept);
});
});
16 changes: 14 additions & 2 deletions apps/web/src/fork/whiteboard/boardChannel.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function isBoardOffered(input: {
);
}

/** A message is the board's only from this origin and from the registered frame's window. */
/** A message is the board's only from this origin and from that board frame's window. */
export function isFromBoardFrame(input: {
readonly origin: string;
readonly ownOrigin: string;
Expand Down Expand Up @@ -55,7 +55,7 @@ export function deliverableHighlight(
: null;
}

/** A frame mounting or unmounting for one thread drops a highlight waiting for another. */
/** Showing a board for one thread drops a highlight waiting for another. */
export function keepPendingFor(
pending: PendingHighlight | null,
threadKey: string,
Expand Down Expand Up @@ -99,3 +99,15 @@ interface RectLike {
export function hoverOriginInFrame(chip: RectLike, frame: RectLike): { x: number; y: number } {
return { x: chip.left - frame.left, y: chip.top + chip.height / 2 - frame.top };
}

/**
* The live boards to keep, given most recently shown first: the shown board always, and a hidden
* one while its thread still lists the Whiteboard tab, `max` boards in all.
*/
export function liveBoardsToKeep<B extends { readonly shown: boolean; readonly listed: boolean }>(
mostRecentFirst: ReadonlyArray<B>,
max: number,
): B[] {
let room = max - mostRecentFirst.filter((board) => board.shown).length;
return mostRecentFirst.filter((board) => board.shown || (board.listed && room-- > 0));
}
Loading
Loading