Skip to content

Open source personal finance application

feat: add stat/UI utility components (NumberStepper, ContextMenu, ListGroupSum)

Ilya Komichev946135955391
diff --git a/app/components/stat/categories/useCategoryLongPress.ts b/app/components/stat/categories/useCategoryLongPress.ts
new file mode 100644
index 00000000..10eb45f0
--- /dev/null
+++ b/app/components/stat/categories/useCategoryLongPress.ts
@@ -0,0 +1,52 @@
+import type { CategoryId } from '~/components/categories/types'
+
+import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
+import { statDateKey } from '~/components/stat/injectionKeys'
+import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'
+
+export function useCategoryLongPress(
+ categoryId: () => CategoryId,
+ onShortPress: () => void,
+) {
+ const statDate = inject(statDateKey)!
+ const categoriesStore = useCategoriesStore()
+ const trnsFormStore = useTrnsFormStore()
+
+ const longPressRef = ref(null)
+
+ onLongPress(
+ longPressRef,
+ () => {
+ const isTransactible = categoriesStore.isTransactible(categoryId())
+ if (!isTransactible)
+ return
+
+ trnsFormStore.openFormForCreate()
+ trnsFormStore.$patch((state) => {
+ state.values.amount = [0, 0, 0]
+ state.values.amountRaw = ['', '', '']
+ state.values.categoryId = categoryId()
+ state.ui.isShow = true
+
+ const isDayDate = statDate.params.value.intervalSelected !== -1 && statDate.params.value.intervalsBy === 'day'
+ if (isDayDate && statDate.selectedInterval.value?.start) {
+ state.values.date = statDate.selectedInterval.value!.start
+ }
+ else {
+ state.values.date = Date.now()
+ }
+ })
+ },
+ {
+ onMouseUp: (duration: number, distance: number, isLongPress: boolean) => {
+ if (!isLongPress && distance < 100) {
+ onShortPress()
+ }
+ },
+ },
+ )
+
+ return {
+ longPressRef,
+ }
+}
diff --git a/app/components/stat/date/Selector.vue b/app/components/stat/date/Selector.vue
index 620c57ca..b93547db 100644
--- a/app/components/stat/date/Selector.vue
+++ b/app/components/stat/date/Selector.vue
@@ -39,7 +39,7 @@ const dateRange = ref({
start: parseUCalendarDate(statDate.range.value.start),
})
-function onSelectRange(value: any) {
+function onSelectRange(value: { end: unknown, start: unknown }) {
const range = {
end: value.end,
start: value.start,
diff --git a/app/components/stat/useStatPage.ts b/app/components/stat/useStatPage.ts
new file mode 100644
index 00000000..026d4881
--- /dev/null
+++ b/app/components/stat/useStatPage.ts
@@ -0,0 +1,70 @@
+import type { DeepPartial } from '~~/utils/types'
+
+import { useStorage } from '@vueuse/core'
+
+import type { FilterProvider } from '~/components/stat/filter/types'
+import type { StatTabSlug } from '~/components/stat/types'
+import type { MiniItemConfig } from '~/components/stat/useStatConfig'
+import type { TrnsGetterProps } from '~/components/trns/types'
+
+import { useStatDate } from '~/components/date/useStatDate'
+import { useFilter } from '~/components/stat/filter/useFilter'
+import { filterKey, statConfigKey, statDateKey } from '~/components/stat/injectionKeys'
+import { useStatConfig } from '~/components/stat/useStatConfig'
+import { useTrnsStore } from '~/components/trns/useTrnsStore'
+
+type UseStatPageOptions = {
+ getTrnsFilter: (ctx: {
+ activeTab: Ref<StatTabSlug>
+ filter: FilterProvider
+ }) => TrnsGetterProps
+ statConfigProps?: DeepPartial<MiniItemConfig>
+ storageKeyPrefix: string
+ syncEmptyCategoriesWithFilter?: boolean
+}
+
+export function useStatPage(options: UseStatPageOptions) {
+ const route = useRoute()
+ const trnsStore = useTrnsStore()
+
+ const filter = useFilter()
+ provide(filterKey, filter)
+
+ const activeTab = useStorage<StatTabSlug>(`${options.storageKeyPrefix}-tab`, 'summary')
+ const storageKey = computed(() => `${options.storageKeyPrefix}-${activeTab.value}`)
+
+ const trnsIds = computed(() =>
+ trnsStore.getStoreTrnsIds(options.getTrnsFilter({ activeTab, filter })),
+ )
+
+ const maxRange = computed(() => trnsStore.getRange(trnsIds.value))
+
+ const statConfig = useStatConfig({
+ props: options.statConfigProps,
+ storageKey: storageKey.value,
+ })
+ provide(statConfigKey, statConfig)
+
+ const statDate = useStatDate({
+ key: storageKey.value,
+ maxRange,
+ queryParams: route.query,
+ })
+ provide(statDateKey, statDate)
+
+ if (options.syncEmptyCategoriesWithFilter) {
+ watch(filter.categoriesIds, () => {
+ statConfig.config.value.isShowEmptyCategories = filter.categoriesIds.value.length > 0
+ })
+ }
+
+ return {
+ activeTab,
+ filter,
+ maxRange,
+ statConfig,
+ statDate,
+ storageKey,
+ trnsIds,
+ }
+}
diff --git a/app/components/trns/ListGroupSum.vue b/app/components/trns/ListGroupSum.vue
new file mode 100644
index 00000000..66d489b3
--- /dev/null
+++ b/app/components/trns/ListGroupSum.vue
@@ -0,0 +1,33 @@
+<script setup lang="ts">
+import { useCurrenciesStore } from '~/components/currencies/useCurrenciesStore'
+import { TrnType } from '~/components/trns/types'
+
+defineProps<{
+ expense: number
+ income: number
+}>()
+
+const currenciesStore = useCurrenciesStore()
+</script>
+
+<template>
+ <Amount
+ v-if="income !== 0"
+ :amount="income"
+ :currencyCode="currenciesStore.base"
+ :isShowBaseRate="false"
+ :type="TrnType.Income"
+ colorize="income"
+ variant="sm"
+ />
+
+ <Amount
+ v-if="expense !== 0"
+ :amount="expense"
+ :currencyCode="currenciesStore.base"
+ :isShowBaseRate="false"
+ :type="TrnType.Expense"
+ isShowMinus
+ variant="sm"
+ />
+</template>
diff --git a/app/components/ui/ContextMenuOptional.vue b/app/components/ui/ContextMenuOptional.vue
new file mode 100644
index 00000000..43f07ded
--- /dev/null
+++ b/app/components/ui/ContextMenuOptional.vue
@@ -0,0 +1,12 @@
+<script setup lang="ts">
+const props = defineProps<{
+ items?: any[][]
+}>()
+</script>
+
+<template>
+ <UContextMenu v-if="props.items" :items="props.items">
+ <slot />
+ </UContextMenu>
+ <slot v-else />
+</template>
diff --git a/app/components/ui/NumberStepper.vue b/app/components/ui/NumberStepper.vue
new file mode 100644
index 00000000..5333e042
--- /dev/null
+++ b/app/components/ui/NumberStepper.vue
@@ -0,0 +1,45 @@
+<script setup lang="ts">
+const props = defineProps<{
+ max?: number
+ min?: number
+ modelValue: number
+}>()
+
+const emit = defineEmits<{
+ 'update:modelValue': [value: number]
+}>()
+
+function update(value: number) {
+ if (props.min !== undefined && value < props.min)
+ return
+ if (props.max !== undefined && value > props.max)
+ return
+ emit('update:modelValue', value)
+}
+</script>
+
+<template>
+ <div class="flex gap-1">
+ <UiChipButton
+ :class="[{ '!hover:transparent opacity-30': props.min !== undefined && props.modelValue <= props.min }]"
+ @click="update(props.modelValue - 1)"
+ >
+ <Icon name="lucide:minus" />
+ </UiChipButton>
+
+ <FormInput
+ :modelValue="props.modelValue"
+ :min="props.min"
+ class="!w-16 max-w-24 !px-2 text-center"
+ type="number"
+ @update:modelValue="value => update(+value)"
+ />
+
+ <UiChipButton
+ :class="[{ '!hover:transparent opacity-30': props.max !== undefined && props.modelValue >= props.max }]"
+ @click="update(props.modelValue + 1)"
+ >
+ <Icon name="lucide:plus" />
+ </UiChipButton>
+ </div>
+</template>