feat: select the most recent historical period by default

This commit is contained in:
YouXam
2026-01-07 03:08:08 +08:00
parent 9b5a3b763d
commit 733b7ccea6

View File

@@ -66,7 +66,10 @@ export function HistoricalPeriods({ periods, apiKey, userId }: HistoricalPeriods
setPeriodOptions(initialOptions); setPeriodOptions(initialOptions);
if (!selectedPeriod && initialOptions.length > 0) { if (!selectedPeriod && initialOptions.length > 0) {
setSelectedPeriod(initialOptions[0].period); const lastOption = initialOptions[initialOptions.length - 1];
if (lastOption) {
setSelectedPeriod(lastOption.period);
}
} }
// Fetch costs individually to update progressively // Fetch costs individually to update progressively
@@ -74,6 +77,8 @@ export function HistoricalPeriods({ periods, apiKey, userId }: HistoricalPeriods
for (let i = 0; i < periods.length; i++) { for (let i = 0; i < periods.length; i++) {
const period = periods[i]; const period = periods[i];
if (!period) continue;
try { try {
const response = await fetch(`/api/periods/${period.index}/summary`, { const response = await fetch(`/api/periods/${period.index}/summary`, {
headers: { 'X-API-Key': apiKey }, headers: { 'X-API-Key': apiKey },
@@ -102,7 +107,10 @@ export function HistoricalPeriods({ periods, apiKey, userId }: HistoricalPeriods
useEffect(() => { useEffect(() => {
if (periods.length > 0 && !selectedPeriod) { if (periods.length > 0 && !selectedPeriod) {
setSelectedPeriod(periods[0] || null); // Select the most recent historical period const lastPeriod = periods[periods.length - 1];
if (lastPeriod) {
setSelectedPeriod(lastPeriod); // Select the most recent historical period
}
} }
}, [periods]); }, [periods]);