Open source personal finance application
fix(navigation): implement back navigation skipping for wallet and category detail pages
Ilya Komichev313e06863ce0
diff --git a/app/components/categories/page/id/Index.vue b/app/components/categories/page/id/Index.vueindex aee442c3..23fb1c36 100644--- a/app/components/categories/page/id/Index.vue+++ b/app/components/categories/page/id/Index.vue@@ -76,6 +76,7 @@ provide(filterKey, filter)const categoryId = computed(() => route.params.id) as ComputedRef<CategoryId>const category = computed(() => categoriesStore.items[categoryId.value])+const categoryDetailHistoryPattern = /^\/categories\/[^/]+$/const preCategoriesIds = computed(() => categoriesStore.getChildrenIds(categoryId.value))const categoriesIdsOrParent = computed(() => categoriesStore.getChildrenIdsOrParent(categoryId.value))@@ -220,6 +221,7 @@ const categoriesIds = computed(() => categoriesStore.getChildrenIds(categoryId.v<UiPage v-if="category"><StatHeaderv-model:activeTab="activeTab"+ :backSkipPattern="categoryDetailHistoryPattern":backTo="category.parentId ? `/categories/${category.parentId}` : '/categories'":hideTabs="!!singleTrnType"filterWalletsdiff --git a/app/components/stat/Header.vue b/app/components/stat/Header.vueindex 23e4aba5..3d588584 100644--- a/app/components/stat/Header.vue+++ b/app/components/stat/Header.vue@@ -7,6 +7,7 @@ import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'import { useWalletsStore } from '~/components/wallets/useWalletsStore'const props = defineProps<{+ backSkipPattern?: RegExpbackTo?: stringconfigWallets?: booleanfilterCategories?: boolean@@ -33,12 +34,10 @@ function onClickWallet(walletId: WalletId) {filter.toggleWalletId(walletId)trnsFormStore.values.walletId = walletId}--const isPopoverOpen = ref(false)</script><template>- <UiHeader :backTo="backTo">+ <UiHeader :backSkipPattern="backSkipPattern" :backTo="backTo"><slot name="title" /><template #actions>diff --git a/app/components/ui/Header.vue b/app/components/ui/Header.vueindex e48b01aa..8663e760 100644--- a/app/components/ui/Header.vue+++ b/app/components/ui/Header.vue@@ -1,8 +1,9 @@<script setup lang="ts">import { useSearch } from '~/components/search/useSearch'-import { canGoBack } from '~/composables/useNavigationHistory'+import { canGoBack, navigateBackSkipping } from '~/composables/useNavigationHistory'-const { backTo, hideSearch } = defineProps<{+const { backSkipPattern, backTo, hideSearch } = defineProps<{+ backSkipPattern?: RegExpbackTo?: stringhideSearch?: boolean}>()@@ -11,10 +12,15 @@ const { isSearchOpen } = useSearch()const router = useRouter()function onBack() {- if (canGoBack.value)+ if (backTo && backSkipPattern) {+ navigateBackSkipping(router, backTo, backSkipPattern)+ }+ else if (canGoBack.value) {router.back()- else if (backTo)+ }+ else if (backTo) {router.replace(backTo)+ }}</script>diff --git a/app/components/wallets/page/id/Index.vue b/app/components/wallets/page/id/Index.vueindex 265bf2ff..fb9aa22d 100644--- a/app/components/wallets/page/id/Index.vue+++ b/app/components/wallets/page/id/Index.vue@@ -26,6 +26,7 @@ provide(filterKey, filter)const walletId = computed(() => route.params.id as WalletId)const wallet = computed(() => walletsStore.items?.[walletId.value])+const walletDetailHistoryPattern = /^\/wallets\/[^/]+$/const activeTab = useStorage<StatTabSlug>(`${walletId.value}-tab`, 'summary')const storageKey = computed(() => `${walletId.value}-${activeTab.value}`)@@ -113,6 +114,7 @@ async function onDeleteConfirm() {<UiPage v-if="wallet"><StatHeaderv-model:activeTab="activeTab"+ :backSkipPattern="walletDetailHistoryPattern"backTo="/wallets"filterCategories>diff --git a/app/composables/useNavigationHistory.test.ts b/app/composables/useNavigationHistory.test.tsindex 602bd534..b33b7ca3 100644--- a/app/composables/useNavigationHistory.test.ts+++ b/app/composables/useNavigationHistory.test.ts@@ -113,6 +113,47 @@ describe('useNavigationHistory', () => {expect(router.push).toHaveBeenCalledWith('/x')})+ it('navigateBackSkipping traverses to the nearest earlier entry outside the skipped wallet pages', async () => {+ const traverseTo = vi.fn()+ stubNavigation([+ { index: 0, key: 'k0', url: 'http://localhost/dashboard' },+ { index: 1, key: 'k1', url: 'http://localhost/wallets/first' },+ { index: 2, key: 'k2', url: 'http://localhost/wallets/second' },+ { index: 3, key: 'k3', url: 'http://localhost/wallets/third' },+ ], 3, { traverseTo })+ const { navigateBackSkipping } = await import('~/composables/useNavigationHistory')+ const router = { back: vi.fn(), push: vi.fn(), replace: vi.fn() }+ navigateBackSkipping(router, '/wallets', /^\/wallets\/[^/]+$/)+ expect(traverseTo).toHaveBeenCalledWith('k0')+ expect(router.replace).not.toHaveBeenCalled()+ })++ it('navigateBackSkipping replaces with the fallback when history contains only skipped pages', async () => {+ stubNavigation([+ { index: 0, key: 'k0', url: 'http://localhost/wallets/first' },+ { index: 1, key: 'k1', url: 'http://localhost/wallets/second' },+ ], 1)+ const { navigateBackSkipping } = await import('~/composables/useNavigationHistory')+ const router = { back: vi.fn(), push: vi.fn(), replace: vi.fn() }+ navigateBackSkipping(router, '/wallets', /^\/wallets\/[^/]+$/)+ expect(router.replace).toHaveBeenCalledWith('/wallets')+ })++ it('navigateBackSkipping traverses to an earlier fallback even when it matches the skipped pattern', async () => {+ const traverseTo = vi.fn()+ stubNavigation([+ { index: 0, key: 'k0', url: 'http://localhost/dashboard' },+ { index: 1, key: 'k1', url: 'http://localhost/categories/parent' },+ { index: 2, key: 'k2', url: 'http://localhost/categories/child-a' },+ { index: 3, key: 'k3', url: 'http://localhost/categories/child-b' },+ ], 3, { traverseTo })+ const { navigateBackSkipping } = await import('~/composables/useNavigationHistory')+ const router = { back: vi.fn(), push: vi.fn(), replace: vi.fn() }+ navigateBackSkipping(router, '/categories/parent', /^\/categories\/[^/]+$/)+ expect(traverseTo).toHaveBeenCalledWith('k1')+ expect(router.replace).not.toHaveBeenCalled()+ })+it('canGoBack stays false in environments without Navigation API', async () => {stubNoNavigation()const { canGoBack, navigateAfterSave } = await import('~/composables/useNavigationHistory')diff --git a/app/composables/useNavigationHistory.ts b/app/composables/useNavigationHistory.tsindex 850d6c06..50849a48 100644--- a/app/composables/useNavigationHistory.ts+++ b/app/composables/useNavigationHistory.ts@@ -42,6 +42,25 @@ function previousEntryUrl(): string | null {return prev ? getEntryPathname(prev) : null}+function findEarlierEntry(matcher: (pathname: string) => boolean): NavigationApiEntry | null {+ if (!navigation?.currentEntry)+ return null++ const entries = navigation.entries()+ for (let i = navigation.currentEntry.index - 1; i >= 0; i--) {+ const entry = entries[i]+ if (entry && matcher(getEntryPathname(entry)))+ return entry+ }++ return null+}++function matchesPath(pathname: string, pattern: RegExp) {+ pattern.lastIndex = 0+ return pattern.test(pathname)+}+/** Use back() if target is the previous entry to avoid duplicating it on top. */export function navigateAfterSave(router: RouterLike, target: string) {if (previousEntryUrl() === target)@@ -52,15 +71,23 @@ export function navigateAfterSave(router: RouterLike, target: string) {/** Traverse to target if already in history; push otherwise — avoids cycles. */export function navigateToAncestor(router: RouterLike, target: string) {- if (navigation?.currentEntry) {- const entries = navigation.entries()- for (let i = navigation.currentEntry.index - 1; i >= 0; i--) {- const entry = entries[i]- if (entry && getEntryPathname(entry) === target) {- navigation.traverseTo(entry.key)- return- }- }+ const entry = findEarlierEntry(pathname => pathname === target)+ if (entry) {+ navigation?.traverseTo(entry.key)+ return}router.push(target)}++/**+ * Skip sibling detail pages and return to the nearest earlier non-matching page.+ * Falls back to replace() when the stack only contains skipped entries.+ */+export function navigateBackSkipping(router: RouterLike, fallback: string, skipPattern: RegExp) {+ const entry = findEarlierEntry(pathname => pathname === fallback || !matchesPath(pathname, skipPattern))+ if (entry) {+ navigation?.traverseTo(entry.key)+ return+ }+ router.replace(fallback)+}