diff options
| author | diogo464 <[email protected]> | 2025-08-11 16:53:28 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-08-11 16:53:28 +0100 |
| commit | a361e446d0a168d00ab908b22902dccf47ed36a1 (patch) | |
| tree | 26410afd87d5f45174d14fe37181f6820695e6e7 | |
| parent | 951e817ca45c075e17bd0870539f659cc1627f0b (diff) | |
Calculate directory sizes as sum of all contents
- Add recursive directory size calculation to Drive_tree
- Directory sizes now represent total size of all nested contents
- Rename sortNodes to calculateSizesAndSort for clarity
- Process children first (bottom-up) then calculate parent sizes
- Maintain existing sorting functionality
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
| -rw-r--r-- | frontend/lib/drive_server.ts | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/frontend/lib/drive_server.ts b/frontend/lib/drive_server.ts index e6dd833..6531641 100644 --- a/frontend/lib/drive_server.ts +++ b/frontend/lib/drive_server.ts | |||
| @@ -142,8 +142,23 @@ export async function Drive_tree(): Promise<DriveTreeResponse> { | |||
| 142 | } | 142 | } |
| 143 | }); | 143 | }); |
| 144 | 144 | ||
| 145 | // Third pass: sort all levels (directories first, then files, both alphabetically) | 145 | // Third pass: calculate directory sizes and sort all levels |
| 146 | const sortNodes = (nodes: DriveTreeNode[]): DriveTreeNode[] => { | 146 | const calculateSizesAndSort = (nodes: DriveTreeNode[]): DriveTreeNode[] => { |
| 147 | // First, recursively process children and calculate their sizes | ||
| 148 | nodes.forEach(node => { | ||
| 149 | if (node.children) { | ||
| 150 | node.children = calculateSizesAndSort(node.children); | ||
| 151 | |||
| 152 | // If this is a directory, calculate its size as sum of all children | ||
| 153 | if (node.type === "dir") { | ||
| 154 | node.size = node.children.reduce((total, child) => { | ||
| 155 | return total + (child.size || 0); | ||
| 156 | }, 0); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | }); | ||
| 160 | |||
| 161 | // Then sort: directories first, then files, both alphabetically | ||
| 147 | const sorted = nodes.sort((a, b) => { | 162 | const sorted = nodes.sort((a, b) => { |
| 148 | // Directories first, then files | 163 | // Directories first, then files |
| 149 | if (a.type === "dir" && b.type === "file") return -1; | 164 | if (a.type === "dir" && b.type === "file") return -1; |
| @@ -153,15 +168,8 @@ export async function Drive_tree(): Promise<DriveTreeResponse> { | |||
| 153 | return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); | 168 | return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); |
| 154 | }); | 169 | }); |
| 155 | 170 | ||
| 156 | // Recursively sort children | ||
| 157 | sorted.forEach(node => { | ||
| 158 | if (node.children) { | ||
| 159 | node.children = sortNodes(node.children); | ||
| 160 | } | ||
| 161 | }); | ||
| 162 | |||
| 163 | return sorted; | 171 | return sorted; |
| 164 | }; | 172 | }; |
| 165 | 173 | ||
| 166 | return { root: sortNodes(rootNodes) }; | 174 | return { root: calculateSizesAndSort(rootNodes) }; |
| 167 | } \ No newline at end of file | 175 | } \ No newline at end of file |
