chore: automate latest month and year calculation#305
Conversation
WalkthroughThe pull request refactors the report selection logic in the treasury page. A new function, getLatestReport, is introduced to dynamically determine the most recent report from a predefined MONTHS array based on report type. The ReportSelection component now uses this function to initialize its state, displaying only relevant year and month options. Additionally, minor JSX formatting tweaks have been applied and the AnyQuestions component has been streamlined using concise arrow function syntax. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ReportSelection
participant getLatestReport
Note over ReportSelection: On page load
User ->> ReportSelection: Render Treasury Page
ReportSelection ->> getLatestReport: Request latest report for type
getLatestReport -->> ReportSelection: Returns latest report (or empty object)
ReportSelection ->> User: Render updated options for reports
Possibly related PRs
Suggested reviewers
Poem
Tip 🌐 Web search-backed reviews and chat
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/pages/treasury.js (2)
169-186: Add PropTypes and memoization for better maintainability and performance.While the arrow function refactoring improves consistency, consider these enhancements:
- Add PropTypes validation
- Memoize components to prevent unnecessary re-renders
+import PropTypes from 'prop-types'; +import { memo } from 'react'; -const AnyQuestions = () => ( +const AnyQuestions = memo(() => ( <div className={styles.anyQuestions}> ... </div> -); +)); -const TreasuryReports = ({ intl }) => ( +const TreasuryReports = memo(({ intl }) => ( <Layout> ... </Layout> -); +)); +TreasuryReports.propTypes = { + intl: PropTypes.shape({ + locale: PropTypes.string.isRequired, + formatMessage: PropTypes.func.isRequired, + }).isRequired, +};Also applies to: 240-250
188-238: Add error handling and optimize performance in ReportSelection.The component could benefit from these improvements:
- Add error handling for network failures when downloading reports
- Add loading state while fetching reports
- Memoize getLatestReport results to prevent unnecessary re-renders
const ReportSelection = ({ type }) => { - const latestReport = getLatestReport(type); + const latestReport = useMemo(() => getLatestReport(type), [type]); + const [isLoading, setIsLoading] = useState(false); + const [error, setError] = useState(null); const [selectedYear, setSelectedYear] = useState(latestReport.year); const [selectedMonth, setSelectedMonth] = useState(latestReport.month); const availableYears = useMemo( () => [...new Set(MONTHS.filter((m) => m[type]).map((m) => m.year))].sort().reverse(), [type] ); const availableMonths = useMemo( () => MONTHS.filter((m) => m.year === selectedYear && m[type]).map((m) => m.month), [selectedYear, type] ); const handleDownload = async (url) => { try { setIsLoading(true); setError(null); const response = await fetch(url); if (!response.ok) throw new Error('Failed to download report'); // Handle successful download } catch (err) { setError(err.message); } finally { setIsLoading(false); } }; return ( <section className={styles[`${type}`]}> {/* ... existing JSX ... */} + {isLoading && <div>Loading...</div>} + {error && <div className="error">{error}</div>} </section> ); }; +ReportSelection.propTypes = { + type: PropTypes.oneOf(['treasuryReport', 'riskReport']).isRequired, +};
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/pages/treasury.js(4 hunks)
🔇 Additional comments (1)
src/pages/treasury.js (1)
9-148: Verify the January 2025 report entry.The MONTHS array includes a treasury report for January 2025, but according to the current date (February 2025), this appears to be a future-dated entry. Please verify if this is intentional or if it should be removed.




Summary by CodeRabbit
New Features
Refactor