Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/stores/workspaceStore.ts

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: Pre-existing: dupName regex doesn't handle multi-dot filenames ideally

The dupName construction at line 502 uses /(\.\w+)$/ to split name from extension. For files like App.d.ts, this only captures the last extension segment (.ts), producing App.d (copy).ts instead of App (copy).d.ts. Similarly, for dotfiles like .gitignore, it strips the entire name to produce (copy).gitignore (note the leading space). This is a pre-existing issue not introduced by this PR, but worth noting since the duplicateFile function was modified.

(Refers to line 502)

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function updateChildrenPaths(children: FileNode[], oldPath: string, newPath: str
return children.map((child) => {
const updatedChild: FileNode = {
...child,
path: child.path.replace(oldPath, newPath),
path: child.path.startsWith(oldPath) ? newPath + child.path.slice(oldPath.length) : child.path,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: The updateChildrenPaths fix correctly addresses a real String.replace bug

The old code child.path.replace(oldPath, newPath) had two issues: (1) String.replace with a string pattern only replaces the first occurrence, which is fine for prefix replacement but semantically unclear; (2) more importantly, it could match oldPath as a substring at any position within child.path, not just at the start. For example, renaming a folder /a to /ab could incorrectly transform a child path /b/a/file to /b/ab/file if the old path appeared as a substring elsewhere. The new startsWith check is strictly correct.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

};
if (updatedChild.children) {
updatedChild.children = updateChildrenPaths(updatedChild.children, oldPath, newPath);
Expand All @@ -201,6 +201,20 @@ function updateChildrenPaths(children: FileNode[], oldPath: string, newPath: str
});
}

function deepCloneFileNode(node: FileNode, newParentPath: string): FileNode {
const cloned: FileNode = {
...node,
id: genId('file'),
path: `${newParentPath}/${node.name}`,
};
if (node.children) {
cloned.children = node.children.map((child) =>
deepCloneFileNode(child, cloned.path)
);
}
return cloned;
}

function addChildToFolder(
nodes: FileNode[],
parentPath: string,
Expand Down Expand Up @@ -492,6 +506,11 @@ export const useWorkspaceStore = create<WorkspaceState>((set, get) => ({
name: dupName,
path: `${parentPath}/${dupName}`,
};
if (node.children) {
newNode.children = node.children.map((child) =>
deepCloneFileNode(child, newNode.path)
);
}
if (!parentPath) {
return { files: [...s.files, newNode] };
}
Expand Down