Skip to content

Commit e87b6a0

Browse files
committed
Update providers to export metricsMap
1 parent 6ecc7de commit e87b6a0

File tree

11 files changed

+46
-37
lines changed

11 files changed

+46
-37
lines changed

src/components/Aggregates/AggregateDataWidget.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import React, {
66
useRef,
77
useMemo,
88
} from "react";
9-
import type { AggregateData } from "../../types/data";
9+
import type { AggregateData, MetricsMap } from "../../types/data";
1010
import type { PageInfoWidget } from "../../types/widgets";
1111
import { useDocumentInfo } from "payload/components/utilities";
12-
import { MetricMap } from "../../providers/plausible/utilities";
1312
import { useTheme } from "payload/dist/admin/components/utilities/Theme";
1413

1514
type Props = {
1615
options: PageInfoWidget;
16+
metricsMap: MetricsMap;
1717
};
1818

19-
const AggregateDataWidget: React.FC<Props> = ({ options }) => {
19+
const AggregateDataWidget: React.FC<Props> = ({ options, metricsMap }) => {
2020
const [data, setData] = useState<AggregateData>([]);
2121
const [isLoading, setIsLoading] = useState<boolean>(true);
2222
const theme = useTheme();
@@ -36,7 +36,7 @@ const AggregateDataWidget: React.FC<Props> = ({ options }) => {
3636

3737
useEffect(() => {
3838
if (pageId) {
39-
const getAggregateData = fetch(`/api/analytics/pageAggregateData`, {
39+
const getAggregateData = fetch(`/api/analytics/pageAggregate`, {
4040
method: "post",
4141
credentials: "include",
4242
headers: {
@@ -64,13 +64,13 @@ const AggregateDataWidget: React.FC<Props> = ({ options }) => {
6464

6565
const metricValues: string[] = [];
6666

67-
Object.entries(MetricMap).forEach(([key, value]) => {
67+
Object.entries(metricsMap).forEach(([key, value]) => {
6868
/* @ts-ignore */
6969
if (metrics.includes(key)) metricValues.push(value.label);
7070
});
7171

7272
return metricValues.join(", ");
73-
}, [options]);
73+
}, [options, metricsMap]);
7474

7575
return (
7676
<section
@@ -110,6 +110,7 @@ const AggregateDataWidget: React.FC<Props> = ({ options }) => {
110110
};
111111

112112
export const getAggregateDataWidget = (
113+
metricsMap: MetricsMap,
113114
props?: any,
114115
options?: PageInfoWidget
115116
) => {

src/components/Charts/PageViewsChart.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import React, {
66
useRef,
77
useMemo,
88
} from "react";
9-
import type { ChartDataPoint, ChartData } from "../../types/data";
10-
import type { PageChartWidget, Metrics } from "../../types/widgets";
9+
import type { ChartDataPoint, ChartData, MetricsMap } from "../../types/data";
10+
import type { PageChartWidget } from "../../types/widgets";
1111
import type { AxisOptions } from "react-charts";
1212
import { useDocumentInfo } from "payload/components/utilities";
13-
import { MetricMap } from "../../providers/plausible/utilities";
1413
import { useTheme } from "payload/dist/admin/components/utilities/Theme";
1514

1615
type Props = {
1716
options: PageChartWidget;
17+
metricsMap: MetricsMap;
1818
};
1919

2020
const ChartComponent = lazy(() =>
@@ -23,7 +23,7 @@ const ChartComponent = lazy(() =>
2323
})
2424
);
2525

26-
const PageViewsChart: React.FC<Props> = ({ options }) => {
26+
const PageViewsChart: React.FC<Props> = ({ options, metricsMap }) => {
2727
const [chartData, setChartData] = useState<ChartData>([]);
2828
const [isLoading, setIsLoading] = useState<boolean>(true);
2929
const theme = useTheme();
@@ -44,7 +44,7 @@ const PageViewsChart: React.FC<Props> = ({ options }) => {
4444

4545
useEffect(() => {
4646
if (pageId) {
47-
const getChartData = fetch(`/api/analytics/pageChartData`, {
47+
const getChartData = fetch(`/api/analytics/pageChart`, {
4848
method: "post",
4949
credentials: "include",
5050
headers: {
@@ -73,7 +73,7 @@ const PageViewsChart: React.FC<Props> = ({ options }) => {
7373
if (metrics) {
7474
const metricValues: string[] = [];
7575

76-
Object.entries(MetricMap).forEach(([key, value]) => {
76+
Object.entries(metricsMap).forEach(([key, value]) => {
7777
// @ts-ignore
7878
if (metrics.includes(key)) metricValues.push(value.label);
7979
});
@@ -82,7 +82,7 @@ const PageViewsChart: React.FC<Props> = ({ options }) => {
8282
} else {
8383
return "No metrics defined for this widget";
8484
}
85-
}, [label, metrics]);
85+
}, [label, metrics, metricsMap]);
8686

8787
const primaryAxis = React.useMemo<AxisOptions<ChartDataPoint>>(() => {
8888
return {
@@ -140,7 +140,11 @@ const PageViewsChart: React.FC<Props> = ({ options }) => {
140140
);
141141
};
142142

143-
export const getPageViewsChart = (props?: any, options?: PageChartWidget) => {
143+
export const getPageViewsChart = (
144+
metricsMap: MetricsMap,
145+
props?: any,
146+
options?: PageChartWidget
147+
) => {
144148
const combinedProps: Props = {
145149
...props,
146150
options,

src/components/Live/LiveDataWidget.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import React, {
88
} from "react";
99
import type { LiveData } from "../../types/data";
1010
import type { LiveWidget } from "../../types/widgets";
11-
import { MetricMap } from "../../providers/plausible/utilities";
1211
import { useTheme } from "payload/dist/admin/components/utilities/Theme";
1312

1413
type Props = {};
@@ -21,7 +20,7 @@ const LiveDataWidget: React.FC<Props> = ({}) => {
2120
/* const { label } = options; */
2221

2322
useEffect(() => {
24-
const getLiveData = fetch(`/api/analytics/liveData`, {
23+
const getLiveData = fetch(`/api/analytics/live`, {
2524
method: "post",
2625
credentials: "include",
2726
headers: {

src/extendWebpackConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export const extendWebpackConfig =
2121
? existingWebpackConfig.resolve.alias
2222
: {}),
2323
express: mockModulePath,
24-
[path.resolve(__dirname, "./providers/plausible/client")]:
25-
mockModulePath,
24+
[path.resolve(__dirname, "./providers/")]: mockModulePath,
25+
[path.resolve(__dirname, "./routes/")]: mockModulePath,
2626
},
2727
},
2828
};

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const payloadDashboardAnalytics =
9595
...targetCollection.widgets.map((widget, index) => {
9696
const field = PageWidgetMap[widget.type];
9797

98-
return field(widget, index);
98+
return field(widget, index, apiProvider.metricsMap);
9999
}),
100100
],
101101
};
@@ -121,7 +121,7 @@ const payloadDashboardAnalytics =
121121
...targetGlobal.widgets.map((widget, index) => {
122122
const field = PageWidgetMap[widget.type];
123123

124-
return field(widget, index);
124+
return field(widget, index, apiProvider.metricsMap);
125125
}),
126126
],
127127
};

src/providers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
AggregateData,
77
LiveData,
88
ReportData,
9+
MetricsMap,
910
} from "../types/data";
1011

1112
type BaseOptions = {
@@ -46,6 +47,7 @@ export type ApiProvider = {
4647
getPageChartData: (options: PageChartOptions) => Promise<ChartData>;
4748
getLiveData: (options: LiveDataOptions) => Promise<LiveData>;
4849
getReportData: (options: ReportDataOptions) => Promise<ReportData>;
50+
metricsMap: MetricsMap;
4951
};
5052

5153
const getProvider = (provider: Provider) => {

src/providers/plausible/client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { PlausibleProvider } from "../../types/providers";
2-
import type { AllAvailableMetrics } from "../../types/widgets";
2+
import type { Metrics } from "../../types/widgets";
33
import { MetricMap } from "./utilities";
44

55
type ClientOptions = {
66
endpoint: string;
77
timeframe?: string;
8-
metrics?: AllAvailableMetrics[];
8+
metrics?: Metrics[];
99
};
1010

1111
function client(provider: PlausibleProvider, options: ClientOptions) {
@@ -54,6 +54,7 @@ function client(provider: PlausibleProvider, options: ClientOptions) {
5454
baseUrl: baseUrl,
5555
metric: plausibleMetrics,
5656
url: url,
57+
metricsMap: MetricMap,
5758
fetch: async (customUrl?: string) => {
5859
const fetchUrl = customUrl ?? url.toString();
5960

src/providers/plausible/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import type {
1515
ReportDataOptions,
1616
} from "..";
1717

18+
import { MetricMap } from "./utilities";
19+
1820
const plausible = (provider: PlausibleProvider): ApiProvider => {
1921
return {
2022
getGlobalAggregateData: async (options: GlobalAggregateOptions) =>
@@ -29,6 +31,7 @@ const plausible = (provider: PlausibleProvider): ApiProvider => {
2931
await getLiveData(provider, options),
3032
getReportData: async (options: ReportDataOptions) =>
3133
await getReportData(provider, options),
34+
metricsMap: MetricMap,
3235
};
3336
};
3437

src/types/data.d.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AllAvailableMetrics } from "./widgets";
1+
import type { Metrics, Properties } from "./widgets";
22

33
export interface ChartDataPoint {
44
timestamp: Date;
@@ -13,7 +13,7 @@ export interface ChartDataSeries {
1313
export type ChartData = ChartDataSeries[];
1414

1515
export type AggregateData = Array<{
16-
label: AllAvailableMetrics;
16+
label: Metrics;
1717
value: string | number;
1818
}>;
1919

@@ -23,7 +23,6 @@ export type LiveData = {
2323

2424
export type ReportData = {}[];
2525

26-
export type MetricsMap = Record<
27-
AllAvailableMetrics,
28-
{ label: string; value: string }
29-
>;
26+
export type MetricsMap = Record<Metrics, { label: string; value: string }>;
27+
28+
export type PropertyMap = Record<Properties, { label: string; value: string }>;

src/types/widgets.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export type Properties =
1616
| "source"
1717
| "country";
1818

19-
export type AllAvailableMetrics = Metrics;
20-
2119
/* Keeping this for later */
2220
/* export type Reports = "topSources" | "topPages" | "topCountries"; */
2321

0 commit comments

Comments
 (0)