Skip to content

Commit 7113d11

Browse files
committed
Fix tab context menu
1 parent 24a1e93 commit 7113d11

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

src/frontend/components/IconMenu/file.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function FileMenu() {
3434
const newNotebook = await openNewFile()
3535
dispatch(addNotebook(newNotebook))
3636
dispatch(setActiveNotebookTabNumber(activeNotebookTabNumber + 1))
37-
dispatch(updateActiveNotebookName(newNotebook.name));
37+
dispatch(updateActiveNotebookName(newNotebook?.name));
3838

3939
}
4040

src/frontend/components/NotebookTabPanel/index.tsx

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import Tab from '@mui/material/Tab';
44
import Box from '@mui/material/Box';
55
import { AppState } from '../../lib/typings/types';
66
import { useSelector, useDispatch } from 'react-redux';
7-
import { updateActiveNotebookName, setActiveNotebookTabNumber, updateNotebooks } from '../../lib/state/reducer';
7+
import {
8+
updateActiveNotebookName,
9+
setActiveNotebookTabNumber,
10+
updateNotebooks
11+
} from '../../lib/state/reducer';
812
import Menu from '@mui/material/Menu';
913
import MenuItem from '@mui/material/MenuItem';
1014

@@ -16,20 +20,25 @@ export default function NotebookTabPanel() {
1620
} | null>(null);
1721

1822
const dispatch = useDispatch();
19-
const { notebooks, activeNotebookTabNumber } = useSelector((state: { app: AppState }) => state.app)
23+
const { notebooks, activeNotebookTabNumber, activeNotebookName } = useSelector((state: { app: AppState }) => state.app)
2024
const tabNames = Object.keys(notebooks)
2125

22-
const handleChange = (event: any, newValue: number) => {
26+
const handleTabChange = (event: any, newValue: number) => {
2327
const currentNotebookTab = notebooks[event.target.innerText]
2428
dispatch(updateActiveNotebookName(currentNotebookTab.name));
2529
dispatch(setActiveNotebookTabNumber(newValue));
2630
};
2731

28-
const handleContextMenu = (event: React.MouseEvent) => {
32+
const handleContextMenu = (event: React.MouseEvent, name: string) => {
2933
// @ts-ignore
3034
if (event.target.innerText === "Dashboard") {
3135
return;
3236
}
37+
38+
if (name !== tabNames[activeNotebookTabNumber]) {
39+
return
40+
}
41+
3342
event.preventDefault();
3443
setContextMenu(
3544
contextMenu === null
@@ -41,47 +50,56 @@ export default function NotebookTabPanel() {
4150
);
4251
};
4352

44-
const handleClose = () => {
53+
const handleContextMenuClose = () => {
4554
setContextMenu(null);
4655
};
4756

48-
const handleCloseTab = (name: string) => {
49-
if (name === "Dashboard") {
50-
return;
51-
}
57+
const handleCloseCurrentTab = () => {
58+
const tabName = tabNames[activeNotebookTabNumber]
5259
const newNotebooks = { ...notebooks };
53-
delete newNotebooks[name];
54-
const tabIndexBeforeCurrentTab = tabNames.indexOf(name) - 1;
60+
delete newNotebooks[tabName];
61+
const tabIndexBeforeCurrentTab = tabNames.indexOf(tabName) - 1; //On close set Tab immediately after as the next active Tab
5562
const tabNameBeforeCurrentTab = tabNames[tabIndexBeforeCurrentTab];
5663
dispatch(updateActiveNotebookName(tabNameBeforeCurrentTab || "Dashboard"));
5764
dispatch(setActiveNotebookTabNumber(tabIndexBeforeCurrentTab));
5865
dispatch(updateNotebooks(newNotebooks));
59-
handleClose();
66+
handleContextMenuClose();
67+
}
68+
69+
const handleCloseOtherTabs = () => {
70+
const tabName = tabNames[activeNotebookTabNumber]
71+
72+
const newNotebooks = {
73+
"Dashboard": { ...notebooks["Dashboard"] },
74+
[tabName]: { ...notebooks[tabName] },
75+
}
76+
dispatch(updateNotebooks(newNotebooks));
77+
dispatch(updateActiveNotebookName(tabName));
78+
dispatch(setActiveNotebookTabNumber(1)); //Set to one, because remaining Tab is after Dashboard Tab
79+
handleContextMenuClose();
6080
}
6181

82+
6283
const CustomTabComponent = (props: any) => {
6384
const { name } = props
6485
return (
6586
<div
66-
onContextMenu={handleContextMenu}
87+
onContextMenu={(e) => handleContextMenu(e, name)}
6788
style={{ cursor: 'context-menu' }}
6889
>
6990
<Tab {...props} style={{ textTransform: "none" }} />
7091
<Menu
7192
open={contextMenu !== null}
72-
onClose={handleClose}
93+
onClose={handleContextMenuClose}
7394
anchorReference="anchorPosition"
7495
anchorPosition={
7596
contextMenu !== null
7697
? { top: contextMenu.mouseY, left: contextMenu.mouseX }
7798
: undefined
7899
}
79100
>
80-
<MenuItem onClick={() => handleCloseTab(name)}>Close Tab</MenuItem>
81-
<MenuItem onClick={handleClose}>Close All Other Tabs</MenuItem>
82-
<MenuItem onClick={handleClose}>Rename Notebook</MenuItem>
83-
<MenuItem onClick={handleClose}>Delete Notebook</MenuItem>
84-
101+
<MenuItem onClick={() => handleCloseCurrentTab()}>Close Tab</MenuItem>
102+
<MenuItem id={name} onClick={() => handleCloseOtherTabs()}>Close All Other Tabs</MenuItem>
85103
</Menu>
86104
</div>
87105
);
@@ -91,7 +109,7 @@ export default function NotebookTabPanel() {
91109
return (
92110
<div className='col-span-12'>
93111
<Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
94-
<Tabs value={activeNotebookTabNumber} onChange={handleChange} aria-label="basic tabs example">
112+
<Tabs value={activeNotebookTabNumber} onChange={handleTabChange} aria-label="basic tabs example">
95113
{
96114
tabNames.map((name, i) => {
97115
return (

0 commit comments

Comments
 (0)