Files
tehran_taj/apps/user-app/src/stores/navStore.ts
T

86 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-09-09 06:46:27 +03:30
// Ported from legacy henza_pwa/src/store/navStore.ts (a hand-rolled reactive
// class) to a real Pinia store, preserving the same state shape and setter
// behavior. The route list itself was redesigned to a 5-tab bottom nav
// (Home, Search, Cart, Orders, Profile) — see components/ui/Navigation.vue.
2026-09-09 06:46:27 +03:30
import { defineStore } from 'pinia'
interface NavItem {
name: string
path: string
icon: string
label: string
2026-09-09 06:46:27 +03:30
index: number
}
interface NavState {
navBarRoutes: NavItem[]
activeLink: number
showNav: boolean
// legacy passes `undefined` to clear the transition (router/index.ts:270)
pageTransition: string | undefined
/**
* The history index this app started at, or null before the first
* navigation.
*
* Answers "is there anywhere inside this app to go back to?", which
* `history.length` cannot: it also counts entries from before the app was
* opened, so it says yes for a link opened in a fresh tab and the back button
* would leave the app. Comparing the CURRENT history position against the one
* we started at is direction-proof — it stays correct however many times the
* person goes back and forward, where a plain counter would only ever grow.
*/
entryIndex: number | null
2026-09-09 06:46:27 +03:30
}
export const useNavStore = defineStore('nav', {
state: (): NavState => ({
navBarRoutes: [
{ name: 'home', path: '/home', icon: 'vuesax-linear:home-2', label: 'خانه', index: 1 },
{ name: 'search', path: '/search', icon: 'vuesax-linear:search-normal-1', label: 'جستجو', index: 2 },
{ name: 'cart', path: '/cart', icon: 'vuesax-linear:bag-happy', label: 'سبد خرید', index: 3 },
{ name: 'ordersList', path: '/orders', icon: 'vuesax-linear:receipt-text', label: 'سفارشات', index: 4 },
{ name: 'userPage::hasSubRoute', path: '/user', icon: 'vuesax-linear:user', label: 'پروفایل', index: 5 },
2026-09-09 06:46:27 +03:30
],
activeLink: 1,
2026-09-09 06:46:27 +03:30
showNav: true,
pageTransition: 'right',
entryIndex: null,
2026-09-09 06:46:27 +03:30
}),
getters: {
/**
* True when `router.back()` lands somewhere inside this app rather than on
* whatever the person was looking at before they opened it.
*/
canGoBack: (state) => {
if (state.entryIndex === null) return false
const current = window.history.state?.position
// `position` is written by vue-router on every entry it creates. If it is
// missing (a hand-written history entry, an old browser), assume we must
// not leave the app and let the caller fall back.
return typeof current === 'number' ? current > state.entryIndex : false
},
},
2026-09-09 06:46:27 +03:30
actions: {
/**
* Records where in history this app instance began. Called once, on the
* first navigation — every later entry sits above it, so anything above is
* ours to go back through.
*/
noteFirstNavigation() {
if (this.entryIndex !== null) return
const position = window.history.state?.position
this.entryIndex = typeof position === 'number' ? position : 0
},
2026-09-09 06:46:27 +03:30
setNavVisibility(data: boolean) {
this.showNav = data
},
setActiveLink(activeLink: number) {
this.activeLink = activeLink
},
setPgaTransition(transition: string | undefined) {
this.pageTransition = transition
},
},
})