Repository navigation
Expand file tree
/
Copy pathuseFileMetadata.ts
More file actions
55 lines (52 loc) · 1.88 KB
/
Copy pathuseFileMetadata.ts
File metadata and controls
55 lines (52 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {
AuthFilesystemReadScope,
type EnvironmentId,
type ScopedThreadRef,
} from "@t3tools/contracts";
import { resolvePathLinkTarget, splitFilePathPosition } from "@t3tools/shared/fileLinks";
import {
isAbsolutePath,
isNetworkPath,
normalizeProjectPathForComparison,
} from "@t3tools/shared/path";
import { useContext } from "react";
import { useEnvironmentScope } from "../state/session";
import { projectEnvironment } from "../state/projects";
import { useEnvironmentQuery } from "../state/query";
import { FileMetadataThreadContext } from "./FileMetadataThreadProvider";
export function getFileMetadataQueryAtom(
environmentId: EnvironmentId,
path: string,
cwd?: string,
threadRef?: ScopedThreadRef | null,
) {
const hostPath = splitFilePathPosition(path).path;
const targetPath = normalizeProjectPathForComparison(
hostPath.startsWith("~/") || !cwd ? hostPath : resolvePathLinkTarget(hostPath, cwd),
);
const absolute = isAbsolutePath(targetPath) || targetPath.startsWith("~/");
return (
// Servers stat what they are asked about, and a network path leaks the
// Windows user's NTLM credentials to its host before any chip is clicked.
absolute && !isNetworkPath(targetPath) && targetPath.length <= 512
? projectEnvironment.fileMetadata({
environmentId,
input: {
path: targetPath,
...(threadRef?.environmentId === environmentId ? { threadId: threadRef.threadId } : {}),
},
})
: null
);
}
export function useFileMetadata(
environmentId: EnvironmentId | null | undefined,
path: string,
cwd?: string,
) {
const canRead = useEnvironmentScope(environmentId ?? null, AuthFilesystemReadScope);
const threadRef = useContext(FileMetadataThreadContext);
return useEnvironmentQuery(
environmentId && canRead ? getFileMetadataQueryAtom(environmentId, path, cwd, threadRef) : null,
).data;
}