Skip to content

Commit f4ca44e

Browse files
committed
fix(web): retain public GitHub image action fallbacks
1 parent 43366ad commit f4ca44e

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

‎apps/web/src/components/media/MediaActions.test.tsx‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type AuthSessionState,
66
} from "@t3tools/contracts";
77
import { AsyncResult } from "effect/reactivity";
8+
import * as Cause from "effect/Cause";
89
import { createElement, isValidElement, type ReactNode } from "react";
910
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
1011

@@ -180,6 +181,67 @@ it("copies a CORS-blocked image through its source environment while keeping the
180181
expect(await item.items["image/png"]).toEqual(png);
181182
});
182183

184+
it.each([false, true])(
185+
"copies a public GitHub fallback after asset minting fails (CORS blocked: %s)",
186+
async (corsBlocked) => {
187+
const { readMediaPng } =
188+
await vi.importActual<typeof import("./mediaContent")>("./mediaContent");
189+
state.png.mockImplementation(readMediaPng);
190+
const url = "https://raw.githubusercontent.com/example/repo/main/image.png";
191+
const resource = { _tag: "github-media" as const, cwd: "/repo", url };
192+
state.mint.mockResolvedValueOnce(
193+
AsyncResult.failure(Cause.fail(new Error("GitHub unavailable"))),
194+
);
195+
const png = new Blob(["public image bytes"], { type: "image/png" });
196+
using fetch = vi.spyOn(globalThis, "fetch");
197+
if (corsBlocked) fetch.mockRejectedValueOnce(new TypeError("Failed to fetch"));
198+
fetch.mockResolvedValue(new Response(png));
199+
200+
await useMediaActions({
201+
kind: "image",
202+
name: "image.png",
203+
src: url,
204+
environmentId,
205+
asset: { environmentId, resource },
206+
}).copyImage();
207+
208+
expect(state.mint.mock.calls).toEqual([
209+
[{ environmentId, input: { resource } }],
210+
...(corsBlocked
211+
? [[{ environmentId, input: { resource: { _tag: "remote-image", url } } }]]
212+
: []),
213+
]);
214+
expect(fetch.mock.calls.map(([url]) => url)).toEqual([
215+
url,
216+
...(corsBlocked ? ["https://host.test/api/assets/image.png"] : []),
217+
]);
218+
const [item] = state.clipboard.mock.calls[0]![0];
219+
expect(await item.items["image/png"]).toEqual(png);
220+
},
221+
);
222+
223+
it("remints GitHub media before using its public display URL", async () => {
224+
const { readMediaPng } = await vi.importActual<typeof import("./mediaContent")>("./mediaContent");
225+
state.png.mockImplementation(readMediaPng);
226+
const png = new Blob(["authenticated image bytes"], { type: "image/png" });
227+
using fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(png));
228+
const url = "https://raw.githubusercontent.com/example/repo/main/private.png";
229+
const resource = { _tag: "github-media" as const, cwd: "/repo", url };
230+
231+
await useMediaActions({
232+
kind: "image",
233+
name: "private.png",
234+
src: url,
235+
environmentId,
236+
asset: { environmentId, resource },
237+
}).copyImage();
238+
239+
expect(state.mint).toHaveBeenCalledExactlyOnceWith({ environmentId, input: { resource } });
240+
expect(fetch).toHaveBeenCalledExactlyOnceWith("https://host.test/api/assets/image.png");
241+
const [item] = state.clipboard.mock.calls[0]![0];
242+
expect(await item.items["image/png"]).toEqual(png);
243+
});
244+
183245
it.each(["workspace-file", "media-file"] as const)(
184246
"disables denied %s byte actions and prevents imperative requests",
185247
async (_tag) => {

‎apps/web/src/components/media/MediaActions.tsx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ export function useMediaActions(source: MediaActionSource) {
8989
const connection = readPreparedConnection(environmentId);
9090
if (!connection) throw new Error("Reconnect to this environment and try again.");
9191
const result = await createAssetUrl({ environmentId, input: { resource } });
92-
if (result._tag === "Failure") throw squashAtomCommandFailure(result);
92+
if (result._tag === "Failure") {
93+
if (resource._tag === "github-media") return resource.url;
94+
throw squashAtomCommandFailure(result);
95+
}
9396
assertCanReadMedia();
9497
const url = resolveAssetUrl(connection.httpBaseUrl, result.value.relativeUrl);
9598
if (!url) throw new Error("The environment returned an invalid media URL.");
@@ -100,7 +103,7 @@ export function useMediaActions(source: MediaActionSource) {
100103
};
101104
const copyImage = async () => {
102105
assertCanReadMedia();
103-
const environmentId = source.environmentId;
106+
const environmentId = source.environmentId ?? sourceAsset?.environmentId;
104107
if (!navigator.clipboard?.write || typeof ClipboardItem === "undefined") {
105108
throw new Error(
106109
"Image copying is unavailable. Use a secure browser connection or save the image.",
@@ -112,7 +115,10 @@ export function useMediaActions(source: MediaActionSource) {
112115
"image/png": actionUrl().then((src) =>
113116
readMediaPng(
114117
src,
115-
!source.asset && environmentId && /^https?:\/\//i.test(src)
118+
(!sourceAsset ||
119+
(sourceAsset.resource._tag === "github-media" && src === sourceAsset.resource.url)) &&
120+
environmentId &&
121+
/^https?:\/\//i.test(src)
116122
? () =>
117123
actionUrl({
118124
environmentId,

0 commit comments

Comments
 (0)