Skip to content

Commit 54ec32e

Browse files
committed
Add config for navigation components
1 parent 59294ed commit 54ec32e

File tree

6 files changed

+130
-26
lines changed

6 files changed

+130
-26
lines changed

demo/src/payload.config.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ export default buildConfig({
5151
plugins: [
5252
payloadDashboardAnalytics({
5353
provider: plausibleProvider,
54+
navigation: {
55+
BeforeNavLinks: [
56+
{
57+
type: "live",
58+
},
59+
],
60+
AfterNavLinks: [
61+
{
62+
type: "live",
63+
},
64+
],
65+
},
5466
collections: [
5567
{
5668
slug: Posts.slug,
@@ -62,28 +74,11 @@ export default buildConfig({
6274
timeframe: "30d",
6375
idMatcher: (document: any) => `/articles/${document.slug}`,
6476
},
65-
/* {
66-
type: "chart",
67-
metrics: ["sessions"],
68-
timeframe: "7d",
69-
idMatcher: (document: any) => `/articles/${document.slug}`,
70-
},
71-
{
72-
type: "chart",
73-
metrics: ["sessionDuration"],
74-
timeframe: "currentMonth",
75-
idMatcher: (document: any) => `/articles/${document.slug}`,
76-
}, */
77-
{
78-
type: "info",
79-
metrics: ["views"],
80-
idMatcher: (document: any) => `/articles/${document.slug}`,
81-
label: "hidden",
82-
},
8377
{
8478
type: "info",
85-
metrics: ["sessions", "sessionDuration"],
86-
timeframe: "6mo",
79+
label: "Page data",
80+
metrics: ["views", "sessions", "sessionDuration"],
81+
timeframe: "currentMonth",
8782
idMatcher: (document: any) => `/articles/${document.slug}`,
8883
},
8984
],

src/components/Aggregates/AggregateDataWidget.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const AggregateDataWidget: React.FC<Props> = ({ options }) => {
7878
marginBottom: "1.5rem",
7979
border: "1px solid",
8080
borderColor: "var(--theme-elevation-100)",
81-
padding: "0.5rem",
81+
padding: "1rem",
8282
}}
8383
>
8484
{label !== "hidden" && (
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import React, {
2+
useEffect,
3+
useState,
4+
lazy,
5+
useReducer,
6+
useRef,
7+
useMemo,
8+
} from "react";
9+
import type { LiveData } from "../../types/data";
10+
import type { LiveWidget } from "../../types/widgets";
11+
import { MetricMap } from "../../providers/plausible/utilities";
12+
import { useTheme } from "payload/dist/admin/components/utilities/Theme";
13+
14+
type Props = {};
15+
16+
const LiveDataWidget: React.FC<Props> = ({}) => {
17+
const [data, setData] = useState<LiveData>();
18+
const [isLoading, setIsLoading] = useState<boolean>(true);
19+
const theme = useTheme();
20+
21+
/* const { label } = options; */
22+
23+
useEffect(() => {
24+
const getLiveData = fetch(`/api/analytics/liveData`, {
25+
method: "post",
26+
credentials: "include",
27+
headers: {
28+
Accept: "application/json",
29+
"Content-Type": "application/json",
30+
},
31+
body: JSON.stringify({}),
32+
}).then((response) => response.json());
33+
34+
getLiveData.then((data: LiveData) => {
35+
setData(data);
36+
setIsLoading(false);
37+
});
38+
}, []);
39+
40+
const heading = useMemo(() => {
41+
/* if (label) return label; */
42+
43+
return "hidden";
44+
}, []);
45+
46+
return (
47+
<section
48+
style={{
49+
marginBottom: "1.5rem",
50+
border: "1px solid",
51+
borderColor: "var(--theme-elevation-100)",
52+
padding: "0.5rem",
53+
width: "100%",
54+
}}
55+
>
56+
{heading !== "hidden" && (
57+
<h1 style={{ fontSize: "1.25rem", marginBottom: "0.75rem" }}>
58+
{heading}
59+
</h1>
60+
)}
61+
<div>
62+
{isLoading ? (
63+
<>Loading...</>
64+
) : (
65+
<ul style={{ margin: "0", listStyle: "none", padding: "0" }}>
66+
<li style={{ display: "flex", justifyContent: "space-between" }}>
67+
<div style={{ fontWeight: "700" }}>Live visitors</div>
68+
<div>{data?.visitors}</div>
69+
</li>
70+
</ul>
71+
)}
72+
</div>
73+
</section>
74+
);
75+
};
76+
77+
export const getLiveDataWidget = (props?: any, options?: LiveWidget) => {
78+
const combinedProps: Props = {
79+
...props,
80+
options,
81+
};
82+
return <LiveDataWidget {...combinedProps} />;
83+
};
84+
85+
export default { LiveDataWidget, getLiveDataWidget };

src/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import getLiveData from "./routes/getLiveData";
1818
import type { CollectionConfig } from "payload/dist/collections/config/types";
1919
import { getPageViewsChart } from "./components/Charts/PageViewsChart";
2020
import { getAggregateDataWidget } from "./components/Aggregates/AggregateDataWidget";
21+
import LiveDataWidget from "./components/Live/LiveDataWidget";
2122

2223
const PageWidgetMap: Record<
2324
PageWidgets["type"],
@@ -49,17 +50,29 @@ const payloadDashboardAnalytics =
4950
(incomingConfig: DashboardAnalyticsConfig) =>
5051
(config: PayloadConfig): PayloadConfig => {
5152
const { admin, collections } = config;
52-
const { provider } = incomingConfig;
53+
const { provider, navigation } = incomingConfig;
5354
const endpoints = config.endpoints ?? [];
5455
const apiProvider = getProvider(provider);
5556

5657
const processedConfig: PayloadConfig = {
5758
...config,
5859
admin: {
5960
...admin,
60-
/* components: {
61-
beforeDashboard: [() => getViewsChart()],
62-
}, */
61+
components: {
62+
...admin?.components,
63+
...(navigation?.BeforeNavLinks && {
64+
beforeNavLinks: [
65+
...(admin?.components?.beforeNavLinks ?? []),
66+
LiveDataWidget.LiveDataWidget,
67+
],
68+
}),
69+
...(navigation?.AfterNavLinks && {
70+
afterNavLinks: [
71+
...(admin?.components?.afterNavLinks ?? []),
72+
LiveDataWidget.LiveDataWidget,
73+
],
74+
}),
75+
},
6376
webpack: extendWebpackConfig(config),
6477
},
6578
endpoints: [

src/types/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { PlausibleProvider } from "./providers";
22
import type {
33
DashboardWidgets,
44
PageWidgets,
5-
IdMatcherFunction,
5+
NavigationWidgets,
66
} from "./widgets";
77

88
export interface ItemConfig {
@@ -24,4 +24,8 @@ export type Provider = PlausibleProvider;
2424
export type DashboardAnalyticsConfig = {
2525
provider: Provider;
2626
collections?: Collection[];
27+
navigation?: {
28+
BeforeNavLinks?: NavigationWidgets[];
29+
AfterNavLinks?: NavigationWidgets[];
30+
};
2731
};

src/types/widgets.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ export interface InfoWidget {
2929
timeframe?: Timeframes;
3030
}
3131

32+
export interface LiveWidget {
33+
type: "live";
34+
//label?: string | "hidden";
35+
}
36+
3237
export interface PageInfoWidget extends InfoWidget {
3338
idMatcher: IdMatcherFunction;
3439
}
3540

3641
export type DashboardWidgets = ChartWidget | InfoWidget;
3742

43+
export type NavigationWidgets = LiveWidget;
44+
3845
export type PageWidgets = PageChartWidget | PageInfoWidget;

0 commit comments

Comments
 (0)