Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/desktop-client/src/components/reports/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type HeaderProps = {
show1Month?: boolean;
allMonths: Array<{ name: string; pretty: string }>;
earliestTransaction: string;
latestTransaction: string;
firstDayOfWeekIdx?: SyncedPrefs['firstDayOfWeekIdx'];
onChangeDates: (
start: TimeFrame['start'],
Expand All @@ -59,6 +60,7 @@ export function Header({
show1Month,
allMonths,
earliestTransaction,
latestTransaction,
firstDayOfWeekIdx,
onChangeDates,
filters,
Expand Down Expand Up @@ -129,6 +131,7 @@ export function Header({
onChangeDates(
...validateStart(
allMonths[allMonths.length - 1].name,
allMonths[0].name,
newValue,
end,
),
Expand All @@ -144,6 +147,7 @@ export function Header({
onChangeDates(
...validateEnd(
allMonths[allMonths.length - 1].name,
allMonths[0].name,
start,
newValue,
),
Expand Down Expand Up @@ -191,6 +195,7 @@ export function Header({
...getLiveRange(
'Year to date',
earliestTransaction,
latestTransaction,
true,
firstDayOfWeekIdx,
),
Expand All @@ -209,6 +214,7 @@ export function Header({
...getLiveRange(
'Last year',
earliestTransaction,
latestTransaction,
false,
firstDayOfWeekIdx,
),
Expand All @@ -227,6 +233,7 @@ export function Header({
...getLiveRange(
'Prior year to date',
earliestTransaction,
latestTransaction,
false,
firstDayOfWeekIdx,
),
Expand All @@ -241,7 +248,10 @@ export function Header({
variant="bare"
onPress={() =>
onChangeDates(
...getFullRange(allMonths[allMonths.length - 1].name),
...getFullRange(
allMonths[allMonths.length - 1].name,
allMonths[0].name,
),
)
}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type ReportSidebarProps = {
defaultItems: (item: string) => void;
defaultModeItems: (graph: string, item: string) => void;
earliestTransaction: string;
latestTransaction: string;
firstDayOfWeekIdx: SyncedPrefs['firstDayOfWeekIdx'];
isComplexCategoryCondition?: boolean;
};
Expand Down Expand Up @@ -93,6 +94,7 @@ export function ReportSidebar({
defaultItems,
defaultModeItems,
earliestTransaction,
latestTransaction,
firstDayOfWeekIdx,
isComplexCategoryCondition = false,
}: ReportSidebarProps) {
Expand All @@ -110,6 +112,7 @@ export function ReportSidebar({
...getLiveRange(
cond,
earliestTransaction,
latestTransaction,
customReportItems.includeCurrentInterval,
firstDayOfWeekIdx,
),
Expand Down Expand Up @@ -545,6 +548,7 @@ export function ReportSidebar({
onChangeDates(
...validateStart(
earliestTransaction,
latestTransaction,
newValue,
customReportItems.endDate,
customReportItems.interval,
Expand Down Expand Up @@ -578,6 +582,7 @@ export function ReportSidebar({
onChangeDates(
...validateEnd(
earliestTransaction,
latestTransaction,
customReportItems.startDate,
newValue,
customReportItems.interval,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ import { getSpecificRange, validateRange } from './reportRanges';
export function getLiveRange(
cond: string,
earliestTransaction: string,
latestTransaction: string,
includeCurrentInterval: boolean,
firstDayOfWeekIdx?: SyncedPrefs['firstDayOfWeekIdx'],
): [string, string, TimeFrame['mode']] {
let dateStart = earliestTransaction;
let dateEnd = monthUtils.currentDay();
let dateEnd = latestTransaction;
const rangeName = ReportOptions.dateRangeMap.get(cond);
switch (rangeName) {
case 'yearToDate':
[dateStart, dateEnd] = validateRange(
earliestTransaction,
latestTransaction,
monthUtils.getYearStart(monthUtils.currentMonth()) + '-01',
monthUtils.currentDay(),
);
break;
case 'lastYear':
[dateStart, dateEnd] = validateRange(
earliestTransaction,
latestTransaction,
monthUtils.getYearStart(
monthUtils.prevYear(monthUtils.currentMonth()),
) + '-01',
Expand All @@ -35,6 +38,7 @@ export function getLiveRange(
case 'priorYearToDate':
[dateStart, dateEnd] = validateRange(
earliestTransaction,
latestTransaction,
monthUtils.getYearStart(
monthUtils.prevYear(monthUtils.currentMonth()),
) + '-01',
Expand All @@ -43,7 +47,7 @@ export function getLiveRange(
break;
case 'allTime':
dateStart = earliestTransaction;
dateEnd = monthUtils.currentDay();
dateEnd = latestTransaction;
break;
default:
if (typeof rangeName === 'number') {
Expand Down
20 changes: 13 additions & 7 deletions packages/desktop-client/src/components/reports/reportRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type SyncedPrefs } from 'loot-core/types/prefs';

export function validateStart(
earliest: string,
latest: string,
start: string,
end: string,
interval?: string,
Expand Down Expand Up @@ -35,6 +36,7 @@ export function validateStart(
}
return boundedRange(
earliest,
latest,
dateStart,
interval ? end : monthUtils.monthFromDate(end),
interval,
Expand All @@ -44,6 +46,7 @@ export function validateStart(

export function validateEnd(
earliest: string,
latest: string,
start: string,
end: string,
interval?: string,
Expand Down Expand Up @@ -75,15 +78,20 @@ export function validateEnd(
}
return boundedRange(
earliest,
latest,
interval ? start : monthUtils.monthFromDate(start),
dateEnd,
interval,
firstDayOfWeekIdx,
);
}

export function validateRange(earliest: string, start: string, end: string) {
const latest = monthUtils.currentDay();
export function validateRange(
earliest: string,
latest: string,
start: string,
end: string,
) {
if (end > latest) {
end = latest;
}
Expand All @@ -95,12 +103,12 @@ export function validateRange(earliest: string, start: string, end: string) {

function boundedRange(
earliest: string,
latest: string,
start: string,
end: string,
interval?: string,
firstDayOfWeekIdx?: SyncedPrefs['firstDayOfWeekIdx'],
): [string, string, 'static'] {
let latest: string;
switch (interval) {
case 'Daily':
latest = monthUtils.currentDay();
Expand All @@ -115,7 +123,6 @@ function boundedRange(
latest = monthUtils.currentDay();
break;
default:
latest = monthUtils.currentMonth();
break;
}

Expand Down Expand Up @@ -154,8 +161,7 @@ export function getSpecificRange(
return [dateStart, dateEnd, 'static'];
}

export function getFullRange(start: string) {
const end = monthUtils.currentMonth();
export function getFullRange(start: string, end: string) {
return [start, end, 'full'] as const;
}

Expand All @@ -179,7 +185,7 @@ export function calculateTimeRange(
const mode = timeFrame?.mode ?? defaultTimeFrame?.mode ?? 'sliding-window';

if (mode === 'full') {
return getFullRange(start);
return getFullRange(start, end);
}
if (mode === 'sliding-window') {
const offset = monthUtils.differenceInCalendarMonths(end, start);
Expand Down
68 changes: 42 additions & 26 deletions packages/desktop-client/src/components/reports/reports/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,33 +260,47 @@ function CalendarInner({ widget, parameters }: CalendarInnerProps) {

useEffect(() => {
async function run() {
try {
const trans = await send('get-earliest-transaction');
const currentMonth = monthUtils.currentMonth();
let earliestMonth = trans
? monthUtils.monthFromDate(parseISO(fromDateRepr(trans.date)))
: currentMonth;

// Make sure the month selects are at least populates with a
// year's worth of months. We can undo this when we have fancier
// date selects.
const yearAgo = monthUtils.subMonths(monthUtils.currentMonth(), 12);
if (earliestMonth > yearAgo) {
earliestMonth = yearAgo;
}
const earliestTransaction = await send('get-earliest-transaction');
setEarliestTransaction(
earliestTransaction
? earliestTransaction.date
: monthUtils.currentDay(),
);

const latestTransaction = await send('get-latest-transaction');
setLatestTransaction(
latestTransaction ? latestTransaction.date : monthUtils.currentDay(),
);

const allMonths = monthUtils
.rangeInclusive(earliestMonth, monthUtils.currentMonth())
.map(month => ({
name: month,
pretty: monthUtils.format(month, 'MMMM, yyyy', locale),
}))
.reverse();

setAllMonths(allMonths);
} catch (error) {
console.error('Error fetching earliest transaction:', error);
const currentMonth = monthUtils.currentMonth();
let earliestMonth = earliestTransaction
? monthUtils.monthFromDate(
parseISO(fromDateRepr(earliestTransaction.date)),
)
: currentMonth;
const latestMonth = latestTransaction
? monthUtils.monthFromDate(
parseISO(fromDateRepr(latestTransaction.date)),
)
: currentMonth;

// Make sure the month selects are at least populates with a
// year's worth of months. We can undo this when we have fancier
// date selects.
const yearAgo = monthUtils.subMonths(latestMonth, 12);
if (earliestMonth > yearAgo) {
earliestMonth = yearAgo;
}

const allMonths = monthUtils
.rangeInclusive(earliestMonth, latestMonth)
.map(month => ({
name: month,
pretty: monthUtils.format(month, 'MMMM, yyyy', locale),
}))
.reverse();

setAllMonths(allMonths);
}
run();
}, [locale]);
Expand Down Expand Up @@ -477,7 +491,8 @@ function CalendarInner({ widget, parameters }: CalendarInnerProps) {
},
);

const [earliestTransaction, _] = useState('');
const [earliestTransaction, setEarliestTransaction] = useState('');
const [latestTransaction, setLatestTransaction] = useState('');

return (
<Page
Expand Down Expand Up @@ -512,6 +527,7 @@ function CalendarInner({ widget, parameters }: CalendarInnerProps) {
start={start}
end={end}
earliestTransaction={earliestTransaction}
latestTransaction={latestTransaction}
firstDayOfWeekIdx={firstDayOfWeekIdx}
mode={mode}
onChangeDates={onChangeDates}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,27 @@ function CashFlowInner({ widget }: CashFlowInnerProps) {

useEffect(() => {
async function run() {
const trans = await send('get-earliest-transaction');
const earliestMonth = trans
? monthUtils.monthFromDate(d.parseISO(trans.date))
const earliestTransaction = await send('get-earliest-transaction');
setEarliestTransaction(
earliestTransaction
? earliestTransaction.date
: monthUtils.currentDay(),
);

const latestTransaction = await send('get-latest-transaction');
setLatestTransaction(
latestTransaction ? latestTransaction.date : monthUtils.currentDay(),
);

const earliestMonth = earliestTransaction
? monthUtils.monthFromDate(d.parseISO(earliestTransaction.date))
: monthUtils.currentMonth();
const latestMonth = latestTransaction
? monthUtils.monthFromDate(d.parseISO(latestTransaction.date))
: monthUtils.currentMonth();

const allMonths = monthUtils
.rangeInclusive(earliestMonth, monthUtils.currentMonth())
.rangeInclusive(earliestMonth, latestMonth)
.map(month => ({
name: month,
pretty: monthUtils.format(month, 'MMMM, yyyy', locale),
Expand Down Expand Up @@ -206,7 +220,8 @@ function CashFlowInner({ widget }: CashFlowInnerProps) {
});
};

const [earliestTransaction, _] = useState('');
const [earliestTransaction, setEarliestTransaction] = useState('');
const [latestTransaction, setLatestTransaction] = useState('');
const [_firstDayOfWeekIdx] = useSyncedPref('firstDayOfWeekIdx');
const firstDayOfWeekIdx = _firstDayOfWeekIdx || '0';

Expand Down Expand Up @@ -248,6 +263,7 @@ function CashFlowInner({ widget }: CashFlowInnerProps) {
start={start}
end={end}
earliestTransaction={earliestTransaction}
latestTransaction={latestTransaction}
firstDayOfWeekIdx={firstDayOfWeekIdx}
mode={mode}
show1Month
Expand Down
Loading
Loading