A local-first personal finance app
make one month schedule upcoming length dynamic and add current month option (#4168)
Matt Fiddamancf360ad398a2
diff --git a/packages/desktop-client/src/components/schedules/UpcomingLength.tsx b/packages/desktop-client/src/components/schedules/UpcomingLength.tsxindex ef142dcf23..5195110eee 100644--- a/packages/desktop-client/src/components/schedules/UpcomingLength.tsx+++ b/packages/desktop-client/src/components/schedules/UpcomingLength.tsx@@ -19,7 +19,8 @@ function useUpcomingLengthOptions() {{ value: '1', label: t('1 day') },{ value: '7', label: t('1 week') },{ value: '14', label: t('2 weeks') },- { value: '30', label: t('1 month') },+ { value: 'oneMonth', label: t('1 month') },+ { value: 'currentMonth', label: t('end of the current month') },];return { upcomingLengthOptions };diff --git a/packages/loot-core/src/client/data-hooks/schedules.tsx b/packages/loot-core/src/client/data-hooks/schedules.tsxindex 03b95b42b3..5824f6bb30 100644--- a/packages/loot-core/src/client/data-hooks/schedules.tsx+++ b/packages/loot-core/src/client/data-hooks/schedules.tsx@@ -11,7 +11,11 @@ import React, {import { useSyncedPref } from '@actual-app/web/src/hooks/useSyncedPref';import { q, type Query } from '../../shared/query';-import { getHasTransactionsQuery, getStatus } from '../../shared/schedules';+import {+ getUpcomingDays,+ getHasTransactionsQuery,+ getStatus,+} from '../../shared/schedules';import {type TransactionEntity,type ScheduleEntity,@@ -27,7 +31,7 @@ function loadStatuses(schedules: readonly ScheduleEntity[],onData: (data: ScheduleStatuses) => void,onError: (error: Error) => void,- upcomingLength: string,+ upcomingLength: number,) {return liveQuery<TransactionEntity>(getHasTransactionsQuery(schedules), {onData: data => {@@ -73,6 +77,7 @@ export function useSchedules({statuses: new Map(),});const [upcomingLength] = useSyncedPref('upcomingScheduledTransactionLength');+ const upcomingDays = getUpcomingDays(upcomingLength);const scheduleQueryRef = useRef<LiveQuery<ScheduleEntity> | null>(null);const statusQueryRef = useRef<LiveQuery<TransactionEntity> | null>(null);@@ -111,7 +116,7 @@ export function useSchedules({}},onError,- upcomingLength,+ upcomingDays,);},onError,@@ -122,7 +127,7 @@ export function useSchedules({scheduleQueryRef.current?.unsubscribe();statusQueryRef.current?.unsubscribe();};- }, [query, upcomingLength]);+ }, [query, upcomingDays]);return {isLoading,diff --git a/packages/loot-core/src/client/data-hooks/transactions.ts b/packages/loot-core/src/client/data-hooks/transactions.tsindex a0336c9295..b2f2103c2b 100644--- a/packages/loot-core/src/client/data-hooks/transactions.ts+++ b/packages/loot-core/src/client/data-hooks/transactions.ts@@ -11,6 +11,7 @@ import {getScheduledAmount,extractScheduleConds,getNextDate,+ getUpcomingDays,} from '../../shared/schedules';import { ungroupTransactions } from '../../shared/transactions';import {@@ -152,16 +153,17 @@ export function usePreviewTransactions(): UsePreviewTransactionsResult {return [];}- const today = d.startOfDay(parseDate(currentDay()));- const upcomingPeriodEnd = d.startOfDay(- parseDate(addDays(today, parseInt(upcomingLength ?? '7'))),- );-// Kick off an async rules applicationconst schedulesForPreview = schedules.filter(s =>isForPreview(s, statuses),);+ const today = d.startOfDay(parseDate(currentDay()));++ const upcomingPeriodEnd = d.startOfDay(+ parseDate(addDays(today, getUpcomingDays(upcomingLength))),+ );+return schedulesForPreview.map(schedule => {const { date: dateConditions } = extractScheduleConds(diff --git a/packages/loot-core/src/shared/schedules.test.ts b/packages/loot-core/src/shared/schedules.test.tsindex 67f82e6ce8..ccabf7f5ec 100644--- a/packages/loot-core/src/shared/schedules.test.ts+++ b/packages/loot-core/src/shared/schedules.test.ts@@ -17,15 +17,15 @@ describe('schedules', () => {describe('getStatus', () => {it('returns completed if completed', () => {- expect(getStatus(todayString, true, false, '7')).toBe('completed');+ expect(getStatus(todayString, true, false, 7)).toBe('completed');});it('returns paid if has transactions', () => {- expect(getStatus(todayString, false, true, '7')).toBe('paid');+ expect(getStatus(todayString, false, true, 7)).toBe('paid');});it('returns due if today', () => {- expect(getStatus(todayString, false, false, '7')).toBe('due');+ expect(getStatus(todayString, false, false, 7)).toBe('due');});it.each([1, 7, 14, 30])(@@ -35,26 +35,26 @@ describe('schedules', () => {const tomorrow = monthUtils.addDays(today, 1);const upcomingDate = monthUtils.addDays(today, daysOut);const scheduledDate = monthUtils.addDays(today, daysOut + 1);- expect(- getStatus(tomorrow, false, false, upcomingLength.toString()),- ).toBe('upcoming');- expect(- getStatus(upcomingDate, false, false, upcomingLength.toString()),- ).toBe('upcoming');- expect(- getStatus(scheduledDate, false, false, upcomingLength.toString()),- ).toBe('scheduled');+ expect(getStatus(tomorrow, false, false, upcomingLength)).toBe(+ 'upcoming',+ );+ expect(getStatus(upcomingDate, false, false, upcomingLength)).toBe(+ 'upcoming',+ );+ expect(getStatus(scheduledDate, false, false, upcomingLength)).toBe(+ 'scheduled',+ );},);it('returns missed if past', () => {- expect(getStatus(monthUtils.addDays(today, -1), false, false, '7')).toBe(+ expect(getStatus(monthUtils.addDays(today, -1), false, false, 7)).toBe('missed',);});it('returns scheduled if not due, upcoming, or missed', () => {- expect(getStatus(monthUtils.addDays(today, 8), false, false, '7')).toBe(+ expect(getStatus(monthUtils.addDays(today, 8), false, false, 7)).toBe('scheduled',);});diff --git a/packages/loot-core/src/shared/schedules.ts b/packages/loot-core/src/shared/schedules.tsindex faa1cd455b..d6afb9cb0c 100644--- a/packages/loot-core/src/shared/schedules.ts+++ b/packages/loot-core/src/shared/schedules.ts@@ -11,7 +11,7 @@ export function getStatus(nextDate: string,completed: boolean,hasTrans: boolean,- upcomingLength: string,+ upcomingLength: number,) {const today = monthUtils.currentDay();if (completed) {@@ -22,7 +22,7 @@ export function getStatus(return 'due';} else if (nextDate > today &&- nextDate <= monthUtils.addDays(today, parseInt(upcomingLength ?? '7'))+ nextDate <= monthUtils.addDays(today, upcomingLength)) {return 'upcoming';} else if (nextDate < today) {@@ -348,3 +348,26 @@ export function describeSchedule(schedule, payee) {return `Next: ${schedule.next_date}`;}}++export function getUpcomingDays(upcomingLength = '7') {+ const today = monthUtils.currentDay();++ switch (upcomingLength) {+ case 'currentMonth': {+ const day = monthUtils.getDay(today);+ const end = monthUtils.getDay(monthUtils.getMonthEnd(today));+ return end - day + 1;+ }+ case 'oneMonth': {+ const month = monthUtils.getMonth(today);+ return (+ monthUtils.differenceInCalendarDays(+ monthUtils.nextMonth(month),+ month,+ ) + 1+ );+ }+ default:+ return parseInt(upcomingLength, 10);+ }+}diff --git a/upcoming-release-notes/4168.md b/upcoming-release-notes/4168.mdnew file mode 100644index 0000000000..ac796ad5bd--- /dev/null+++ b/upcoming-release-notes/4168.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [matt-fidd]+---++Improve one month schedule upcoming length and introduce current month option