Merge pull request #293 from gopikrishnan13/last_visited_tab
feat: Preserve last visited tab
This commit is contained in:
commit
454973c0e7
80
frontend/src/composables/useActiveTabManager.js
Normal file
80
frontend/src/composables/useActiveTabManager.js
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
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()
|
||||||
|
}, 300)
|
||||||
|
|
||||||
|
function setActiveTabInUrl(tabName) {
|
||||||
|
window.location.hash = '#' + tabName.toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
|
function getActiveTabFromUrl() {
|
||||||
|
return route.hash.replace('#', '')
|
||||||
|
}
|
||||||
|
|
||||||
|
function findTabIndex(tabName) {
|
||||||
|
return tabs.value.findIndex(
|
||||||
|
(tabOptions) => tabOptions.name.toLowerCase() === tabName,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTabIndex(tabName) {
|
||||||
|
let index = findTabIndex(tabName)
|
||||||
|
return index !== -1 ? index : 0 // Default to the first tab if not found
|
||||||
|
}
|
||||||
|
|
||||||
|
function getActiveTabFromLocalStorage() {
|
||||||
|
return activieTab.value
|
||||||
|
}
|
||||||
|
|
||||||
|
function getActiveTab() {
|
||||||
|
let activeTab = getActiveTabFromUrl()
|
||||||
|
if (activeTab) {
|
||||||
|
let index = findTabIndex(activeTab)
|
||||||
|
if (index !== -1) {
|
||||||
|
preserveLastVisitedTab(activeTab)
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
let lastVisitedTab = getActiveTabFromLocalStorage()
|
||||||
|
if (lastVisitedTab) {
|
||||||
|
setActiveTabInUrl(lastVisitedTab)
|
||||||
|
return getTabIndex(lastVisitedTab)
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0 // Default to the first tab if nothing is found
|
||||||
|
}
|
||||||
|
|
||||||
|
const tabIndex = ref(getActiveTab())
|
||||||
|
|
||||||
|
watch(tabIndex, (tabIndexValue) => {
|
||||||
|
let currentTab = tabs.value[tabIndexValue].name
|
||||||
|
setActiveTabInUrl(currentTab)
|
||||||
|
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 }
|
||||||
|
}
|
||||||
@ -354,6 +354,8 @@ import {
|
|||||||
} from 'frappe-ui'
|
} from 'frappe-ui'
|
||||||
import { ref, computed, h, onMounted, onBeforeUnmount } from 'vue'
|
import { ref, computed, h, onMounted, onBeforeUnmount } from 'vue'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
|
import { useActiveTabManager } from '@/composables/useActiveTabManager'
|
||||||
|
|
||||||
|
|
||||||
const { $dialog, $socket, makeCall } = globalStore()
|
const { $dialog, $socket, makeCall } = globalStore()
|
||||||
const { statusOptions, getDealStatus } = statusesStore()
|
const { statusOptions, getDealStatus } = statusesStore()
|
||||||
@ -516,7 +518,6 @@ usePageMeta(() => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const tabIndex = ref(0)
|
|
||||||
const tabs = computed(() => {
|
const tabs = computed(() => {
|
||||||
let tabOptions = [
|
let tabOptions = [
|
||||||
{
|
{
|
||||||
@ -559,6 +560,7 @@ const tabs = computed(() => {
|
|||||||
]
|
]
|
||||||
return tabOptions.filter((tab) => (tab.condition ? tab.condition() : true))
|
return tabOptions.filter((tab) => (tab.condition ? tab.condition() : true))
|
||||||
})
|
})
|
||||||
|
const { tabIndex } = useActiveTabManager(tabs, 'lastDealTab')
|
||||||
|
|
||||||
const fieldsLayout = createResource({
|
const fieldsLayout = createResource({
|
||||||
url: 'crm.api.doc.get_sidebar_fields',
|
url: 'crm.api.doc.get_sidebar_fields',
|
||||||
|
|||||||
@ -330,6 +330,7 @@ import {
|
|||||||
} from 'frappe-ui'
|
} from 'frappe-ui'
|
||||||
import { ref, computed, onMounted, watch } from 'vue'
|
import { ref, computed, onMounted, watch } from 'vue'
|
||||||
import { useRouter, useRoute } from 'vue-router'
|
import { useRouter, useRoute } from 'vue-router'
|
||||||
|
import { useActiveTabManager } from '@/composables/useActiveTabManager'
|
||||||
|
|
||||||
const { $dialog, $socket, makeCall } = globalStore()
|
const { $dialog, $socket, makeCall } = globalStore()
|
||||||
const { getContactByName, contacts } = contactsStore()
|
const { getContactByName, contacts } = contactsStore()
|
||||||
@ -463,8 +464,6 @@ usePageMeta(() => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const tabIndex = ref(0)
|
|
||||||
|
|
||||||
const tabs = computed(() => {
|
const tabs = computed(() => {
|
||||||
let tabOptions = [
|
let tabOptions = [
|
||||||
{
|
{
|
||||||
@ -508,6 +507,8 @@ const tabs = computed(() => {
|
|||||||
return tabOptions.filter((tab) => (tab.condition ? tab.condition() : true))
|
return tabOptions.filter((tab) => (tab.condition ? tab.condition() : true))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const { tabIndex } = useActiveTabManager(tabs, 'lastLeadTab')
|
||||||
|
|
||||||
watch(tabs, (value) => {
|
watch(tabs, (value) => {
|
||||||
if (value && route.params.tabName) {
|
if (value && route.params.tabName) {
|
||||||
let index = value.findIndex(
|
let index = value.findIndex(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user