|
1 | 1 | "use client";
|
| 2 | +import { DatePickerWithRange } from "@/components/ui/DatePickerWithRange"; |
| 3 | +import { |
| 4 | + Select, |
| 5 | + SelectContent, |
| 6 | + SelectItem, |
| 7 | + SelectTrigger, |
| 8 | + SelectValue, |
| 9 | +} from "@/components/ui/select"; |
2 | 10 | import {
|
3 | 11 | useWalletUsageAggregate,
|
4 | 12 | useWalletUsagePeriod,
|
5 | 13 | } from "@3rdweb-sdk/react/hooks/useApi";
|
6 |
| -import { useMemo } from "react"; |
| 14 | +import { differenceInDays, format, subDays } from "date-fns"; |
| 15 | +import { useState } from "react"; |
7 | 16 | import { ConnectAnalyticsDashboardUI } from "./ConnectAnalyticsDashboardUI";
|
8 | 17 |
|
9 | 18 | export function ConnectAnalyticsDashboard(props: {
|
10 | 19 | clientId: string;
|
11 | 20 | }) {
|
12 |
| - const allTimeFrom = useMemo( |
13 |
| - () => new Date(Date.now() - 135 * 24 * 60 * 60 * 1000), |
14 |
| - [], |
| 21 | + const [range, setRange] = useState<Range>(() => |
| 22 | + getLastNDaysRange("last-120"), |
15 | 23 | );
|
16 |
| - const from = useMemo( |
17 |
| - () => new Date(Date.now() - 135 * 24 * 60 * 60 * 1000), |
18 |
| - [], |
| 24 | + |
| 25 | + // use date-fns to calculate the number of days in the range |
| 26 | + const daysInRange = differenceInDays(range.to, range.from); |
| 27 | + |
| 28 | + const [intervalType, setIntervalType] = useState<"day" | "week">( |
| 29 | + daysInRange > 30 ? "week" : "day", |
19 | 30 | );
|
20 |
| - const to = useMemo(() => new Date(), []); |
| 31 | + |
21 | 32 | const walletUsageQuery = useWalletUsagePeriod({
|
22 | 33 | clientId: props.clientId,
|
23 |
| - from, |
24 |
| - to, |
25 |
| - period: "day", |
| 34 | + from: range.from, |
| 35 | + to: range.to, |
| 36 | + period: intervalType, |
26 | 37 | });
|
| 38 | + |
27 | 39 | const walletUsageAggregateQuery = useWalletUsageAggregate({
|
28 | 40 | clientId: props.clientId,
|
29 |
| - from: allTimeFrom, |
30 |
| - to, |
| 41 | + from: range.from, |
| 42 | + to: range.to, |
31 | 43 | });
|
32 | 44 |
|
33 | 45 | return (
|
34 |
| - <ConnectAnalyticsDashboardUI |
35 |
| - walletUsage={walletUsageQuery.data || []} |
36 |
| - aggregateWalletUsage={walletUsageAggregateQuery.data || []} |
37 |
| - isPending={ |
38 |
| - walletUsageQuery.isPending || walletUsageAggregateQuery.isPending |
| 46 | + <div> |
| 47 | + <div className="flex gap-3"> |
| 48 | + <DateRangeSelector |
| 49 | + range={range} |
| 50 | + setRange={(newRange) => { |
| 51 | + setRange(newRange); |
| 52 | + const days = differenceInDays(newRange.to, newRange.from); |
| 53 | + setIntervalType(days > 30 ? "week" : "day"); |
| 54 | + }} |
| 55 | + /> |
| 56 | + <IntervalSelector |
| 57 | + intervalType={intervalType} |
| 58 | + setIntervalType={setIntervalType} |
| 59 | + /> |
| 60 | + </div> |
| 61 | + <div className="h-4" /> |
| 62 | + <ConnectAnalyticsDashboardUI |
| 63 | + walletUsage={walletUsageQuery.data || []} |
| 64 | + aggregateWalletUsage={walletUsageAggregateQuery.data || []} |
| 65 | + isPending={ |
| 66 | + walletUsageQuery.isPending || walletUsageAggregateQuery.isPending |
| 67 | + } |
| 68 | + /> |
| 69 | + </div> |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +const durationPresets = [ |
| 74 | + { |
| 75 | + name: "Last 7 Days", |
| 76 | + id: "last-7", |
| 77 | + days: 7, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "Last 30 Days", |
| 81 | + id: "last-30", |
| 82 | + days: 30, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "Last 60 Days", |
| 86 | + id: "last-60", |
| 87 | + days: 60, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "Last 120 Days", |
| 91 | + id: "last-120", |
| 92 | + days: 120, |
| 93 | + }, |
| 94 | +] as const; |
| 95 | + |
| 96 | +type DurationId = (typeof durationPresets)[number]["id"]; |
| 97 | + |
| 98 | +type Range = { |
| 99 | + type: DurationId | "custom"; |
| 100 | + label?: string; |
| 101 | + from: Date; |
| 102 | + to: Date; |
| 103 | +}; |
| 104 | + |
| 105 | +function getLastNDaysRange(id: DurationId) { |
| 106 | + const durationInfo = durationPresets.find((preset) => preset.id === id); |
| 107 | + if (!durationInfo) { |
| 108 | + throw new Error("Invalid duration id"); |
| 109 | + } |
| 110 | + |
| 111 | + const todayDate = new Date(); |
| 112 | + |
| 113 | + const value: Range = { |
| 114 | + type: id, |
| 115 | + from: subDays(todayDate, durationInfo.days), |
| 116 | + to: todayDate, |
| 117 | + label: durationInfo.name, |
| 118 | + }; |
| 119 | + |
| 120 | + return value; |
| 121 | +} |
| 122 | + |
| 123 | +function DateRangeSelector(props: { |
| 124 | + range: Range; |
| 125 | + setRange: (range: Range) => void; |
| 126 | +}) { |
| 127 | + const { range, setRange } = props; |
| 128 | + |
| 129 | + return ( |
| 130 | + <DatePickerWithRange |
| 131 | + from={range.from} |
| 132 | + to={range.to} |
| 133 | + setFrom={(from) => |
| 134 | + setRange({ |
| 135 | + from, |
| 136 | + to: range.to, |
| 137 | + type: "custom", |
| 138 | + }) |
| 139 | + } |
| 140 | + setTo={(to) => |
| 141 | + setRange({ |
| 142 | + from: range.from, |
| 143 | + to, |
| 144 | + type: "custom", |
| 145 | + }) |
39 | 146 | }
|
| 147 | + header={ |
| 148 | + <div className="mb-2 border-border border-b p-4"> |
| 149 | + <Select |
| 150 | + value={range.type} |
| 151 | + onValueChange={(id: DurationId) => { |
| 152 | + setRange(getLastNDaysRange(id)); |
| 153 | + }} |
| 154 | + > |
| 155 | + <SelectTrigger className="flex bg-transparent"> |
| 156 | + <SelectValue placeholder="Select" /> |
| 157 | + </SelectTrigger> |
| 158 | + <SelectContent position="popper"> |
| 159 | + {durationPresets.map((preset) => ( |
| 160 | + <SelectItem key={preset.id} value={preset.id}> |
| 161 | + {preset.name} |
| 162 | + </SelectItem> |
| 163 | + ))} |
| 164 | + |
| 165 | + {range.type === "custom" && ( |
| 166 | + <SelectItem value="custom"> |
| 167 | + {format(range.from, "LLL dd, y")} -{" "} |
| 168 | + {format(range.to, "LLL dd, y")} |
| 169 | + </SelectItem> |
| 170 | + )} |
| 171 | + </SelectContent> |
| 172 | + </Select> |
| 173 | + </div> |
| 174 | + } |
| 175 | + labelOverride={range.label} |
| 176 | + className="w-auto" |
40 | 177 | />
|
41 | 178 | );
|
42 | 179 | }
|
| 180 | + |
| 181 | +function IntervalSelector(props: { |
| 182 | + intervalType: "day" | "week"; |
| 183 | + setIntervalType: (intervalType: "day" | "week") => void; |
| 184 | +}) { |
| 185 | + return ( |
| 186 | + <Select |
| 187 | + value={props.intervalType} |
| 188 | + onValueChange={(value: "day" | "week") => { |
| 189 | + props.setIntervalType(value); |
| 190 | + }} |
| 191 | + > |
| 192 | + <SelectTrigger className="w-auto hover:bg-muted"> |
| 193 | + <SelectValue placeholder="Select" /> |
| 194 | + </SelectTrigger> |
| 195 | + <SelectContent position="popper"> |
| 196 | + <SelectItem value="week"> Weekly </SelectItem> |
| 197 | + <SelectItem value="day"> Daily</SelectItem> |
| 198 | + </SelectContent> |
| 199 | + </Select> |
| 200 | + ); |
| 201 | +} |
0 commit comments