-
Notifications
You must be signed in to change notification settings - Fork 122
/
Dashboard.tsx
178 lines (166 loc) · 6.34 KB
/
Dashboard.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import React, { FC } from "react";
import { Col, Row } from "antd";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
import dayjsGenerateConfig from "rc-picker/lib/generate/dayjs";
import generatePicker from "antd/es/date-picker/generatePicker";
import { useQuery, UseQueryResult } from "@tanstack/react-query";
import advancedFormat from "dayjs/plugin/advancedFormat";
import customParseFormat from "dayjs/plugin/customParseFormat";
import localeData from "dayjs/plugin/localeData";
import weekday from "dayjs/plugin/weekday";
import weekOfYear from "dayjs/plugin/weekOfYear";
import weekYear from "dayjs/plugin/weekYear";
import { Events, GetRevenue, GetSubscriptions } from "../../api/api";
import { RevenueType } from "../../types/revenue-type";
import MetricBarGraph from "./MetricBarGraph";
import { SubscriptionTotals } from "../../types/subscription-type";
import { components, operations } from "../../gen-types";
import { PageLayout } from "../base/PageLayout";
import { CustomerByPlanPie } from "./CustomerByPlanPie";
import NumberDisplay from "./NumberDisplay";
dayjs.extend(customParseFormat);
dayjs.extend(advancedFormat);
dayjs.extend(weekday);
dayjs.extend(localeData);
dayjs.extend(weekOfYear);
dayjs.extend(weekYear);
dayjs.extend(duration);
const DatePicker = generatePicker<dayjs.Dayjs>(dayjsGenerateConfig);
const { RangePicker } = DatePicker;
const dateFormat = "YYYY/MM/DD";
const defaultDate = [dayjs().subtract(1, "months").add(1, "day"), dayjs()];
const Dashboard: FC = () => {
const [dateRange, setDateRange] = React.useState<any>(defaultDate);
const {
data: recentPeriodData,
isLoading: recentPeriodIsLoading,
}: UseQueryResult<components["schemas"]["PeriodMetricRevenueResponse"]> =
useQuery<components["schemas"]["PeriodMetricRevenueResponse"]>(
["revent_total_revenue", dateRange],
() =>
GetRevenue.getMonthlyRevenue(
dateRange[0].format("YYYY-MM-DD"),
dateRange[1].format("YYYY-MM-DD")
).then((res) => res)
);
const {
data: previousPeriodData,
isLoading: previousPeriodIsLoading,
}: UseQueryResult<components["schemas"]["PeriodMetricRevenueResponse"]> =
useQuery<components["schemas"]["PeriodMetricRevenueResponse"]>(
["previous_total_revenue", dateRange],
() =>
GetRevenue.getMonthlyRevenue(
dateRange[0]
.subtract(dayjs.duration(dateRange[1].diff(dateRange[0])))
.subtract(1, "day")
.format("YYYY-MM-DD"),
dateRange[0].subtract(1, "day").format("YYYY-MM-DD")
).then((res) => res)
);
const { data: subscriptionData, isLoading: subscriptionLoading } =
useQuery<SubscriptionTotals>(["subscription_overview", dateRange], () =>
GetSubscriptions.getSubscriptionOverview(
dateRange[0].format("YYYY-MM-DD"),
dateRange[1].format("YYYY-MM-DD"),
dateRange[0]
.subtract(dayjs.duration(dateRange[1].diff(dateRange[0])))
.format("YYYY-MM-DD"),
dateRange[1].subtract(1, "month").format("YYYY-MM-DD")
).then((res) => res)
);
const { data: eventData, isLoading: eventLoading } = useQuery(
["event_count", dateRange],
() =>
Events.getEventCount(
dateRange[0].format("YYYY-MM-DD"),
dateRange[1].format("YYYY-MM-DD"),
dateRange[0]
.subtract(dayjs.duration(dateRange[1].diff(dateRange[0])))
.format("YYYY-MM-DD"),
dateRange[1].subtract(1, "month").format("YYYY-MM-DD")
).then((res) => res)
);
return (
<PageLayout
title="Dashboard"
className="text-[24px]"
extra={[
<RangePicker
id="preset"
key="range-picker"
format={dateFormat}
ranges={{
"This month": [dayjs().startOf("month"), dayjs().endOf("month")],
"Last month": [
dayjs().subtract(1, "months").startOf("month"),
dayjs().subtract(1, "months").endOf("month"),
],
"This year": [dayjs().startOf("year"), dayjs().endOf("year")],
"All time": [dayjs().subtract(10, "years"), dayjs()],
}}
defaultValue={dateRange}
onCalendarChange={(dates) => {
setDateRange(dates);
}}
/>,
]}
>
<Row gutter={[16, 16]}>
<Col span="24">
<div className="grid grid-cols-12 gap-18 justify-center align-baseline space-x-4">
<div className="col-span-3">
<NumberDisplay
metric_1={recentPeriodData?.earned_revenue}
metric_2={previousPeriodData?.earned_revenue}
isLoadingMetric1={recentPeriodIsLoading}
isLoadingMetric2={previousPeriodIsLoading}
title="Earned Revenue"
currency="USD"
/>
</div>
<div className="col-span-3">
<NumberDisplay
metric_1={subscriptionData?.period_1_total_subscriptions}
metric_2={subscriptionData?.period_2_total_subscriptions}
isLoadingMetric1={subscriptionLoading}
isLoadingMetric2={subscriptionLoading}
title="Total Subscriptions"
/>
</div>
<div className="col-span-3">
<NumberDisplay
metric_1={eventData?.total_events_period_1}
metric_2={eventData?.total_events_period_2}
isLoadingMetric1={false}
isLoadingMetric2={false}
title="Events Tracked"
/>{" "}
</div>
<div className="col-span-3">
<NumberDisplay
metric_1={subscriptionData?.period_1_new_subscriptions}
metric_2={subscriptionData?.period_2_new_subscriptions}
isLoadingMetric1={subscriptionLoading}
isLoadingMetric2={subscriptionLoading}
title="New Subscriptions"
/>{" "}
</div>
</div>
</Col>
<Col span="24">
<div className="grid grid-cols-12 gap-18 justify-center space-x-4">
<div className="col-span-8">
<MetricBarGraph range={dateRange} />
</div>
<div className="col-span-4">
<CustomerByPlanPie />
</div>
</div>
</Col>
</Row>
</PageLayout>
);
};
export default Dashboard;