Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MonthsContext to tsx #1711

Closed
wants to merge 3 commits into from
Closed
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
30 changes: 0 additions & 30 deletions packages/desktop-client/src/components/budget/MonthsContext.js

This file was deleted.

57 changes: 57 additions & 0 deletions packages/desktop-client/src/components/budget/MonthsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { createContext, type ReactNode } from 'react';

import * as monthUtils from 'loot-core/src/shared/months';

type BoundsParams = {
start: string | Date;
end: string | Date;
};

type GetValidMonthBoundsParams = {
bounds: BoundsParams;
startMonth: string | Date;
endMonth: string | Date;
};

export function getValidMonthBounds({
bounds,
startMonth,
endMonth,
}: GetValidMonthBoundsParams) {
return {
start: startMonth < bounds.start ? bounds.start : startMonth,
end: endMonth > bounds.end ? bounds.end : endMonth,
};
}

export let MonthsContext = createContext(null);

type MonthsProviderProps = {
startMonth: string | Date;
numMonths: number;
monthBounds: BoundsParams;
type: string;
children?: ReactNode;
};

export function MonthsProvider({
startMonth,
numMonths,
monthBounds,
type,
children,
}: MonthsProviderProps) {
let endMonth = monthUtils.addMonths(startMonth, numMonths - 1);
let bounds = getValidMonthBounds({
bounds: monthBounds,
startMonth,
endMonth,
});
let months = monthUtils.rangeInclusive(bounds.start, bounds.end);

return (
<MonthsContext.Provider value={{ months, type }}>
{children}
</MonthsContext.Provider>
);
}
6 changes: 6 additions & 0 deletions upcoming-release-notes/1711.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [Jod929]
---

Refactor budget/MonthsContext to tsx