Skip to content

Commit f97746b

Browse files
Copilothotlong
andcommitted
Address code review: extract aggregation helper in ReportViewer, simplify map warning message
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 53e8cb2 commit f97746b

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

packages/plugin-map/src/ObjectMap.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export const ObjectMap: React.FC<ObjectMapProps> = ({
415415
<div className={className}>
416416
{invalidCount > 0 && (
417417
<div className="mb-2 p-2 text-sm text-yellow-800 bg-yellow-50 border border-yellow-200 rounded">
418-
{invalidCount} record{invalidCount !== 1 ? 's' : ''} with missing or invalid coordinates {invalidCount !== 1 ? 'were' : 'was'} excluded from the map.
418+
{`${invalidCount} record${invalidCount !== 1 ? 's' : ''} with missing or invalid coordinates excluded from the map.`}
419419
</div>
420420
)}
421421
<div className="relative border rounded-lg overflow-hidden bg-muted" style={{ height: '600px', width: '100%' }}>

packages/plugin-report/src/ReportViewer.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,28 @@ export const ReportViewer: React.FC<ReportViewerProps> = ({ schema, onRefresh })
4545
};
4646

4747
const handlePrint = () => {
48-
console.log('Print report');
4948
window.print();
5049
};
5150

5251
const handleRefresh = () => {
5352
onRefresh?.();
5453
};
5554

55+
const computeAggregation = (fieldName: string, aggregation?: string): string | number => {
56+
if (!data) return 0;
57+
const values = data.map(item => Number(item[fieldName]) || 0);
58+
switch (aggregation) {
59+
case 'count': return data.length;
60+
case 'sum': return values.reduce((sum, v) => sum + v, 0);
61+
case 'avg': return values.length > 0
62+
? (values.reduce((sum, v) => sum + v, 0) / values.length).toFixed(2)
63+
: 0;
64+
case 'min': return values.length > 0 ? Math.min(...values) : 0;
65+
case 'max': return values.length > 0 ? Math.max(...values) : 0;
66+
default: return '';
67+
}
68+
};
69+
5670
if (!report) {
5771
return (
5872
<Card>
@@ -159,17 +173,7 @@ export const ReportViewer: React.FC<ReportViewerProps> = ({ schema, onRefresh })
159173
<CardContent className="p-4">
160174
<div className="text-sm text-muted-foreground">{field.label || field.name}</div>
161175
<div className="text-2xl font-bold">
162-
{field.aggregation === 'count' && data?.length}
163-
{field.aggregation === 'sum' && data?.reduce((sum, item) => sum + (Number(item[field.name]) || 0), 0)}
164-
{field.aggregation === 'avg' && data && data.length > 0
165-
? (data.reduce((sum, item) => sum + (Number(item[field.name]) || 0), 0) / data.length).toFixed(2)
166-
: field.aggregation === 'avg' ? 0 : null}
167-
{field.aggregation === 'min' && data && data.length > 0
168-
? Math.min(...data.map(item => Number(item[field.name]) || 0))
169-
: field.aggregation === 'min' ? 0 : null}
170-
{field.aggregation === 'max' && data && data.length > 0
171-
? Math.max(...data.map(item => Number(item[field.name]) || 0))
172-
: field.aggregation === 'max' ? 0 : null}
176+
{computeAggregation(field.name, field.aggregation)}
173177
</div>
174178
</CardContent>
175179
</Card>

0 commit comments

Comments
 (0)