From 67357b591d2d9f081d4cd486d84593b14f482385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Wed, 1 Jul 2026 15:25:20 -0300 Subject: [PATCH] Highlight client purchase patterns --- src/pages/ClientDetails.tsx | 60 ++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/src/pages/ClientDetails.tsx b/src/pages/ClientDetails.tsx index 99d85b3..1f7e359 100644 --- a/src/pages/ClientDetails.tsx +++ b/src/pages/ClientDetails.tsx @@ -29,6 +29,22 @@ type CustomTooltipProps = { label?: string; }; +type PatternPoint = { + label: string; + value: number; +}; + +const getTopPatternPoint = (points: PatternPoint[]) => ( + points.reduce((topPoint, point) => { + if (!topPoint || point.value > topPoint.value) return point; + return topPoint; + }, null) +); + +const getPatternShare = (value: number, total: number) => ( + total > 0 ? Math.round((value / total) * 100) : 0 +); + const CustomTooltip = ({ active, payload, label }: CustomTooltipProps) => { if (active && payload && payload.length) { return ( @@ -248,6 +264,10 @@ const ClientDetails = () => { const paginatedOrders = groupedOrders.slice(startIndex, startIndex + ordersPerPage); const hasWeekdayPattern = purchaseWeekdays.some(day => day.value > 0); const hasHourPattern = purchaseHours.some(hour => hour.value > 0); + const weekdayPatternTotal = purchaseWeekdays.reduce((total, day) => total + day.value, 0); + const hourPatternTotal = purchaseHours.reduce((total, hour) => total + hour.value, 0); + const topWeekday = getTopPatternPoint(purchaseWeekdays); + const topHour = getTopPatternPoint(purchaseHours); const isRefreshing = isLoading && Boolean(details); const isSingleDayRange = formatDateKey(dateRange.start) === formatDateKey(dateRange.end); @@ -390,14 +410,26 @@ const ClientDetails = () => {
-
-

Compras por Dia

- +
+
+

Compras por Dia

+ {hasWeekdayPattern && topWeekday && ( +

+ + Dia mais forte: {topWeekday.label} + · + {topWeekday.value} {topWeekday.value === 1 ? 'pedido' : 'pedidos'} + · + {getPatternShare(topWeekday.value, weekdayPatternTotal)}% +

+ )} +
+ {purchaseWeekdayRangeLabel}
{hasWeekdayPattern ? ( -
+
@@ -420,14 +452,26 @@ const ClientDetails = () => {
-
-

Compras por Horário

- +
+
+

Compras por Horário

+ {hasHourPattern && topHour && ( +

+ + Horário mais forte: {topHour.label} + · + {topHour.value} {topHour.value === 1 ? 'pedido' : 'pedidos'} + · + {getPatternShare(topHour.value, hourPatternTotal)}% +

+ )} +
+ {purchaseHourRangeLabel}
{hasHourPattern ? ( -
+