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

filesystem.ts

83 lines2.4 KB

1import { type FilesystemBrowseEntry, WS_METHODS } from "@t3tools/contracts";
2import { Atom } from "effect/unstable/reactivity";
3​
4import type { EnvironmentConnectionPhase } from "../connection/presentation.ts";
5import type { EnvironmentRegistry } from "../connection/registry.ts";
6import {
7 canNavigateUp,
8 getBrowseDirectoryPath,
9 getBrowseLeafPathSegment,
10 getBrowseParentPath,
11 hasTrailingPathSeparator,
12 isFilesystemBrowseQuery,
13} from "./projects.ts";
14import { createEnvironmentRpcQueryAtomFamily } from "./runtime.ts";
15​
16export function getFilesystemBrowsePath(query: string, platform = "", enabled = true) {
17 const isBrowsing = enabled && isFilesystemBrowseQuery(query, platform);
18 const directoryPath = isBrowsing ? getBrowseDirectoryPath(query) : "";
19 const filterQuery =
20 isBrowsing && !hasTrailingPathSeparator(query) ? getBrowseLeafPathSegment(query) : "";
21 const parentPath = isBrowsing ? getBrowseParentPath(directoryPath) : null;
22​
23 return {
24 isBrowsing,
25 directoryPath,
26 filterQuery,
27 parentPath,
28 canBrowseUp: isBrowsing && canNavigateUp(directoryPath),
29 };
31​
32export function filterFilesystemBrowseEntries(
33 entries: ReadonlyArray<FilesystemBrowseEntry>,
34 query: string,
35) {
36 const lowerQuery = query.toLowerCase();
37 const showHidden = query.startsWith(".");
38 const visibleEntries = entries.filter(
39 (entry) =>
40 entry.name.toLowerCase().startsWith(lowerQuery) &&
41 (showHidden || !entry.name.startsWith(".")),
42 );
43 const exactEntry =
44 query.length > 0 ? (visibleEntries.find((entry) => entry.name === query) ?? null) : null;
45​
46 return { visibleEntries, exactEntry };
48​
49export function createBrowseNavigationCoordinator() {
50 let generation = 0;
51​
52 return {
53 invalidate: () => {
54 generation += 1;
55 },
56 run: async (load: () => Promise<void>, commit: () => void) => {
57 const navigationGeneration = ++generation;
58 await load();
59 if (navigationGeneration !== generation) {
60 return false;
61 }
62 commit();
63 return true;
64 },
65 };
67​
68export function canPreloadBrowsePath(
69 connectionPhase: EnvironmentConnectionPhase | null | undefined,
70): boolean {
71 return connectionPhase === "connected";
73​
74export function createFilesystemEnvironmentAtoms<R, E>(
75 runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
76) {
77 return {
78 browse: createEnvironmentRpcQueryAtomFamily(runtime, {
79 label: "environment-data:filesystem:browse",
80 tag: WS_METHODS.filesystemBrowse,
81 }),
82 };