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,24 +66,29 @@ 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
const updatedOptions = [...initialOptions]; const updatedOptions = [...initialOptions];
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 },
}); });
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json();
updatedOptions[i] = { updatedOptions[i] = {
period, period,
totalCost: data.totals?.totalCost || 0 totalCost: data.totals?.totalCost || 0
}; };
} else { } else {
updatedOptions[i] = { period, totalCost: 0 }; updatedOptions[i] = { period, totalCost: 0 };
@@ -91,7 +96,7 @@ export function HistoricalPeriods({ periods, apiKey, userId }: HistoricalPeriods
} catch { } catch {
updatedOptions[i] = { period, totalCost: 0 }; updatedOptions[i] = { period, totalCost: 0 };
} }
// Update state after each fetch for progressive loading // Update state after each fetch for progressive loading
setPeriodOptions([...updatedOptions]); setPeriodOptions([...updatedOptions]);
} }
@@ -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]);