From e150bece4dce65a3d7a39f77b7b9fe5fd4c2bb31 Mon Sep 17 00:00:00 2001 From: Wavering Ana Date: Sat, 1 Feb 2025 00:01:06 -0500 Subject: [PATCH] Fix source count in statistics --- frontend/src/components/StatisticsModal.tsx | 22 ++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/StatisticsModal.tsx b/frontend/src/components/StatisticsModal.tsx index db936dd..619f850 100644 --- a/frontend/src/components/StatisticsModal.tsx +++ b/frontend/src/components/StatisticsModal.tsx @@ -10,7 +10,7 @@ import { } from "recharts"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { toast } from "@/hooks/use-toast"; -import { useState, useEffect } from "react"; +import { useState, useEffect, useMemo } from "react"; import { getLinkClickStats, getLinkSourceStats } from "../api/client"; import { ClickStats, SourceStats } from "../types/api"; @@ -97,6 +97,20 @@ export function StatisticsModal({ isOpen, onClose, linkId }: StatisticsModalProp } }, [isOpen, linkId]); + const aggregatedSources = useMemo(() => { + const sourceMap = sourcesData.reduce>( + (acc, { source, count }) => ({ + ...acc, + [source]: (acc[source] || 0) + count + }), + {} + ); + + return Object.entries(sourceMap) + .map(([source, count]) => ({ source, count })) + .sort((a, b) => b.count - a.count); + }, [sourcesData]); + return ( @@ -138,7 +152,7 @@ export function StatisticsModal({ isOpen, onClose, linkId }: StatisticsModalProp
    - {sourcesData.map((source, index) => ( + {aggregatedSources.map((source, index) => (
  • {source.source} - - {source.count} clicks - + {source.count} clicks
  • ))}