@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 138 lines 3.73 KB
Raw
1 import * as Schema from "effect/Schema";
2
3 import { getLocalStorageItem, setLocalStorageItem } from "~/hooks/useLocalStorage";
4
5 export interface FileTreeExpansionModel {
6 getItem(path: string): unknown;
7 }
8
9 type DirectoryHandle = {
10 isDirectory(): boolean;
11 isExpanded(): boolean;
12 expand(): void;
13 collapse(): void;
14 };
15
16 function asDirectoryHandle(item: unknown): DirectoryHandle | null {
17 if (
18 typeof item !== "object" ||
19 item === null ||
20 !("isDirectory" in item) ||
21 typeof item.isDirectory !== "function" ||
22 !item.isDirectory() ||
23 !("isExpanded" in item) ||
24 typeof item.isExpanded !== "function" ||
25 !("expand" in item) ||
26 typeof item.expand !== "function" ||
27 !("collapse" in item) ||
28 typeof item.collapse !== "function"
29 ) {
30 return null;
31 }
32 return item as DirectoryHandle;
33 }
34
35 /**
36 * Calls `listener` once per task, however many events the model emitted in it. Expanding every
37 * folder emits one event per folder, and a listener that rescans the folders is quadratic otherwise.
38 */
39 export function subscribeOncePerTask(
40 model: { subscribe(listener: () => void): () => void },
41 listener: () => void,
42 ): () => void {
43 let queued = false;
44 let subscribed = true;
45 const unsubscribe = model.subscribe(() => {
46 if (queued) return;
47 queued = true;
48 queueMicrotask(() => {
49 queued = false;
50 if (subscribed) listener();
51 });
52 });
53 return () => {
54 subscribed = false;
55 unsubscribe();
56 };
57 }
58
59 export function areAllDirectoriesExpanded(
60 model: FileTreeExpansionModel,
61 directoryPaths: readonly string[],
62 ): boolean {
63 return (
64 directoryPaths.length > 0 &&
65 directoryPaths.every((path) => {
66 const item = asDirectoryHandle(model.getItem(path));
67 return item !== null && item.isExpanded();
68 })
69 );
70 }
71
72 export function setAllDirectoriesExpanded(
73 model: FileTreeExpansionModel,
74 directoryPaths: readonly string[],
75 expanded: boolean,
76 ): void {
77 for (const path of directoryPaths) {
78 const item = asDirectoryHandle(model.getItem(path));
79 if (item === null || item.isExpanded() === expanded) continue;
80 if (expanded) item.expand();
81 else item.collapse();
82 }
83 }
84
85 /** Calls made while `run` is running rerun it once it returns, instead of nesting. */
86 export function coalesceReentrantCalls(run: () => void): () => void {
87 let running = false;
88 let calledAgain = false;
89 return () => {
90 if (running) {
91 calledAgain = true;
92 return;
93 }
94 running = true;
95 try {
96 do {
97 calledAgain = false;
98 run();
99 } while (calledAgain);
100 } finally {
101 running = false;
102 }
103 };
104 }
105
106 const EXPANDED_DIRECTORIES_STORAGE_KEY = "t3code.fileTreeExpandedDirectories";
107 // Roots are kept most recent first and capped so worktrees that come and go do not grow storage.
108 const MAX_STORED_ROOTS = 20;
109 const StoredExpandedDirectories = Schema.Array(
110 Schema.Struct({ root: Schema.String, paths: Schema.Array(Schema.String) }),
111 );
112
113 function readStoredExpandedDirectories() {
114 try {
115 return getLocalStorageItem(EXPANDED_DIRECTORIES_STORAGE_KEY, StoredExpandedDirectories) ?? [];
116 } catch (error) {
117 console.error(error);
118 return [];
119 }
120 }
121
122 export function readExpandedDirectories(root: string): readonly string[] {
123 return readStoredExpandedDirectories().find((entry) => entry.root === root)?.paths ?? [];
124 }
125
126 export function writeExpandedDirectories(root: string, paths: readonly string[]): void {
127 const others = readStoredExpandedDirectories().filter((entry) => entry.root !== root);
128 const next = paths.length > 0 ? [{ root, paths }, ...others] : others;
129 try {
130 setLocalStorageItem(
131 EXPANDED_DIRECTORIES_STORAGE_KEY,
132 next.slice(0, MAX_STORED_ROOTS),
133 StoredExpandedDirectories,
134 );
135 } catch (error) {
136 console.error(error);
137 }
138 }