fix(mobile): save linked media from chat (#10271)
This commit is contained in:
1 parent
b7465a3bc9
commit
a495385584
4 files changed
+163
-9
No files matched your search
@@ -178,6 +178,7 @@ import {
|
||||
resolveWorkspaceRelativeFilePath,
|
||||
} from "../files/filePath";
|
||||
import { fileChipMenu, resolveFileChipTarget, type FileChipAction } from "./fileChipMenu";
|
||||
import { useFileChipShare } from "./useFileChipShare";
|
||||
import {
|
||||
MarkdownImageAvailableWidthContext,
|
||||
ThreadMarkdownImage,
|
||||
@@ -1964,6 +1965,12 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
|
||||
const { copiedRowId, expandedWorkGroups, expandedWorkRows, expandedTurnIds } = interactionState;
|
||||
const [expandedFile, setExpandedFile] = useState<FilePreviewSource | null>(null);
|
||||
const [expandedVideo, setExpandedVideo] = useState<VideoPreviewSource | null>(null);
|
||||
const fileShareSourceIdentifier = useId();
|
||||
const shareFileChip = useFileChipShare(
|
||||
props.environmentId,
|
||||
props.threadId,
|
||||
fileShareSourceIdentifier,
|
||||
);
|
||||
useEffect(() => {
|
||||
setExpandedVideo(null);
|
||||
setExpandedFile(null);
|
||||
@@ -2116,10 +2123,13 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
|
||||
case "open-file":
|
||||
onMarkdownLinkPress(href);
|
||||
return;
|
||||
case "save":
|
||||
shareFileChip(target);
|
||||
return;
|
||||
}
|
||||
},
|
||||
}),
|
||||
[onMarkdownLinkPress, props.workspaceRoot],
|
||||
[onMarkdownLinkPress, props.workspaceRoot, shareFileChip],
|
||||
);
|
||||
const renderMarkdownImage = useCallback<MarkdownImageRenderer>(
|
||||
(image) => {
|
||||
@@ -2710,7 +2720,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<PresentationSource identifier={fileShareSourceIdentifier} style={{ flex: 1 }}>
|
||||
<View className="flex-1" onLayout={handleViewportLayout}>
|
||||
<View className="flex-1">
|
||||
<KeyboardAwareLegendList
|
||||
@@ -2869,10 +2879,9 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
|
||||
/>
|
||||
</View>
|
||||
) : null}
|
||||
<VideoPreviewModal source={expandedVideo} onRequestClose={() => setExpandedVideo(null)} />
|
||||
<FilePreviewModal source={expandedFile} onRequestClose={() => setExpandedFile(null)} />
|
||||
</View>
|
||||
|
||||
<VideoPreviewModal source={expandedVideo} onRequestClose={() => setExpandedVideo(null)} />
|
||||
<FilePreviewModal source={expandedFile} onRequestClose={() => setExpandedFile(null)} />
|
||||
</>
|
||||
</PresentationSource>
|
||||
);
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import { ThreadId } from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { fileChipMenu, resolveFileChipTarget } from "./fileChipMenu";
|
||||
import { fileChipMenu, fileChipShareSource, resolveFileChipTarget } from "./fileChipMenu";
|
||||
|
||||
describe("resolveFileChipTarget", () => {
|
||||
it("resolves a workspace-relative link to both paths", () => {
|
||||
@@ -42,3 +43,45 @@ describe("fileChipMenu", () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("file chip downloads", () => {
|
||||
const threadId = ThreadId.make("thread-1");
|
||||
|
||||
it.each([
|
||||
[
|
||||
"/tmp/maria-counter/maria-counter-final.mp4",
|
||||
"/tmp/maria-counter/maria-counter-final.mp4",
|
||||
"video/mp4",
|
||||
],
|
||||
["/tmp/take%2520%23one.mp4:12", "/tmp/take%20#one.mp4", "video/mp4"],
|
||||
["/tmp/report.pdf", "/tmp/report.pdf", "application/pdf"],
|
||||
["screens/image.PNG", "/repo/screens/image.PNG", "image/png"],
|
||||
])("offers a host download for %s", (href, path, mimeType) => {
|
||||
const target = resolveFileChipTarget(href, "/repo")!;
|
||||
expect(fileChipMenu(target).actions).toContainEqual({
|
||||
id: "save",
|
||||
title: "Save or share",
|
||||
});
|
||||
expect(fileChipShareSource(target, threadId)).toEqual({
|
||||
name: path.split("/").at(-1),
|
||||
mimeType,
|
||||
resource: { _tag: "media-file", threadId, path },
|
||||
});
|
||||
});
|
||||
|
||||
it("retains the thread context for a relative file without a known workspace root", () => {
|
||||
expect(
|
||||
fileChipShareSource(resolveFileChipTarget("clips/demo.mp4", null)!, threadId),
|
||||
).toMatchObject({
|
||||
resource: { _tag: "media-file", threadId, path: "clips/demo.mp4" },
|
||||
});
|
||||
});
|
||||
|
||||
it("does not offer downloads the host asset endpoint cannot serve", () => {
|
||||
for (const href of ["src/app.ts", "/tmp/archive.zip", "/tmp/clip.mp4.txt"]) {
|
||||
const target = resolveFileChipTarget(href, "/repo")!;
|
||||
expect(fileChipShareSource(target, threadId)).toBeNull();
|
||||
expect(fileChipMenu(target).actions.some(({ id }) => id === "save")).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,8 @@
|
||||
import { fileBasename } from "@t3tools/client-runtime/markdown-links";
|
||||
import type { ThreadId } from "@t3tools/contracts";
|
||||
import { resolveMarkdownLinkPresentation } from "@t3tools/mobile-markdown-text/links";
|
||||
import type { MarkdownFileContextMenu } from "@t3tools/mobile-markdown-text/types";
|
||||
import { hostPreviewMimeTypeFromExtension } from "@t3tools/shared/filePreview";
|
||||
|
||||
import {
|
||||
isAbsolutePath,
|
||||
@@ -7,7 +10,7 @@ import {
|
||||
resolveWorkspaceRelativeFilePath,
|
||||
} from "../files/filePath";
|
||||
|
||||
export type FileChipAction = "copy-full-path" | "copy-relative-path" | "open-file";
|
||||
export type FileChipAction = "copy-full-path" | "copy-relative-path" | "open-file" | "save";
|
||||
|
||||
export interface FileChipTarget {
|
||||
/** The host path, when the link is absolute or the workspace root is known. */
|
||||
@@ -36,7 +39,28 @@ export function resolveFileChipTarget(
|
||||
};
|
||||
}
|
||||
|
||||
/** The same actions the web file chip offers on right-click. Opening is what a tap does. */
|
||||
function fileChipMetadata(target: FileChipTarget) {
|
||||
const path = target.fullPath ?? target.relativePath;
|
||||
if (!path) return null;
|
||||
const name = fileBasename(path);
|
||||
const dot = name.lastIndexOf(".");
|
||||
const mimeType = dot < 0 ? null : hostPreviewMimeTypeFromExtension(name.slice(dot));
|
||||
return mimeType ? { path, name, mimeType } : null;
|
||||
}
|
||||
|
||||
/** Use literal resolved paths so encoded filename characters are not decoded twice. */
|
||||
export function fileChipShareSource(target: FileChipTarget, threadId: ThreadId) {
|
||||
const metadata = fileChipMetadata(target);
|
||||
return metadata
|
||||
? {
|
||||
name: metadata.name,
|
||||
mimeType: metadata.mimeType,
|
||||
resource: { _tag: "media-file" as const, threadId, path: metadata.path },
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
/** Saving is available for the media and documents the host asset endpoint can serve. */
|
||||
export function fileChipMenu(target: FileChipTarget): MarkdownFileContextMenu {
|
||||
return {
|
||||
title: target.fullPath ?? target.relativePath ?? "",
|
||||
@@ -44,6 +68,14 @@ export function fileChipMenu(target: FileChipTarget): MarkdownFileContextMenu {
|
||||
...(target.fullPath ? [{ id: "copy-full-path", title: "Copy full path" }] : []),
|
||||
...(target.relativePath ? [{ id: "copy-relative-path", title: "Copy relative path" }] : []),
|
||||
{ id: "open-file", title: "Open in file viewer" },
|
||||
...(fileChipMetadata(target)
|
||||
? [
|
||||
{
|
||||
id: "save",
|
||||
title: "Save or share",
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
|
||||
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
|
||||
import * as Option from "effect/Option";
|
||||
import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
|
||||
import { Alert } from "react-native";
|
||||
|
||||
import { downloadAndShareAttachment } from "../../lib/attachmentDownload";
|
||||
import { assetEnvironment } from "../../state/assets";
|
||||
import { usePreparedConnection } from "../../state/session";
|
||||
import { useAtomQueryRunner } from "../../state/use-atom-query-runner";
|
||||
import { fileChipShareSource, type FileChipTarget } from "./fileChipMenu";
|
||||
|
||||
/** Fetches host files through the selected environment before opening the native save/share sheet. */
|
||||
export function useFileChipShare(
|
||||
environmentId: EnvironmentId,
|
||||
threadId: ThreadId,
|
||||
sourceIdentifier: string,
|
||||
) {
|
||||
const connection = usePreparedConnection(environmentId);
|
||||
const httpBaseUrl = Option.isSome(connection) ? connection.value.httpBaseUrl : null;
|
||||
const createUrl = useAtomQueryRunner(assetEnvironment.createUrl, {
|
||||
refresh: true,
|
||||
reportFailure: false,
|
||||
});
|
||||
const connectionRef = useRef(httpBaseUrl);
|
||||
useLayoutEffect(() => {
|
||||
connectionRef.current = httpBaseUrl;
|
||||
}, [httpBaseUrl]);
|
||||
const requestRef = useRef<AbortController | null>(null);
|
||||
useEffect(() => () => requestRef.current?.abort(), []);
|
||||
|
||||
const share = useCallback(
|
||||
(target: FileChipTarget) => {
|
||||
const source = fileChipShareSource(target, threadId);
|
||||
if (!source || requestRef.current) return;
|
||||
const request = new AbortController();
|
||||
requestRef.current = request;
|
||||
const httpBaseUrl = connectionRef.current;
|
||||
void (async () => {
|
||||
if (httpBaseUrl === null) throw new Error("Reconnect to the environment and try again.");
|
||||
const result = await createUrl({ environmentId, input: { resource: source.resource } });
|
||||
if (request.signal.aborted) return;
|
||||
const url =
|
||||
result._tag === "Success" ? resolveAssetUrl(httpBaseUrl, result.value.relativeUrl) : null;
|
||||
if (url === null) throw new Error("The file could not be loaded. Reconnect and try again.");
|
||||
await downloadAndShareAttachment({
|
||||
url,
|
||||
attachment: source,
|
||||
signal: request.signal,
|
||||
sourceIdentifier,
|
||||
});
|
||||
})()
|
||||
.catch((error: unknown) => {
|
||||
if (!request.signal.aborted) {
|
||||
Alert.alert(
|
||||
"Could not share file",
|
||||
error instanceof Error ? error.message : "Try again.",
|
||||
);
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
if (requestRef.current === request) {
|
||||
requestRef.current = null;
|
||||
}
|
||||
});
|
||||
},
|
||||
[createUrl, environmentId, sourceIdentifier, threadId],
|
||||
);
|
||||
return share;
|
||||
}
|
||||
Reference in new issue
Block a user