Skip to content

Open source personal finance application

refactor(menu): replace flag-soup context-menu composables with builders

Ilya Komichev0e20a2cc69ad
diff --git a/app/app/components/categories/SelectorModal.vue b/app/app/components/categories/SelectorModal.vue
index 79117dd0..ff65980f 100644
--- a/app/app/components/categories/SelectorModal.vue
+++ b/app/app/components/categories/SelectorModal.vue
@@ -5,7 +5,7 @@ import type { CategoryId } from '~/components/categories/types'
import { useCategoriesExpanded } from '~/components/categories/useCategoriesExpanded'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
-import { useCategoryContextMenu } from '~/components/categories/useCategoryContextMenu'
+import { isMenuableCategory, useCategoryMenuItems } from '~/components/categories/useCategoryMenuItems'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
import { showErrorToast, showSuccessToast } from '~/composables/useStoreSync'
@@ -181,26 +181,20 @@ async function onDeleteConfirm() {
}, 300)
}
-const { getCategoryContextMenuItems: getLaptopMenuItems } = useCategoryContextMenu({
- excludeOpen: true,
- onDelete: onClickDelete,
- returnBackOnSave: true,
- withQuickToggles: true,
-})
-
-const { getCategoryContextMenuItems: getMobileMenuItems } = useCategoryContextMenu({
- excludeOpen: true,
- onDelete: onClickDelete,
- onEdit: (id) => {
- editingCategoryId.value = id
- },
- withQuickToggles: true,
-})
+const categoryMenu = useCategoryMenuItems()
function getCategoryContextMenuItems(categoryId: CategoryId) {
- return isLaptop.value
- ? getLaptopMenuItems(categoryId)
- : getMobileMenuItems(categoryId)
+ if (!isMenuableCategory(categoryId))
+ return undefined
+ const editOpts = isLaptop.value
+ ? { returnBack: true }
+ : { onEdit: (id: CategoryId) => { editingCategoryId.value = id } }
+ const qt = categoryMenu.quickToggles(categoryId)
+ return [
+ [categoryMenu.edit(categoryId, editOpts)],
+ ...(qt ? [qt] : []),
+ [categoryMenu.delete(categoryId, onClickDelete)],
+ ]
}
function onSelect(id: CategoryId) {
diff --git a/app/app/components/categories/page/List.vue b/app/app/components/categories/page/List.vue
index a972333a..d0bfb499 100644
--- a/app/app/components/categories/page/List.vue
+++ b/app/app/components/categories/page/List.vue
@@ -5,7 +5,7 @@ import type { CategoryId } from '~/components/categories/types'
import { useCategoriesExpanded } from '~/components/categories/useCategoriesExpanded'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
-import { useCategoryContextMenu } from '~/components/categories/useCategoryContextMenu'
+import { isMenuableCategory, useCategoryMenuItems } from '~/components/categories/useCategoryMenuItems'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
import { showErrorToast, showSuccessToast } from '~/composables/useStoreSync'
@@ -73,9 +73,17 @@ async function onDeleteConfirm() {
}, 300)
}
-const { getCategoryContextMenuItems } = useCategoryContextMenu({
- onDelete: categoryId => onClickDelete(categoryId),
-})
+const categoryMenu = useCategoryMenuItems()
+
+function getCategoryContextMenuItems(categoryId: CategoryId) {
+ if (!isMenuableCategory(categoryId))
+ return undefined
+ const open = categoryMenu.open(categoryId)
+ return [
+ [...(open ? [open] : []), categoryMenu.edit(categoryId)],
+ [categoryMenu.delete(categoryId, onClickDelete)],
+ ]
+}
</script>
<template>
diff --git a/app/app/components/categories/page/id/Index.vue b/app/app/components/categories/page/id/Index.vue
index 0885e2e2..1c3cadb7 100644
--- a/app/app/components/categories/page/id/Index.vue
+++ b/app/app/components/categories/page/id/Index.vue
@@ -6,7 +6,7 @@ import type { CategoryId } from '~/components/categories/types'
import type { StatTabSlug } from '~/components/stat/types'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
-import { useCategoryContextMenu } from '~/components/categories/useCategoryContextMenu'
+import { isMenuableCategory, useCategoryMenuItems } from '~/components/categories/useCategoryMenuItems'
import { useStatDate } from '~/components/date/useStatDate'
import { calculateBestIntervalsBy } from '~/components/date/utils'
import { useFilter } from '~/components/stat/filter/useFilter'
@@ -68,9 +68,17 @@ async function onDeleteChildConfirm() {
}, 300)
}
-const { getCategoryContextMenuItems } = useCategoryContextMenu({
- onDelete: childCategoryId => onClickDeleteChild(childCategoryId),
-})
+const categoryMenu = useCategoryMenuItems()
+
+function getCategoryContextMenuItems(categoryId: CategoryId) {
+ if (!isMenuableCategory(categoryId))
+ return undefined
+ const open = categoryMenu.open(categoryId)
+ return [
+ [...(open ? [open] : []), categoryMenu.edit(categoryId)],
+ [categoryMenu.delete(categoryId, onClickDeleteChild)],
+ ]
+}
provide(filterKey, filter)
diff --git a/app/app/components/categories/useCategoryContextMenu.ts b/app/app/components/categories/useCategoryContextMenu.ts
deleted file mode 100644
index 08c7c4a1..00000000
--- a/app/app/components/categories/useCategoryContextMenu.ts
+++ /dev/null
@@ -1,98 +0,0 @@
-import type { CategoryId } from '~/components/categories/types'
-
-import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
-
-export function useCategoryContextMenu(options?: {
- excludeOpen?: boolean
- onDelete?: (categoryId: CategoryId) => void
- onEdit?: (categoryId: CategoryId) => void
- returnBackOnSave?: boolean
- withQuickToggles?: boolean
-}) {
- const { t } = useI18n()
- const router = useRouter()
- const categoriesStore = useCategoriesStore()
-
- function toggleFlag(categoryId: CategoryId, key: 'showInLastUsed' | 'showInQuickSelector') {
- const cat = categoriesStore.items[categoryId]
- if (!cat)
- return
- categoriesStore.saveCategory({
- id: categoryId,
- isUpdateChildCategoriesColor: false,
- values: { ...cat, [key]: !cat[key] },
- })
- }
-
- function getCategoryContextMenuItems(categoryId: CategoryId) {
- if (categoryId === 'transfer' || categoryId === 'adjustment')
- return undefined
-
- const cat = categoriesStore.items[categoryId]
- if (!cat)
- return undefined
-
- const isChild = cat.parentId !== 0
- const hasChildren = categoriesStore.hasChildren(categoryId)
- const editPath = options?.returnBackOnSave
- ? `/categories/${categoryId}/edit?returnBack=1`
- : `/categories/${categoryId}/edit`
-
- const editGroup = [
- ...(!options?.excludeOpen && !isChild
- ? [{
- icon: 'lucide:square-arrow-out-up-right',
- label: t('base.open'),
- onSelect: () => router.push(`/categories/${categoryId}`),
- }]
- : []),
- {
- icon: 'lucide:pencil',
- label: t('base.edit'),
- onSelect: () => options?.onEdit
- ? options.onEdit(categoryId)
- : router.push(editPath),
- },
- ]
-
- const groups: any[][] = [editGroup]
-
- // Quick toggles only for leaf categories (those selectable in trnForm).
- // Recent toggle is hidden when category is already a favorite — favorites
- // supersede recents in the UI, so the recent flag is moot.
- if (options?.withQuickToggles && !hasChildren) {
- const toggles: any[] = [{
- icon: 'lucide:star',
- label: cat.showInQuickSelector
- ? t('categories.actions.removeFromFavorites')
- : t('categories.actions.addToFavorites'),
- onSelect: () => toggleFlag(categoryId, 'showInQuickSelector'),
- }]
-
- if (!cat.showInQuickSelector) {
- toggles.push({
- icon: 'lucide:clock',
- label: cat.showInLastUsed
- ? t('categories.actions.removeFromRecent')
- : t('categories.actions.addToRecent'),
- onSelect: () => toggleFlag(categoryId, 'showInLastUsed'),
- })
- }
-
- groups.push(toggles)
- }
-
- if (options?.onDelete) {
- groups.push([{
- color: 'error' as const,
- icon: 'lucide:trash-2',
- label: t('base.delete'),
- onSelect: () => options.onDelete!(categoryId),
- }])
- }
-
- return groups
- }
-
- return { getCategoryContextMenuItems }
-}
diff --git a/app/app/components/categories/useCategoryMenuItems.ts b/app/app/components/categories/useCategoryMenuItems.ts
new file mode 100644
index 00000000..385d71f4
--- /dev/null
+++ b/app/app/components/categories/useCategoryMenuItems.ts
@@ -0,0 +1,89 @@
+import type { CategoryId } from '~/components/categories/types'
+
+import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
+
+type Item = { color?: 'error', icon: string, label: string, onSelect: () => void }
+
+export function isMenuableCategory(categoryId: CategoryId): boolean {
+ return categoryId !== 'transfer' && categoryId !== 'adjustment'
+}
+
+export function useCategoryMenuItems() {
+ const { t } = useI18n()
+ const router = useRouter()
+ const categoriesStore = useCategoriesStore()
+
+ function toggleFlag(categoryId: CategoryId, key: 'showInLastUsed' | 'showInQuickSelector') {
+ const cat = categoriesStore.items[categoryId]
+ if (!cat)
+ return
+ categoriesStore.saveCategory({
+ id: categoryId,
+ isUpdateChildCategoriesColor: false,
+ values: { ...cat, [key]: !cat[key] },
+ })
+ }
+
+ return {
+ delete(categoryId: CategoryId, onDelete: (id: CategoryId) => void): Item {
+ return {
+ color: 'error',
+ icon: 'lucide:trash-2',
+ label: t('base.delete'),
+ onSelect: () => onDelete(categoryId),
+ }
+ },
+
+ edit(categoryId: CategoryId, opts?: { onEdit?: (id: CategoryId) => void, returnBack?: boolean }): Item {
+ return {
+ icon: 'lucide:pencil',
+ label: t('base.edit'),
+ onSelect: () => opts?.onEdit
+ ? opts.onEdit(categoryId)
+ : router.push(opts?.returnBack
+ ? `/categories/${categoryId}/edit?returnBack=1`
+ : `/categories/${categoryId}/edit`),
+ }
+ },
+
+ // Open is only meaningful for root categories (subcategories don't have detail pages).
+ open(categoryId: CategoryId): Item | null {
+ const cat = categoriesStore.items[categoryId]
+ if (!cat || cat.parentId !== 0)
+ return null
+ return {
+ icon: 'lucide:square-arrow-out-up-right',
+ label: t('base.open'),
+ onSelect: () => router.push(`/categories/${categoryId}`),
+ }
+ },
+
+ // Favorite/recent toggles - leaf categories only. Recent is hidden when
+ // already a favorite (favorites supersede recents in the UI).
+ quickToggles(categoryId: CategoryId): Item[] | null {
+ const cat = categoriesStore.items[categoryId]
+ if (!cat || categoriesStore.hasChildren(categoryId))
+ return null
+
+ const items: Item[] = [{
+ icon: 'lucide:star',
+ label: cat.showInQuickSelector
+ ? t('categories.actions.removeFromFavorites')
+ : t('categories.actions.addToFavorites'),
+ onSelect: () => toggleFlag(categoryId, 'showInQuickSelector'),
+ }]
+
+ if (!cat.showInQuickSelector) {
+ items.push({
+ icon: 'lucide:clock',
+ label: cat.showInLastUsed
+ ? t('categories.actions.removeFromRecent')
+ : t('categories.actions.addToRecent'),
+ onSelect: () => toggleFlag(categoryId, 'showInLastUsed'),
+ })
+ }
+
+ return items
+ },
+ }
+}
diff --git a/app/app/components/layout/Sidebar.vue b/app/app/components/layout/Sidebar.vue
index 7089c676..2ba36f70 100644
--- a/app/app/components/layout/Sidebar.vue
+++ b/app/app/components/layout/Sidebar.vue
@@ -4,7 +4,7 @@ import type { WalletId } from '~/components/wallets/types'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
import { compareCategoryIds } from '~/components/categories/utils'
-import { useWalletContextMenu } from '~/components/wallets/useWalletContextMenu'
+import { useWalletMenuItems } from '~/components/wallets/useWalletMenuItems'
import { useWalletsStore } from '~/components/wallets/useWalletsStore'
const props = defineProps<{
@@ -21,7 +21,15 @@ const walletsStore = useWalletsStore()
const categoriesStore = useCategoriesStore()
const isShowLogoMenu = ref(false)
-const { getWalletContextMenuItems } = useWalletContextMenu({ excludeOpen: true, withCreditView: true })
+const walletMenu = useWalletMenuItems()
+
+function getWalletContextMenuItems(walletId: WalletId) {
+ const cv = walletMenu.creditView(walletId)
+ return [
+ ...(cv ? [[cv]] : []),
+ [walletMenu.edit(walletId)],
+ ]
+}
type SidebarTab = 'categories' | 'wallets'
const activeTab = ref<SidebarTab>('wallets')
diff --git a/app/app/components/trnForm/SelectionCategoriesFast.vue b/app/app/components/trnForm/SelectionCategoriesFast.vue
index 271431cd..b2e60bec 100644
--- a/app/app/components/trnForm/SelectionCategoriesFast.vue
+++ b/app/app/components/trnForm/SelectionCategoriesFast.vue
@@ -4,7 +4,7 @@ import { useWindowSize } from '@vueuse/core'
import type { CategoryId } from '~/components/categories/types'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
-import { useCategoryContextMenu } from '~/components/categories/useCategoryContextMenu'
+import { isMenuableCategory, useCategoryMenuItems } from '~/components/categories/useCategoryMenuItems'
import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
import { showErrorToast, showSuccessToast } from '~/composables/useStoreSync'
@@ -81,26 +81,20 @@ async function onDeleteConfirm() {
}, 300)
}
-const { getCategoryContextMenuItems: getLaptopMenuItems } = useCategoryContextMenu({
- excludeOpen: true,
- onDelete: onClickDelete,
- returnBackOnSave: true,
- withQuickToggles: true,
-})
-
-const { getCategoryContextMenuItems: getMobileMenuItems } = useCategoryContextMenu({
- excludeOpen: true,
- onDelete: onClickDelete,
- onEdit: (id) => {
- editingCategoryId.value = id
- },
- withQuickToggles: true,
-})
+const categoryMenu = useCategoryMenuItems()
function getCategoryContextMenuItems(categoryId: CategoryId) {
- return isLaptop.value
- ? getLaptopMenuItems(categoryId)
- : getMobileMenuItems(categoryId)
+ if (!isMenuableCategory(categoryId))
+ return undefined
+ const editOpts = isLaptop.value
+ ? { returnBack: true }
+ : { onEdit: (id: CategoryId) => { editingCategoryId.value = id } }
+ const qt = categoryMenu.quickToggles(categoryId)
+ return [
+ [categoryMenu.edit(categoryId, editOpts)],
+ ...(qt ? [qt] : []),
+ [categoryMenu.delete(categoryId, onClickDelete)],
+ ]
}
const hasFavorites = computed(() => categoriesStore.favoriteCategoriesIds.length > 0)
diff --git a/app/app/components/wallets/Selector.vue b/app/app/components/wallets/Selector.vue
index dc55bea6..b4c3e38c 100644
--- a/app/app/components/wallets/Selector.vue
+++ b/app/app/components/wallets/Selector.vue
@@ -5,8 +5,8 @@ import type { CurrencyCode } from '~/components/currencies/types'
import type { WalletId } from '~/components/wallets/types'
import { WALLET_STORAGE_KEYS } from '~/components/wallets/constants'
-import { useWalletContextMenu } from '~/components/wallets/useWalletContextMenu'
import { useWalletDelete } from '~/components/wallets/useWalletDelete'
+import { useWalletMenuItems } from '~/components/wallets/useWalletMenuItems'
import { useWalletsStore } from '~/components/wallets/useWalletsStore'
const props = defineProps<{
@@ -64,26 +64,18 @@ const hasNoMatches = computed(() =>
!!searchQuery.value && selectedWalletsIdsWithCurrency.value.length === 0,
)
-const { getWalletContextMenuItems: getLaptopMenuItems } = useWalletContextMenu({
- excludeOpen: true,
- onDelete: requestDelete,
- returnBackOnSave: true,
-})
-
-const { getWalletContextMenuItems: getMobileMenuItems } = useWalletContextMenu({
- excludeOpen: true,
- onDelete: requestDelete,
- onEdit: (id) => {
- editingWalletId.value = id
- },
-})
+const walletMenu = useWalletMenuItems()
function getWalletContextMenuItems(walletId: WalletId) {
if (!props.withHeader)
return undefined
- return isLaptop.value
- ? getLaptopMenuItems(walletId)
- : getMobileMenuItems(walletId)
+ const editOpts = isLaptop.value
+ ? { returnBack: true }
+ : { onEdit: (id: WalletId) => { editingWalletId.value = id } }
+ return [
+ [walletMenu.edit(walletId, editOpts)],
+ [walletMenu.delete(walletId, requestDelete)],
+ ]
}
function onClickWallet(walletId: WalletId) {
diff --git a/app/app/components/wallets/page/ListItem.vue b/app/app/components/wallets/page/ListItem.vue
index f1954bdb..ae826610 100644
--- a/app/app/components/wallets/page/ListItem.vue
+++ b/app/app/components/wallets/page/ListItem.vue
@@ -1,7 +1,7 @@
<script setup lang="ts">
import type { WalletId } from '~/components/wallets/types'
-import { useWalletContextMenu } from '~/components/wallets/useWalletContextMenu'
+import { useWalletMenuItems } from '~/components/wallets/useWalletMenuItems'
import { useWalletsStore } from '~/components/wallets/useWalletsStore'
const props = defineProps<{
@@ -13,11 +13,15 @@ const emit = defineEmits<{
}>()
const walletsStore = useWalletsStore()
+const m = useWalletMenuItems()
-const { getWalletContextMenuItems } = useWalletContextMenu({
- excludeOpen: true,
- onDelete: (id: WalletId) => emit('delete', id),
- withCreditView: true,
+const contextMenuItems = computed(() => {
+ const cv = m.creditView(props.walletId)
+ return [
+ ...(cv ? [[cv]] : []),
+ [m.edit(props.walletId)],
+ [m.delete(props.walletId, id => emit('delete', id))],
+ ]
})
</script>
@@ -25,7 +29,7 @@ const { getWalletContextMenuItems } = useWalletContextMenu({
<WalletsItem
:wallet="walletsStore.itemsComputed[props.walletId]!"
:walletId="props.walletId"
- :contextMenuItems="getWalletContextMenuItems(props.walletId)"
+ :contextMenuItems="contextMenuItems"
:lineWidth="2"
class="group"
isShowBaseRate
diff --git a/app/app/components/wallets/useWalletContextMenu.ts b/app/app/components/wallets/useWalletContextMenu.ts
deleted file mode 100644
index 9ccfc559..00000000
--- a/app/app/components/wallets/useWalletContextMenu.ts
+++ /dev/null
@@ -1,66 +0,0 @@
-import type { WalletId } from '~/components/wallets/types'
-
-import { useWalletCreditView } from '~/components/wallets/useWalletCreditView'
-import { useWalletsStore } from '~/components/wallets/useWalletsStore'
-
-export function useWalletContextMenu(options?: {
- excludeOpen?: boolean
- onDelete?: (walletId: WalletId) => void
- onEdit?: (walletId: WalletId) => void
- returnBackOnSave?: boolean
- withCreditView?: boolean
-}) {
- const { t } = useI18n()
- const router = useRouter()
- const walletsStore = useWalletsStore()
-
- function getWalletContextMenuItems(walletId: WalletId) {
- const editPath = options?.returnBackOnSave
- ? `/wallets/${walletId}/edit?returnBack=1`
- : `/wallets/${walletId}/edit`
-
- const editGroup = [
- ...(!options?.excludeOpen
- ? [{
- icon: 'lucide:square-arrow-out-up-right',
- label: t('base.open'),
- onSelect: () => router.push(`/wallets/${walletId}`),
- }]
- : []),
- {
- icon: 'lucide:pencil',
- label: t('base.edit'),
- onSelect: () => options?.onEdit
- ? options.onEdit(walletId)
- : router.push(editPath),
- },
- ]
-
- const groups: any[][] = [editGroup]
-
- const wallet = walletsStore.itemsComputed[walletId]
- if (options?.withCreditView && wallet?.type === 'credit' && wallet.creditLimit > 0) {
- const { cycle, view } = useWalletCreditView(walletId)
- groups.unshift([{
- icon: view.value === 'debt' ? 'lucide:wallet' : 'lucide:credit-card',
- label: view.value === 'debt'
- ? t('wallets.creditView.showAvailable')
- : t('wallets.creditView.showDebt'),
- onSelect: cycle,
- }])
- }
-
- if (options?.onDelete) {
- groups.push([{
- color: 'error' as const,
- icon: 'lucide:trash-2',
- label: t('base.delete'),
- onSelect: () => options.onDelete!(walletId),
- }])
- }
-
- return groups
- }
-
- return { getWalletContextMenuItems }
-}
diff --git a/app/app/components/wallets/useWalletMenuItems.ts b/app/app/components/wallets/useWalletMenuItems.ts
new file mode 100644
index 00000000..6482c3b3
--- /dev/null
+++ b/app/app/components/wallets/useWalletMenuItems.ts
@@ -0,0 +1,57 @@
+import type { WalletId } from '~/components/wallets/types'
+
+import { useWalletCreditView } from '~/components/wallets/useWalletCreditView'
+import { useWalletsStore } from '~/components/wallets/useWalletsStore'
+
+type Item = { color?: 'error', icon: string, label: string, onSelect: () => void }
+
+export function useWalletMenuItems() {
+ const { t } = useI18n()
+ const router = useRouter()
+ const walletsStore = useWalletsStore()
+
+ return {
+ creditView(walletId: WalletId): Item | null {
+ const wallet = walletsStore.itemsComputed[walletId]
+ if (!wallet || wallet.type !== 'credit' || wallet.creditLimit <= 0)
+ return null
+ const { cycle, view } = useWalletCreditView(walletId)
+ return {
+ icon: view.value === 'debt' ? 'lucide:wallet' : 'lucide:credit-card',
+ label: view.value === 'debt'
+ ? t('wallets.creditView.showAvailable')
+ : t('wallets.creditView.showDebt'),
+ onSelect: cycle,
+ }
+ },
+
+ delete(walletId: WalletId, onDelete: (id: WalletId) => void): Item {
+ return {
+ color: 'error',
+ icon: 'lucide:trash-2',
+ label: t('base.delete'),
+ onSelect: () => onDelete(walletId),
+ }
+ },
+
+ edit(walletId: WalletId, opts?: { onEdit?: (id: WalletId) => void, returnBack?: boolean }): Item {
+ return {
+ icon: 'lucide:pencil',
+ label: t('base.edit'),
+ onSelect: () => opts?.onEdit
+ ? opts.onEdit(walletId)
+ : router.push(opts?.returnBack
+ ? `/wallets/${walletId}/edit?returnBack=1`
+ : `/wallets/${walletId}/edit`),
+ }
+ },
+
+ open(walletId: WalletId): Item {
+ return {
+ icon: 'lucide:square-arrow-out-up-right',
+ label: t('base.open'),
+ onSelect: () => router.push(`/wallets/${walletId}`),
+ }
+ },
+ }
+}