@devries-cameron20 / t3code Mirror · read-only / commits / f5f8c8d18b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

fix(web): opening a file in the preview browser shows its current contents (#38470)

Julius Marminge committed Nov 17, 2026 at 23:42 UTC f5f8c8d18baf6a194fea9c7a3734158c2b7ea57b
2 files changed +77
apps/web/src/components/files/FilePreviewPanel.browser.test.tsx
+76
@@ -19,6 +19,8 @@ const state = vi.hoisted(() => ({
19 writeFile: vi.fn(),
20 assetUrl: { _tag: "Loading" } as import("~/assets/assetUrls").AssetUrlState,
21 mintUrl: vi.fn<() => Promise<string | null>>(),
22 + browserAssetMints: 0,
23 + openedBrowserUrls: [] as string[],
24 }));
25
26 vi.mock(import("@t3tools/client-runtime/state/runtime"), async (original) => ({
@@ -65,6 +67,56 @@ vi.mock(import("~/assets/assetUrls"), async (original) => ({
67 useAssetUrlRefresh: () => state.mintUrl,
68 useAssetUrlState: () => state.assetUrl,
69 }));
70 +vi.mock(import("~/browser/previewRuntime"), async (original) => ({
71 + ...(await original()),
72 + usePreviewAvailable: () => true,
73 +}));
74 +vi.mock(import("~/state/environments"), async (original) => ({
75 + ...(await original()),
76 + useEnvironmentHttpBaseUrl: () => "http://environment.test",
77 +}));
78 +vi.mock(import("~/state/assets"), async (original) => {
79 + const actual = await original();
80 + const { Atom } = await import("effect/reactivity");
81 + const Effect = await import("effect/Effect");
82 + // Like the real query family, a minted URL stays cached until the atom is refreshed.
83 + const createUrl = Atom.family((_key: string) =>
84 + Atom.make(
85 + Effect.sync(() => ({
86 + relativeUrl: `/api/assets/mint-${++state.browserAssetMints}/page.html`,
87 + expiresAt: Number.MAX_SAFE_INTEGER,
88 + })),
89 + ).pipe(Atom.keepAlive),
90 + );
91 + return {
92 + ...actual,
93 + assetEnvironment: {
94 + ...actual.assetEnvironment,
95 + createUrl: ((target: { input: { resource: { path: string } } }) =>
96 + createUrl(target.input.resource.path)) as never,
97 + },
98 + };
99 +});
100 +vi.mock(import("~/browser/openFileInPreview"), async (original) => {
101 + const actual = await original();
102 + return {
103 + ...actual,
104 + openFileInPreview: (async (input: Parameters<typeof actual.openFileInPreview>[0]) => {
105 + const result = await input.createAssetUrl({
106 + environmentId: input.threadRef.environmentId,
107 + input: {
108 + resource: {
109 + _tag: "workspace-file",
110 + threadId: input.threadRef.threadId,
111 + path: input.filePath,
112 + },
113 + },
114 + });
115 + if (result._tag === "Success") state.openedBrowserUrls.push(result.value.relativeUrl);
116 + return AsyncResult.success(undefined);
117 + }) as never,
118 + };
119 +});
120 // The Word engine compiles a large WebAssembly bundle; these tests only need the URL it loads.
121 vi.mock(import("./OfficeFilePreview"), () => ({
122 OfficeFilePreview: ({ src }: { src: string }) => <div data-testid="office-preview">{src}</div>,
@@ -121,6 +173,7 @@ function setup() {
173 state.file = null;
174 state.optimistic = null;
175 state.assetUrl = { _tag: "Loading" };
176 + state.openedBrowserUrls = [];
177 let mints = 0;
178 state.mintUrl.mockReset().mockImplementation(async () => `/assets/report.docx?mint=${++mints}`);
179 state.writeFile.mockReset().mockResolvedValue(AsyncResult.success(undefined));
@@ -409,6 +462,29 @@ describe("workspace document previews", () => {
462 });
463 });
464
465 +describe("open file in preview browser", () => {
466 + it("mints a fresh asset URL on every click so the browser cannot reuse cached bytes", async () => {
467 + using _settings = setup();
468 + state.file = {
469 + relativePath: "page.html",
470 + contents: "<h1>page</h1>",
471 + byteLength: 13,
472 + truncated: false,
473 + };
474 + await using screen = await render(filePreview("page.html"));
475 + const button = screen.getByRole("button", { name: "Open file in preview browser" });
476 +
477 + await userEvent.click(button);
478 + await vi.waitFor(() => expect(state.openedBrowserUrls).toHaveLength(1));
479 + await userEvent.click(button);
480 + await vi.waitFor(() => expect(state.openedBrowserUrls).toHaveLength(2));
481 +
482 + expect(state.openedBrowserUrls[1], "the second click reused the first signed URL").not.toBe(
483 + state.openedBrowserUrls[0],
484 + );
485 + });
486 +});
487 +
488 describe("rendered Markdown select all", () => {
489 it("selects only the rendered file, including after a task toggle", async () => {
490 using _settings = setup();
apps/web/src/components/files/FilePreviewPanel.tsx
+1
@@ -1202,6 +1202,7 @@ export default function FilePreviewPanel({
1202 const previewAvailable = usePreviewAvailable(environmentId);
1203 const createAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, {
1204 reportFailure: false,
1205 + refresh: true,
1206 });
1207 const openPreview = useAtomCommand(previewEnvironment.open, {
1208 reportFailure: false,