Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

filesystem.ts

126 lines3.8 KB

1import {
2 AuthFilesystemReadScope,
3 type FilesystemBrowseEntry,
4 WS_METHODS,
5 sessionGrantsScope,
6 type SessionGrantInput,
7} from "@t3tools/contracts";
8import { Atom } from "effect/reactivity";
9​
10import type {
11 EnvironmentConnectionPhase,
12 EnvironmentConnectionPresentation,
13} from "../connection/presentation.ts";
14import type { EnvironmentRegistry } from "../connection/registry.ts";
15import {
16 canNavigateUp,
17 getBrowseDirectoryPath,
18 getBrowseLeafPathSegment,
19 getBrowseParentPath,
20 hasTrailingPathSeparator,
21 isFilesystemBrowseQuery,
22} from "./projects.ts";
23import { createEnvironmentRpcQueryAtomFamily } from "./runtime.ts";
24​
25export function resolveFilesystemReadAccess(input: {
26 readonly isCatalogReady: boolean;
27 readonly connection: Pick<EnvironmentConnectionPresentation, "phase" | "error"> | null;
28 readonly session: SessionGrantInput | null;
29 readonly sessionError: string | null;
30}) {
31 if (input.sessionError !== null) {
32 return { canReadFiles: false, isPending: false, error: input.sessionError };
33 }
34 if (input.session === null) {
35 // Wait for the catalog before interpreting a missing presentation as offline.
36 // Once ready, an offline environment cannot finish its session check.
37 const isPending =
38 !input.isCatalogReady ||
39 input.connection?.phase === "connected" ||
40 input.connection?.phase === "connecting" ||
41 input.connection?.phase === "reconnecting";
42 return {
43 canReadFiles: false,
44 isPending,
45 error: isPending ? null : (input.connection?.error ?? "This environment is not connected."),
46 };
47 }
48 return {
49 canReadFiles: sessionGrantsScope(input.session, AuthFilesystemReadScope),
50 isPending: false,
51 error: null,
52 };
54​
55export function getFilesystemBrowsePath(query: string, platform = "", enabled = true) {
56 const isBrowsing = enabled && isFilesystemBrowseQuery(query, platform);
57 const directoryPath = isBrowsing ? getBrowseDirectoryPath(query) : "";
58 const filterQuery =
59 isBrowsing && !hasTrailingPathSeparator(query) ? getBrowseLeafPathSegment(query) : "";
60 const parentPath = isBrowsing ? getBrowseParentPath(directoryPath) : null;
61​
62 return {
63 isBrowsing,
64 directoryPath,
65 filterQuery,
66 parentPath,
67 canBrowseUp: isBrowsing && canNavigateUp(directoryPath),
68 };
70​
71export function filterFilesystemBrowseEntries(
72 entries: ReadonlyArray<FilesystemBrowseEntry>,
73 query: string,
74) {
75 const lowerQuery = query.toLowerCase();
76 const showHidden = query.startsWith(".");
77 const visibleEntries = entries.filter(
78 (entry) =>
79 entry.name.toLowerCase().startsWith(lowerQuery) &&
80 (showHidden || !entry.name.startsWith(".")),
81 );
82 const exactEntry =
83 query.length > 0 ? (visibleEntries.find((entry) => entry.name === query) ?? null) : null;
84​
85 return { visibleEntries, exactEntry };
87​
88export function createBrowseNavigationCoordinator() {
89 let generation = 0;
90​
91 return {
92 invalidate: () => {
93 generation += 1;
94 },
95 run: async (load: () => Promise<void>, commit: () => void) => {
96 const navigationGeneration = ++generation;
97 await load();
98 if (navigationGeneration !== generation) {
99 return false;
100 }
101 commit();
102 return true;
103 },
104 };
106​
107export function canPreloadBrowsePath(
108 connectionPhase: EnvironmentConnectionPhase | null | undefined,
109): boolean {
110 return connectionPhase === "connected";
112​
113export function createFilesystemEnvironmentAtoms<R, E>(
114 runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
115) {
116 return {
117 getMetadata: createEnvironmentRpcQueryAtomFamily(runtime, {
118 label: "environment-data:filesystem:get-metadata",
119 tag: WS_METHODS.filesystemGetMetadata,
120 }),
121 browse: createEnvironmentRpcQueryAtomFamily(runtime, {
122 label: "environment-data:filesystem:browse",
123 tag: WS_METHODS.filesystemBrowse,
124 }),
125 };