diff options
| author | diogo464 <[email protected]> | 2025-08-11 16:48:14 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-08-11 16:48:14 +0100 |
| commit | 741d42c7ab635180ab416dc749e1bcb774a22f5e (patch) | |
| tree | 221f5f991293edfb6abfe50b7fc692caf9bca3cc /frontend/lib | |
| parent | 83ff4ce9b3e93a361dd120ab2428e6d854af75bb (diff) | |
Simplify frontend by removing FileItem conversion layer
- Remove FileItem interface and 300+ lines of mock data
- Eliminate transformTreeNodes() conversion function
- Update component to use DriveTreeNode[] directly
- Rename /api/list to /api/tree with server-side tree building
- Add Drive_tree() function and DriveTreeNode types
- Significantly reduce code complexity and memory usage
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
Diffstat (limited to 'frontend/lib')
| -rw-r--r-- | frontend/lib/drive_server.ts | 51 | ||||
| -rw-r--r-- | frontend/lib/drive_shared.ts | 4 | ||||
| -rw-r--r-- | frontend/lib/drive_types.ts | 15 |
3 files changed, 69 insertions, 1 deletions
diff --git a/frontend/lib/drive_server.ts b/frontend/lib/drive_server.ts index 2f80002..7e2193c 100644 --- a/frontend/lib/drive_server.ts +++ b/frontend/lib/drive_server.ts | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | import { spawnSync } from 'child_process' | 1 | import { spawnSync } from 'child_process' |
| 2 | import { DriveLsEntry } from './drive_types' | 2 | import { DriveLsEntry, DriveTreeNode, DriveTreeResponse } from './drive_types' |
| 3 | import { Drive_split_path, Drive_basename } from './drive_shared' | ||
| 3 | 4 | ||
| 4 | /** | 5 | /** |
| 5 | * Server-only drive functions that use Node.js APIs | 6 | * Server-only drive functions that use Node.js APIs |
| @@ -94,4 +95,52 @@ export async function Drive_mkdir(path: string, email: string) { | |||
| 94 | if (result.status !== 0) { | 95 | if (result.status !== 0) { |
| 95 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`); | 96 | throw new Error(`fctdrive exited with code ${result.status}: ${result.stderr}`); |
| 96 | } | 97 | } |
| 98 | } | ||
| 99 | |||
| 100 | /// builds a filesystem tree from Drive_ls entries | ||
| 101 | export async function Drive_tree(): Promise<DriveTreeResponse> { | ||
| 102 | const entries = await Drive_ls('/', true); | ||
| 103 | |||
| 104 | const nodesMap = new Map<string, DriveTreeNode>(); | ||
| 105 | const rootNodes: DriveTreeNode[] = []; | ||
| 106 | |||
| 107 | // First pass: create all nodes | ||
| 108 | entries.forEach(entry => { | ||
| 109 | const node: DriveTreeNode = { | ||
| 110 | path: entry.path, | ||
| 111 | name: Drive_basename(entry.path), | ||
| 112 | type: entry.type, | ||
| 113 | lastmod: entry.lastmod, | ||
| 114 | blob: entry.blob, | ||
| 115 | size: entry.size, | ||
| 116 | author: entry.author, | ||
| 117 | children: entry.type === "dir" ? [] : undefined | ||
| 118 | }; | ||
| 119 | nodesMap.set(entry.path, node); | ||
| 120 | }); | ||
| 121 | |||
| 122 | // Second pass: build hierarchy | ||
| 123 | entries.forEach(entry => { | ||
| 124 | const pathParts = Drive_split_path(entry.path); | ||
| 125 | const node = nodesMap.get(entry.path)!; | ||
| 126 | |||
| 127 | if (pathParts.length === 1) { | ||
| 128 | // Root level item | ||
| 129 | rootNodes.push(node); | ||
| 130 | } else { | ||
| 131 | // Find parent path by reconstructing it | ||
| 132 | const parentParts = pathParts.slice(0, -1); | ||
| 133 | const parentPath = '/' + parentParts.join('/'); | ||
| 134 | const parent = nodesMap.get(parentPath); | ||
| 135 | |||
| 136 | if (parent && parent.children) { | ||
| 137 | parent.children.push(node); | ||
| 138 | } else { | ||
| 139 | // If parent not found, add to root | ||
| 140 | rootNodes.push(node); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | }); | ||
| 144 | |||
| 145 | return { root: rootNodes }; | ||
| 97 | } \ No newline at end of file | 146 | } \ No newline at end of file |
diff --git a/frontend/lib/drive_shared.ts b/frontend/lib/drive_shared.ts index 99cb5ed..c9d9ac9 100644 --- a/frontend/lib/drive_shared.ts +++ b/frontend/lib/drive_shared.ts | |||
| @@ -12,4 +12,8 @@ export function Drive_parent(path: string): string | null { | |||
| 12 | if (parts.length <= 1) | 12 | if (parts.length <= 1) |
| 13 | return null; | 13 | return null; |
| 14 | return parts[parts.length - 2]; | 14 | return parts[parts.length - 2]; |
| 15 | } | ||
| 16 | |||
| 17 | export function Drive_split_path(path: string): string[] { | ||
| 18 | return path.split('/').filter(part => part.length > 0); | ||
| 15 | } \ No newline at end of file | 19 | } \ No newline at end of file |
diff --git a/frontend/lib/drive_types.ts b/frontend/lib/drive_types.ts index 2bbedf8..ced8271 100644 --- a/frontend/lib/drive_types.ts +++ b/frontend/lib/drive_types.ts | |||
| @@ -6,3 +6,18 @@ export interface DriveLsEntry { | |||
| 6 | size: number | null | 6 | size: number | null |
| 7 | author: string | 7 | author: string |
| 8 | } | 8 | } |
| 9 | |||
| 10 | export interface DriveTreeNode { | ||
| 11 | path: string | ||
| 12 | name: string | ||
| 13 | type: "dir" | "file" | ||
| 14 | lastmod: number | ||
| 15 | blob: string | null | ||
| 16 | size: number | null | ||
| 17 | author: string | ||
| 18 | children?: DriveTreeNode[] // Only for directories | ||
| 19 | } | ||
| 20 | |||
| 21 | export interface DriveTreeResponse { | ||
| 22 | root: DriveTreeNode[] | ||
| 23 | } | ||
