|
| 1 | +import React, { useEffect, useState, lazy, useMemo } from "react"; |
| 2 | +import type { ChartDataPoint, ChartData } from "../../types/data"; |
| 3 | +import type { AxisOptions } from "react-charts"; |
| 4 | +import { useTheme } from "payload/dist/admin/components/utilities/Theme"; |
| 5 | + |
| 6 | +type Props = {}; |
| 7 | + |
| 8 | +const ChartComponent = lazy(() => |
| 9 | + import("react-charts").then((module) => { |
| 10 | + return { default: module.Chart }; |
| 11 | + }) |
| 12 | +); |
| 13 | + |
| 14 | +const GlobalViewsChart: React.FC<Props> = ({}) => { |
| 15 | + const [chartData, setChartData] = useState<ChartData>([]); |
| 16 | + const [isLoading, setIsLoading] = useState<boolean>(true); |
| 17 | + const theme = useTheme(); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const getChartData = fetch(`/api/analytics/globalChart`, { |
| 21 | + method: "post", |
| 22 | + credentials: "include", |
| 23 | + headers: { |
| 24 | + Accept: "application/json", |
| 25 | + "Content-Type": "application/json", |
| 26 | + }, |
| 27 | + body: JSON.stringify({ |
| 28 | + timeframe: "30d", |
| 29 | + metrics: ["views"], |
| 30 | + }), |
| 31 | + }).then((response) => response.json()); |
| 32 | + |
| 33 | + getChartData.then((data: ChartData) => { |
| 34 | + setChartData(data); |
| 35 | + setIsLoading(false); |
| 36 | + }); |
| 37 | + }, []); |
| 38 | + |
| 39 | + const timeframeIndicator = useMemo(() => { |
| 40 | + return new Date().toLocaleString("default", { month: "long" }); |
| 41 | + }, []); |
| 42 | + |
| 43 | + const chartLabel = useMemo(() => { |
| 44 | + return "Sitewide views"; |
| 45 | + }, []); |
| 46 | + |
| 47 | + const primaryAxis = React.useMemo<AxisOptions<ChartDataPoint>>(() => { |
| 48 | + return { |
| 49 | + getValue: (datum) => datum.timestamp, |
| 50 | + show: false, |
| 51 | + elementType: "line", |
| 52 | + showDatumElements: false, |
| 53 | + }; |
| 54 | + }, []); |
| 55 | + |
| 56 | + const secondaryAxes = React.useMemo<AxisOptions<ChartDataPoint>[]>( |
| 57 | + () => [ |
| 58 | + { |
| 59 | + getValue: (datum) => { |
| 60 | + return datum.value; |
| 61 | + }, |
| 62 | + elementType: "line", |
| 63 | + }, |
| 64 | + ], |
| 65 | + [] |
| 66 | + ); |
| 67 | + |
| 68 | + return ( |
| 69 | + <section |
| 70 | + style={{ |
| 71 | + marginBottom: "1.5rem", |
| 72 | + }} |
| 73 | + > |
| 74 | + {chartData?.length && chartData.length > 0 ? ( |
| 75 | + <> |
| 76 | + <h1 style={{ fontSize: "1.25rem", marginBottom: "0.5rem" }}> |
| 77 | + {chartLabel} ({timeframeIndicator}) |
| 78 | + </h1> |
| 79 | + <div style={{ minHeight: "200px", position: "relative" }}> |
| 80 | + <ChartComponent |
| 81 | + options={{ |
| 82 | + data: chartData, |
| 83 | + dark: theme.theme === "dark", |
| 84 | + initialHeight: 220, |
| 85 | + tooltip: false, |
| 86 | + /* @ts-ignore */ |
| 87 | + primaryAxis, |
| 88 | + /* @ts-ignore */ |
| 89 | + secondaryAxes, |
| 90 | + }} |
| 91 | + /> |
| 92 | + </div> |
| 93 | + </> |
| 94 | + ) : isLoading ? ( |
| 95 | + <> Loading...</> |
| 96 | + ) : ( |
| 97 | + <div>No data found for {chartLabel}.</div> |
| 98 | + )} |
| 99 | + </section> |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | +export default GlobalViewsChart; |
0 commit comments