forked from frappe/crm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Rework preserve last visited tab
- Loading branch information
1 parent
b5078b9
commit 0b6d071
Showing
3 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters