Skip to content

Commit e150bec

Browse files
committed
Fix source count in statistics
1 parent d8c5a79 commit e150bec

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

frontend/src/components/StatisticsModal.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "recharts";
1111
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
1212
import { toast } from "@/hooks/use-toast";
13-
import { useState, useEffect } from "react";
13+
import { useState, useEffect, useMemo } from "react";
1414

1515
import { getLinkClickStats, getLinkSourceStats } from "../api/client";
1616
import { ClickStats, SourceStats } from "../types/api";
@@ -97,6 +97,20 @@ export function StatisticsModal({ isOpen, onClose, linkId }: StatisticsModalProp
9797
}
9898
}, [isOpen, linkId]);
9999

100+
const aggregatedSources = useMemo(() => {
101+
const sourceMap = sourcesData.reduce<Record<string, number>>(
102+
(acc, { source, count }) => ({
103+
...acc,
104+
[source]: (acc[source] || 0) + count
105+
}),
106+
{}
107+
);
108+
109+
return Object.entries(sourceMap)
110+
.map(([source, count]) => ({ source, count }))
111+
.sort((a, b) => b.count - a.count);
112+
}, [sourcesData]);
113+
100114
return (
101115
<Dialog open={isOpen} onOpenChange={onClose}>
102116
<DialogContent className="max-w-3xl">
@@ -138,7 +152,7 @@ export function StatisticsModal({ isOpen, onClose, linkId }: StatisticsModalProp
138152
</CardHeader>
139153
<CardContent>
140154
<ul className="space-y-2">
141-
{sourcesData.map((source, index) => (
155+
{aggregatedSources.map((source, index) => (
142156
<li
143157
key={source.source}
144158
className="flex items-center justify-between py-2 border-b last:border-0"
@@ -149,9 +163,7 @@ export function StatisticsModal({ isOpen, onClose, linkId }: StatisticsModalProp
149163
</span>
150164
{source.source}
151165
</span>
152-
<span className="text-sm font-medium">
153-
{source.count} clicks
154-
</span>
166+
<span className="text-sm font-medium">{source.count} clicks</span>
155167
</li>
156168
))}
157169
</ul>

0 commit comments

Comments
 (0)