Open History to see the last commit
1import type { AssetResource, ProjectReadFileError, ScopedThreadRef } from "@t3tools/contracts";
2import { mediaFileReference } from "@t3tools/client-runtime/media-reference";
3import { isHostPath, workspaceRelativeFilePath } from "@t3tools/shared/path";
4
5export function resolveFilePreviewPath(path: string | null, cwd: string): string | null {
6 if (path === null) return null;
7 return path === "." || workspaceRelativeFilePath(path, cwd) === "." ? null : path;
8}
9
10export const isMarkdownPreviewFile = (path: string): boolean => /\.(?:md|mdx)$/i.test(path);
11
12export function filePreviewReadErrorMessage(error: ProjectReadFileError): string {
13 switch (error.failure) {
14 case "path_not_file":
15 return "The path is a directory or special file, not a regular file.";
16 case "binary_file":
17 return "The file is binary and cannot be displayed as text.";
18 case "workspace_path_outside_root":
19 return "The requested path is outside the workspace.";
20 case "resolved_path_outside_root":
21 return "The path resolves to a location outside the workspace.";
22 case "operation_failed":
23 return error.operation === "realpath-workspace-root"
24 ? "The workspace folder could not be accessed."
25 : "The file could not be accessed or read. It may be missing or inaccessible.";
26 default:
27 return error.message;
28 }
29}
30
31export function shouldShowFileExplorer(input: {
32 readonly relativePath: string | null;
33 readonly explorerOpen: boolean;
34 readonly attachmentOpen: boolean;
35}): boolean {
36 if (input.attachmentOpen || (input.relativePath && isHostPath(input.relativePath))) {
37 return false;
38 }
39 return input.explorerOpen || input.relativePath === null;
40}
41
42export function setMarkdownTaskChecked(
43 markdown: string,
44 markerOffset: number,
45 checked: boolean,
46): string {
47 if (
48 markerOffset < 0 ||
49 markdown[markerOffset] !== "[" ||
50 !/[ xX]/.test(markdown[markerOffset + 1] ?? "") ||
51 markdown[markerOffset + 2] !== "]"
52 ) {
53 return markdown;
54 }
55
56 return `${markdown.slice(0, markerOffset + 1)}${checked ? "x" : " "}${markdown.slice(markerOffset + 2)}`;
57}
58
59/**
60 * The server only signs `workspace-file` assets below the workspace root, so a
61 * file opened from outside it, such as an absolute link in the thread, goes
62 * through the host `media-file` resource the direct media viewer uses.
63 */
64export function workspaceAssetResource(input: {
65 readonly kind: "workspace-file" | "media-file";
66 readonly threadRef: ScopedThreadRef;
67 readonly draft: boolean;
68 readonly workspaceRoot: string;
69 readonly absolutePath: string;
70}): AssetResource {
71 if (input.draft) {
72 return { _tag: "draft-workspace-file", cwd: input.workspaceRoot, path: input.absolutePath };
73 }
74 const insideWorkspace =
75 mediaFileReference(input.absolutePath, input.workspaceRoot).relativePath !== undefined;
76 return {
77 _tag: insideWorkspace ? input.kind : "media-file",
78 threadId: input.threadRef.threadId,
79 path: input.absolutePath,
80 };
81}