@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 155 lines 4.36 KB
Raw
1 import { msg } from "gt-react-native";
2
3 import { m, type MessageResolver } from "../../i18n/messages";
4
5 export interface ZipEntry {
6 readonly path: string;
7 readonly name: string;
8 readonly isDirectory: boolean;
9 readonly sizeBytes: number;
10 readonly mimeType: string;
11 readonly bytes: Uint8Array<ArrayBuffer>;
12 }
13
14 export interface ZipTreeNode {
15 readonly path: string;
16 readonly label: string;
17 readonly entry: ZipEntry | null;
18 readonly children: ReadonlyArray<ZipTreeNode>;
19 readonly fileCount: number;
20 }
21
22 export interface ZipTreeRow {
23 readonly node: ZipTreeNode;
24 readonly depth: number;
25 }
26
27 interface MutableNode {
28 path: string;
29 name: string;
30 entry: ZipEntry | null;
31 children: Map<string, MutableNode>;
32 }
33
34 const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
35
36 function freeze(node: MutableNode): ZipTreeNode {
37 let current = node;
38 let label = node.name;
39 while (current.path !== "" && current.entry === null && current.children.size === 1) {
40 const [only] = current.children.values();
41 if (!only || only.entry !== null) break;
42 current = only;
43 label = `${label}/${only.name}`;
44 }
45 const children = [...current.children.values()]
46 .sort((left, right) =>
47 (left.entry === null) !== (right.entry === null)
48 ? left.entry === null
49 ? -1
50 : 1
51 : collator.compare(left.name, right.name),
52 )
53 .map(freeze);
54 return {
55 path: current.path,
56 label,
57 entry: current.entry,
58 children,
59 fileCount:
60 current.entry === null ? children.reduce((sum, child) => sum + child.fileCount, 0) : 1,
61 };
62 }
63
64 export function buildZipTree(entries: ReadonlyArray<ZipEntry>): ReadonlyArray<ZipTreeNode> {
65 const root: MutableNode = { path: "", name: "", entry: null, children: new Map() };
66 for (const entry of entries) {
67 const parts = entry.path.split("/").filter(Boolean);
68 let current = root;
69 parts.forEach((part, index) => {
70 const leaf = index === parts.length - 1 && !entry.isDirectory;
71 const key = leaf ? part : `${part}/`;
72 let child = current.children.get(key);
73 if (!child) {
74 child = {
75 path: parts.slice(0, index + 1).join("/"),
76 name: part,
77 entry: leaf ? entry : null,
78 children: new Map(),
79 };
80 current.children.set(key, child);
81 }
82 current = child;
83 });
84 }
85 return freeze(root).children;
86 }
87
88 function flatten(
89 output: ZipTreeRow[],
90 node: ZipTreeNode,
91 depth: number,
92 collapsed: ReadonlySet<string>,
93 query: string,
94 ): boolean {
95 if (node.entry) {
96 if (query && !node.entry.path.toLowerCase().includes(query)) return false;
97 output.push({ node, depth });
98 return true;
99 }
100 const children: ZipTreeRow[] = [];
101 let visible = !query;
102 if (query || !collapsed.has(node.path)) {
103 for (const child of node.children) {
104 if (flatten(children, child, depth + 1, collapsed, query)) visible = true;
105 }
106 }
107 if (!visible) return false;
108 output.push({ node, depth }, ...children);
109 return true;
110 }
111
112 export function flattenZipTree(
113 nodes: ReadonlyArray<ZipTreeNode>,
114 collapsed: ReadonlySet<string>,
115 query: string,
116 ): ReadonlyArray<ZipTreeRow> {
117 const output: ZipTreeRow[] = [];
118 const normalized = query.trim().toLowerCase();
119 for (const node of nodes) flatten(output, node, 0, collapsed, normalized);
120 return output;
121 }
122
123 export function zipFolderLabel(label: string) {
124 const parts = label.split("/");
125 const name = parts.at(-1) ?? label;
126 if (parts.length > 3) return { prefix: `${parts[0]}/…/${parts.at(-2)}/`, name };
127 return { prefix: parts.length > 1 ? `${parts.slice(0, -1).join("/")}/` : "", name };
128 }
129
130 export function zipDownloadTimeLeft(
131 input: {
132 readonly receivedBytes: number;
133 readonly totalBytes: number;
134 readonly elapsedMs: number;
135 },
136 resolve: MessageResolver = m,
137 ) {
138 if (input.receivedBytes <= 0 || input.totalBytes <= input.receivedBytes || input.elapsedMs < 1000)
139 return null;
140 const seconds = Math.ceil(
141 ((input.totalBytes - input.receivedBytes) * input.elapsedMs) / input.receivedBytes / 1000,
142 );
143 return seconds < 60
144 ? resolve(
145 msg(
146 "{seconds, plural, one {About {seconds} second left} other {About {seconds} seconds left}}",
147 { seconds },
148 ),
149 )
150 : resolve(
151 msg("{minutes, plural, other {About {minutes} minutes left}}", {
152 minutes: Math.ceil(seconds / 60),
153 }),
154 );
155 }