Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 93 additions & 8 deletions apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import * as Schema from "effect/Schema";
import { videoMimeType } from "@t3tools/shared/video";
import {
filePreviewKind,
hostPreviewMimeTypeFromExtension,
isWorkspaceBrowserPreviewPath,
isWorkspaceImagePreviewPath,
mediaMimeTypeFromExtension,
Expand Down Expand Up @@ -123,11 +124,16 @@ function FileHeader(props: {
readonly fileInspectorSupported: boolean;
readonly onBack: () => void;
readonly onReturnToThread: () => void;
readonly onDownload?: (() => void) | undefined;
readonly downloadLabel?: string;
readonly downloading?: boolean;
readonly downloadDisabled?: boolean;
readonly actions: ReadonlyArray<{
readonly id: string;
readonly title: string;
readonly icon: string;
readonly inline: boolean;
readonly disabled?: boolean;
readonly onPress: () => unknown;
}>;
}) {
Expand All @@ -150,20 +156,33 @@ function FileHeader(props: {
}
: undefined
}
actions={
props.fileInspectorSupported
actions={[
...(props.onDownload
? [
{
accessibilityLabel: props.downloading
? gt("Opening share sheet…")
: (props.downloadLabel ?? gt("Save or share")),
icon: "tray.and.arrow.down" as const,
onPress: props.onDownload,
disabled: props.downloading || props.downloadDisabled,
loading: props.downloading,
},
]
: []),
...(props.fileInspectorSupported
? [
{
accessibilityLabel: panes.auxiliaryPaneVisible
? gt("Hide file navigator")
: gt("Show file navigator"),
icon: "sidebar.right",
icon: "sidebar.right" as const,
selected: panes.auxiliaryPaneVisible,
onPress: toggleAuxiliaryPane,
},
]
: undefined
}
: []),
]}
menus={[
{
title: gt("File actions"),
Expand All @@ -178,11 +197,14 @@ function FileHeader(props: {
items: modes.map((action) => ({
...action,
selected: action.id === props.activeMode,
disabled: action.disabled,
})),
},
]
: []),
...props.actions.filter(({ inline }) => !inline),
...props.actions
.filter(({ inline }) => !inline)
.map((action) => ({ ...action, disabled: action.disabled })),
],
},
]}
Expand Down Expand Up @@ -215,6 +237,15 @@ function normalizeRouteLine(value: string | null): number | null {
return Number.isInteger(parsed) && parsed > 0 ? parsed : null;
}

function hostDownloadMimeType(path: string): string {
const name = basename(path);
const dot = name.lastIndexOf(".");
return (
(dot > 0 ? hostPreviewMimeTypeFromExtension(name.slice(dot)) : null) ??
"application/octet-stream"
);
}

function defaultViewMode(path: string | null): FileViewMode {
return path !== null &&
(isWorkspaceBrowserPreviewPath(path) ||
Expand Down Expand Up @@ -829,7 +860,41 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
resource: assetPreview.resource,
}
: undefined;
const mediaActions = useMediaActions(mediaSource);
// Preview URLs refuse spreadsheets, source, and other non-media files. A download asks for the
// file as an attachment, which the server will serve for any type.
const downloadSource: MediaActionsSource | undefined =
mediaSource !== undefined ||
environmentId === null ||
relativePath === null ||
directoryPath !== null
? undefined
: threadId !== null && targetPath !== null
? {
name: basename(relativePath),
mimeType: hostDownloadMimeType(relativePath),
environmentId,
threadId,
resource: {
_tag: "media-file",
threadId,
path: targetPath,
disposition: "attachment",
},
}
: cwd !== null
? {
name: basename(relativePath),
mimeType: hostDownloadMimeType(relativePath),
environmentId,
resource: {
_tag: "draft-workspace-file",
cwd,
path: relativePath,
disposition: "attachment",
},
}
: undefined;
const mediaActions = useMediaActions(mediaSource ?? downloadSource);
const videoSource: MediaVideoPreviewSource | null =
environmentId !== null &&
relativePath !== null &&
Expand Down Expand Up @@ -966,11 +1031,22 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
id: action.id,
title: action.title,
icon:
action.id === "save" ? ("square.and.arrow.up" as const) : ("doc.on.doc" as const),
action.id === "save" ? ("tray.and.arrow.down" as const) : ("doc.on.doc" as const),
inline: false,
disabled: action.disabled,
onPress: action.run,
}))
: [
...mediaActions.actions
.filter(({ id }) => id === "save")
.map((action) => ({
id: action.id,
title: action.title,
icon: "tray.and.arrow.down" as const,
inline: false as const,
disabled: action.disabled,
onPress: action.run,
})),
{
id: "copy-path",
title: gt("Copy path"),
Expand Down Expand Up @@ -1138,6 +1214,7 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
}

const headerSubtitle = fileHeaderSubtitle(projectName, relativePath);
const saveFile = mediaActions.actions.find((action) => action.id === "save");

return withInspector(
<View className="flex-1 bg-sheet">
Expand All @@ -1148,6 +1225,14 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
fileInspectorSupported={fileInspector.supported}
onBack={handleBack}
onReturnToThread={handleReturnToThread}
{...(saveFile
? {
onDownload: saveFile.run,
downloadLabel: saveFile.title,
downloading: mediaActions.sharing,
downloadDisabled: saveFile.disabled === true && !mediaActions.sharing,
}
: {})}
actions={fileMenuActions}
/>
<ScreenContent>
Expand Down
25 changes: 11 additions & 14 deletions apps/mobile/src/features/threads/ThreadFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,6 @@ function MessageAttachmentFile(props: {
readonly attachment: ChatFileAttachment;
readonly onPressPreview: (source: FilePreviewSource) => void;
readonly onPlaybackStart: () => void;
readonly standaloneFile?: boolean;
readonly available?: boolean;
}) {
const gt = useGT();
Expand Down Expand Up @@ -837,6 +836,7 @@ function MessageAttachmentFile(props: {
sizeBytes={attachment.sizeBytes}
onOpen={openAttachment}
onPlaybackStart={props.onPlaybackStart}
onDownload={() => shareFile(sourceIdentifier)}
onLongPress={() => shareFile(sourceIdentifier)}
/>
</PresentationSource>
Expand Down Expand Up @@ -894,18 +894,16 @@ function MessageAttachmentFile(props: {
type="monochrome"
/>
</Pressable>
{props.standaloneFile ? (
<Pressable
accessibilityRole="button"
accessibilityLabel={gt("Download {name}", { name: attachment.name })}
accessibilityState={{ disabled, busy: opening }}
disabled={disabled}
className="mr-1 min-h-11 w-11 items-center justify-center rounded-lg active:bg-subtle disabled:opacity-40"
onPress={() => shareFile(sourceIdentifier)}
>
<SymbolView name="arrow.down" size={16} tintColorClassName="accent-foreground-muted" />
</Pressable>
) : null}
<Pressable
accessibilityRole="button"
accessibilityLabel={gt("Download {name}", { name: attachment.name })}
accessibilityState={{ disabled, busy: opening }}
disabled={disabled}
className="mr-1 min-h-11 w-11 items-center justify-center rounded-lg active:bg-subtle disabled:opacity-40"
onPress={() => shareFile(sourceIdentifier)}
>
<SymbolView name="arrow.down" size={16} tintColorClassName="accent-foreground-muted" />
</Pressable>
</View>
</PresentationSource>
);
Expand Down Expand Up @@ -2449,7 +2447,6 @@ function renderFeedEntry(
}
onPressPreview={props.onPressPreview}
onPlaybackStart={props.onPlaybackStart}
standaloneFile
/>
);
})}
Expand Down
57 changes: 35 additions & 22 deletions apps/mobile/src/features/threads/ThreadInlineFilePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,47 @@ type PreviewProps = {
readonly onOpen: () => void;
readonly onPlaybackStart?: () => void;
readonly onLongPress?: () => void;
readonly onDownload?: () => void;
};

function FileCardHeader(props: PreviewProps) {
const gt = useGT();
const messages = useMessages();
return (
<Pressable
accessibilityRole="button"
accessibilityLabel={gt("Open {name}", { name: props.source.name })}
onPress={props.onOpen}
onLongPress={props.onLongPress}
className="h-[72px] flex-row items-center gap-3 p-3 active:bg-subtle"
>
<View className="h-12 w-10 shrink-0 items-center justify-center rounded-lg bg-subtle">
<PierreEntryIcon path={props.source.name ?? "file"} kind="file" size={26} />
</View>
<View className="min-w-0 flex-1 gap-1">
<Text className="text-sm font-t3-medium text-foreground" numberOfLines={1}>
{props.source.name}
</Text>
<Text className="text-xs text-foreground-muted" numberOfLines={1}>
{props.sizeBytes !== undefined
? gt("Audio · {size}", { size: formatAttachmentSize(props.sizeBytes, messages) })
: gt("Audio")}
</Text>
</View>
<SymbolView name="chevron.right" size={12} tintColorClassName="accent-foreground-muted" />
</Pressable>
<View className="h-[72px] flex-row items-center">
<Pressable
accessibilityRole="button"
accessibilityLabel={gt("Open {name}", { name: props.source.name })}
onPress={props.onOpen}
onLongPress={props.onLongPress}
className="h-full min-w-0 flex-1 flex-row items-center gap-3 p-3 active:bg-subtle"
>
<View className="h-12 w-10 shrink-0 items-center justify-center rounded-lg bg-subtle">
<PierreEntryIcon path={props.source.name ?? "file"} kind="file" size={26} />
</View>
<View className="min-w-0 flex-1 gap-1">
<Text className="text-sm font-t3-medium text-foreground" numberOfLines={1}>
{props.source.name}
</Text>
<Text className="text-xs text-foreground-muted" numberOfLines={1}>
{props.sizeBytes !== undefined
? gt("Audio · {size}", { size: formatAttachmentSize(props.sizeBytes, messages) })
: gt("Audio")}
</Text>
</View>
<SymbolView name="chevron.right" size={12} tintColorClassName="accent-foreground-muted" />
</Pressable>
{props.onDownload ? (
<Pressable
accessibilityRole="button"
accessibilityLabel={gt("Download {name}", { name: props.source.name })}
onPress={props.onDownload}
className="mr-1 min-h-11 w-11 items-center justify-center rounded-lg active:bg-subtle"
>
<SymbolView name="arrow.down" size={16} tintColorClassName="accent-foreground-muted" />
</Pressable>
) : null}
</View>
);
}

Expand Down
24 changes: 17 additions & 7 deletions apps/mobile/src/features/threads/fileChipMenu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,25 @@ describe("file chip downloads", () => {
expect(fileChipShareSource(target, threadId)).toEqual({
name: path.split("/").at(-1),
mimeType,
resource: { _tag: "media-file", threadId, path },
resource: { _tag: "media-file", threadId, path, disposition: "attachment" },
});
});

it("does not offer downloads the host asset endpoint cannot serve", () => {
for (const href of ["src/app.ts", "/tmp/archive.7z", "/tmp/clip.mp4.txt"]) {
const target = resolveFileChipTarget(href, "/repo")!;
expect(fileChipShareSource(target, threadId)).toBeNull();
expect(fileChipMenu(target).actions.some(({ id }) => id === "save")).toBe(false);
}
it.each([
["src/app.ts", "/repo/src/app.ts", "app.ts"],
["/tmp/archive.7z", "/tmp/archive.7z", "archive.7z"],
["/tmp/clip.mp4.txt", "/tmp/clip.mp4.txt", "clip.mp4.txt"],
["exports/close-list.csv", "/repo/exports/close-list.csv", "close-list.csv"],
])("offers a download for %s, which the preview endpoint will not inline", (href, path, name) => {
const target = resolveFileChipTarget(href, "/repo")!;
expect(fileChipMenu(target).actions).toContainEqual({
id: "save",
title: "Save or share",
});
expect(fileChipShareSource(target, threadId)).toEqual({
name,
mimeType: "application/octet-stream",
resource: { _tag: "media-file", threadId, path, disposition: "attachment" },
});
});
});
33 changes: 17 additions & 16 deletions apps/mobile/src/features/threads/fileChipMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,31 @@ export function resolveFileChipTarget(
};
}

function fileChipMetadata(target: FileChipTarget) {
function fileChipFile(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;
// An attachment disposition serves every file. Preview types keep their own media type.
const mimeType =
(dot < 0 ? null : hostPreviewMimeTypeFromExtension(name.slice(dot))) ??
"application/octet-stream";
return { path, name, mimeType };
}

/** 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
const file = fileChipFile(target);
return file
? {
name: metadata.name,
mimeType: metadata.mimeType,
resource: { _tag: "media-file" as const, threadId, path: metadata.path },
name: file.name,
mimeType: file.mimeType,
resource: {
_tag: "media-file" as const,
threadId,
path: file.path,
disposition: "attachment" as const,
},
}
: null;
}
Expand All @@ -82,14 +90,7 @@ export function fileChipMenu(
? [{ id: "copy-relative-path", title: resolve(msg("Copy relative path")) }]
: []),
{ id: "open-file", title: resolve(msg("Open in file viewer")) },
...(fileChipMetadata(target)
? [
{
id: "save",
title: resolve(msg("Save or share")),
},
]
: []),
{ id: "save", title: resolve(msg("Save or share")) },
],
};
}
Loading
Loading