close refactor

This commit is contained in:
Jan Prochazka
2020-05-10 20:50:37 +02:00
parent 31671ec573
commit c82f332c42

View File

@@ -45,7 +45,7 @@ const CloseButton = styled.i`
} }
`; `;
function TabContextMenu({ close, closeAll, closeOthers, closeWithSameDb, props }) { function TabContextMenu({ close, closeAll, closeOthers, closeWithSameDb, closeWithOtherDb, props }) {
const { database } = props || {}; const { database } = props || {};
const { conid } = props || {}; const { conid } = props || {};
return ( return (
@@ -56,6 +56,9 @@ function TabContextMenu({ close, closeAll, closeOthers, closeWithSameDb, props }
{conid && database && ( {conid && database && (
<DropDownMenuItem onClick={closeWithSameDb}>Close with same DB - {database}</DropDownMenuItem> <DropDownMenuItem onClick={closeWithSameDb}>Close with same DB - {database}</DropDownMenuItem>
)} )}
{conid && database && (
<DropDownMenuItem onClick={closeWithOtherDb}>Close with other DB than {database}</DropDownMenuItem>
)}
</> </>
); );
} }
@@ -75,10 +78,12 @@ export default function TabsPanel() {
})) }))
); );
}; };
const closeTab = (tabid) => { const closeTabFunc = (closeCondition) => (tabid) => {
setOpenedTabs((files) => { setOpenedTabs((files) => {
const active = files.find((x) => x.tabid == tabid);
if (!active) return files;
let index = _.findIndex(files, (x) => x.tabid == tabid); let index = _.findIndex(files, (x) => x.tabid == tabid);
const newFiles = files.filter((x) => x.tabid != tabid); const newFiles = files.filter((x) => !closeCondition(x, active));
if (!newFiles.find((x) => x.selected)) { if (!newFiles.find((x) => x.selected)) {
while (index >= newFiles.length) index -= 1; while (index >= newFiles.length) index -= 1;
@@ -88,36 +93,22 @@ export default function TabsPanel() {
return newFiles; return newFiles;
}); });
}; };
const closeTab = closeTabFunc((x, active) => x.tabid == active.tabid);
const closeAll = () => { const closeAll = () => {
setOpenedTabs([]); setOpenedTabs([]);
}; };
const closeWithSameDb = (tabid) => { const closeWithSameDb = closeTabFunc(
setOpenedTabs((files) => { (x, active) =>
const closed = files.find((x) => x.tabid == tabid); _.get(x, 'props.conid') == _.get(active, 'props.conid') &&
let index = _.findIndex(files, (x) => x.tabid == tabid); _.get(x, 'props.database') == _.get(active, 'props.database')
const newFiles = files.filter(
(x) =>
_.get(x, 'props.conid') != _.get(closed, 'props.conid') ||
_.get(x, 'props.database') != _.get(closed, 'props.database')
); );
const closeWithOtherDb = closeTabFunc(
if (!newFiles.find((x) => x.selected)) { (x, active) =>
while (index >= newFiles.length) index -= 1; _.get(x, 'props.conid') != _.get(active, 'props.conid') ||
if (index >= 0) newFiles[index].selected = true; _.get(x, 'props.database') != _.get(active, 'props.database')
} );
const closeOthers = closeTabFunc((x, active) => x.tabid != active.tabid);
return newFiles;
});
};
const closeOthers = (tabid) => {
setOpenedTabs((files) => {
const newFiles = files.filter((x) => x.tabid == tabid);
if (newFiles[0]) newFiles[0].selected = true;
return newFiles;
});
};
const handleMouseUp = (e, tabid) => { const handleMouseUp = (e, tabid) => {
if (e.button == 1) { if (e.button == 1) {
closeTab(tabid); closeTab(tabid);
@@ -133,6 +124,7 @@ export default function TabsPanel() {
closeAll={closeAll} closeAll={closeAll}
closeOthers={() => closeOthers(tabid)} closeOthers={() => closeOthers(tabid)}
closeWithSameDb={() => closeWithSameDb(tabid)} closeWithSameDb={() => closeWithSameDb(tabid)}
closeWithOtherDb={() => closeWithOtherDb(tabid)}
props={props} props={props}
/> />
); );