Skip to content

Commit

Permalink
feat: Rework preserve last visited tab
Browse files Browse the repository at this point in the history
  • Loading branch information
gopikrishnan13 committed Sep 25, 2024
1 parent b5078b9 commit 0b6d071
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
68 changes: 68 additions & 0 deletions frontend/src/composables/useActiveTabManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ref, watch } from 'vue';

// Debounce function to delay updates
function debounce(fn, delay) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
}

export function useActiveTabManager(tabs, storageKey) {

const preserveLastVisitedTab = debounce((tabName) => {
localStorage.setItem(storageKey, tabName.toLowerCase());
}, 300);

function setActiveTabInUrl(tabName) {
window.location.hash = '#' + tabName.toLowerCase();
}

function getActiveTabFromUrl() {
return window.location.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 localStorage.getItem(storageKey);
}

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);
});

return { tabIndex };
}
4 changes: 3 additions & 1 deletion frontend/src/pages/Deal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ import {
} from 'frappe-ui'
import { ref, computed, h, onMounted, onBeforeUnmount } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {useActiveTabManager} from '@/composables/useActiveTabManager'
const { $dialog, $socket, makeCall } = globalStore()
const { statusOptions, getDealStatus } = statusesStore()
Expand Down Expand Up @@ -513,7 +515,6 @@ usePageMeta(() => {
}
})
const tabIndex = ref(0)
const tabs = computed(() => {
let tabOptions = [
{
Expand Down Expand Up @@ -556,6 +557,7 @@ const tabs = computed(() => {
]
return tabOptions.filter((tab) => (tab.condition ? tab.condition() : true))
})
const { tabIndex } = useActiveTabManager(tabs, 'lastDealTab')
const fieldsLayout = createResource({
url: 'crm.api.doc.get_sidebar_fields',
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/pages/Lead.vue
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ import {
} from 'frappe-ui'
import { ref, computed, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {useActiveTabManager} from '@/composables/useActiveTabManager'
const { $dialog, $socket, makeCall } = globalStore()
const { getContactByName, contacts } = contactsStore()
Expand Down Expand Up @@ -463,8 +464,6 @@ usePageMeta(() => {
}
})
const tabIndex = ref(0)
const tabs = computed(() => {
let tabOptions = [
{
Expand Down Expand Up @@ -508,6 +507,8 @@ const tabs = computed(() => {
return tabOptions.filter((tab) => (tab.condition ? tab.condition() : true))
})
const { tabIndex } = useActiveTabManager(tabs, 'lastLeadTab')
watch(tabs, (value) => {
if (value && route.params.tabName) {
let index = value.findIndex(
Expand Down

0 comments on commit 0b6d071

Please sign in to comment.