|
| 1 | +"use client"; |
| 2 | +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; |
| 3 | +import { |
| 4 | + type ChartConfig, |
| 5 | + ChartContainer, |
| 6 | + ChartTooltip, |
| 7 | + ChartTooltipContent, |
| 8 | +} from "@/components/ui/chart"; |
| 9 | +import { formatDate } from "date-fns"; |
| 10 | +import { useMemo } from "react"; |
| 11 | +import { |
| 12 | + Bar, |
| 13 | + CartesianGrid, |
| 14 | + BarChart as RechartsBarChart, |
| 15 | + XAxis, |
| 16 | +} from "recharts"; |
| 17 | +import type { EngineCloudStats } from "types/analytics"; |
| 18 | +import { EmptyStateCard } from "../../../../../components/Analytics/EmptyStateCard"; |
| 19 | + |
| 20 | +export function EngineCloudBarChartCardUI({ |
| 21 | + rawData, |
| 22 | +}: { rawData: EngineCloudStats[] }) { |
| 23 | + const { data, pathnames, chartConfig, isAllEmpty } = useMemo(() => { |
| 24 | + // Dynamically collect all unique pathnames |
| 25 | + const pathnameSet = new Set<string>(); |
| 26 | + for (const item of rawData) { |
| 27 | + // Ignore empty pathname ''. |
| 28 | + if (item.pathname) { |
| 29 | + pathnameSet.add(item.pathname); |
| 30 | + } |
| 31 | + } |
| 32 | + const pathnames = Array.from(pathnameSet); |
| 33 | + |
| 34 | + // Group by date, then by pathname |
| 35 | + const dateMap = new Map<string, Record<string, number>>(); |
| 36 | + for (const { date, pathname, totalRequests } of rawData) { |
| 37 | + const map = dateMap.get(date) ?? {}; |
| 38 | + map[pathname] = Number(totalRequests) || 0; |
| 39 | + dateMap.set(date, map); |
| 40 | + } |
| 41 | + |
| 42 | + // Build data array for recharts |
| 43 | + const data = Array.from(dateMap.entries()).map(([date, value]) => { |
| 44 | + let total = 0; |
| 45 | + for (const pathname of pathnames) { |
| 46 | + if (!value[pathname]) value[pathname] = 0; |
| 47 | + total += value[pathname]; |
| 48 | + } |
| 49 | + return { date, ...value, total }; |
| 50 | + }); |
| 51 | + |
| 52 | + // Chart config |
| 53 | + const chartConfig: ChartConfig = {}; |
| 54 | + for (const pathname of pathnames) { |
| 55 | + chartConfig[pathname] = { label: pathname }; |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + data, |
| 60 | + pathnames, |
| 61 | + chartConfig, |
| 62 | + isAllEmpty: data.every((d) => d.total === 0), |
| 63 | + }; |
| 64 | + }, [rawData]); |
| 65 | + |
| 66 | + console.log("[DEBUG] data:", data); |
| 67 | + |
| 68 | + if (data.length === 0 || isAllEmpty) { |
| 69 | + return <EmptyStateCard metric="RPC" link="https://portal.thirdweb.com/" />; |
| 70 | + } |
| 71 | + |
| 72 | + return ( |
| 73 | + <Card> |
| 74 | + <CardHeader className="flex flex-col items-stretch space-y-0 border-b p-0"> |
| 75 | + <div className="flex flex-1 flex-col justify-center gap-1 p-6"> |
| 76 | + <CardTitle className="font-semibold text-lg"> |
| 77 | + Engine Cloud Requests |
| 78 | + </CardTitle> |
| 79 | + </div> |
| 80 | + </CardHeader> |
| 81 | + <CardContent className="px-2 sm:p-6 sm:pl-0"> |
| 82 | + <ChartContainer |
| 83 | + config={chartConfig} |
| 84 | + className="aspect-auto h-[250px] w-full pt-6" |
| 85 | + > |
| 86 | + <RechartsBarChart |
| 87 | + accessibilityLayer |
| 88 | + data={data} |
| 89 | + margin={{ |
| 90 | + left: 12, |
| 91 | + right: 12, |
| 92 | + }} |
| 93 | + > |
| 94 | + <CartesianGrid vertical={false} /> |
| 95 | + <XAxis |
| 96 | + dataKey="date" |
| 97 | + tickLine={false} |
| 98 | + axisLine={false} |
| 99 | + tickMargin={8} |
| 100 | + minTickGap={32} |
| 101 | + tickFormatter={(value: string) => { |
| 102 | + const date = new Date(value); |
| 103 | + return date.toLocaleDateString("en-US", { |
| 104 | + month: "short", |
| 105 | + day: "numeric", |
| 106 | + }); |
| 107 | + }} |
| 108 | + /> |
| 109 | + <ChartTooltip |
| 110 | + content={ |
| 111 | + <ChartTooltipContent |
| 112 | + labelFormatter={(d) => formatDate(new Date(d), "MMM d")} |
| 113 | + valueFormatter={(_value, _item) => { |
| 114 | + const value = typeof _value === "number" ? _value : 0; |
| 115 | + return <span className="inline-flex gap-1.5">{value}</span>; |
| 116 | + }} |
| 117 | + /> |
| 118 | + } |
| 119 | + /> |
| 120 | + {pathnames.map((pathname, idx) => ( |
| 121 | + <Bar |
| 122 | + key={pathname} |
| 123 | + stackId="a" |
| 124 | + dataKey={pathname} |
| 125 | + radius={[ |
| 126 | + idx === pathnames.length - 1 ? 4 : 0, |
| 127 | + idx === pathnames.length - 1 ? 4 : 0, |
| 128 | + idx === 0 ? 4 : 0, |
| 129 | + idx === 0 ? 4 : 0, |
| 130 | + ]} |
| 131 | + fill={`hsl(var(--chart-${idx + 1}))`} |
| 132 | + strokeWidth={1} |
| 133 | + className="stroke-background" |
| 134 | + /> |
| 135 | + ))} |
| 136 | + </RechartsBarChart> |
| 137 | + </ChartContainer> |
| 138 | + </CardContent> |
| 139 | + </Card> |
| 140 | + ); |
| 141 | +} |
0 commit comments