Skip to content

Commit 5d2b08e

Browse files
tellahoCarl
andauthored
feat(channels): surface canvas content in channel settings (#426)
Signed-off-by: Carl <acda9e433d19dcd0e6b6840f7f4b98f3a56f1fab98049d444c087019e6d36560@buzz.block.builderlab.xyz> Co-authored-by: Carl <acda9e433d19dcd0e6b6840f7f4b98f3a56f1fab98049d444c087019e6d36560@buzz.block.builderlab.xyz>
1 parent 329fe2a commit 5d2b08e

9 files changed

Lines changed: 469 additions & 12 deletions

‎src/bundled/channels/ChannelCanvasDialog.test.tsx‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
screen,
99
waitFor,
1010
} from "@testing-library/react";
11-
import { StrictMode } from "react";
11+
import { StrictMode, useRef, useState } from "react";
1212
import userEvent from "@testing-library/user-event";
1313
import { afterEach, beforeEach, expect, it, vi } from "vitest";
1414
import { keypair, signed } from "../../features/relay/testing";
@@ -163,3 +163,57 @@ it("ignores the stale StrictMode read after the current read enables typing and
163163
release(head);
164164
}
165165
});
166+
167+
it("returns focus to the ingress on close and retains edits without publishing", async () => {
168+
const user = userEvent.setup();
169+
const canvas = {
170+
available: true,
171+
read: vi.fn(async () => head),
172+
save: vi.fn(async () => head),
173+
};
174+
function Harness() {
175+
const [open, setOpen] = useState(false);
176+
const trigger = useRef<HTMLButtonElement>(null);
177+
return (
178+
<>
179+
<button type="button" ref={trigger} onClick={() => setOpen(true)}>
180+
Canvas
181+
</button>
182+
{open && (
183+
<ChannelCanvasDialog
184+
canvas={canvas}
185+
scope={scope}
186+
channelId={channelId}
187+
open={open}
188+
onOpenChange={setOpen}
189+
finalFocus={trigger}
190+
/>
191+
)}
192+
</>
193+
);
194+
}
195+
render(<Harness />);
196+
const trigger = screen.getByRole("button", { name: "Canvas" });
197+
await user.click(trigger);
198+
const text = screen.getByRole("textbox", { name: "Canvas Markdown" });
199+
await waitFor(() => expect(text).toHaveValue("Saved"));
200+
await user.clear(text);
201+
await user.type(text, "Local draft");
202+
await user.keyboard("{Escape}");
203+
await waitFor(() => expect(trigger).toHaveFocus());
204+
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
205+
expect(canvas.save).not.toHaveBeenCalled();
206+
await user.keyboard("{Enter}");
207+
await waitFor(() =>
208+
expect(
209+
screen.getByRole("textbox", { name: "Canvas Markdown" }),
210+
).toBeEnabled(),
211+
);
212+
expect(screen.getByRole("textbox", { name: "Canvas Markdown" })).toHaveValue(
213+
"Local draft",
214+
);
215+
await user.click(screen.getByRole("button", { name: "Close Canvas" }));
216+
await waitFor(() => expect(trigger).toHaveFocus());
217+
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
218+
expect(canvas.save).not.toHaveBeenCalled();
219+
});

‎src/bundled/channels/ChannelCanvasDialog.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ChannelCanvas } from "../../features/channel-templates/capability"
33
import type { RelayEvent } from "../../features/relay/events";
44
import { readView, writeView } from "../../shared/view-state";
55
import { Button } from "../../shared/design-system/ui/Button";
6-
import { Dialog } from "../../shared/design-system/ui/Dialog";
6+
import { Dialog, type DialogProps } from "../../shared/design-system/ui/Dialog";
77
import { Textarea } from "../../shared/design-system/ui/Textarea";
88
import styles from "./ChannelTemplates.module.css";
99

@@ -14,12 +14,14 @@ export function ChannelCanvasDialog({
1414
channelId,
1515
open,
1616
onOpenChange,
17+
finalFocus,
1718
}: {
1819
canvas: ChannelCanvas;
1920
scope: string;
2021
channelId: string;
2122
open: boolean;
2223
onOpenChange(open: boolean): void;
24+
finalFocus?: DialogProps["finalFocus"];
2325
}) {
2426
const key = `canvas-draft-v1:${channelId}`;
2527
const [saved] = useState(() => {
@@ -99,6 +101,7 @@ export function ChannelCanvasDialog({
99101
<Dialog
100102
open={open}
101103
onOpenChange={onOpenChange}
104+
finalFocus={finalFocus}
102105
preventClose={busy}
103106
title="Channel Canvas"
104107
closeLabel="Close Canvas"

‎src/bundled/channels/ChannelDetailsEditor.test.tsx‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,20 +299,34 @@ it("keeps details readable without editing authority or host support", async ()
299299
).toBeVisible();
300300
});
301301

302-
it("Escape dismisses the select, then the edit dialog, then Settings", async () => {
302+
it("orders metadata, Canvas, and actions and dismisses each edit layer with Escape", async () => {
303303
const h = harness();
304304
const user = userEvent.setup();
305305
const close = vi.fn();
306306
render(
307307
<ChannelSettingsPanel
308308
channel={channel}
309309
details={h.capability}
310+
openCanvas={() => {}}
311+
setupTools={<button type="button">Leave channel</button>}
310312
close={close}
311313
>
312314
Diagnostics
313315
</ChannelSettingsPanel>,
314316
);
315-
await user.click(await screen.findByRole("button", { name: "Edit details" }));
317+
const edit = await screen.findByRole("button", { name: "Edit details" });
318+
const canvas = screen.getByRole("button", { name: "Canvas" });
319+
expect(
320+
screen.getByText(channel.description).compareDocumentPosition(canvas) &
321+
Node.DOCUMENT_POSITION_FOLLOWING,
322+
).toBeTruthy();
323+
await user.tab();
324+
expect(canvas).toHaveFocus();
325+
await user.tab();
326+
expect(edit).toHaveFocus();
327+
await user.tab();
328+
expect(screen.getByRole("button", { name: "Leave channel" })).toHaveFocus();
329+
await user.click(edit);
316330
expect(
317331
screen.getByRole("dialog", { name: "Edit channel details" }),
318332
).toBeVisible();

‎src/bundled/channels/ChannelSettingsPanel.test.tsx‎

Lines changed: 210 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// @vitest-environment jsdom
22
import "@testing-library/jest-dom/vitest";
3-
import { cleanup, render, screen } from "@testing-library/react";
3+
import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
44
import userEvent from "@testing-library/user-event";
55
import { afterEach, expect, it, vi } from "vitest";
6+
import { keypair, signed } from "../../features/relay/testing";
7+
import type { ChannelCanvas } from "../../features/channel-templates/capability";
68
import { ChannelSettingsPanel } from "./ChannelSettingsPanel";
79

810
afterEach(cleanup);
@@ -77,3 +79,210 @@ it("does not invent unknown metadata and still exposes diagnostics without a cha
7779
await user.click(screen.getByText("Diagnostics"));
7880
expect(screen.getByText("Diagnostics content")).toBeVisible();
7981
});
82+
83+
it("opens Canvas from its own keyboard-accessible row before setup actions", async () => {
84+
const user = userEvent.setup();
85+
const openCanvas = vi.fn();
86+
render(
87+
<ChannelSettingsPanel
88+
channel={{ id: "alpha", name: "Alpha", channelType: "stream" }}
89+
close={() => {}}
90+
openCanvas={openCanvas}
91+
setupTools={<button type="button">Leave channel</button>}
92+
>
93+
Diagnostics content
94+
</ChannelSettingsPanel>,
95+
);
96+
const canvas = screen.getByRole("button", { name: "Canvas" });
97+
expect(canvas).toHaveAttribute("aria-haspopup", "dialog");
98+
expect(canvas).not.toHaveTextContent("Shared notes and plans");
99+
expect(openCanvas).not.toHaveBeenCalled();
100+
await user.tab();
101+
expect(canvas).toHaveFocus();
102+
await user.keyboard("{Enter}");
103+
expect(openCanvas).toHaveBeenCalledTimes(1);
104+
expect(openCanvas).toHaveBeenLastCalledWith(canvas);
105+
await user.tab();
106+
expect(screen.getByRole("button", { name: "Leave channel" })).toHaveFocus();
107+
await user.click(canvas);
108+
expect(openCanvas).toHaveBeenCalledTimes(2);
109+
});
110+
111+
it("omits Canvas without a writable channel or an opener", () => {
112+
const channel = { id: "alpha", name: "Alpha" };
113+
const { rerender } = render(
114+
<ChannelSettingsPanel channel={channel} close={() => {}}>
115+
Diagnostics content
116+
</ChannelSettingsPanel>,
117+
);
118+
expect(
119+
screen.queryByRole("button", { name: "Canvas" }),
120+
).not.toBeInTheDocument();
121+
for (const unavailable of [
122+
undefined,
123+
{ ...channel, readOnly: true as const },
124+
]) {
125+
rerender(
126+
<ChannelSettingsPanel
127+
channel={unavailable}
128+
close={() => {}}
129+
openCanvas={() => {}}
130+
>
131+
Diagnostics content
132+
</ChannelSettingsPanel>,
133+
);
134+
expect(
135+
screen.queryByRole("button", { name: "Canvas" }),
136+
).not.toBeInTheDocument();
137+
}
138+
});
139+
140+
const head = signed(keypair(), {
141+
kind: 40100,
142+
content: "# Google root-link preview submission deep dive",
143+
tags: [["h", "alpha"]],
144+
});
145+
function previewFixture() {
146+
return {
147+
available: true,
148+
read: vi.fn<ChannelCanvas["read"]>().mockResolvedValue(head),
149+
save: vi.fn<ChannelCanvas["save"]>(),
150+
};
151+
}
152+
function panel(
153+
canvas: ChannelCanvas,
154+
id = "alpha",
155+
canvasOpen = false,
156+
readOnly?: true,
157+
) {
158+
return (
159+
<ChannelSettingsPanel
160+
canvas={canvas}
161+
channel={{ id, name: id, ...(readOnly ? { readOnly } : {}) }}
162+
canvasOpen={canvasOpen}
163+
openCanvas={() => {}}
164+
close={() => {}}
165+
>
166+
Diagnostics
167+
</ChannelSettingsPanel>
168+
);
169+
}
170+
171+
it("shows Canvas as the first line and saved plain text as the secondary preview", async () => {
172+
const canvas = previewFixture();
173+
const { container } = render(panel(canvas));
174+
expect(screen.getByText("Loading preview…")).toBeVisible();
175+
await screen.findByText("Google root-link preview submission deep dive");
176+
const row = screen.getByRole("button", { name: "Canvas" });
177+
expect(row).toHaveAccessibleDescription(
178+
"Google root-link preview submission deep dive",
179+
);
180+
expect(row.querySelector(".buzz-choice-row-label")).toHaveTextContent(
181+
/^Canvas$/,
182+
);
183+
expect(row.querySelector(".buzz-choice-row-description")).toHaveTextContent(
184+
"Google root-link preview submission deep dive",
185+
);
186+
expect(canvas.read).toHaveBeenCalledWith("alpha");
187+
expect(canvas.save).not.toHaveBeenCalled();
188+
expect(container.querySelector("img, script")).toBeNull();
189+
});
190+
191+
it.each([
192+
undefined,
193+
{ ...head, content: " \n" },
194+
{ ...head, content: "<!-- comment -->" },
195+
])("uses neutral empty copy for %j", async (event) => {
196+
const canvas = previewFixture();
197+
canvas.read.mockResolvedValue(event);
198+
render(panel(canvas));
199+
expect(await screen.findByText("No content yet")).toBeVisible();
200+
expect(screen.getByRole("button", { name: "Canvas" })).toBeEnabled();
201+
});
202+
203+
it("keeps a failed preview openable and refreshes saved content after closing the editor", async () => {
204+
const canvas = previewFixture();
205+
canvas.read.mockRejectedValueOnce(new Error("Offline"));
206+
const { rerender } = render(panel(canvas));
207+
expect(
208+
await screen.findByText("Preview unavailable. Open to retry."),
209+
).toBeVisible();
210+
expect(screen.getByRole("button", { name: "Canvas" })).toBeEnabled();
211+
rerender(panel(canvas, "alpha", true));
212+
expect(canvas.read).toHaveBeenCalledTimes(1);
213+
rerender(panel(canvas));
214+
expect(
215+
await screen.findByText("Google root-link preview submission deep dive"),
216+
).toBeVisible();
217+
rerender(panel(canvas, "alpha", true));
218+
canvas.read.mockResolvedValue({ ...head, content: "# Updated document" });
219+
rerender(panel(canvas));
220+
expect(await screen.findByText("Updated document")).toBeVisible();
221+
expect(canvas.save).not.toHaveBeenCalled();
222+
});
223+
224+
it("ignores an old channel's pending preview and hides content immediately on scope or access changes", async () => {
225+
const canvas = previewFixture();
226+
let release!: (event: typeof head) => void;
227+
canvas.read.mockReturnValueOnce(
228+
new Promise((resolve) => {
229+
release = resolve;
230+
}),
231+
);
232+
const { rerender } = render(panel(canvas));
233+
try {
234+
canvas.read.mockResolvedValue({ ...head, content: "# Beta document" });
235+
rerender(panel(canvas, "beta"));
236+
expect(await screen.findByText("Beta document")).toBeVisible();
237+
} finally {
238+
await act(async () => release(head));
239+
}
240+
expect(
241+
screen.queryByText("Google root-link preview submission deep dive"),
242+
).not.toBeInTheDocument();
243+
const otherCommunity = previewFixture();
244+
otherCommunity.read.mockResolvedValue(undefined);
245+
rerender(panel(otherCommunity, "beta"));
246+
expect(screen.queryByText("Beta document")).not.toBeInTheDocument();
247+
expect(await screen.findByText("No content yet")).toBeVisible();
248+
rerender(panel(otherCommunity, "beta", false, true));
249+
expect(
250+
screen.queryByRole("button", { name: /^Canvas/ }),
251+
).not.toBeInTheDocument();
252+
await waitFor(() => expect(otherCommunity.read).toHaveBeenCalledTimes(1));
253+
});
254+
255+
it("replaces a saved preview with loading and failure states during refresh", async () => {
256+
const canvas = previewFixture();
257+
const { rerender } = render(panel(canvas));
258+
await screen.findByText("Google root-link preview submission deep dive");
259+
rerender(panel(canvas, "alpha", true));
260+
let reject!: (error: Error) => void;
261+
canvas.read.mockReturnValueOnce(
262+
new Promise((_, rejectRead) => {
263+
reject = rejectRead;
264+
}),
265+
);
266+
try {
267+
rerender(panel(canvas));
268+
expect(canvas.read).toHaveBeenCalledTimes(2);
269+
expect(screen.getByText("Loading preview…")).toBeVisible();
270+
expect(
271+
screen.queryByText("Google root-link preview submission deep dive"),
272+
).not.toBeInTheDocument();
273+
expect(
274+
screen.getByRole("button", { name: "Canvas" }),
275+
).toHaveAccessibleDescription("Loading preview…");
276+
} finally {
277+
await act(async () => reject(new Error("Offline")));
278+
}
279+
expect(screen.getByText("Preview unavailable. Open to retry.")).toBeVisible();
280+
expect(
281+
screen.getByRole("button", { name: "Canvas" }),
282+
).toHaveAccessibleDescription("Preview unavailable. Open to retry.");
283+
expect(screen.getByRole("button", { name: "Canvas" })).toBeEnabled();
284+
expect(
285+
screen.queryByText("Google root-link preview submission deep dive"),
286+
).not.toBeInTheDocument();
287+
expect(canvas.save).not.toHaveBeenCalled();
288+
});

0 commit comments

Comments
 (0)