fix: useMemo移到early return之前,修复hooks顺序错误
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

React要求hooks在每次渲染中顺序一致,不能在条件return之后调用。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
kkfluous
2026-03-28 23:30:07 +08:00
parent 454b2f0913
commit f4cf5d1cb6

View File

@@ -372,6 +372,30 @@ export default function App() {
return mp;
}), [modalWeeklyDetail, modalFilters.plateNumber]);
const customerPieData = useMemo(() => {
let data: { name: string; value: number }[] = [];
if (customerChartView === 'region') {
const map: Record<string, number> = {};
customerData.forEach(item => { map[item.region] = (map[item.region] || 0) + item.total; });
data = Object.entries(map).map(([name, value]) => ({ name, value })).sort((a, b) => b.value - a.value);
} else {
const map: Record<string, number> = {};
customerData.forEach(item => { map[item.city] = (map[item.city] || 0) + item.total; });
const tot = Object.values(map).reduce((a, b) => a + b, 0);
const threshold = tot * 0.05;
let other = 0;
Object.entries(map).forEach(([name, value]) => {
if (value >= threshold) data.push({ name, value });
else other += value;
});
if (other > 0) data.push({ name: '其他', value: other });
data.sort((a, b) => b.value - a.value);
}
return data;
}, [customerData, customerChartView]);
const watermarkText = useMemo(() => `羚牛氢能-${new Date().toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }).replace(/\//g, '-')}`, [lastUpdate]);
if (loading && !summary) {
return (
<div className="min-h-screen bg-[#F8F9FB] flex items-center justify-center">
@@ -399,30 +423,6 @@ export default function App() {
const SUMMARY = summary!;
const customerPieData = useMemo(() => {
let data: { name: string; value: number }[] = [];
if (customerChartView === 'region') {
const map: Record<string, number> = {};
customerData.forEach(item => { map[item.region] = (map[item.region] || 0) + item.total; });
data = Object.entries(map).map(([name, value]) => ({ name, value })).sort((a, b) => b.value - a.value);
} else {
const map: Record<string, number> = {};
customerData.forEach(item => { map[item.city] = (map[item.city] || 0) + item.total; });
const tot = Object.values(map).reduce((a, b) => a + b, 0);
const threshold = tot * 0.05;
let other = 0;
Object.entries(map).forEach(([name, value]) => {
if (value >= threshold) data.push({ name, value });
else other += value;
});
if (other > 0) data.push({ name: '其他', value: other });
data.sort((a, b) => b.value - a.value);
}
return data;
}, [customerData, customerChartView]);
const watermarkText = useMemo(() => `羚牛氢能-${new Date().toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }).replace(/\//g, '-')}`, [lastUpdate]);
return (
<div className="min-h-screen bg-[#F8F9FB] text-gray-800 font-sans p-6 pb-20 md:pb-6 relative">
{/* Watermark */}