@devries-cameron20 / t3code Mirror · read-only
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, 04:45 UTC

main
ts 95 lines 3.22 KB
Raw
1 import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
2 import { fileBasename } from "@t3tools/shared/path";
3 import type { ThreadId } from "@t3tools/contracts";
4 import { resolveMarkdownLinkPresentation } from "../../lib/markdownLinks";
5 import type { MarkdownFileContextMenu } from "../../native/SelectableMarkdownText.types";
6 import { hostPreviewMimeTypeFromExtension } from "@t3tools/shared/filePreview";
7 import { msg } from "gt-react-native";
8
9 import { m, type MessageResolver } from "../../i18n/messages";
10
11 import {
12 isAbsolutePath,
13 resolveFileLinkPath,
14 resolveWorkspaceFilePath,
15 resolveWorkspaceRelativeFilePath,
16 } from "../files/filePath";
17
18 export interface FileChipTarget {
19 readonly fullPath?: string;
20 readonly relativePath?: string;
21 }
22
23 export function fileChipClipboardPath(
24 target: FileChipTarget,
25 action: "copy-full-path" | "copy-relative-path",
26 ): string | undefined {
27 const path = action === "copy-full-path" ? target.fullPath : target.relativePath;
28 return path === undefined ? undefined : formatFilePathForClipboard(path, target.fullPath);
29 }
30
31 export function resolveFileChipTarget(
32 href: string,
33 workspaceRoot: string | null | undefined,
34 ): FileChipTarget | null {
35 const presentation = resolveMarkdownLinkPresentation(href);
36 if (presentation.kind !== "file") return null;
37 const path = resolveFileLinkPath(workspaceRoot, presentation.path);
38 if (path === null) return null;
39 const relativePath = resolveWorkspaceRelativeFilePath(workspaceRoot, path);
40 const fullPath = isAbsolutePath(path)
41 ? path
42 : workspaceRoot && relativePath
43 ? resolveWorkspaceFilePath(workspaceRoot, relativePath)
44 : undefined;
45 if (!fullPath && !relativePath) return null;
46 return {
47 ...(fullPath ? { fullPath } : {}),
48 ...(relativePath ? { relativePath } : {}),
49 };
50 }
51
52 function fileChipMetadata(target: FileChipTarget) {
53 const path = target.fullPath ?? target.relativePath;
54 if (!path) return null;
55 const name = fileBasename(path);
56 const dot = name.lastIndexOf(".");
57 const mimeType = dot < 0 ? null : hostPreviewMimeTypeFromExtension(name.slice(dot));
58 return mimeType ? { path, name, mimeType } : null;
59 }
60
61 /** Use literal resolved paths so encoded filename characters are not decoded twice. */
62 export function fileChipShareSource(target: FileChipTarget, threadId: ThreadId) {
63 const metadata = fileChipMetadata(target);
64 return metadata
65 ? {
66 name: metadata.name,
67 mimeType: metadata.mimeType,
68 resource: { _tag: "media-file" as const, threadId, path: metadata.path },
69 }
70 : null;
71 }
72
73 export function fileChipMenu(
74 target: FileChipTarget,
75 resolve: MessageResolver = m,
76 ): MarkdownFileContextMenu {
77 return {
78 title: target.fullPath ?? target.relativePath ?? "",
79 actions: [
80 ...(target.fullPath ? [{ id: "copy-full-path", title: resolve(msg("Copy full path")) }] : []),
81 ...(target.relativePath
82 ? [{ id: "copy-relative-path", title: resolve(msg("Copy relative path")) }]
83 : []),
84 { id: "open-file", title: resolve(msg("Open in file viewer")) },
85 ...(fileChipMetadata(target)
86 ? [
87 {
88 id: "save",
89 title: resolve(msg("Save or share")),
90 },
91 ]
92 : []),
93 ],
94 };
95 }