Skip to content

Commit 2cdd153

Browse files
committed
fix(web): set correct intermediate directory paths in buildFileTree
When constructing the file tree from a flat list in buildFileTree, new intermediate directory nodes were assigned item.path (the path of the leaf item being processed) instead of their actual directory path. This caused intermediate directory nodes to have full file paths instead of their directory path (e.g. directory 'src' receiving path 'src/components/Button.tsx'), breaking folder expansion state tracking (openPaths.has(node.path)) and corrupting directory paths returned by getTree. Reconstruct the node path using parts.slice(0, i + 1).join('/') so both directories and leaves have the correct path.
1 parent 390e8b2 commit 2cdd153

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎packages/web/src/features/git/utils.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,28 @@ test('buildFileTree builds a sorted tree from a flat list', () => {
103103
});
104104
});
105105

106+
test('buildFileTree correctly sets intermediate directory paths', () => {
107+
const flatList: { type: string, path: string }[] = [
108+
{ type: 'blob', path: 'src/components/buttons/PrimaryButton.tsx' },
109+
{ type: 'blob', path: 'src/index.ts' },
110+
];
111+
112+
const tree = buildFileTree(flatList);
113+
114+
const srcNode = tree.children.find((c) => c.name === 'src');
115+
expect(srcNode?.path).toBe('src');
116+
expect(srcNode?.type).toBe('tree');
117+
118+
const compNode = srcNode?.children.find((c) => c.name === 'components');
119+
expect(compNode?.path).toBe('src/components');
120+
expect(compNode?.type).toBe('tree');
121+
122+
const btnDir = compNode?.children.find((c) => c.name === 'buttons');
123+
expect(btnDir?.path).toBe('src/components/buttons');
124+
expect(btnDir?.type).toBe('tree');
125+
126+
const btnFile = btnDir?.children.find((c) => c.name === 'PrimaryButton.tsx');
127+
expect(btnFile?.path).toBe('src/components/buttons/PrimaryButton.tsx');
128+
expect(btnFile?.type).toBe('blob');
129+
});
130+

‎packages/web/src/features/git/utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const buildFileTree = (flatList: { type: string, path: string }[]): FileT
6767
if (!next) {
6868
next = {
6969
name: part,
70-
path: item.path,
70+
path: parts.slice(0, i + 1).join('/'),
7171
type: nodeType,
7272
children: [],
7373
};

0 commit comments

Comments
 (0)