diff --git a/src/renderer/screens/DatabaseScreen/MainView.tsx b/src/renderer/screens/DatabaseScreen/MainView.tsx index 8c8c5df..141b7c5 100644 --- a/src/renderer/screens/DatabaseScreen/MainView.tsx +++ b/src/renderer/screens/DatabaseScreen/MainView.tsx @@ -6,6 +6,7 @@ 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 } = @@ -13,10 +14,14 @@ export default function MainView() { 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) => ( )); - }, [newWindow]); + }, [newWindow, tabs]); return ( diff --git a/src/renderer/utils/generateDatabaseName.ts b/src/renderer/utils/generateDatabaseName.ts index c834fcd..41d72d9 100644 --- a/src/renderer/utils/generateDatabaseName.ts +++ b/src/renderer/utils/generateDatabaseName.ts @@ -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 + ); } diff --git a/src/renderer/utils/generateIncrementalName.ts b/src/renderer/utils/generateIncrementalName.ts new file mode 100644 index 0000000..182804a --- /dev/null +++ b/src/renderer/utils/generateIncrementalName.ts @@ -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 ''; +}