1- import { Config as PayloadConfig } from "payload/config" ;
2- import { DashboardAnalyticsConfig } from "./types" ;
1+ import type { Config as PayloadConfig } from "payload/config" ;
2+ import type { DashboardAnalyticsConfig } from "./types" ;
33import { extendWebpackConfig } from "./extendWebpackConfig" ;
44import getProvider from "./providers" ;
55import getGlobalAggregateData from "./routes/getGlobalAggregateData" ;
6+ import getGlobalChartData from "./routes/getGlobalChartData" ;
7+ import type { CollectionConfig } from "payload/dist/collections/config/types" ;
8+ import { getViewsChart } from "./components/Charts/ViewsChart" ;
69
710const payloadDashboardAnalytics =
811 ( incomingConfig : DashboardAnalyticsConfig ) =>
912 ( config : PayloadConfig ) : PayloadConfig => {
10- const { admin } = config ;
13+ const { admin, collections } = config ;
1114 const { provider } = incomingConfig ;
1215 const endpoints = config . endpoints ?? [ ] ;
1316 const apiProvider = getProvider ( provider ) ;
@@ -18,7 +21,42 @@ const payloadDashboardAnalytics =
1821 ...admin ,
1922 webpack : extendWebpackConfig ( config ) ,
2023 } ,
21- endpoints : [ ...endpoints , getGlobalAggregateData ( apiProvider ) ] ,
24+ endpoints : [
25+ ...endpoints ,
26+ getGlobalAggregateData ( apiProvider ) ,
27+ getGlobalChartData ( apiProvider ) ,
28+ ] ,
29+ ...( collections && {
30+ collections : collections . map ( ( collection ) => {
31+ const targetCollection = incomingConfig . collections ?. find ( ( col ) => {
32+ if ( col . slug === collection . slug ) return true ;
33+ return false ;
34+ } ) ;
35+
36+ if ( targetCollection ) {
37+ const collectionConfigWithHooks : CollectionConfig = {
38+ ...collection ,
39+ fields : [
40+ ...collection . fields ,
41+ {
42+ type : "ui" ,
43+ name : "viewschart" ,
44+ admin : {
45+ position : "sidebar" ,
46+ components : {
47+ Field : ( props ) => getViewsChart ( props ) ,
48+ } ,
49+ } ,
50+ } ,
51+ ] ,
52+ } ;
53+
54+ return collectionConfigWithHooks ;
55+ }
56+
57+ return collection ;
58+ } ) ,
59+ } ) ,
2260 } ;
2361
2462 return processedConfig ;
0 commit comments