import { ref, computed } from 'vue'; import { useI18n } from '@/locales/helpers.ts'; import { useSettingsStore } from '@/stores/setting.ts'; import { useAccountsStore } from '@/stores/account.ts'; import { useTransactionsStore } from '@/stores/transaction.ts'; import { useTransactionCategoriesStore } from '@/stores/transactionCategory.ts'; import { useOverviewStore } from '@/stores/overview.ts'; import { useStatisticsStore } from '@/stores/statistics.ts'; import type { NameValue, TypeAndDisplayName } from '@/core/base.ts'; import type { LocalizedTimezoneInfo } from '@/core/timezone.ts'; import { CategoryType } from '@/core/category.ts'; import type { Account } from '@/models/account.ts'; import { isObjectEmpty } from '@/lib/common.ts'; export function useAppSettingPageBase() { const { tt, getAllTimezones, getAllTimezoneTypesUsedForStatistics, getAllCurrencySortingTypes, setTimeZone } = useI18n(); const settingsStore = useSettingsStore(); const accountsStore = useAccountsStore(); const transactionsStore = useTransactionsStore(); const transactionCategoriesStore = useTransactionCategoriesStore(); const overviewStore = useOverviewStore(); const statisticsStore = useStatisticsStore(); const loadingAccounts = ref(false); const loadingTransactionCategories = ref(false); const allThemes = computed(() => { return [ { name: tt('System Default'), value: 'auto' }, { name: tt('Light'), value: 'light' }, { name: tt('Dark'), value: 'dark' } ]; }); const allTimezones = computed(() => getAllTimezones(true)); const allTimezoneTypesUsedForStatistics = computed(() => getAllTimezoneTypesUsedForStatistics()); const allCurrencySortingTypes = computed(() => getAllCurrencySortingTypes()); const allAutoSaveTransactionDraftTypes = computed(() => { return [ { name: tt('Disabled'), value: 'disabled' }, { name: tt('Enabled'), value: 'enabled' }, { name: tt('Show Confirmation Every Time'), value: 'confirmation' } ]; }); const hasAnyAccount = computed(() => accountsStore.allPlainAccounts.length > 0); const hasAnyVisibleAccount = computed(() => accountsStore.allVisibleAccountsCount > 0); const hasAnyTransactionCategory = computed(() => !isObjectEmpty(transactionCategoriesStore.allTransactionCategoriesMap)); const timeZone = computed({ get: () => settingsStore.appSettings.timeZone, set: (value) => { settingsStore.setTimeZone(value); setTimeZone(value); transactionsStore.updateTransactionListInvalidState(true); overviewStore.updateTransactionOverviewInvalidState(true); statisticsStore.updateTransactionStatisticsInvalidState(true); } }); const isAutoUpdateExchangeRatesData = computed({ get: () => settingsStore.appSettings.autoUpdateExchangeRatesData, set: (value) => settingsStore.setAutoUpdateExchangeRatesData(value) }); const showAccountBalance = computed({ get: () => settingsStore.appSettings.showAccountBalance, set: (value) => settingsStore.setShowAccountBalance(value) }); const showAmountInHomePage = computed({ get: () => settingsStore.appSettings.showAmountInHomePage, set: (value) => settingsStore.setShowAmountInHomePage(value) }); const timezoneUsedForStatisticsInHomePage = computed({ get: () => settingsStore.appSettings.timezoneUsedForStatisticsInHomePage, set: (value: number) => { settingsStore.setTimezoneUsedForStatisticsInHomePage(value); overviewStore.updateTransactionOverviewInvalidState(true); } }); const showTotalAmountInTransactionListPage = computed({ get: () => settingsStore.appSettings.showTotalAmountInTransactionListPage, set: (value) => settingsStore.setShowTotalAmountInTransactionListPage(value) }); const showTagInTransactionListPage = computed({ get: () => settingsStore.appSettings.showTagInTransactionListPage, set: (value) => settingsStore.setShowTagInTransactionListPage(value) }); const itemsCountInTransactionListPage = computed({ get: () => settingsStore.appSettings.itemsCountInTransactionListPage, set: (value) => settingsStore.setItemsCountInTransactionListPage(value) }); const autoSaveTransactionDraft = computed({ get: () => settingsStore.appSettings.autoSaveTransactionDraft, set: (value: string) => { settingsStore.setAutoSaveTransactionDraft(value); if (value === 'disabled') { transactionsStore.clearTransactionDraft(); } } }); const isAutoGetCurrentGeoLocation = computed({ get: () => settingsStore.appSettings.autoGetCurrentGeoLocation, set: (value) => settingsStore.setAutoGetCurrentGeoLocation(value) }); const currencySortByInExchangeRatesPage = computed({ get: () => settingsStore.appSettings.currencySortByInExchangeRatesPage, set: (value: number) => settingsStore.setCurrencySortByInExchangeRatesPage(value) }); const accountsIncludedInHomePageOverviewDisplayContent = computed(() => { const excludeAccountIds = settingsStore.appSettings.overviewAccountFilterInHomePage; return getIncludedAccountsDisplayContent(excludeAccountIds, accountsStore.allPlainAccounts); }); const accountsIncludedInTotalDisplayContent = computed(() => { const excludeAccountIds = settingsStore.appSettings.totalAmountExcludeAccountIds; return getIncludedAccountsDisplayContent(excludeAccountIds, accountsStore.allVisiblePlainAccounts); }); const transactionCategoriesIncludedInHomePageOverviewDisplayContent = computed(() => { const excludeAccountIds = settingsStore.appSettings.overviewTransactionCategoryFilterInHomePage; return getIncludedTransactionCategoriesDisplayContent(excludeAccountIds); }); function getIncludedAccountsDisplayContent(excludeAccountIds: Record, allAccounts: Account[]): string { if (loadingAccounts.value || !allAccounts || !allAccounts.length) { return ''; } let hasExcludeAccount = false; for (const accountId in excludeAccountIds) { if (!Object.prototype.hasOwnProperty.call(excludeAccountIds, accountId)) { continue; } if (excludeAccountIds[accountId] && accountsStore.allAccountsMap[accountId]) { hasExcludeAccount = true; break; } } if (!hasExcludeAccount) { return tt('All'); } let allAccountExcluded = true; for (let i = 0; i < allAccounts.length; i++) { const account = allAccounts[i]; if (!excludeAccountIds[account.id]) { allAccountExcluded = false; break; } } if (allAccountExcluded) { return tt('None'); } return tt('Partial'); } function getIncludedTransactionCategoriesDisplayContent(excludeTransactionCategoryIds: Record): string { if (loadingTransactionCategories.value || !transactionCategoriesStore.allTransactionCategoriesMap) { return ''; } let hasExcludeTransactionCategory = false; for (const transactionCategoryId in excludeTransactionCategoryIds) { if (!Object.prototype.hasOwnProperty.call(excludeTransactionCategoryIds, transactionCategoryId)) { continue; } if (excludeTransactionCategoryIds[transactionCategoryId] && transactionCategoriesStore.allTransactionCategoriesMap[transactionCategoryId]) { hasExcludeTransactionCategory = true; break; } } if (!hasExcludeTransactionCategory) { return tt('All'); } let allTransactionCategoryExcluded = true; for (const transactionCategoryId in transactionCategoriesStore.allTransactionCategoriesMap) { if (!Object.prototype.hasOwnProperty.call(transactionCategoriesStore.allTransactionCategoriesMap, transactionCategoryId)) { continue; } const transactionCategory = transactionCategoriesStore.allTransactionCategoriesMap[transactionCategoryId]; if (transactionCategory.type !== CategoryType.Income && transactionCategory.type !== CategoryType.Expense) { continue; } if (!excludeTransactionCategoryIds[transactionCategory.id]) { allTransactionCategoryExcluded = false; break; } } if (allTransactionCategoryExcluded) { return tt('None'); } return tt('Partial'); } return { // states loadingAccounts, loadingTransactionCategories, // computed states allThemes, allTimezones, allTimezoneTypesUsedForStatistics, allCurrencySortingTypes, allAutoSaveTransactionDraftTypes, timeZone, hasAnyAccount, hasAnyVisibleAccount, hasAnyTransactionCategory, isAutoUpdateExchangeRatesData, showAccountBalance, showAmountInHomePage, itemsCountInTransactionListPage, timezoneUsedForStatisticsInHomePage, showTotalAmountInTransactionListPage, showTagInTransactionListPage, autoSaveTransactionDraft, isAutoGetCurrentGeoLocation, currencySortByInExchangeRatesPage, accountsIncludedInHomePageOverviewDisplayContent, accountsIncludedInTotalDisplayContent, transactionCategoriesIncludedInHomePageOverviewDisplayContent }; }