Skip to content

Commit ccf4205

Browse files
committed
Changes to fix rename problem
1 parent 08f544a commit ccf4205

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/reactComponents/FileManageModal.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default function FileManageModal(props: FileManageModalProps) {
6565
const [copyModalOpen, setCopyModalOpen] = React.useState(false);
6666

6767
React.useEffect(() => {
68-
if (!props.project || props.tabType === null) {
68+
if (!props.project || props.tabType === null || !props.isOpen) {
6969
setModules([]);
7070
return;
7171
}
@@ -89,7 +89,7 @@ export default function FileManageModal(props: FileManageModalProps) {
8989
// Sort modules alphabetically by name
9090
moduleList.sort((a, b) => a.name.localeCompare(b.name));
9191
setModules(moduleList);
92-
}, [props.project, props.tabType]);
92+
}, [props.project, props.tabType, props.isOpen]);
9393

9494
/** Handles renaming a module. */
9595
const handleRename = async (origModule: Module, newClassName: string): Promise<void> => {
@@ -106,19 +106,10 @@ export default function FileManageModal(props: FileManageModalProps) {
106106
);
107107
await props.onProjectChanged();
108108

109-
const newModules = modules.map((module) => {
110-
if (module.path === origModule.path) {
111-
return {...module, title: newClassName, path: newModulePath};
112-
}
113-
return module;
114-
});
115-
116-
setModules(newModules);
117-
118109
// Close the rename modal first
119110
setRenameModalOpen(false);
120111

121-
// Automatically select and open the newly created module
112+
// Automatically select and open the renamed module
122113
props.gotoTab(newModulePath);
123114
props.onClose();
124115

src/reactComponents/Tabs.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,32 @@ export function Component(props: TabsProps): React.JSX.Element {
381381
// Effect to handle active tab changes
382382
React.useEffect(() => {
383383
if (activeKey !== props.activeTab) {
384-
if (!isTabOpen(props.activeTab)) {
385-
addTab(props.activeTab);
384+
if (!isTabOpen(props.activeTab) && props.project) {
385+
// Check if this is a renamed module by looking for a tab that points to a non-existent path
386+
const targetModule = storageProject.findModuleByModulePath(props.project, props.activeTab);
387+
if (targetModule) {
388+
// Find a tab whose module path no longer exists (indicating it was renamed)
389+
const staleTab = props.tabList.find((tab) => {
390+
if (tab.type === TabType.ROBOT) return false;
391+
const tabModule = storageProject.findModuleByModulePath(props.project!, tab.key);
392+
// If the tab's path doesn't exist, check if it matches the type of the target module
393+
return !tabModule &&
394+
((tab.type === TabType.MECHANISM && targetModule.moduleType === storageModule.ModuleType.MECHANISM) ||
395+
(tab.type === TabType.OPMODE && targetModule.moduleType === storageModule.ModuleType.OPMODE));
396+
});
397+
398+
if (staleTab) {
399+
// Update the stale tab with the new path and title
400+
const newTabs = props.tabList.map((tab) =>
401+
tab.key === staleTab.key ? { ...tab, key: props.activeTab, title: targetModule.className } : tab
402+
);
403+
props.setTabList(newTabs);
404+
} else {
405+
addTab(props.activeTab);
406+
}
407+
} else {
408+
addTab(props.activeTab);
409+
}
386410
}
387411
handleTabChange(props.activeTab);
388412
}

0 commit comments

Comments
 (0)