Skip to content

Commit 74c69ff

Browse files
committed
Update widget logic
1 parent bdec160 commit 74c69ff

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

demo/src/payload.config.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,15 @@ export default buildConfig({
5454
payloadDashboardAnalytics({
5555
provider: plausibleProvider,
5656
navigation: {
57-
BeforeNavLinks: [
58-
{
59-
type: "live",
60-
},
61-
],
62-
AfterNavLinks: [
57+
afterNavLinks: [
6358
{
6459
type: "live",
6560
},
6661
],
6762
},
63+
dashboard: {
64+
beforeDashboard: ["topPages", "viewsChart"],
65+
},
6866
globals: [
6967
{
7068
slug: "homepage",

src/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type {
44
PageInfoWidget,
55
PageChartWidget,
66
PageWidgets,
7+
NavigationWidgets,
8+
DashboardWidgets,
79
} from "./types/widgets";
810
import type { Field } from "payload/dist/fields/config/types";
911
import { extendWebpackConfig } from "./extendWebpackConfig";
@@ -19,7 +21,7 @@ import type { CollectionConfig } from "payload/dist/collections/config/types";
1921
import type { GlobalConfig } from "payload/dist/globals/config/types";
2022
import { getPageViewsChart } from "./components/Charts/PageViewsChart";
2123
import { getAggregateDataWidget } from "./components/Aggregates/AggregateDataWidget";
22-
import LiveDataWidget from "./components/Live/LiveDataWidget";
24+
import LiveDataComponent from "./components/Live/LiveDataWidget";
2325

2426
const PageWidgetMap: Record<
2527
PageWidgets["type"],
@@ -47,6 +49,10 @@ const PageWidgetMap: Record<
4749
}),
4850
};
4951

52+
const NavigationWidgetMap: Record<NavigationWidgets["type"], React.FC> = {
53+
live: LiveDataComponent.LiveDataWidget,
54+
};
55+
5056
const payloadDashboardAnalytics =
5157
(incomingConfig: DashboardAnalyticsConfig) =>
5258
(config: PayloadConfig): PayloadConfig => {
@@ -61,16 +67,20 @@ const payloadDashboardAnalytics =
6167
...admin,
6268
components: {
6369
...admin?.components,
64-
...(navigation?.BeforeNavLinks && {
70+
...(navigation?.beforeNavLinks && {
6571
beforeNavLinks: [
6672
...(admin?.components?.beforeNavLinks ?? []),
67-
LiveDataWidget.LiveDataWidget,
73+
...navigation.beforeNavLinks.map(
74+
(widget) => NavigationWidgetMap[widget.type]
75+
),
6876
],
6977
}),
70-
...(navigation?.AfterNavLinks && {
78+
...(navigation?.afterNavLinks && {
7179
afterNavLinks: [
7280
...(admin?.components?.afterNavLinks ?? []),
73-
LiveDataWidget.LiveDataWidget,
81+
...navigation.afterNavLinks.map(
82+
(widget) => NavigationWidgetMap[widget.type]
83+
),
7484
],
7585
}),
7686
},

src/types/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export type DashboardAnalyticsConfig = {
2828
collections?: Collection[];
2929
globals?: Global[];
3030
navigation?: {
31-
BeforeNavLinks?: NavigationWidgets[];
32-
AfterNavLinks?: NavigationWidgets[];
31+
beforeNavLinks?: NavigationWidgets[];
32+
afterNavLinks?: NavigationWidgets[];
33+
};
34+
dashboard?: {
35+
beforeDashboard?: DashboardWidgets[];
36+
dashboard?: DashboardWidgets[];
37+
afterDashboard?: DashboardWidgets[];
3338
};
3439
};

src/types/widgets.d.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ export type Metrics =
99
| "bounceRate"
1010
| "sessionDuration";
1111

12+
export type AllAvailableMetrics = Metrics;
13+
14+
/* Keeping this for later */
15+
/* export type Reports = "topSources" | "topPages" | "topCountries"; */
16+
1217
export interface ChartWidget {
1318
type: "chart";
14-
label?: string | "hidden";
1519
metrics: Metrics[];
1620
timeframe?: Timeframes;
21+
label?: string | "hidden";
1722
}
1823

1924
export interface PageChartWidget extends ChartWidget {
2025
idMatcher: IdMatcherFunction;
2126
}
2227

23-
export type AllAvailableMetrics = Metrics;
24-
2528
export interface InfoWidget {
2629
type: "info";
2730
label?: string | "hidden";
@@ -31,14 +34,21 @@ export interface InfoWidget {
3134

3235
export interface LiveWidget {
3336
type: "live";
34-
//label?: string | "hidden";
37+
}
38+
39+
export interface ReportWidget {
40+
type: "report";
41+
report: Reports;
42+
timeframe?: Timeframes;
3543
}
3644

3745
export interface PageInfoWidget extends InfoWidget {
3846
idMatcher: IdMatcherFunction;
3947
}
4048

41-
export type DashboardWidgets = ChartWidget | InfoWidget;
49+
/* export type DashboardWidgets = ChartWidget | InfoWidget | ReportWidget; */
50+
51+
export type DashboardWidgets = "topPages" | "viewsChart";
4252

4353
export type NavigationWidgets = LiveWidget;
4454

0 commit comments

Comments
 (0)