diff --git a/src/services/IndexedDbPersistenceService.ts b/src/services/IndexedDbPersistenceService.ts index fe6d6cef..60ce1812 100644 --- a/src/services/IndexedDbPersistenceService.ts +++ b/src/services/IndexedDbPersistenceService.ts @@ -504,14 +504,7 @@ class IndexedDbPersistenceService implements PersistenceService { } async deleteGroupByTitle(title: string): Promise { - const groups = await this.getGroups() - // .then(groups => { - for (const g of groups) { - if (g.title === title) { - await this.db.delete('groups', g.id) - } - } - // }) + return this.db.delete('groups', title) } private async initDatabase(dbName: string): Promise { diff --git a/src/stores/groupsStore.ts b/src/stores/groupsStore.ts index 2753e34b..751a22cf 100644 --- a/src/stores/groupsStore.ts +++ b/src/stores/groupsStore.ts @@ -66,6 +66,7 @@ export const useGroupsStore = defineStore('groups', () => { } function onCreated(group: chrome.tabGroups.TabGroup) { + console.debug("group: onCreated", group) if (inBexMode() && chrome?.tabGroups && group.title) { // update currentTabGroups chrome.tabGroups.query({}, (groups) => { @@ -75,11 +76,19 @@ export const useGroupsStore = defineStore('groups', () => { } function onUpdated(group: chrome.tabGroups.TabGroup) { + console.log("group: onUpdated", group) if (inBexMode() && chrome?.tabGroups) { // update currentTabGroups chrome.tabGroups.query({}, (groups) => { currentTabGroups.value = groups + console.log("set currentTabGroups to", groups) + + // update tabGroups + for (const g of groups) { + useGroupsStore().persistGroup(g) + } + // update the group names for matching group ids for (const ts of [...useTabsStore().tabsets.values()]) { let matchForTabset = false @@ -99,7 +108,7 @@ export const useGroupsStore = defineStore('groups', () => { // update color changes for (const g of groups) { - const tabGroup = findGroup([...tabGroups.value.values()], undefined, g.title) + const tabGroup = findGroup([...tabGroups.value.values()], undefined, g.title) if (tabGroup && tabGroup.color !== g.color) { console.log("updating group", tabGroup, g) storage.updateGroup(g) @@ -159,7 +168,15 @@ export const useGroupsStore = defineStore('groups', () => { } function persistGroup(group: chrome.tabGroups.TabGroup) { - storage.addGroup(JSON.parse(JSON.stringify(group)) as chrome.tabGroups.TabGroup) + if (group.title) { + storage.getGroups().then(existingGroups => { + if (existingGroups.findIndex(g => g.title === group.title) < 0) { + storage.addGroup(JSON.parse(JSON.stringify(group)) as chrome.tabGroups.TabGroup) + } else { + console.debug("group '" + group.title + "' exists already") + } + }) + } } function updateGroup(group: chrome.tabGroups.TabGroup) { @@ -171,11 +188,11 @@ export const useGroupsStore = defineStore('groups', () => { // console.log("found group to delete", groupFound) // // if (groupFound) { - // remove here in groupsStore - tabGroups.value.delete(title) - // delete in DB - return storage.deleteGroupByTitle(title) - // } + // remove here in groupsStore + tabGroups.value.delete(title) + // delete in DB + return storage.deleteGroupByTitle(title) + // } // delete in all tabs? @@ -185,7 +202,7 @@ export const useGroupsStore = defineStore('groups', () => { return { initialize, initListeners, - // groupFor, + // groupFor, groupForName, currentGroupForName, currentGroupForId, diff --git a/test/vitest/__tests__/domain/groups/DeleteChromeGroupCommand.test.ts b/test/vitest/__tests__/domain/groups/DeleteChromeGroupCommand.test.ts index 213f8037..58ada9dd 100644 --- a/test/vitest/__tests__/domain/groups/DeleteChromeGroupCommand.test.ts +++ b/test/vitest/__tests__/domain/groups/DeleteChromeGroupCommand.test.ts @@ -44,10 +44,10 @@ describe('DeleteChromeGroupCommand', () => { const groups = await db.getGroups() expect(groups.length).toBe(1) - const cmd = await new DeleteChromeGroupCommand("groupName").execute() + await new DeleteChromeGroupCommand("groupName").execute() const groupsAfterDeletion = await db.getGroups() - //expect(groupsAfterDeletion.length).toBe(0) + expect(groupsAfterDeletion.length).toBe(0) })