fix(web): share artifact files instead of URLs

This commit is contained in:
2026-10-21 12:36:06 +01:00
parent 93be42b06e
commit 870c70aefa
10 changed files with 204 additions and 16 deletions
+52
View File
@@ -0,0 +1,52 @@
// File sharing never falls back to sending a private artifact URL. Keep prepared
// files local to the owning button so a slow fetch can be shared on a fresh tap.
const prepared = new WeakMap();
const pending = new WeakSet();
export function canShareFiles(environment = globalThis) {
return typeof environment.navigator?.share === "function" &&
typeof environment.navigator?.canShare === "function" &&
typeof environment.File === "function";
}
export async function shareArtifactFile(resolved, artifact, owner, environment = globalThis) {
if (!resolved || !canShareFiles(environment)) return { state: "error", message: "File sharing is not supported in this browser. Use Open in browser to save the file." };
if (pending.has(owner)) return { state: "busy", message: "Preparing file…" };
pending.add(owner);
try {
let entry = prepared.get(owner);
if (!entry || entry.url !== resolved) {
const response = await environment.fetch(resolved, { credentials: "same-origin", redirect: "error" });
if (!response.ok) throw new Error(`Could not fetch the artifact (HTTP ${response.status}).`);
const blob = await response.blob();
const name = String(artifact?.name || new URL(resolved).pathname.split("/").pop() || "artifact")
.split(/[\\/]/).pop().replace(/[\x00-\x1f\x7f]/g, "_") || "artifact";
const file = new environment.File([blob], name, { type: blob.type || artifact?.content_type || "application/octet-stream" });
entry = { url: resolved, file };
if (!environment.navigator.canShare({ files: [file] })) {
return { state: "error", message: "This browser cannot share this file type. Use Open in browser to save the file." };
}
prepared.set(owner, entry);
}
if (owner.isConnected === false) {
prepared.delete(owner);
return { state: "canceled", message: "Sharing canceled." };
}
// Fetching may outlast the browser's transient activation. Do not attempt a
// doomed share or substitute a URL: the next tap uses the prepared file.
if (environment.navigator.userActivation?.isActive === false) {
return { state: "ready", message: "File ready. Tap Share again to choose where to send it." };
}
await environment.navigator.share({ files: [entry.file] });
prepared.delete(owner);
return { state: "shared", message: "File shared." };
} catch (error) {
if (error?.name === "AbortError") return { state: "canceled", message: "Sharing canceled." };
if (error?.name === "NotAllowedError" && prepared.has(owner)) {
return { state: "ready", message: "Sharing was blocked. Tap Share again, or use Open in browser to save the file." };
}
return { state: "error", message: "Could not share the file. Use Open in browser to save it, or try Share again." };
} finally {
pending.delete(owner);
}
}
+17 -7
View File
@@ -3,6 +3,7 @@
// textContent, never innerHTML.
import { artifactBrowserLinkProps } from "./artifactNavigation.js";
import { canShareFiles, shareArtifactFile } from "./artifactSharing.js";
export { artifactBrowserLinkProps } from "./artifactNavigation.js";
export function h(tag, props = {}, ...children) {
@@ -49,9 +50,9 @@ export function canCopyLink() {
return typeof navigator !== "undefined" && Boolean(navigator.clipboard?.writeText);
}
/** Whether the Web Share API can be used for a share control. */
/** Whether the browser exposes file sharing (not just URL sharing). */
export function canShare() {
return typeof navigator !== "undefined" && typeof navigator.share === "function";
return canShareFiles();
}
export async function copyText(text) {
@@ -72,12 +73,21 @@ export async function copyArtifactLink(resolved) {
}
}
export async function shareArtifact(resolved) {
if (!resolved || !canShare()) return;
export async function shareArtifact(resolved, artifact, trigger) {
if (!trigger || trigger.getAttribute("aria-busy") === "true") return;
const host = trigger.closest(".artifact-tile-wrap, .artifact-list-row, .viewer-panel");
let status = host?.querySelector(".artifact-share-status");
if (!status && host) {
status = h("div", { class: "artifact-share-status muted", role: "status", "aria-live": "polite" });
host.append(status);
}
if (status) status.textContent = "Preparing file…";
trigger.setAttribute("aria-busy", "true");
try {
await navigator.share({ url: resolved });
} catch {
// The share chooser may be dismissed.
const result = await shareArtifactFile(resolved, artifact, trigger);
if (status && trigger.isConnected) status.textContent = result.message;
} finally {
trigger.removeAttribute("aria-busy");
}
}
+2 -1
View File
@@ -773,7 +773,8 @@ function openArtifactViewer(viewable, startIndex, trigger = null) {
"data-viewer-focus": "share",
text: "Share",
disabled: !canShare(),
onclick: () => shareArtifact(resolved),
title: canShare() ? "Share file" : "File sharing is not supported in this browser",
onclick: (event) => shareArtifact(resolved, artifact, event.currentTarget),
}),
);
}
+5 -4
View File
@@ -1240,7 +1240,7 @@ function renderArtifactListRow(artifact, viewable, viewerIndexOf) {
open.addEventListener("click", () => previewImage
? openArtifactViewer(viewable, at, open) : openArtifactInBrowser(resolved));
wrap.append(open, renderTileActions(resolved, media, opensViewer && at != null
? (trigger) => openArtifactViewer(viewable, at, trigger) : null, name, previewImage));
? (trigger) => openArtifactViewer(viewable, at, trigger) : null, name, previewImage, artifact));
return wrap;
}
@@ -1341,12 +1341,12 @@ function renderArtifactTile(artifact, viewable, viewerIndexOf) {
);
tile.addEventListener("click", openTab);
}
wrap.append(tile, renderTileActions(resolved, media, opensViewer ? openViewer : null, name, kind === ArtifactKind.Image));
wrap.append(tile, renderTileActions(resolved, media, opensViewer ? openViewer : null, name, kind === ArtifactKind.Image, artifact));
return wrap;
}
/** The compact per-tile actions: icon-only link/share, shown on hover or focus. */
function renderTileActions(resolved, media, preview = null, name = "artifact", previewImage = false) {
function renderTileActions(resolved, media, preview = null, name = "artifact", previewImage = false, artifact = null) {
const actions = h("div", { class: "artifact-actions" });
const stop = (fn) => (event) => {
event.stopPropagation();
@@ -1363,8 +1363,9 @@ function renderTileActions(resolved, media, preview = null, name = "artifact", p
);
}
actions.append(
artifactAction("share", "Share", !resolved || !canShare(), stop(() => shareArtifact(resolved))),
artifactAction("share", "Share", !resolved || !canShare(), stop((trigger) => shareArtifact(resolved, artifact, trigger))),
);
if (!canShare()) actions.lastElementChild.title = "File sharing is not supported in this browser";
return actions;
}