Skip to content

Open source personal finance application

refactor: optimize template lookups, simplify conditions, use CSS breakpoints

Ilya Komichev4edf4a9c795e
diff --git a/app/components/categories/Form.vue b/app/components/categories/Form.vue
index 231b2086..2789595f 100644
--- a/app/components/categories/Form.vue
+++ b/app/components/categories/Form.vue
@@ -248,8 +248,8 @@ async function onSave() {
</FormElement>
<div
- v-for="iconGroup in icons"
- :key="JSON.stringify(iconGroup)"
+ v-for="(iconGroup, idx) in icons"
+ :key="idx"
class="flex flex-wrap gap-3 pb-8 last:placeholder:text-blue-400"
>
<div
diff --git a/app/components/color/Palette.vue b/app/components/color/Palette.vue
index 46aad2cf..38bd1343 100644
--- a/app/components/color/Palette.vue
+++ b/app/components/color/Palette.vue
@@ -18,13 +18,24 @@ const { t } = useI18n()
const categoriesStore = useCategoriesStore()
const walletsStore = useWalletsStore()
-function findCategoryIconByColor(color: string) {
- return categoriesStore.categoriesRootIds.map(id => categoriesStore.items[id]).find(c => c?.color === color)?.icon
-}
+const categoryIconByColor = computed(() => {
+ const map = new Map<string, string>()
+ for (const id of categoriesStore.categoriesRootIds) {
+ const cat = categoriesStore.items[id]
+ if (cat?.color && cat.icon)
+ map.set(cat.color, cat.icon)
+ }
+ return map
+})
-function findWalletWithColor(color: string) {
- return Object.values(walletsStore.items ?? {}).some(w => w?.color === color)
-}
+const walletColorsUsed = computed(() => {
+ const set = new Set<string>()
+ for (const w of Object.values(walletsStore.items ?? {})) {
+ if (w?.color)
+ set.add(w.color)
+ }
+ return set
+})
</script>
<template>
@@ -47,14 +58,14 @@ function findWalletWithColor(color: string) {
>
<div class="flex-center h-10" :style="{ background: color }">
<Icon
- v-if="props.isCategory && (findCategoryIconByColor(color) || color === props.activeColor)"
- :name="props.activeColor === color ? props.icon : findCategoryIconByColor(color)"
+ v-if="props.isCategory && (categoryIconByColor.has(color) || color === props.activeColor)"
+ :name="props.activeColor === color ? props.icon : categoryIconByColor.get(color)"
size="20"
class="text-icon-primary"
/>
<div
- v-if="props.isWallet && findWalletWithColor(color)"
+ v-if="props.isWallet && walletColorsUsed.has(color)"
class="bg-item-4 size-3 rounded-full"
/>
</div>
diff --git a/app/components/layout/Sidebar.vue b/app/components/layout/Sidebar.vue
index bcb5c622..d9b7fb31 100644
--- a/app/components/layout/Sidebar.vue
+++ b/app/components/layout/Sidebar.vue
@@ -17,7 +17,6 @@ const router = useRouter()
const userStore = useUserStore()
const { isDemo } = useDemo()
const { t } = useI18n()
-const { width } = useWindowSize()
const walletsStore = useWalletsStore()
const isShowLogoMenu = ref(false)
@@ -27,10 +26,9 @@ const { getWalletContextMenuItems } = useWalletContextMenu()
<template>
<aside
:class="{
- 'w-72': props.isShowSidebar && width >= 767,
- 'w-12': !props.isShowSidebar || width < 767,
+ 'md:w-72': props.isShowSidebar,
}"
- class="fixed inset-y-0 left-0 z-40 hidden h-dvh overflow-hidden transition-all duration-300 ease-in-out sm:block"
+ class="fixed inset-y-0 left-0 z-40 hidden h-dvh w-12 overflow-hidden transition-all duration-300 ease-in-out sm:block"
>
<div class="relative h-full overflow-hidden overflow-y-auto overscroll-contain">
<!-- Small menu -->
@@ -41,8 +39,8 @@ const { getWalletContextMenuItems } = useWalletContextMenu()
/>
<div
- v-if="width >= 768 && props.isShowSidebar"
- class="grid h-full content-start gap-8 overflow-hidden overflow-y-auto"
+ v-if="props.isShowSidebar"
+ class="hidden h-full content-start gap-8 overflow-hidden overflow-y-auto md:grid"
>
<div class="px-2 pt-5">
<BottomSheetOrDropdown
diff --git a/app/components/stat/categories/Section.vue b/app/components/stat/categories/Section.vue
index 0cfdd999..e0f9a273 100644
--- a/app/components/stat/categories/Section.vue
+++ b/app/components/stat/categories/Section.vue
@@ -25,6 +25,7 @@ const isExpanded = ref(false)
const roundCategories = computed(() => computeCategoriesWithData(props.selectedTrnsIds ?? [], statConfig.config.value.catsRound.isGrouped, props.preCategoriesIds))
const visibleCategories = computed(() => isExpanded.value ? roundCategories.value : roundCategories.value.slice(0, visibleCategoriesLimit))
+const filteredSet = computed(() => new Set(props.filteredCategoriesIds))
</script>
<template>
@@ -34,9 +35,9 @@ const visibleCategories = computed(() => isExpanded.value ? roundCategories.valu
:key="item.id"
:item="item"
:class="{
- 'opacity-60': props.filteredCategoriesIds?.length > 0 && !props.filteredCategoriesIds?.includes(item.id),
- 'opacity-50': !props.filteredCategoriesIds?.includes(item.id) && item.value === 0,
- '!border-(--ui-primary)': props.filteredCategoriesIds?.includes(item.id),
+ 'opacity-60': filteredSet.size > 0 && !filteredSet.has(item.id),
+ 'opacity-50': !filteredSet.has(item.id) && item.value === 0,
+ '!border-(--ui-primary)': filteredSet.has(item.id),
}"
class="transition-opacity"
isShowAmount
diff --git a/app/components/stat/categories/Section2.vue b/app/components/stat/categories/Section2.vue
index d00ff50b..61024777 100644
--- a/app/components/stat/categories/Section2.vue
+++ b/app/components/stat/categories/Section2.vue
@@ -235,14 +235,14 @@ function toggleRoot(id: CategoryId) {
:biggestCatNumber="linesBiggestCatNumber"
:lineWidth="statConfig.config.value.catsList.isLines ? 0 : 1"
class="grow"
- @click="(item.categories && item.categories?.length >= 1) ? toggleRoot(item.id) : emit('clickCategory', item.id)"
+ @click="item.categories?.length ? toggleRoot(item.id) : emit('clickCategory', item.id)"
/>
</div>
</template>
<!-- Inside -->
<div
- v-if="categoriesOpened[item.id]?.show && item.categories && item.categories?.length >= 1"
+ v-if="categoriesOpened[item.id]?.show && item.categories?.length"
class="mt-[-2px] ml-5 -translate-x-px pb-3 pl-3"
>
<div
diff --git a/app/components/wallets/page/List.vue b/app/components/wallets/page/List.vue
index 516168ce..29ec1faf 100644
--- a/app/components/wallets/page/List.vue
+++ b/app/components/wallets/page/List.vue
@@ -56,6 +56,10 @@ const {
typeGroupsStatus,
walletsToggledMap,
} = useWalletsGrouping(selectedWalletsIds, groupedBy)
+
+function hasGroups(groups: Record<string, unknown> | undefined) {
+ return groups ? Object.keys(groups).length > 0 : false
+}
</script>
<template>
@@ -221,7 +225,7 @@ const {
v-for="(content, groupPrimary) in groupedWalletsWithIds"
:key="groupPrimary"
:class="{
- 'bg-item-2 rounded-sm': Object.keys(content.groups ?? {}).length === 0,
+ 'bg-item-2 rounded-sm': !hasGroups(content.groups),
}"
:isShown="
walletsToggledMap[groupedBy]?.[groupPrimary]?.show ?? true
@@ -255,7 +259,7 @@ const {
</template>
<div
- v-if="Object.keys(content.groups ?? {}).length > 0"
+ v-if="hasGroups(content.groups)"
class="grid gap-2"
>
<UiToggleControlled
diff --git a/app/layouts/default.vue b/app/layouts/default.vue
index 6facbb1f..0d084220 100644
--- a/app/layouts/default.vue
+++ b/app/layouts/default.vue
@@ -21,7 +21,9 @@ const { loadDataFromCache, loadDataFromDB } = useInitApp()
const { width } = useWindowSize()
const isShowSidebar = useCookie('finapp.isShowSidebar', { default: () => true })
+const isOnboardedHint = useCookie('finapp.isOnboarded', { default: () => false })
const isOnboarded = computed(() => walletsStore.hasItems && categoriesStore.hasItems && trnsStore.hasItems)
+watch(isOnboarded, val => (isOnboardedHint.value = val))
const { error, status } = useAsyncData(
'app',
@@ -72,9 +74,9 @@ defineShortcuts({
<div
class="flex min-h-dvh flex-col transition-all duration-300 ease-in-out"
:class="{
- 'sm:pl-12': isOnboarded,
- 'md:pl-72': isOnboarded && isShowSidebar && width >= 767,
- 'md:pr-[360px]': isOnboarded && trnsFormStore.ui.isShow && width >= 767,
+ 'sm:pl-12': isOnboardedHint || isOnboarded,
+ 'md:pl-72': (isOnboardedHint || isOnboarded) && isShowSidebar,
+ 'md:pr-[360px]': isOnboarded && trnsFormStore.ui.isShow,
}"
style="margin-left: env(safe-area-inset-left)"
>