Open source personal finance application
refactor(wallets): move credit-view toggle from long-press into context menu
Ilya Komichev7b55bef55aaa
diff --git a/app/app/components/layout/Sidebar.vue b/app/app/components/layout/Sidebar.vueindex 44571364..0dbd6004 100644--- a/app/app/components/layout/Sidebar.vue+++ b/app/app/components/layout/Sidebar.vue@@ -21,7 +21,7 @@ const walletsStore = useWalletsStore()const categoriesStore = useCategoriesStore()const isShowLogoMenu = ref(false)-const { getWalletContextMenuItems } = useWalletContextMenu()+const { getWalletContextMenuItems } = useWalletContextMenu({ withCreditView: true })type SidebarTab = 'categories' | 'wallets'const activeTab = ref<SidebarTab>('wallets')diff --git a/app/app/components/ui/ContextMenuMy.vue b/app/app/components/ui/ContextMenuMy.vueindex 3ddbd1ee..afc6fbcc 100644--- a/app/app/components/ui/ContextMenuMy.vue+++ b/app/app/components/ui/ContextMenuMy.vue@@ -21,7 +21,7 @@ const props = withDefaults(defineProps<{}>(), {descriptionKey: 'description',labelKey: 'label',- modal: true,+ modal: false,portal: true,pressOpenDelay: 400,})@@ -43,8 +43,13 @@ onMounted(() => {function onOpen(v: boolean) {emit('update:open', v)- if (v)+ if (v) {closeOtherContextMenus(close)+ window.addEventListener('scroll', close, { capture: true, once: true, passive: true })+ }+ else {+ window.removeEventListener('scroll', close, { capture: true })+ }}const appConfig = useAppConfig()diff --git a/app/app/components/wallets/ItemBody.vue b/app/app/components/wallets/ItemBody.vueindex 0010a079..150c4072 100644--- a/app/app/components/wallets/ItemBody.vue+++ b/app/app/components/wallets/ItemBody.vue@@ -1,11 +1,9 @@<script setup lang="ts">-import { useStorage } from '@vueuse/core'-import type { WalletId, WalletItemComputed } from '~/components/wallets/types'import { useCurrenciesStore } from '~/components/currencies/useCurrenciesStore'-import { WALLET_STORAGE_KEYS } from '~/components/wallets/constants'import { getCreditAvailable } from '~/components/wallets/types'+import { useWalletCreditView } from '~/components/wallets/useWalletCreditView'const props = defineProps<{activeItemId?: WalletId | null@@ -38,8 +36,7 @@ const classes = computed(() => ([},]))-const creditViews = ['debt', 'summary'] as const-const activeCreditView = useStorage<typeof creditViews[number]>(`${WALLET_STORAGE_KEYS.creditViewPrefix}${props.walletId}`, 'debt')+const { view: activeCreditView } = useWalletCreditView(props.walletId)const walletCreditLimit = computed(() =>props.wallet.type === 'credit' ? props.wallet.creditLimit : 0,@@ -55,23 +52,11 @@ const creditAmount = computed(() => {return 0}})--const longPressRef = ref(null)-if (!props.isSort) {- onLongPress(- longPressRef,- () => {- const nextIndex = (creditViews.findIndex(i => i === activeCreditView.value) + 1) % creditViews.length- activeCreditView.value = creditViews[nextIndex]- },- )-}</script><template><UiElementv-if="wallet"- ref="longPressRef":isActive="activeItemId === props.walletId":insideClasses="`${props.insideClasses ?? ''} min-h-[46px]`":lineWidth="props.lineWidth"diff --git a/app/app/components/wallets/page/ListItem.vue b/app/app/components/wallets/page/ListItem.vueindex 7de6c85e..aae1df9f 100644--- a/app/app/components/wallets/page/ListItem.vue+++ b/app/app/components/wallets/page/ListItem.vue@@ -16,6 +16,7 @@ const walletsStore = useWalletsStore()const { getWalletContextMenuItems } = useWalletContextMenu({onDelete: (id: WalletId) => emit('delete', id),+ withCreditView: true,})</script>diff --git a/app/app/components/wallets/useWalletContextMenu.ts b/app/app/components/wallets/useWalletContextMenu.tsindex 398ca5da..9ccfc559 100644--- a/app/app/components/wallets/useWalletContextMenu.ts+++ b/app/app/components/wallets/useWalletContextMenu.ts@@ -1,13 +1,18 @@import type { WalletId } from '~/components/wallets/types'+import { useWalletCreditView } from '~/components/wallets/useWalletCreditView'+import { useWalletsStore } from '~/components/wallets/useWalletsStore'+export function useWalletContextMenu(options?: {excludeOpen?: booleanonDelete?: (walletId: WalletId) => voidonEdit?: (walletId: WalletId) => voidreturnBackOnSave?: boolean+ withCreditView?: boolean}) {const { t } = useI18n()const router = useRouter()+ const walletsStore = useWalletsStore()function getWalletContextMenuItems(walletId: WalletId) {const editPath = options?.returnBackOnSave@@ -33,6 +38,18 @@ export function useWalletContextMenu(options?: {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,diff --git a/app/app/components/wallets/useWalletCreditView.ts b/app/app/components/wallets/useWalletCreditView.tsnew file mode 100644index 00000000..52023dfd--- /dev/null+++ b/app/app/components/wallets/useWalletCreditView.ts@@ -0,0 +1,22 @@+import { useStorage } from '@vueuse/core'++import type { WalletId } from '~/components/wallets/types'++import { WALLET_STORAGE_KEYS } from '~/components/wallets/constants'++export const CREDIT_VIEWS = ['debt', 'summary'] as const+export type CreditView = typeof CREDIT_VIEWS[number]++export function useWalletCreditView(walletId: WalletId) {+ const view = useStorage<CreditView>(+ `${WALLET_STORAGE_KEYS.creditViewPrefix}${walletId}`,+ 'debt',+ )++ function cycle() {+ const i = CREDIT_VIEWS.findIndex(v => v === view.value)+ view.value = CREDIT_VIEWS[(i + 1) % CREDIT_VIEWS.length]!+ }++ return { cycle, view }+}diff --git a/app/i18n/locales/en-US.js b/app/i18n/locales/en-US.jsindex 96a314c1..0348e31a 100644--- a/app/i18n/locales/en-US.js+++ b/app/i18n/locales/en-US.js@@ -468,6 +468,10 @@ export default {wallets: {ariaSwitch: 'Switch wallets',createNewTitle: 'New wallet',+ creditView: {+ showAvailable: 'Show available credit',+ showDebt: 'Show current debt',+ },errors: {deleteFailed: 'Failed to delete wallet',orderFailed: 'Failed to save wallet order',diff --git a/app/i18n/locales/ru-RU.js b/app/i18n/locales/ru-RU.jsindex 3bfeb07d..c00d0c94 100644--- a/app/i18n/locales/ru-RU.js+++ b/app/i18n/locales/ru-RU.js@@ -468,6 +468,10 @@ export default {wallets: {ariaSwitch: 'Поменять кошельки',createNewTitle: 'Добавление кошелька',+ creditView: {+ showAvailable: 'Показать доступный лимит',+ showDebt: 'Показать текущий долг',+ },errors: {deleteFailed: 'Не удалось удалить кошелек',orderFailed: 'Не удалось сохранить порядок кошельков',