Skip to content

Commit bdec160

Browse files
committed
Add configuration for globals
1 parent 54ec32e commit bdec160

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

demo/src/globals/Homepage.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { GlobalConfig } from "payload/types";
2+
3+
const Homepage: GlobalConfig = {
4+
slug: "homepage",
5+
fields: [],
6+
};
7+
8+
export default Homepage;

demo/src/payload.config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Posts from "./collections/Posts";
55
import Tags from "./collections/Tags";
66
import Users from "./collections/Users";
77
import Media from "./collections/Media";
8+
import Homepage from "./globals/Homepage";
89
import payloadDashboardAnalytics from "../../src/index";
910
import { PlausibleProvider } from "../../src/types/providers";
1011

@@ -42,6 +43,7 @@ export default buildConfig({
4243
},
4344
},
4445
collections: [Categories, Posts, Tags, Users, Media],
46+
globals: [Homepage],
4547
typescript: {
4648
outputFile: path.resolve(__dirname, "payload-types.ts"),
4749
},
@@ -63,6 +65,27 @@ export default buildConfig({
6365
},
6466
],
6567
},
68+
globals: [
69+
{
70+
slug: "homepage",
71+
widgets: [
72+
{
73+
type: "chart",
74+
label: "Views and visitors",
75+
metrics: ["views", "visitors", "sessions"],
76+
timeframe: "30d",
77+
idMatcher: () => `/`,
78+
},
79+
{
80+
type: "info",
81+
label: "Page data",
82+
metrics: ["views", "sessions", "sessionDuration"],
83+
timeframe: "currentMonth",
84+
idMatcher: () => `/`,
85+
},
86+
],
87+
},
88+
],
6689
collections: [
6790
{
6891
slug: Posts.slug,

src/index.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import getPageAggregateData from "./routes/getPageAggregateData";
1616
import getLiveData from "./routes/getLiveData";
1717

1818
import type { CollectionConfig } from "payload/dist/collections/config/types";
19+
import type { GlobalConfig } from "payload/dist/globals/config/types";
1920
import { getPageViewsChart } from "./components/Charts/PageViewsChart";
2021
import { getAggregateDataWidget } from "./components/Aggregates/AggregateDataWidget";
2122
import LiveDataWidget from "./components/Live/LiveDataWidget";
@@ -49,7 +50,7 @@ const PageWidgetMap: Record<
4950
const payloadDashboardAnalytics =
5051
(incomingConfig: DashboardAnalyticsConfig) =>
5152
(config: PayloadConfig): PayloadConfig => {
52-
const { admin, collections } = config;
53+
const { admin, collections, globals } = config;
5354
const { provider, navigation } = incomingConfig;
5455
const endpoints = config.endpoints ?? [];
5556
const apiProvider = getProvider(provider);
@@ -85,10 +86,12 @@ const payloadDashboardAnalytics =
8586
],
8687
...(collections && {
8788
collections: collections.map((collection) => {
88-
const targetCollection = incomingConfig.collections?.find((col) => {
89-
if (col.slug === collection.slug) return true;
90-
return false;
91-
});
89+
const targetCollection = incomingConfig.collections?.find(
90+
(pluginCollection) => {
91+
if (pluginCollection.slug === collection.slug) return true;
92+
return false;
93+
}
94+
);
9295

9396
if (targetCollection) {
9497
const collectionConfigWithHooks: CollectionConfig = {
@@ -109,6 +112,32 @@ const payloadDashboardAnalytics =
109112
return collection;
110113
}),
111114
}),
115+
...(globals && {
116+
globals: globals.map((global) => {
117+
const targetGlobal = incomingConfig.globals?.find((pluginGlobal) => {
118+
if (pluginGlobal.slug === global.slug) return true;
119+
return false;
120+
});
121+
122+
if (targetGlobal) {
123+
const globalConfigWithHooks: GlobalConfig = {
124+
...global,
125+
fields: [
126+
...global.fields,
127+
...targetGlobal.widgets.map((widget, index) => {
128+
const field = PageWidgetMap[widget.type];
129+
130+
return field(widget, index);
131+
}),
132+
],
133+
};
134+
135+
return globalConfigWithHooks;
136+
}
137+
138+
return global;
139+
}),
140+
}),
112141
};
113142

114143
return processedConfig;

src/types/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ export interface Collection extends PageItemConfig {
1717
slug: string;
1818
}
1919

20-
export interface Global extends PageItemConfig {}
20+
export interface Global extends PageItemConfig {
21+
slug: string;
22+
}
2123

2224
export type Provider = PlausibleProvider;
2325

2426
export type DashboardAnalyticsConfig = {
2527
provider: Provider;
2628
collections?: Collection[];
29+
globals?: Global[];
2730
navigation?: {
2831
BeforeNavLinks?: NavigationWidgets[];
2932
AfterNavLinks?: NavigationWidgets[];

0 commit comments

Comments
 (0)