Skip to content

Commit

Permalink
fix: also watch hash change and change tab accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
shariquerik committed Sep 27, 2024
1 parent 97af42f commit 028ad81
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion frontend/src/composables/useActiveTabManager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useDebounceFn, useStorage } from '@vueuse/core'

export function useActiveTabManager(tabs, storageKey) {
const activieTab = useStorage(storageKey, 'activity')
const route = useRoute()

const preserveLastVisitedTab = useDebounceFn((tabName) => {
activieTab.value = tabName.toLowerCase()
Expand All @@ -13,7 +15,7 @@ export function useActiveTabManager(tabs, storageKey) {
}

function getActiveTabFromUrl() {
return window.location.hash.replace('#', '')
return route.hash.replace('#', '')
}

function findTabIndex(tabName) {
Expand Down Expand Up @@ -59,5 +61,20 @@ export function useActiveTabManager(tabs, storageKey) {
preserveLastVisitedTab(currentTab)
})

watch(
() => route.hash,
(tabValue) => {
if (!tabValue) return

let tabName = tabValue.replace('#', '')
let index = findTabIndex(tabName)
if (index === -1) index = 0

let currentTab = tabs.value[index].name
preserveLastVisitedTab(currentTab)
tabIndex.value = index
},
)

return { tabIndex }
}

0 comments on commit 028ad81

Please sign in to comment.