Skip to content

Commit

Permalink
TAB-424 start group management for chrome tabs - fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evandor committed Oct 12, 2023
1 parent 5420d22 commit 8d21b90
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
9 changes: 1 addition & 8 deletions src/services/IndexedDbPersistenceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,7 @@ class IndexedDbPersistenceService implements PersistenceService {
}

async deleteGroupByTitle(title: string): Promise<void> {
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<IDBPDatabase> {
Expand Down
33 changes: 25 additions & 8 deletions src/stores/groupsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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?

Expand All @@ -185,7 +202,7 @@ export const useGroupsStore = defineStore('groups', () => {
return {
initialize,
initListeners,
// groupFor,
// groupFor,
groupForName,
currentGroupForName,
currentGroupForId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})


Expand Down

0 comments on commit 8d21b90

Please sign in to comment.