@@ -9,27 +9,23 @@ import React, {
99import type {
1010 DashboardAnalyticsConfig ,
1111 ChartDataPoint ,
12+ ChartData ,
1213 ChartWidget ,
13- IdMatcherFunction ,
14+ ChartWidgetMetrics ,
1415} from "../../types" ;
1516import type { AxisOptions } from "react-charts" ;
1617import { useDocumentInfo } from "payload/components/utilities" ;
1718import { MetricMap } from "../../providers/plausible/client" ;
1819import { useTheme } from "payload/dist/admin/components/utilities/Theme" ;
1920
20- type ChartData = {
21- label : string ;
22- data : ChartDataPoint [ ] ;
23- } ;
24-
25- type ChartOptions = {
21+ /* type ChartOptions = {
2622 timeframe?: string;
27- metric : ChartWidget [ "metric " ] ;
23+ metrics : ChartWidget["metrics "];
2824 idMatcher: IdMatcherFunction;
29- } ;
25+ }; */
3026
3127type Props = {
32- options : ChartOptions ;
28+ options : ChartWidget ;
3329} ;
3430
3531const ChartComponent = lazy ( ( ) =>
@@ -38,14 +34,14 @@ const ChartComponent = lazy(() =>
3834 } )
3935) ;
4036
41- const ViewsChart : React . FC < Props > = ( { options } ) => {
42- const [ chartData , setChartData ] = useState < ChartData [ ] > ( [ ] ) ;
37+ const PageViewsChart : React . FC < Props > = ( { options } ) => {
38+ const [ chartData , setChartData ] = useState < ChartData > ( [ ] ) ;
4339 const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ;
4440 const chartRef = useRef < any > ( null ) ;
4541 const theme = useTheme ( ) ;
4642 const { publishedDoc } = useDocumentInfo ( ) ;
4743
48- const { timeframe, metric , idMatcher } = options ;
44+ const { timeframe, metrics , idMatcher, label } = options ;
4945
5046 const pageId = useMemo ( ( ) => {
5147 if ( publishedDoc ) return idMatcher ( publishedDoc ) ;
@@ -68,19 +64,19 @@ const ViewsChart: React.FC<Props> = ({ options }) => {
6864 } ,
6965 body : JSON . stringify ( {
7066 timeframe : timeframe ,
71- metric : metric ,
67+ metrics : metrics ,
7268 pageId : pageId ,
7369 } ) ,
7470 } ) . then ( ( response ) => response . json ( ) ) ;
7571
76- getChartData . then ( ( data : ChartDataPoint [ ] ) => {
77- const processedData : ChartData [ ] = [
72+ getChartData . then ( ( data : ChartData ) => {
73+ /* const processedData: ChartData = [
7874 {
79- label : MetricMap [ metric ] . label ,
75+ label: "test" ,
8076 data: data,
8177 },
82- ] ;
83- setChartData ( processedData ) ;
78+ ]; */
79+ setChartData ( data ) ;
8480 setIsLoading ( false ) ;
8581 } ) ;
8682 } else {
@@ -98,6 +94,19 @@ const ViewsChart: React.FC<Props> = ({ options }) => {
9894 importChart ( ) ;
9995 } , [ ] ) ;
10096
97+ const chartLabel = useMemo ( ( ) => {
98+ if ( label ) return label ;
99+
100+ const metricValues : string [ ] = [ ] ;
101+
102+ Object . entries ( MetricMap ) . forEach ( ( [ key , value ] ) => {
103+ /* @ts -ignore */
104+ if ( metrics . includes ( key ) ) metricValues . push ( value . label ) ;
105+ } ) ;
106+
107+ return metricValues . join ( ", " ) ;
108+ } , [ options ] ) ;
109+
101110 const primaryAxis = React . useMemo < AxisOptions < ChartDataPoint > > ( ( ) => {
102111 return {
103112 getValue : ( datum ) => datum . timestamp ,
@@ -131,15 +140,15 @@ const ViewsChart: React.FC<Props> = ({ options }) => {
131140 chartData . length > 0 ? (
132141 < >
133142 < h1 style = { { fontSize : "1.25rem" , marginBottom : "0.5rem" } } >
134- { MetricMap [ metric ] . label } ({ timeframeIndicator } )
143+ { chartLabel } ({ timeframeIndicator } )
135144 </ h1 >
136145 < div style = { { minHeight : "200px" , position : "relative" } } >
137146 < ChartComponent
138147 options = { {
139148 data : chartData ,
140149 dark : theme . theme === "dark" ,
141150 initialHeight : 220 ,
142- tooltip : false ,
151+ tooltip : options . metrics . length > 1 ,
143152 /* @ts -ignore */
144153 primaryAxis,
145154 /* @ts -ignore */
@@ -151,18 +160,18 @@ const ViewsChart: React.FC<Props> = ({ options }) => {
151160 ) : isLoading ? (
152161 < > Loading...</ >
153162 ) : (
154- < div > No { MetricMap [ metric ] . label } data found.</ div >
163+ < div > No data found for { chartLabel } .</ div >
155164 ) }
156165 </ section >
157166 ) ;
158167} ;
159168
160- export const getViewsChart = ( props ?: any , options ?: ChartOptions ) => {
169+ export const getPageViewsChart = ( props ?: any , options ?: ChartWidget ) => {
161170 const combinedProps : Props = {
162171 ...props ,
163172 options,
164173 } ;
165- return < ViewsChart { ...combinedProps } /> ;
174+ return < PageViewsChart { ...combinedProps } /> ;
166175} ;
167176
168- export default { ViewsChart , getViewsChart } ;
177+ export default { PageViewsChart , getPageViewsChart } ;
0 commit comments