Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions src/renderer/screens/DatabaseScreen/MainView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ import { useAppFeature } from 'renderer/contexts/AppFeatureProvider';
import { useWindowTab } from 'renderer/contexts/WindowTabProvider';
import QueryWindow from './QueryWindow';
import DatabaseTableList from 'renderer/components/DatabaseTable/DatabaseTableList';
import generateIncrementalName from 'renderer/utils/generateIncrementalName';

export default function MainView() {
const { newWindow, tabs, setTabs, selectedTab, setSelectedTab } =
useWindowTab();
const { enableDebug } = useAppFeature();

const onNewTabClick = useCallback(() => {
newWindow('Unnamed Query', (key, name) => (
const incrementalTabName = generateIncrementalName(
tabs.map((tab) => tab.name),
'Unnamed Query'
);
newWindow(incrementalTabName, (key, name) => (
<QueryWindow tabKey={key} name={name} />
));
}, [newWindow]);
}, [newWindow, tabs]);

return (
<Splitter primaryIndex={1} secondaryInitialSize={200}>
Expand Down
13 changes: 5 additions & 8 deletions src/renderer/utils/generateDatabaseName.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { ConnectionStoreItem } from 'drivers/SQLLikeConnection';
import generateIncrementalName from './generateIncrementalName';

export default function generateDatabaseName(
dbs: ConnectionStoreItem[],
name: string
) {
const finalName = name.replace(/ \(\d+\)$/g, '');

for (let i = 0; i < 100; i++) {
const newName = i > 0 ? `${finalName} (${i})` : finalName;
if (!dbs.find((db) => db.name === newName)) return newName;
}

return '';
return generateIncrementalName(
dbs.map((db) => db.name),
name
);
}
13 changes: 13 additions & 0 deletions src/renderer/utils/generateIncrementalName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function generateIncrementalName(
nameList: string[],
name: string
) {
const finalName = name.replace(/ \(\d+\)$/g, '');

for (let i = 0; i < 1000; i++) {
const newName = i > 0 ? `${finalName} (${i})` : finalName;
if (!nameList.find((prevName) => prevName === newName)) return newName;
}

return '';
}