Skip to content

Open source personal finance application

refactor: add wallet sorting to store, clean demo import

Ilya Komichevdc6aac7f129c
diff --git a/app/components/demo/useDemo.ts b/app/components/demo/useDemo.ts
index 746a3e2a..beaca254 100644
--- a/app/components/demo/useDemo.ts
+++ b/app/components/demo/useDemo.ts
@@ -9,7 +9,7 @@ import type { Wallets } from '~/components/wallets/types'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
import { useCurrenciesStore } from '~/components/currencies/useCurrenciesStore'
import currencies from '~/components/demo/currencies.json'
-import { data, expenseRules, incomeRules, oneOffExpenses, salaryConfig, transferRules, walletDebitRub } from '~/components/demo/data'
+import { data, expenseRules, incomeRules, oneOffExpenses, salaryConfig, transferRules } from '~/components/demo/data'
import { TrnType } from '~/components/trns/types'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
import { useUserStore } from '~/components/user/useUserStore'
diff --git a/app/components/wallets/useWalletsStore.ts b/app/components/wallets/useWalletsStore.ts
index 806dd6a7..e2c78b63 100644
--- a/app/components/wallets/useWalletsStore.ts
+++ b/app/components/wallets/useWalletsStore.ts
@@ -11,6 +11,7 @@ import { useDemo } from '~/components/demo/useDemo'
import { pushOfflineOp } from '~/components/offline/helpers'
import { isReplaying } from '~/components/offline/replay'
import { STORAGE_KEYS } from '~/components/offline/storageKeys'
+import { TrnType } from '~/components/trns/types'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
import { useUserStore } from '~/components/user/useUserStore'
import { createDebouncedPersist, handleMutationResult, mergeOfflineOps, pushDeleteOp, pushSaveOp } from '~/composables/useStoreSync'
@@ -155,6 +156,40 @@ export const useWalletsStore = defineStore('wallets', () => {
)
})
+ const recentWalletIds = computed<WalletId[]>(() => {
+ if (!hasItems.value || !trnsStore.hasItems)
+ return []
+
+ const trnsItems = trnsStore.items
+ const latestDateByWallet = new Map<WalletId, number>()
+
+ for (const trnId in trnsItems) {
+ const trn = trnsItems[trnId]
+ if (!trn)
+ continue
+
+ if (trn.type === TrnType.Transfer) {
+ const existing1 = latestDateByWallet.get(trn.expenseWalletId)
+ if (!existing1 || trn.date > existing1)
+ latestDateByWallet.set(trn.expenseWalletId, trn.date)
+
+ const existing2 = latestDateByWallet.get(trn.incomeWalletId)
+ if (!existing2 || trn.date > existing2)
+ latestDateByWallet.set(trn.incomeWalletId, trn.date)
+ }
+ else {
+ const existing = latestDateByWallet.get(trn.walletId)
+ if (!existing || trn.date > existing)
+ latestDateByWallet.set(trn.walletId, trn.date)
+ }
+ }
+
+ return [...latestDateByWallet.entries()]
+ .toSorted(([, a], [, b]) => b - a)
+ .map(([id]) => id)
+ .filter(id => items.value?.[id])
+ })
+
// O(N) single pass over all trns — replaces O(W×N) per-wallet getWalletTotal
const walletTotals = computed(() =>
getWalletsTotals({
@@ -219,6 +254,7 @@ export const useWalletsStore = defineStore('wallets', () => {
initWallets,
items,
itemsComputed,
+ recentWalletIds,
saveWallet,
saveWalletsOrder,
setWallets,