@10x-media/analytics 0.1.0-beta.0 → 0.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +127 -0
- package/README.md +38 -357
- package/dist/adapters/ga4/ga4.d.ts +20 -0
- package/dist/adapters/ga4/ga4.js +187 -0
- package/dist/adapters/ga4/ga4.js.map +1 -0
- package/dist/{fetchJson-DZb2_Puj.js → adapters/http/fetchJson.js} +2 -2
- package/dist/adapters/http/fetchJson.js.map +1 -0
- package/dist/adapters/plausible/plausible.d.ts +15 -0
- package/dist/adapters/plausible/plausible.js +148 -0
- package/dist/adapters/plausible/plausible.js.map +1 -0
- package/dist/adapters/posthog/posthog.d.ts +23 -0
- package/dist/adapters/posthog/posthog.js +132 -0
- package/dist/adapters/posthog/posthog.js.map +1 -0
- package/dist/{series-zytbzPoc.js → adapters/series.js} +2 -2
- package/dist/adapters/series.js.map +1 -0
- package/dist/adapters/umami/umami.d.ts +18 -0
- package/dist/adapters/umami/umami.js +139 -0
- package/dist/{exports/adapters → adapters/umami}/umami.js.map +1 -1
- package/dist/binding/resolvePath.js +61 -0
- package/dist/binding/resolvePath.js.map +1 -0
- package/dist/binding/types.d.ts +55 -0
- package/dist/charts/BarList.d.ts +20 -0
- package/dist/charts/BarList.js +73 -0
- package/dist/charts/BarList.js.map +1 -0
- package/dist/charts/TrendChart.d.ts +25 -0
- package/dist/charts/TrendChart.js +195 -0
- package/dist/charts/TrendChart.js.map +1 -0
- package/dist/charts/bars.d.ts +9 -0
- package/dist/charts/bars.js +19 -0
- package/dist/charts/bars.js.map +1 -0
- package/dist/charts/bucket.js +86 -0
- package/dist/charts/bucket.js.map +1 -0
- package/dist/charts/spline.js +84 -0
- package/dist/charts/spline.js.map +1 -0
- package/dist/charts/styles.js +42 -0
- package/dist/charts/styles.js.map +1 -0
- package/dist/core/cacheKey.js +28 -0
- package/dist/core/cacheKey.js.map +1 -0
- package/dist/core/capabilities.d.ts +12 -0
- package/dist/core/capabilities.js +12 -0
- package/dist/core/capabilities.js.map +1 -0
- package/dist/{contract-CWZPKWuR.d.ts → core/contract.d.ts} +42 -5
- package/dist/core/contract.js +10 -0
- package/dist/core/contract.js.map +1 -0
- package/dist/core/granularity.js +17 -0
- package/dist/core/granularity.js.map +1 -0
- package/dist/core/options.d.ts +140 -0
- package/dist/core/options.js +114 -0
- package/dist/core/options.js.map +1 -0
- package/dist/core/registry.js +24 -0
- package/dist/core/registry.js.map +1 -0
- package/dist/core/scopedRead.js +57 -0
- package/dist/core/scopedRead.js.map +1 -0
- package/dist/exports/adapters/ga4.d.ts +2 -20
- package/dist/exports/adapters/ga4.js +1 -176
- package/dist/exports/adapters/native.d.ts +2 -22
- package/dist/exports/adapters/native.js +1 -848
- package/dist/exports/adapters/plausible.d.ts +2 -15
- package/dist/exports/adapters/plausible.js +1 -142
- package/dist/exports/adapters/posthog.d.ts +2 -17
- package/dist/exports/adapters/posthog.js +1 -118
- package/dist/exports/adapters/umami.d.ts +2 -18
- package/dist/exports/adapters/umami.js +1 -138
- package/dist/exports/client.d.ts +5 -77
- package/dist/exports/client.js +4 -386
- package/dist/exports/geo.d.ts +4 -19
- package/dist/exports/geo.js +3 -1
- package/dist/exports/i18n.d.ts +3 -63
- package/dist/exports/i18n.js +2 -2
- package/dist/exports/next.d.ts +2 -0
- package/dist/exports/next.js +2 -0
- package/dist/exports/rsc.d.ts +16 -120
- package/dist/exports/rsc.js +14 -598
- package/dist/exports/types.d.ts +6 -4
- package/dist/exports/types.js +2 -1
- package/dist/fields/AnalyticsPanelClient.js +193 -0
- package/dist/fields/AnalyticsPanelClient.js.map +1 -0
- package/dist/fields/AnalyticsPanelField.d.ts +28 -0
- package/dist/fields/AnalyticsPanelField.js +54 -0
- package/dist/fields/AnalyticsPanelField.js.map +1 -0
- package/dist/fields/AnalyticsStatField.d.ts +31 -0
- package/dist/fields/AnalyticsStatField.js +104 -0
- package/dist/fields/AnalyticsStatField.js.map +1 -0
- package/dist/fields/dateRange/field.js +55 -0
- package/dist/fields/dateRange/field.js.map +1 -0
- package/dist/fields/dateRange/styles.js +23 -0
- package/dist/fields/dateRange/styles.js.map +1 -0
- package/dist/fields/dateRange/types.d.ts +11 -0
- package/dist/fields/dateRange/ui.d.ts +9 -0
- package/dist/fields/dateRange/ui.js +373 -0
- package/dist/fields/dateRange/ui.js.map +1 -0
- package/dist/fields/emptyState.js +46 -0
- package/dist/fields/emptyState.js.map +1 -0
- package/dist/fields/factories.d.ts +60 -0
- package/dist/fields/factories.js +94 -0
- package/dist/fields/factories.js.map +1 -0
- package/dist/fields/format.d.ts +7 -0
- package/dist/fields/format.js +22 -0
- package/dist/fields/format.js.map +1 -0
- package/dist/fields/readForDocument.d.ts +57 -0
- package/dist/fields/readForDocument.js +147 -0
- package/dist/fields/readForDocument.js.map +1 -0
- package/dist/index.d.ts +18 -3
- package/dist/index.js +83 -1028
- package/dist/index.js.map +1 -1
- package/dist/native/collections/events.js +101 -0
- package/dist/native/collections/events.js.map +1 -0
- package/dist/native/collections/rollups.js +115 -0
- package/dist/native/collections/rollups.js.map +1 -0
- package/dist/native/collections/seen.js +47 -0
- package/dist/native/collections/seen.js.map +1 -0
- package/dist/native/geo/composeGeoResolvers.d.ts +7 -0
- package/dist/native/geo/composeGeoResolvers.js +14 -0
- package/dist/native/geo/composeGeoResolvers.js.map +1 -0
- package/dist/{geoResolver-CasIa4T9.d.ts → native/geo/geoResolver.d.ts} +2 -2
- package/dist/native/geo/geoResolver.js +11 -0
- package/dist/native/geo/geoResolver.js.map +1 -0
- package/dist/native/geo/maxmindResolver.d.ts +16 -0
- package/dist/native/geo/maxmindResolver.js +55 -0
- package/dist/native/geo/maxmindResolver.js.map +1 -0
- package/dist/native/ingest/device.js +15 -0
- package/dist/native/ingest/device.js.map +1 -0
- package/dist/native/ingest/endpoint.js +30 -0
- package/dist/native/ingest/endpoint.js.map +1 -0
- package/dist/native/ingest/flushBatch.js +69 -0
- package/dist/native/ingest/flushBatch.js.map +1 -0
- package/dist/native/ingest/normalizeEvent.js +39 -0
- package/dist/native/ingest/normalizeEvent.js.map +1 -0
- package/dist/native/ingest/salt.js +14 -0
- package/dist/native/ingest/salt.js.map +1 -0
- package/dist/native/ingest/source.js +22 -0
- package/dist/native/ingest/source.js.map +1 -0
- package/dist/native/ingest/visitorHash.js +12 -0
- package/dist/native/ingest/visitorHash.js.map +1 -0
- package/dist/native/ingest/writeBuffer.js +55 -0
- package/dist/native/ingest/writeBuffer.js.map +1 -0
- package/dist/native/ingest/writeEvent.js +14 -0
- package/dist/native/ingest/writeEvent.js.map +1 -0
- package/dist/native/nativeAdapter.d.ts +22 -0
- package/dist/native/nativeAdapter.js +216 -0
- package/dist/native/nativeAdapter.js.map +1 -0
- package/dist/native/realtime/buildRealtime.js +53 -0
- package/dist/native/realtime/buildRealtime.js.map +1 -0
- package/dist/native/retention/pruneTask.js +29 -0
- package/dist/native/retention/pruneTask.js.map +1 -0
- package/dist/native/rollupAcc.js +53 -0
- package/dist/native/rollupAcc.js.map +1 -0
- package/dist/native/rollups/applyDistinctDeltas.js +9 -0
- package/dist/native/rollups/applyDistinctDeltas.js.map +1 -0
- package/dist/native/rollups/bumpRollup.js +52 -0
- package/dist/native/rollups/bumpRollup.js.map +1 -0
- package/dist/native/rollups/deltas.js +31 -0
- package/dist/native/rollups/deltas.js.map +1 -0
- package/dist/native/rollups/insertIfNew.js +19 -0
- package/dist/native/rollups/insertIfNew.js.map +1 -0
- package/dist/next/posthogProxyRewrites.d.ts +36 -0
- package/dist/next/posthogProxyRewrites.js +50 -0
- package/dist/next/posthogProxyRewrites.js.map +1 -0
- package/dist/plugin/documentEndpoint.js +80 -0
- package/dist/plugin/documentEndpoint.js.map +1 -0
- package/dist/plugin/paths.js +11 -0
- package/dist/plugin/paths.js.map +1 -0
- package/dist/plugin/realtimeEndpoint.js +36 -0
- package/dist/plugin/realtimeEndpoint.js.map +1 -0
- package/dist/plugin/registerTranslations.js +19 -0
- package/dist/plugin/registerTranslations.js.map +1 -0
- package/dist/plugin/runtime.js +21 -0
- package/dist/plugin/runtime.js.map +1 -0
- package/dist/plugin/warmTask.js +165 -0
- package/dist/plugin/warmTask.js.map +1 -0
- package/dist/providers/collection.js +159 -0
- package/dist/providers/collection.js.map +1 -0
- package/dist/providers/factory.js +62 -0
- package/dist/providers/factory.js.map +1 -0
- package/dist/providers/resolver.js +67 -0
- package/dist/providers/resolver.js.map +1 -0
- package/dist/providers/secrets.js +27 -0
- package/dist/providers/secrets.js.map +1 -0
- package/dist/surfacing/cacheStore.js +26 -0
- package/dist/surfacing/cacheStore.js.map +1 -0
- package/dist/surfacing/coalesce.js +15 -0
- package/dist/surfacing/coalesce.js.map +1 -0
- package/dist/surfacing/engine.js +63 -0
- package/dist/surfacing/engine.js.map +1 -0
- package/dist/surfacing/queue.js +44 -0
- package/dist/surfacing/queue.js.map +1 -0
- package/dist/sync/collection.js +70 -0
- package/dist/sync/collection.js.map +1 -0
- package/dist/sync/syncTask.js +123 -0
- package/dist/sync/syncTask.js.map +1 -0
- package/dist/testing/memoryAdapter.d.ts +1 -1
- package/dist/timeframe/presets.d.ts +6 -0
- package/dist/timeframe/presets.js +59 -0
- package/dist/timeframe/presets.js.map +1 -0
- package/dist/timeframe/tz.js +110 -0
- package/dist/timeframe/tz.js.map +1 -0
- package/dist/translations/de.js +78 -0
- package/dist/translations/de.js.map +1 -0
- package/dist/{translations-keiKtSjF.js → translations/en.js} +30 -25
- package/dist/translations/en.js.map +1 -0
- package/dist/translations/index.d.ts +15 -0
- package/dist/translations/index.js +30 -0
- package/dist/translations/index.js.map +1 -0
- package/dist/translations/keys.d.ts +81 -0
- package/dist/{keys-DEqL4LOT.js → translations/keys.js} +29 -5
- package/dist/translations/keys.js.map +1 -0
- package/dist/translations/metricKeys.js +28 -0
- package/dist/translations/metricKeys.js.map +1 -0
- package/dist/translations/server.js +13 -0
- package/dist/translations/server.js.map +1 -0
- package/dist/translations/useTranslation.js +12 -0
- package/dist/translations/useTranslation.js.map +1 -0
- package/dist/widgets/AnalyticsBreakdownWidget.d.ts +7 -0
- package/dist/widgets/AnalyticsBreakdownWidget.js +85 -0
- package/dist/widgets/AnalyticsBreakdownWidget.js.map +1 -0
- package/dist/widgets/AnalyticsMetricWidget.d.ts +7 -0
- package/dist/widgets/AnalyticsMetricWidget.js +89 -0
- package/dist/widgets/AnalyticsMetricWidget.js.map +1 -0
- package/dist/widgets/AnalyticsRealtimeWidget.d.ts +7 -0
- package/dist/widgets/AnalyticsRealtimeWidget.js +64 -0
- package/dist/widgets/AnalyticsRealtimeWidget.js.map +1 -0
- package/dist/widgets/AnalyticsTrendWidget.d.ts +7 -0
- package/dist/widgets/AnalyticsTrendWidget.js +99 -0
- package/dist/widgets/AnalyticsTrendWidget.js.map +1 -0
- package/dist/widgets/ComparisonDelta.js +83 -0
- package/dist/widgets/ComparisonDelta.js.map +1 -0
- package/dist/widgets/RealtimeCounter.d.ts +19 -0
- package/dist/widgets/RealtimeCounter.js +76 -0
- package/dist/widgets/RealtimeCounter.js.map +1 -0
- package/dist/widgets/breakdownTypes.js +29 -0
- package/dist/widgets/breakdownTypes.js.map +1 -0
- package/dist/widgets/cardChrome.d.ts +8 -0
- package/dist/widgets/cardChrome.js +22 -0
- package/dist/widgets/cardChrome.js.map +1 -0
- package/dist/widgets/comparison.js +43 -0
- package/dist/widgets/comparison.js.map +1 -0
- package/dist/widgets/customWidget.d.ts +20 -0
- package/dist/widgets/customWidget.js +30 -0
- package/dist/widgets/customWidget.js.map +1 -0
- package/dist/widgets/defaults.d.ts +12 -0
- package/dist/widgets/defaults.js +72 -0
- package/dist/widgets/defaults.js.map +1 -0
- package/dist/widgets/range.js +28 -0
- package/dist/widgets/range.js.map +1 -0
- package/dist/widgets/readForWidget.d.ts +31 -0
- package/dist/widgets/readForWidget.js +72 -0
- package/dist/widgets/readForWidget.js.map +1 -0
- package/dist/widgets/readForWidgetBreakdown.d.ts +38 -0
- package/dist/widgets/readForWidgetBreakdown.js +80 -0
- package/dist/widgets/readForWidgetBreakdown.js.map +1 -0
- package/dist/{readForWidgetRealtime-D1BD37ST.d.ts → widgets/readForWidgetRealtime.d.ts} +5 -3
- package/dist/widgets/readForWidgetRealtime.js +63 -0
- package/dist/widgets/readForWidgetRealtime.js.map +1 -0
- package/dist/widgets/readForWidgetSeries.d.ts +44 -0
- package/dist/widgets/readForWidgetSeries.js +117 -0
- package/dist/widgets/readForWidgetSeries.js.map +1 -0
- package/dist/widgets/realtimePoll.js +26 -0
- package/dist/widgets/realtimePoll.js.map +1 -0
- package/dist/widgets/registerWidgets.js +228 -0
- package/dist/widgets/registerWidgets.js.map +1 -0
- package/dist/widgets/types.js +13 -0
- package/dist/widgets/types.js.map +1 -0
- package/package.json +19 -5
- package/dist/RealtimeCounter-BJrjMPSa.js +0 -473
- package/dist/RealtimeCounter-BJrjMPSa.js.map +0 -1
- package/dist/dist-B6HruP5v.js +0 -4731
- package/dist/dist-B6HruP5v.js.map +0 -1
- package/dist/exports/adapters/ga4.js.map +0 -1
- package/dist/exports/adapters/native.js.map +0 -1
- package/dist/exports/adapters/plausible.js.map +0 -1
- package/dist/exports/adapters/posthog.js.map +0 -1
- package/dist/exports/client.js.map +0 -1
- package/dist/exports/rsc.js.map +0 -1
- package/dist/fetchJson-DZb2_Puj.js.map +0 -1
- package/dist/index-CZdCT0WJ.d.ts +0 -98
- package/dist/keys-DEqL4LOT.js.map +0 -1
- package/dist/lib-CE5x1zLQ.js +0 -1058
- package/dist/lib-CE5x1zLQ.js.map +0 -1
- package/dist/maxmindResolver-CXYXXdul.js +0 -99
- package/dist/maxmindResolver-CXYXXdul.js.map +0 -1
- package/dist/presets-CBfq_uJX.d.ts +0 -38
- package/dist/series-zytbzPoc.js.map +0 -1
- package/dist/server-Cuvhgz-S.js +0 -492
- package/dist/server-Cuvhgz-S.js.map +0 -1
- package/dist/translations-keiKtSjF.js.map +0 -1
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { keys } from "../translations/keys.js";
|
|
2
|
+
import { asTranslate } from "../translations/server.js";
|
|
3
|
+
import { METRIC_KEYS } from "../translations/metricKeys.js";
|
|
4
|
+
import { readForWidgetRealtime } from "./readForWidgetRealtime.js";
|
|
5
|
+
import { REALTIME_PATH } from "../plugin/realtimeEndpoint.js";
|
|
6
|
+
import { RealtimeCounter } from "./RealtimeCounter.js";
|
|
7
|
+
import { cardStyle, labelStyle } from "./cardChrome.js";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
//#region src/widgets/AnalyticsRealtimeWidget.tsx
|
|
10
|
+
const STATE_KEY = {
|
|
11
|
+
"not-configured": keys.stateNotConfigured,
|
|
12
|
+
unavailable: keys.stateUnavailable
|
|
13
|
+
};
|
|
14
|
+
const POLL_INTERVAL_MS = 15e3;
|
|
15
|
+
async function AnalyticsRealtimeWidget(props) {
|
|
16
|
+
const data = props.widgetData ?? {};
|
|
17
|
+
const metric = data.metric ?? "visitors";
|
|
18
|
+
const windowMinutes = Number(data.windowMinutes) || 30;
|
|
19
|
+
const t = asTranslate(props.req.i18n.t);
|
|
20
|
+
const locale = props.req.i18n.language ?? "en-US";
|
|
21
|
+
const title = data.title?.trim() || t(keys.widgetRealtimeLabel);
|
|
22
|
+
const result = await readForWidgetRealtime({
|
|
23
|
+
req: props.req,
|
|
24
|
+
metric,
|
|
25
|
+
windowMinutes,
|
|
26
|
+
adapterId: data.dataSource,
|
|
27
|
+
now: /* @__PURE__ */ new Date()
|
|
28
|
+
});
|
|
29
|
+
if (result.status !== "ok") return /* @__PURE__ */ jsxs("div", {
|
|
30
|
+
className: "analytics-realtime-widget",
|
|
31
|
+
style: cardStyle,
|
|
32
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
33
|
+
style: labelStyle,
|
|
34
|
+
children: title
|
|
35
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
36
|
+
style: { color: "var(--theme-elevation-400)" },
|
|
37
|
+
children: t(STATE_KEY[result.status])
|
|
38
|
+
})]
|
|
39
|
+
});
|
|
40
|
+
const caption = `${t(METRIC_KEYS[metric])} ${t(keys.widgetRealtimeCaption)}`;
|
|
41
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
42
|
+
className: "analytics-realtime-widget",
|
|
43
|
+
style: cardStyle,
|
|
44
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
45
|
+
style: labelStyle,
|
|
46
|
+
children: title
|
|
47
|
+
}), /* @__PURE__ */ jsx(RealtimeCounter, {
|
|
48
|
+
endpoint: `/api${REALTIME_PATH}`,
|
|
49
|
+
intervalMs: POLL_INTERVAL_MS,
|
|
50
|
+
metric,
|
|
51
|
+
windowMinutes,
|
|
52
|
+
dataSource: data.dataSource,
|
|
53
|
+
initialActiveNow: result.activeNow,
|
|
54
|
+
initialSeries: result.series,
|
|
55
|
+
locale,
|
|
56
|
+
caption,
|
|
57
|
+
pausedLabel: t(keys.widgetRealtimePaused)
|
|
58
|
+
})]
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
62
|
+
export { AnalyticsRealtimeWidget as default };
|
|
63
|
+
|
|
64
|
+
//# sourceMappingURL=AnalyticsRealtimeWidget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AnalyticsRealtimeWidget.js","names":[],"sources":["../../src/widgets/AnalyticsRealtimeWidget.tsx"],"sourcesContent":["import type { WidgetServerProps } from 'payload'\nimport type { MetricKey } from '../core/contract'\nimport { REALTIME_PATH } from '../plugin/realtimeEndpoint'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport { METRIC_KEYS } from '../translations/metricKeys'\nimport { asTranslate } from '../translations/server'\nimport { cardStyle, labelStyle } from './cardChrome'\nimport { RealtimeCounter } from './RealtimeCounter'\nimport { readForWidgetRealtime, type WidgetRealtimeStatus } from './readForWidgetRealtime'\n\nconst STATE_KEY: Record<Exclude<WidgetRealtimeStatus, 'ok'>, TranslationKey> = {\n\t'not-configured': keys.stateNotConfigured,\n\tunavailable: keys.stateUnavailable,\n}\n\ninterface RealtimeWidgetData {\n\ttitle?: string\n\tmetric?: 'visitors' | 'pageviews'\n\twindowMinutes?: number | string\n\tdataSource?: string\n}\n\nconst POLL_INTERVAL_MS = 15_000\n\nexport default async function AnalyticsRealtimeWidget(props: WidgetServerProps) {\n\tconst data = (props.widgetData ?? {}) as RealtimeWidgetData\n\tconst metric: MetricKey = data.metric ?? 'visitors'\n\tconst windowMinutes = Number(data.windowMinutes) || 30\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = props.req.i18n.language ?? 'en-US'\n\tconst title = data.title?.trim() || t(keys.widgetRealtimeLabel)\n\n\tconst result = await readForWidgetRealtime({\n\t\treq: props.req,\n\t\tmetric,\n\t\twindowMinutes,\n\t\tadapterId: data.dataSource,\n\t\tnow: new Date(),\n\t})\n\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<div className=\"analytics-realtime-widget\" style={cardStyle}>\n\t\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t\t<span style={{ color: 'var(--theme-elevation-400)' }}>{t(STATE_KEY[result.status])}</span>\n\t\t\t</div>\n\t\t)\n\t}\n\n\tconst caption = `${t(METRIC_KEYS[metric])} ${t(keys.widgetRealtimeCaption)}`\n\treturn (\n\t\t<div className=\"analytics-realtime-widget\" style={cardStyle}>\n\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t<RealtimeCounter\n\t\t\t\tendpoint={`/api${REALTIME_PATH}`}\n\t\t\t\tintervalMs={POLL_INTERVAL_MS}\n\t\t\t\tmetric={metric}\n\t\t\t\twindowMinutes={windowMinutes}\n\t\t\t\tdataSource={data.dataSource}\n\t\t\t\tinitialActiveNow={result.activeNow}\n\t\t\t\tinitialSeries={result.series}\n\t\t\t\tlocale={locale}\n\t\t\t\tcaption={caption}\n\t\t\t\tpausedLabel={t(keys.widgetRealtimePaused)}\n\t\t\t/>\n\t\t</div>\n\t)\n}\n"],"mappings":";;;;;;;;;AAUA,MAAM,YAAyE;CAC9E,kBAAkB,KAAK;CACvB,aAAa,KAAK;AACnB;AASA,MAAM,mBAAmB;AAEzB,eAA8B,wBAAwB,OAA0B;CAC/E,MAAM,OAAQ,MAAM,cAAc,CAAC;CACnC,MAAM,SAAoB,KAAK,UAAU;CACzC,MAAM,gBAAgB,OAAO,KAAK,aAAa,KAAK;CACpD,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,MAAM,SAAS,MAAM,IAAI,KAAK,YAAY;CAC1C,MAAM,QAAQ,KAAK,OAAO,KAAK,KAAK,EAAE,KAAK,mBAAmB;CAE9D,MAAM,SAAS,MAAM,sBAAsB;EAC1C,KAAK,MAAM;EACX;EACA;EACA,WAAW,KAAK;EAChB,qBAAK,IAAI,KAAK;CACf,CAAC;CAED,IAAI,OAAO,WAAW,MACrB,OACC,qBAAC,OAAD;EAAK,WAAU;EAA4B,OAAO;YAAlD,CACC,oBAAC,QAAD;GAAM,OAAO;aAAa;EAAY,CAAA,GACtC,oBAAC,QAAD;GAAM,OAAO,EAAE,OAAO,6BAA6B;aAAI,EAAE,UAAU,OAAO,OAAO;EAAQ,CAAA,CACrF;;CAIP,MAAM,UAAU,GAAG,EAAE,YAAY,OAAO,EAAE,GAAG,EAAE,KAAK,qBAAqB;CACzE,OACC,qBAAC,OAAD;EAAK,WAAU;EAA4B,OAAO;YAAlD,CACC,oBAAC,QAAD;GAAM,OAAO;aAAa;EAAY,CAAA,GACtC,oBAAC,iBAAD;GACC,UAAU,OAAO;GACjB,YAAY;GACJ;GACO;GACf,YAAY,KAAK;GACjB,kBAAkB,OAAO;GACzB,eAAe,OAAO;GACd;GACC;GACT,aAAa,EAAE,KAAK,oBAAoB;EACxC,CAAA,CACG;;AAEP"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { WidgetServerProps } from "payload";
|
|
2
|
+
|
|
3
|
+
//#region src/widgets/AnalyticsTrendWidget.d.ts
|
|
4
|
+
declare function AnalyticsTrendWidget(props: WidgetServerProps): Promise<import("react/jsx-runtime").JSX.Element>;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { AnalyticsTrendWidget };
|
|
7
|
+
//# sourceMappingURL=AnalyticsTrendWidget.d.ts.map
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { keys } from "../translations/keys.js";
|
|
2
|
+
import { asTranslate } from "../translations/server.js";
|
|
3
|
+
import { readForWidgetSeries } from "./readForWidgetSeries.js";
|
|
4
|
+
import { METRIC_KEYS, TIMEFRAME_KEYS } from "../translations/metricKeys.js";
|
|
5
|
+
import { formatRangeCaption, resolveCustomRange } from "./range.js";
|
|
6
|
+
import { TrendChart } from "../charts/TrendChart.js";
|
|
7
|
+
import { formatMetricValue } from "../fields/format.js";
|
|
8
|
+
import { cardStyle, labelStyle } from "./cardChrome.js";
|
|
9
|
+
import { ComparisonDelta } from "./ComparisonDelta.js";
|
|
10
|
+
import { bucketByRange, bucketSeries } from "../charts/bucket.js";
|
|
11
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
12
|
+
//#region src/widgets/AnalyticsTrendWidget.tsx
|
|
13
|
+
const STATE_KEY = {
|
|
14
|
+
"not-configured": keys.stateNotConfigured,
|
|
15
|
+
unavailable: keys.stateUnavailable
|
|
16
|
+
};
|
|
17
|
+
async function AnalyticsTrendWidget(props) {
|
|
18
|
+
const data = props.widgetData ?? {};
|
|
19
|
+
const metric = data.metric ?? "pageviews";
|
|
20
|
+
const rawTimeframe = data.timeframe ?? "last30days";
|
|
21
|
+
const customRange = resolveCustomRange(rawTimeframe, data.range);
|
|
22
|
+
const timeframe = rawTimeframe === "custom" ? "last30days" : rawTimeframe;
|
|
23
|
+
const t = asTranslate(props.req.i18n.t);
|
|
24
|
+
const locale = props.req.i18n.language ?? "en-US";
|
|
25
|
+
const title = data.title?.trim() || t(METRIC_KEYS[metric]);
|
|
26
|
+
const result = await readForWidgetSeries({
|
|
27
|
+
req: props.req,
|
|
28
|
+
metric,
|
|
29
|
+
timeframe,
|
|
30
|
+
adapterId: data.dataSource,
|
|
31
|
+
now: /* @__PURE__ */ new Date(),
|
|
32
|
+
range: customRange
|
|
33
|
+
});
|
|
34
|
+
if (result.status !== "ok") return /* @__PURE__ */ jsxs("div", {
|
|
35
|
+
className: "analytics-trend-widget",
|
|
36
|
+
style: cardStyle,
|
|
37
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
38
|
+
style: labelStyle,
|
|
39
|
+
children: title
|
|
40
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
41
|
+
style: { color: "var(--theme-elevation-400)" },
|
|
42
|
+
children: t(STATE_KEY[result.status])
|
|
43
|
+
})]
|
|
44
|
+
});
|
|
45
|
+
const caption = customRange ? formatRangeCaption(customRange, locale) : t(TIMEFRAME_KEYS[timeframe]);
|
|
46
|
+
const trendPoints = (customRange ? bucketByRange(result.points, customRange, result.timezone) : bucketSeries(result.points, timeframe, result.timezone)).map((b) => ({
|
|
47
|
+
...b,
|
|
48
|
+
display: formatMetricValue(metric, b.value, locale)
|
|
49
|
+
}));
|
|
50
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
51
|
+
className: "analytics-trend-widget",
|
|
52
|
+
style: cardStyle,
|
|
53
|
+
children: [
|
|
54
|
+
/* @__PURE__ */ jsx("span", {
|
|
55
|
+
style: labelStyle,
|
|
56
|
+
children: title
|
|
57
|
+
}),
|
|
58
|
+
/* @__PURE__ */ jsx("span", {
|
|
59
|
+
style: {
|
|
60
|
+
fontSize: "1.75rem",
|
|
61
|
+
fontWeight: 700,
|
|
62
|
+
lineHeight: 1.1,
|
|
63
|
+
color: "var(--theme-elevation-800)"
|
|
64
|
+
},
|
|
65
|
+
children: formatMetricValue(metric, result.total, locale)
|
|
66
|
+
}),
|
|
67
|
+
/* @__PURE__ */ jsx(TrendChart, {
|
|
68
|
+
buckets: trendPoints,
|
|
69
|
+
ariaLabel: `${title} ${caption}`,
|
|
70
|
+
minHeight: 180
|
|
71
|
+
}),
|
|
72
|
+
/* @__PURE__ */ jsx("span", {
|
|
73
|
+
style: {
|
|
74
|
+
fontSize: "0.75rem",
|
|
75
|
+
color: "var(--theme-elevation-400)"
|
|
76
|
+
},
|
|
77
|
+
children: caption
|
|
78
|
+
}),
|
|
79
|
+
result.comparisonRange ? /* @__PURE__ */ jsx(ComparisonDelta, {
|
|
80
|
+
current: result.total,
|
|
81
|
+
previous: result.previousTotal,
|
|
82
|
+
metric,
|
|
83
|
+
locale,
|
|
84
|
+
t
|
|
85
|
+
}) : null,
|
|
86
|
+
result.clamped ? /* @__PURE__ */ jsx("span", {
|
|
87
|
+
style: {
|
|
88
|
+
fontSize: "0.6875rem",
|
|
89
|
+
color: "var(--theme-elevation-400)"
|
|
90
|
+
},
|
|
91
|
+
children: t(keys.stateClamped)
|
|
92
|
+
}) : null
|
|
93
|
+
]
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
export { AnalyticsTrendWidget as default };
|
|
98
|
+
|
|
99
|
+
//# sourceMappingURL=AnalyticsTrendWidget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AnalyticsTrendWidget.js","names":[],"sources":["../../src/widgets/AnalyticsTrendWidget.tsx"],"sourcesContent":["import type { WidgetServerProps } from 'payload'\nimport { bucketByRange, bucketSeries } from '../charts/bucket'\nimport { TrendChart } from '../charts/TrendChart'\nimport type { MetricKey } from '../core/contract'\nimport { formatMetricValue } from '../fields/format'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport { METRIC_KEYS, TIMEFRAME_KEYS } from '../translations/metricKeys'\nimport { asTranslate } from '../translations/server'\nimport { ComparisonDelta } from './ComparisonDelta'\nimport { cardStyle, labelStyle } from './cardChrome'\nimport { formatRangeCaption, resolveCustomRange } from './range'\nimport type { WidgetReadStatus } from './readForWidget'\nimport { readForWidgetSeries } from './readForWidgetSeries'\nimport type { MetricWidgetData } from './types'\n\nconst STATE_KEY: Record<Exclude<WidgetReadStatus, 'ok'>, TranslationKey> = {\n\t'not-configured': keys.stateNotConfigured,\n\tunavailable: keys.stateUnavailable,\n}\n\nexport default async function AnalyticsTrendWidget(props: WidgetServerProps) {\n\tconst data = (props.widgetData ?? {}) as MetricWidgetData\n\tconst metric: MetricKey = data.metric ?? 'pageviews'\n\tconst rawTimeframe = data.timeframe ?? 'last30days'\n\tconst customRange = resolveCustomRange(rawTimeframe, data.range)\n\tconst timeframe: TimeframePreset = rawTimeframe === 'custom' ? 'last30days' : rawTimeframe\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = props.req.i18n.language ?? 'en-US'\n\tconst title = data.title?.trim() || t(METRIC_KEYS[metric])\n\n\tconst result = await readForWidgetSeries({\n\t\treq: props.req,\n\t\tmetric,\n\t\ttimeframe,\n\t\tadapterId: data.dataSource,\n\t\tnow: new Date(),\n\t\trange: customRange,\n\t})\n\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<div className=\"analytics-trend-widget\" style={cardStyle}>\n\t\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t\t<span style={{ color: 'var(--theme-elevation-400)' }}>{t(STATE_KEY[result.status])}</span>\n\t\t\t</div>\n\t\t)\n\t}\n\n\tconst caption = customRange\n\t\t? formatRangeCaption(customRange, locale)\n\t\t: t(TIMEFRAME_KEYS[timeframe])\n\tconst buckets = customRange\n\t\t? bucketByRange(result.points, customRange, result.timezone)\n\t\t: bucketSeries(result.points, timeframe, result.timezone)\n\tconst trendPoints = buckets.map((b) => ({\n\t\t...b,\n\t\tdisplay: formatMetricValue(metric, b.value, locale),\n\t}))\n\treturn (\n\t\t<div className=\"analytics-trend-widget\" style={cardStyle}>\n\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t<span\n\t\t\t\tstyle={{\n\t\t\t\t\tfontSize: '1.75rem',\n\t\t\t\t\tfontWeight: 700,\n\t\t\t\t\tlineHeight: 1.1,\n\t\t\t\t\tcolor: 'var(--theme-elevation-800)',\n\t\t\t\t}}\n\t\t\t>\n\t\t\t\t{formatMetricValue(metric, result.total, locale)}\n\t\t\t</span>\n\t\t\t<TrendChart buckets={trendPoints} ariaLabel={`${title} ${caption}`} minHeight={180} />\n\t\t\t<span style={{ fontSize: '0.75rem', color: 'var(--theme-elevation-400)' }}>{caption}</span>\n\t\t\t{result.comparisonRange ? (\n\t\t\t\t<ComparisonDelta\n\t\t\t\t\tcurrent={result.total}\n\t\t\t\t\tprevious={result.previousTotal}\n\t\t\t\t\tmetric={metric}\n\t\t\t\t\tlocale={locale}\n\t\t\t\t\tt={t}\n\t\t\t\t/>\n\t\t\t) : null}\n\t\t\t{result.clamped ? (\n\t\t\t\t<span style={{ fontSize: '0.6875rem', color: 'var(--theme-elevation-400)' }}>\n\t\t\t\t\t{t(keys.stateClamped)}\n\t\t\t\t</span>\n\t\t\t) : null}\n\t\t</div>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;AAgBA,MAAM,YAAqE;CAC1E,kBAAkB,KAAK;CACvB,aAAa,KAAK;AACnB;AAEA,eAA8B,qBAAqB,OAA0B;CAC5E,MAAM,OAAQ,MAAM,cAAc,CAAC;CACnC,MAAM,SAAoB,KAAK,UAAU;CACzC,MAAM,eAAe,KAAK,aAAa;CACvC,MAAM,cAAc,mBAAmB,cAAc,KAAK,KAAK;CAC/D,MAAM,YAA6B,iBAAiB,WAAW,eAAe;CAC9E,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,MAAM,SAAS,MAAM,IAAI,KAAK,YAAY;CAC1C,MAAM,QAAQ,KAAK,OAAO,KAAK,KAAK,EAAE,YAAY,OAAO;CAEzD,MAAM,SAAS,MAAM,oBAAoB;EACxC,KAAK,MAAM;EACX;EACA;EACA,WAAW,KAAK;EAChB,qBAAK,IAAI,KAAK;EACd,OAAO;CACR,CAAC;CAED,IAAI,OAAO,WAAW,MACrB,OACC,qBAAC,OAAD;EAAK,WAAU;EAAyB,OAAO;YAA/C,CACC,oBAAC,QAAD;GAAM,OAAO;aAAa;EAAY,CAAA,GACtC,oBAAC,QAAD;GAAM,OAAO,EAAE,OAAO,6BAA6B;aAAI,EAAE,UAAU,OAAO,OAAO;EAAQ,CAAA,CACrF;;CAIP,MAAM,UAAU,cACb,mBAAmB,aAAa,MAAM,IACtC,EAAE,eAAe,UAAU;CAI9B,MAAM,eAHU,cACb,cAAc,OAAO,QAAQ,aAAa,OAAO,QAAQ,IACzD,aAAa,OAAO,QAAQ,WAAW,OAAO,QAAQ,GAC7B,KAAK,OAAO;EACvC,GAAG;EACH,SAAS,kBAAkB,QAAQ,EAAE,OAAO,MAAM;CACnD,EAAE;CACF,OACC,qBAAC,OAAD;EAAK,WAAU;EAAyB,OAAO;YAA/C;GACC,oBAAC,QAAD;IAAM,OAAO;cAAa;GAAY,CAAA;GACtC,oBAAC,QAAD;IACC,OAAO;KACN,UAAU;KACV,YAAY;KACZ,YAAY;KACZ,OAAO;IACR;cAEC,kBAAkB,QAAQ,OAAO,OAAO,MAAM;GAC1C,CAAA;GACN,oBAAC,YAAD;IAAY,SAAS;IAAa,WAAW,GAAG,MAAM,GAAG;IAAW,WAAW;GAAM,CAAA;GACrF,oBAAC,QAAD;IAAM,OAAO;KAAE,UAAU;KAAW,OAAO;IAA6B;cAAI;GAAc,CAAA;GACzF,OAAO,kBACP,oBAAC,iBAAD;IACC,SAAS,OAAO;IAChB,UAAU,OAAO;IACT;IACA;IACL;GACH,CAAA,IACE;GACH,OAAO,UACP,oBAAC,QAAD;IAAM,OAAO;KAAE,UAAU;KAAa,OAAO;IAA6B;cACxE,EAAE,KAAK,YAAY;GACf,CAAA,IACH;EACA;;AAEP"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { keys } from "../translations/keys.js";
|
|
2
|
+
import { computeDelta } from "./comparison.js";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
//#region src/widgets/ComparisonDelta.tsx
|
|
5
|
+
const SUCCESS = "var(--theme-success-500, rgb(21, 135, 186))";
|
|
6
|
+
const ERROR = "var(--theme-error-500, rgb(218, 75, 72))";
|
|
7
|
+
const NEUTRAL = "var(--theme-elevation-400)";
|
|
8
|
+
/** Metrics where a decrease is the good outcome, inverting the delta colors. */
|
|
9
|
+
const LOWER_IS_BETTER = new Set(["bounceRate"]);
|
|
10
|
+
const colorFor = (direction, metric) => {
|
|
11
|
+
if (direction === "none") return NEUTRAL;
|
|
12
|
+
const goodWhenDown = metric !== void 0 && LOWER_IS_BETTER.has(metric);
|
|
13
|
+
return direction === "up" !== goodWhenDown ? SUCCESS : ERROR;
|
|
14
|
+
};
|
|
15
|
+
const DIRECTION_LABEL = {
|
|
16
|
+
up: keys.comparisonIncrease,
|
|
17
|
+
down: keys.comparisonDecrease,
|
|
18
|
+
none: keys.comparisonNoChange
|
|
19
|
+
};
|
|
20
|
+
const DeltaArrow = ({ direction }) => /* @__PURE__ */ jsx("svg", {
|
|
21
|
+
"aria-hidden": "true",
|
|
22
|
+
focusable: "false",
|
|
23
|
+
width: "8",
|
|
24
|
+
height: "8",
|
|
25
|
+
viewBox: "0 0 8 8",
|
|
26
|
+
style: {
|
|
27
|
+
flexShrink: 0,
|
|
28
|
+
transform: direction === "down" ? "rotate(180deg)" : void 0
|
|
29
|
+
},
|
|
30
|
+
children: /* @__PURE__ */ jsx("path", {
|
|
31
|
+
d: "M4 1.2 7.3 6.8H0.7Z",
|
|
32
|
+
fill: "currentColor"
|
|
33
|
+
})
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* Period-over-period delta chip: a colored arrow, the percentage change, and a localized
|
|
37
|
+
* "vs. previous period" caption. Renders nothing when there is no comparable previous
|
|
38
|
+
* value; when the previous value is 0 (no percentage baseline) the localized direction
|
|
39
|
+
* word stands in for the number. All text routes through typed translation keys; the
|
|
40
|
+
* arrow is decorative and carried by an aria-label with the localized direction word.
|
|
41
|
+
*/
|
|
42
|
+
function ComparisonDelta({ current, previous, metric, locale, t }) {
|
|
43
|
+
const delta = computeDelta(current, previous);
|
|
44
|
+
if (!delta) return null;
|
|
45
|
+
const percentText = delta.percent === null ? null : new Intl.NumberFormat(locale, {
|
|
46
|
+
style: "percent",
|
|
47
|
+
maximumFractionDigits: 1,
|
|
48
|
+
signDisplay: "never"
|
|
49
|
+
}).format(Math.abs(delta.percent) / 100);
|
|
50
|
+
const visible = percentText ?? t(DIRECTION_LABEL[delta.direction]);
|
|
51
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
52
|
+
role: "img",
|
|
53
|
+
"aria-label": `${t(DIRECTION_LABEL[delta.direction])} ${percentText ?? ""} ${t(keys.comparisonVsPrevious)}`.replace(/\s+/g, " ").trim(),
|
|
54
|
+
style: {
|
|
55
|
+
display: "inline-flex",
|
|
56
|
+
gap: "0.375rem",
|
|
57
|
+
alignItems: "center",
|
|
58
|
+
fontSize: "0.75rem",
|
|
59
|
+
lineHeight: 1.25
|
|
60
|
+
},
|
|
61
|
+
children: [/* @__PURE__ */ jsxs("span", {
|
|
62
|
+
style: {
|
|
63
|
+
display: "inline-flex",
|
|
64
|
+
alignItems: "center",
|
|
65
|
+
gap: "0.25rem",
|
|
66
|
+
color: colorFor(delta.direction, metric),
|
|
67
|
+
fontWeight: 600,
|
|
68
|
+
lineHeight: "inherit"
|
|
69
|
+
},
|
|
70
|
+
children: [delta.direction !== "none" ? /* @__PURE__ */ jsx(DeltaArrow, { direction: delta.direction }) : null, visible]
|
|
71
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
72
|
+
style: {
|
|
73
|
+
color: "var(--theme-elevation-400)",
|
|
74
|
+
lineHeight: "inherit"
|
|
75
|
+
},
|
|
76
|
+
children: t(keys.comparisonVsPrevious)
|
|
77
|
+
})]
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { ComparisonDelta };
|
|
82
|
+
|
|
83
|
+
//# sourceMappingURL=ComparisonDelta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ComparisonDelta.js","names":[],"sources":["../../src/widgets/ComparisonDelta.tsx"],"sourcesContent":["import type { MetricKey } from '../core/contract'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport { computeDelta, type DeltaDirection } from './comparison'\n\n// Payload's admin palette maps success to its blue ramp and error to its red ramp; the\n// fallbacks mirror @payloadcms/ui colors.scss so the chip renders identically outside\n// a themed admin shell.\nconst SUCCESS = 'var(--theme-success-500, rgb(21, 135, 186))'\nconst ERROR = 'var(--theme-error-500, rgb(218, 75, 72))'\nconst NEUTRAL = 'var(--theme-elevation-400)'\n\n/** Metrics where a decrease is the good outcome, inverting the delta colors. */\nconst LOWER_IS_BETTER: ReadonlySet<MetricKey> = new Set<MetricKey>(['bounceRate'])\n\nconst colorFor = (direction: DeltaDirection, metric?: MetricKey): string => {\n\tif (direction === 'none') {\n\t\treturn NEUTRAL\n\t}\n\tconst goodWhenDown = metric !== undefined && LOWER_IS_BETTER.has(metric)\n\tconst improved = (direction === 'up') !== goodWhenDown\n\treturn improved ? SUCCESS : ERROR\n}\n\nconst DIRECTION_LABEL: Record<DeltaDirection, TranslationKey> = {\n\tup: keys.comparisonIncrease,\n\tdown: keys.comparisonDecrease,\n\tnone: keys.comparisonNoChange,\n}\n\nconst DeltaArrow = ({ direction }: { direction: 'up' | 'down' }) => (\n\t<svg\n\t\taria-hidden=\"true\"\n\t\tfocusable=\"false\"\n\t\twidth=\"8\"\n\t\theight=\"8\"\n\t\tviewBox=\"0 0 8 8\"\n\t\tstyle={{\n\t\t\tflexShrink: 0,\n\t\t\ttransform: direction === 'down' ? 'rotate(180deg)' : undefined,\n\t\t}}\n\t>\n\t\t<path d=\"M4 1.2 7.3 6.8H0.7Z\" fill=\"currentColor\" />\n\t</svg>\n)\n\nexport interface ComparisonDeltaProps {\n\tcurrent?: number\n\tprevious?: number\n\t/** Inverts good/bad coloring for metrics in {@link LOWER_IS_BETTER}. */\n\tmetric?: MetricKey\n\tlocale: string\n\tt: (key: TranslationKey) => string\n}\n\n/**\n * Period-over-period delta chip: a colored arrow, the percentage change, and a localized\n * \"vs. previous period\" caption. Renders nothing when there is no comparable previous\n * value; when the previous value is 0 (no percentage baseline) the localized direction\n * word stands in for the number. All text routes through typed translation keys; the\n * arrow is decorative and carried by an aria-label with the localized direction word.\n */\nexport function ComparisonDelta({ current, previous, metric, locale, t }: ComparisonDeltaProps) {\n\tconst delta = computeDelta(current, previous)\n\tif (!delta) {\n\t\treturn null\n\t}\n\tconst percentText =\n\t\tdelta.percent === null\n\t\t\t? null\n\t\t\t: new Intl.NumberFormat(locale, {\n\t\t\t\t\tstyle: 'percent',\n\t\t\t\t\tmaximumFractionDigits: 1,\n\t\t\t\t\tsignDisplay: 'never',\n\t\t\t\t}).format(Math.abs(delta.percent) / 100)\n\t// An unchanged value is a real 0%, so it reads as one; only a zero previous value\n\t// (no percentage exists to report) falls back to the direction word.\n\tconst visible = percentText ?? t(DIRECTION_LABEL[delta.direction])\n\tconst ariaLabel = `${t(DIRECTION_LABEL[delta.direction])} ${percentText ?? ''} ${t(\n\t\tkeys.comparisonVsPrevious\n\t)}`\n\t\t.replace(/\\s+/g, ' ')\n\t\t.trim()\n\treturn (\n\t\t<span\n\t\t\trole=\"img\"\n\t\t\taria-label={ariaLabel}\n\t\t\tstyle={{\n\t\t\t\tdisplay: 'inline-flex',\n\t\t\t\tgap: '0.375rem',\n\t\t\t\t// Not `baseline`: the value's own flex box takes its baseline from the arrow\n\t\t\t\t// glyph rather than its text, which lifts the value off the caption's line.\n\t\t\t\talignItems: 'center',\n\t\t\t\tfontSize: '0.75rem',\n\t\t\t\tlineHeight: 1.25,\n\t\t\t}}\n\t\t>\n\t\t\t<span\n\t\t\t\tstyle={{\n\t\t\t\t\tdisplay: 'inline-flex',\n\t\t\t\t\talignItems: 'center',\n\t\t\t\t\tgap: '0.25rem',\n\t\t\t\t\tcolor: colorFor(delta.direction, metric),\n\t\t\t\t\tfontWeight: 600,\n\t\t\t\t\tlineHeight: 'inherit',\n\t\t\t\t}}\n\t\t\t>\n\t\t\t\t{delta.direction !== 'none' ? <DeltaArrow direction={delta.direction} /> : null}\n\t\t\t\t{visible}\n\t\t\t</span>\n\t\t\t<span style={{ color: 'var(--theme-elevation-400)', lineHeight: 'inherit' }}>\n\t\t\t\t{t(keys.comparisonVsPrevious)}\n\t\t\t</span>\n\t\t</span>\n\t)\n}\n"],"mappings":";;;;AAOA,MAAM,UAAU;AAChB,MAAM,QAAQ;AACd,MAAM,UAAU;;AAGhB,MAAM,kBAA0C,IAAI,IAAe,CAAC,YAAY,CAAC;AAEjF,MAAM,YAAY,WAA2B,WAA+B;CAC3E,IAAI,cAAc,QACjB,OAAO;CAER,MAAM,eAAe,WAAW,KAAA,KAAa,gBAAgB,IAAI,MAAM;CAEvE,OADkB,cAAc,SAAU,eACxB,UAAU;AAC7B;AAEA,MAAM,kBAA0D;CAC/D,IAAI,KAAK;CACT,MAAM,KAAK;CACX,MAAM,KAAK;AACZ;AAEA,MAAM,cAAc,EAAE,gBACrB,oBAAC,OAAD;CACC,eAAY;CACZ,WAAU;CACV,OAAM;CACN,QAAO;CACP,SAAQ;CACR,OAAO;EACN,YAAY;EACZ,WAAW,cAAc,SAAS,mBAAmB,KAAA;CACtD;WAEA,oBAAC,QAAD;EAAM,GAAE;EAAsB,MAAK;CAAgB,CAAA;AAC/C,CAAA;;;;;;;;AAmBN,SAAgB,gBAAgB,EAAE,SAAS,UAAU,QAAQ,QAAQ,KAA2B;CAC/F,MAAM,QAAQ,aAAa,SAAS,QAAQ;CAC5C,IAAI,CAAC,OACJ,OAAO;CAER,MAAM,cACL,MAAM,YAAY,OACf,OACA,IAAI,KAAK,aAAa,QAAQ;EAC9B,OAAO;EACP,uBAAuB;EACvB,aAAa;CACd,CAAC,EAAE,OAAO,KAAK,IAAI,MAAM,OAAO,IAAI,GAAG;CAG1C,MAAM,UAAU,eAAe,EAAE,gBAAgB,MAAM,UAAU;CAMjE,OACC,qBAAC,QAAD;EACC,MAAK;EACL,cARgB,GAAG,EAAE,gBAAgB,MAAM,UAAU,EAAE,GAAG,eAAe,GAAG,GAAG,EAChF,KAAK,oBACN,IACE,QAAQ,QAAQ,GAAG,EACnB,KAIoB;EACpB,OAAO;GACN,SAAS;GACT,KAAK;GAGL,YAAY;GACZ,UAAU;GACV,YAAY;EACb;YAXD,CAaC,qBAAC,QAAD;GACC,OAAO;IACN,SAAS;IACT,YAAY;IACZ,KAAK;IACL,OAAO,SAAS,MAAM,WAAW,MAAM;IACvC,YAAY;IACZ,YAAY;GACb;aARD,CAUE,MAAM,cAAc,SAAS,oBAAC,YAAD,EAAY,WAAW,MAAM,UAAY,CAAA,IAAI,MAC1E,OACI;MACN,oBAAC,QAAD;GAAM,OAAO;IAAE,OAAO;IAA8B,YAAY;GAAU;aACxE,EAAE,KAAK,oBAAoB;EACvB,CAAA,CACD;;AAER"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { RealtimePoint } from "./readForWidgetRealtime.js";
|
|
2
|
+
|
|
3
|
+
//#region src/widgets/RealtimeCounter.d.ts
|
|
4
|
+
interface RealtimeCounterProps {
|
|
5
|
+
endpoint: string;
|
|
6
|
+
intervalMs: number;
|
|
7
|
+
metric: string;
|
|
8
|
+
windowMinutes: number;
|
|
9
|
+
dataSource?: string;
|
|
10
|
+
initialActiveNow: number;
|
|
11
|
+
initialSeries: RealtimePoint[];
|
|
12
|
+
locale: string;
|
|
13
|
+
caption: string;
|
|
14
|
+
pausedLabel: string;
|
|
15
|
+
}
|
|
16
|
+
declare function RealtimeCounter(props: RealtimeCounterProps): import("react/jsx-runtime").JSX.Element;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { RealtimeCounter };
|
|
19
|
+
//# sourceMappingURL=RealtimeCounter.d.ts.map
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { TrendChart } from "../charts/TrendChart.js";
|
|
3
|
+
import { buildPollPath, toRealtimePoints } from "./realtimePoll.js";
|
|
4
|
+
import { useEffect, useState } from "react";
|
|
5
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
//#region src/widgets/RealtimeCounter.tsx
|
|
7
|
+
function RealtimeCounter(props) {
|
|
8
|
+
const { endpoint, intervalMs, metric, windowMinutes, dataSource, locale, caption, pausedLabel } = props;
|
|
9
|
+
const [activeNow, setActiveNow] = useState(props.initialActiveNow);
|
|
10
|
+
const [series, setSeries] = useState(props.initialSeries);
|
|
11
|
+
const [paused, setPaused] = useState(false);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
let cancelled = false;
|
|
14
|
+
const path = buildPollPath(endpoint, {
|
|
15
|
+
metric,
|
|
16
|
+
windowMinutes,
|
|
17
|
+
dataSource
|
|
18
|
+
});
|
|
19
|
+
const tick = async () => {
|
|
20
|
+
try {
|
|
21
|
+
const res = await fetch(path, { credentials: "same-origin" });
|
|
22
|
+
if (!res.ok) {
|
|
23
|
+
if (!cancelled) setPaused(true);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const data = await res.json();
|
|
27
|
+
if (cancelled || data.status !== "ok") return;
|
|
28
|
+
setActiveNow(data.activeNow);
|
|
29
|
+
setSeries(data.series);
|
|
30
|
+
setPaused(false);
|
|
31
|
+
} catch {
|
|
32
|
+
if (!cancelled) setPaused(true);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const id = setInterval(tick, intervalMs);
|
|
36
|
+
return () => {
|
|
37
|
+
cancelled = true;
|
|
38
|
+
clearInterval(id);
|
|
39
|
+
};
|
|
40
|
+
}, [
|
|
41
|
+
endpoint,
|
|
42
|
+
intervalMs,
|
|
43
|
+
metric,
|
|
44
|
+
windowMinutes,
|
|
45
|
+
dataSource
|
|
46
|
+
]);
|
|
47
|
+
const nf = new Intl.NumberFormat(locale);
|
|
48
|
+
const points = toRealtimePoints(series, locale);
|
|
49
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
50
|
+
/* @__PURE__ */ jsx("span", {
|
|
51
|
+
style: {
|
|
52
|
+
fontSize: "2rem",
|
|
53
|
+
fontWeight: 700,
|
|
54
|
+
lineHeight: 1.1,
|
|
55
|
+
color: "var(--theme-elevation-800)"
|
|
56
|
+
},
|
|
57
|
+
children: nf.format(activeNow)
|
|
58
|
+
}),
|
|
59
|
+
/* @__PURE__ */ jsx("span", {
|
|
60
|
+
style: {
|
|
61
|
+
fontSize: "0.75rem",
|
|
62
|
+
color: "var(--theme-elevation-400)"
|
|
63
|
+
},
|
|
64
|
+
children: paused ? pausedLabel : caption
|
|
65
|
+
}),
|
|
66
|
+
/* @__PURE__ */ jsx(TrendChart, {
|
|
67
|
+
buckets: points,
|
|
68
|
+
ariaLabel: caption,
|
|
69
|
+
minHeight: 120
|
|
70
|
+
})
|
|
71
|
+
] });
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
export { RealtimeCounter };
|
|
75
|
+
|
|
76
|
+
//# sourceMappingURL=RealtimeCounter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RealtimeCounter.js","names":[],"sources":["../../src/widgets/RealtimeCounter.tsx"],"sourcesContent":["'use client'\n\nimport { useEffect, useState } from 'react'\nimport { TrendChart } from '../charts/TrendChart'\nimport type { RealtimePoint } from './readForWidgetRealtime'\nimport { buildPollPath, toRealtimePoints } from './realtimePoll'\n\nexport interface RealtimeCounterProps {\n\tendpoint: string\n\tintervalMs: number\n\tmetric: string\n\twindowMinutes: number\n\tdataSource?: string\n\tinitialActiveNow: number\n\tinitialSeries: RealtimePoint[]\n\tlocale: string\n\tcaption: string\n\tpausedLabel: string\n}\n\nexport function RealtimeCounter(props: RealtimeCounterProps) {\n\tconst { endpoint, intervalMs, metric, windowMinutes, dataSource, locale, caption, pausedLabel } =\n\t\tprops\n\tconst [activeNow, setActiveNow] = useState(props.initialActiveNow)\n\tconst [series, setSeries] = useState(props.initialSeries)\n\tconst [paused, setPaused] = useState(false)\n\n\tuseEffect(() => {\n\t\tlet cancelled = false\n\t\tconst path = buildPollPath(endpoint, { metric, windowMinutes, dataSource })\n\t\tconst tick = async () => {\n\t\t\ttry {\n\t\t\t\tconst res = await fetch(path, { credentials: 'same-origin' })\n\t\t\t\tif (!res.ok) {\n\t\t\t\t\tif (!cancelled) setPaused(true)\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tconst data = (await res.json()) as {\n\t\t\t\t\tstatus: string\n\t\t\t\t\tactiveNow: number\n\t\t\t\t\tseries: RealtimePoint[]\n\t\t\t\t}\n\t\t\t\tif (cancelled || data.status !== 'ok') return\n\t\t\t\tsetActiveNow(data.activeNow)\n\t\t\t\tsetSeries(data.series)\n\t\t\t\tsetPaused(false)\n\t\t\t} catch {\n\t\t\t\tif (!cancelled) setPaused(true)\n\t\t\t}\n\t\t}\n\t\tconst id = setInterval(tick, intervalMs)\n\t\treturn () => {\n\t\t\tcancelled = true\n\t\t\tclearInterval(id)\n\t\t}\n\t}, [endpoint, intervalMs, metric, windowMinutes, dataSource])\n\n\tconst nf = new Intl.NumberFormat(locale)\n\tconst points = toRealtimePoints(series, locale)\n\treturn (\n\t\t<>\n\t\t\t<span\n\t\t\t\tstyle={{\n\t\t\t\t\tfontSize: '2rem',\n\t\t\t\t\tfontWeight: 700,\n\t\t\t\t\tlineHeight: 1.1,\n\t\t\t\t\tcolor: 'var(--theme-elevation-800)',\n\t\t\t\t}}\n\t\t\t>\n\t\t\t\t{nf.format(activeNow)}\n\t\t\t</span>\n\t\t\t<span style={{ fontSize: '0.75rem', color: 'var(--theme-elevation-400)' }}>\n\t\t\t\t{paused ? pausedLabel : caption}\n\t\t\t</span>\n\t\t\t<TrendChart buckets={points} ariaLabel={caption} minHeight={120} />\n\t\t</>\n\t)\n}\n"],"mappings":";;;;;;AAoBA,SAAgB,gBAAgB,OAA6B;CAC5D,MAAM,EAAE,UAAU,YAAY,QAAQ,eAAe,YAAY,QAAQ,SAAS,gBACjF;CACD,MAAM,CAAC,WAAW,gBAAgB,SAAS,MAAM,gBAAgB;CACjE,MAAM,CAAC,QAAQ,aAAa,SAAS,MAAM,aAAa;CACxD,MAAM,CAAC,QAAQ,aAAa,SAAS,KAAK;CAE1C,gBAAgB;EACf,IAAI,YAAY;EAChB,MAAM,OAAO,cAAc,UAAU;GAAE;GAAQ;GAAe;EAAW,CAAC;EAC1E,MAAM,OAAO,YAAY;GACxB,IAAI;IACH,MAAM,MAAM,MAAM,MAAM,MAAM,EAAE,aAAa,cAAc,CAAC;IAC5D,IAAI,CAAC,IAAI,IAAI;KACZ,IAAI,CAAC,WAAW,UAAU,IAAI;KAC9B;IACD;IACA,MAAM,OAAQ,MAAM,IAAI,KAAK;IAK7B,IAAI,aAAa,KAAK,WAAW,MAAM;IACvC,aAAa,KAAK,SAAS;IAC3B,UAAU,KAAK,MAAM;IACrB,UAAU,KAAK;GAChB,QAAQ;IACP,IAAI,CAAC,WAAW,UAAU,IAAI;GAC/B;EACD;EACA,MAAM,KAAK,YAAY,MAAM,UAAU;EACvC,aAAa;GACZ,YAAY;GACZ,cAAc,EAAE;EACjB;CACD,GAAG;EAAC;EAAU;EAAY;EAAQ;EAAe;CAAU,CAAC;CAE5D,MAAM,KAAK,IAAI,KAAK,aAAa,MAAM;CACvC,MAAM,SAAS,iBAAiB,QAAQ,MAAM;CAC9C,OACC,qBAAA,UAAA,EAAA,UAAA;EACC,oBAAC,QAAD;GACC,OAAO;IACN,UAAU;IACV,YAAY;IACZ,YAAY;IACZ,OAAO;GACR;aAEC,GAAG,OAAO,SAAS;EACf,CAAA;EACN,oBAAC,QAAD;GAAM,OAAO;IAAE,UAAU;IAAW,OAAO;GAA6B;aACtE,SAAS,cAAc;EACnB,CAAA;EACN,oBAAC,YAAD;GAAY,SAAS;GAAQ,WAAW;GAAS,WAAW;EAAM,CAAA;CACjE,EAAA,CAAA;AAEJ"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { keys } from "../translations/keys.js";
|
|
2
|
+
//#region src/widgets/breakdownTypes.ts
|
|
3
|
+
const BREAKDOWN_SPECS = [
|
|
4
|
+
{
|
|
5
|
+
slug: "analytics-breakdown-pages",
|
|
6
|
+
dimension: "page",
|
|
7
|
+
label: keys.widgetBreakdownPages
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
slug: "analytics-breakdown-sources",
|
|
11
|
+
dimension: "source",
|
|
12
|
+
label: keys.widgetBreakdownSources
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
slug: "analytics-breakdown-devices",
|
|
16
|
+
dimension: "device",
|
|
17
|
+
label: keys.widgetBreakdownDevices
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
slug: "analytics-breakdown-countries",
|
|
21
|
+
dimension: "country",
|
|
22
|
+
label: keys.widgetBreakdownCountries
|
|
23
|
+
}
|
|
24
|
+
];
|
|
25
|
+
const breakdownSpecBySlug = (slug) => BREAKDOWN_SPECS.find((s) => s.slug === slug);
|
|
26
|
+
//#endregion
|
|
27
|
+
export { BREAKDOWN_SPECS, breakdownSpecBySlug };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=breakdownTypes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"breakdownTypes.js","names":[],"sources":["../../src/widgets/breakdownTypes.ts"],"sourcesContent":["import type { DimensionKey, MetricKey } from '../core/contract'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport type { WidgetRange } from './types'\n\nexport interface BreakdownWidgetData {\n\ttitle?: string\n\tmetric?: MetricKey\n\ttimeframe?: TimeframePreset | 'custom'\n\trange?: WidgetRange\n\tlimit?: number\n\tdataSource?: string\n}\n\nexport interface BreakdownSpec {\n\tslug: string\n\tdimension: DimensionKey\n\tlabel: TranslationKey\n}\n\nexport const BREAKDOWN_SPECS: BreakdownSpec[] = [\n\t{ slug: 'analytics-breakdown-pages', dimension: 'page', label: keys.widgetBreakdownPages },\n\t{ slug: 'analytics-breakdown-sources', dimension: 'source', label: keys.widgetBreakdownSources },\n\t{ slug: 'analytics-breakdown-devices', dimension: 'device', label: keys.widgetBreakdownDevices },\n\t{\n\t\tslug: 'analytics-breakdown-countries',\n\t\tdimension: 'country',\n\t\tlabel: keys.widgetBreakdownCountries,\n\t},\n]\n\nexport const breakdownSpecBySlug = (slug: string): BreakdownSpec | undefined =>\n\tBREAKDOWN_SPECS.find((s) => s.slug === slug)\n"],"mappings":";;AAoBA,MAAa,kBAAmC;CAC/C;EAAE,MAAM;EAA6B,WAAW;EAAQ,OAAO,KAAK;CAAqB;CACzF;EAAE,MAAM;EAA+B,WAAW;EAAU,OAAO,KAAK;CAAuB;CAC/F;EAAE,MAAM;EAA+B,WAAW;EAAU,OAAO,KAAK;CAAuB;CAC/F;EACC,MAAM;EACN,WAAW;EACX,OAAO,KAAK;CACb;AACD;AAEA,MAAa,uBAAuB,SACnC,gBAAgB,MAAM,MAAM,EAAE,SAAS,IAAI"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/widgets/cardChrome.ts
|
|
2
|
+
const cardStyle = {
|
|
3
|
+
display: "flex",
|
|
4
|
+
flexDirection: "column",
|
|
5
|
+
gap: "0.375rem",
|
|
6
|
+
padding: "var(--base, 1rem)",
|
|
7
|
+
background: "var(--theme-elevation-50)",
|
|
8
|
+
border: "1px solid var(--theme-elevation-150)",
|
|
9
|
+
borderRadius: "var(--style-radius-m, 6px)",
|
|
10
|
+
boxSizing: "border-box"
|
|
11
|
+
};
|
|
12
|
+
const labelStyle = {
|
|
13
|
+
fontSize: "0.6875rem",
|
|
14
|
+
fontWeight: 600,
|
|
15
|
+
letterSpacing: "0.04em",
|
|
16
|
+
textTransform: "uppercase",
|
|
17
|
+
color: "var(--theme-elevation-500)"
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
export { cardStyle, labelStyle };
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=cardChrome.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cardChrome.js","names":[],"sources":["../../src/widgets/cardChrome.ts"],"sourcesContent":["import type { CSSProperties } from 'react'\n\nexport const cardStyle: CSSProperties = {\n\tdisplay: 'flex',\n\tflexDirection: 'column',\n\tgap: '0.375rem',\n\tpadding: 'var(--base, 1rem)',\n\tbackground: 'var(--theme-elevation-50)',\n\tborder: '1px solid var(--theme-elevation-150)',\n\tborderRadius: 'var(--style-radius-m, 6px)',\n\tboxSizing: 'border-box',\n}\n\nexport const labelStyle: CSSProperties = {\n\tfontSize: '0.6875rem',\n\tfontWeight: 600,\n\tletterSpacing: '0.04em',\n\ttextTransform: 'uppercase',\n\tcolor: 'var(--theme-elevation-500)',\n}\n"],"mappings":";AAEA,MAAa,YAA2B;CACvC,SAAS;CACT,eAAe;CACf,KAAK;CACL,SAAS;CACT,YAAY;CACZ,QAAQ;CACR,cAAc;CACd,WAAW;AACZ;AAEA,MAAa,aAA4B;CACxC,UAAU;CACV,YAAY;CACZ,eAAe;CACf,eAAe;CACf,OAAO;AACR"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { addDaysInTz, startOfDayInTz } from "../timeframe/tz.js";
|
|
2
|
+
//#region src/widgets/comparison.ts
|
|
3
|
+
const DAY_MS = 864e5;
|
|
4
|
+
/**
|
|
5
|
+
* Comparison windows beyond a year are not meaningful period-over-period reads; the cap
|
|
6
|
+
* also rejects the unbounded `allTime` range, which would otherwise compare against a
|
|
7
|
+
* pre-1970 window.
|
|
8
|
+
*/
|
|
9
|
+
const MAX_COMPARISON_DAYS = 366;
|
|
10
|
+
/**
|
|
11
|
+
* The comparable window immediately preceding `range`, aligned to whole reporting-timezone
|
|
12
|
+
* days so daily rollup stores (which stamp each day at its local midnight) never lose their
|
|
13
|
+
* first day to a mid-day window start. It spans the same count of calendar days the current
|
|
14
|
+
* range touches (the current partial day counts as one) and ends 1ms before the current
|
|
15
|
+
* range's first day. Returns null when the range spans more than
|
|
16
|
+
* {@link MAX_COMPARISON_DAYS} days, where a previous period is not meaningful.
|
|
17
|
+
*/
|
|
18
|
+
const previousWindow = (range, tz = "UTC") => {
|
|
19
|
+
const firstDay = startOfDayInTz(range.start, tz);
|
|
20
|
+
const lastDay = startOfDayInTz(range.end, tz);
|
|
21
|
+
const days = Math.round((lastDay.getTime() - firstDay.getTime()) / DAY_MS) + 1;
|
|
22
|
+
if (!Number.isFinite(days) || days < 1 || days > MAX_COMPARISON_DAYS) return null;
|
|
23
|
+
return {
|
|
24
|
+
start: addDaysInTz(firstDay, -days, tz),
|
|
25
|
+
end: /* @__PURE__ */ new Date(firstDay.getTime() - 1)
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Period-over-period delta for one metric value against its previous-window value.
|
|
30
|
+
* Returns null when either side is missing so the caller can omit the comparison.
|
|
31
|
+
*/
|
|
32
|
+
const computeDelta = (current, previous) => {
|
|
33
|
+
if (current === void 0 || previous === void 0) return null;
|
|
34
|
+
const diff = current - previous;
|
|
35
|
+
return {
|
|
36
|
+
direction: diff > 0 ? "up" : diff < 0 ? "down" : "none",
|
|
37
|
+
percent: previous === 0 ? null : diff / previous * 100
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
//#endregion
|
|
41
|
+
export { computeDelta, previousWindow };
|
|
42
|
+
|
|
43
|
+
//# sourceMappingURL=comparison.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"comparison.js","names":[],"sources":["../../src/widgets/comparison.ts"],"sourcesContent":["import type { DateRange } from '../core/contract'\nimport { addDaysInTz, DEFAULT_TIMEZONE, startOfDayInTz } from '../timeframe/tz'\n\nexport type DeltaDirection = 'up' | 'down' | 'none'\n\nexport interface MetricDelta {\n\tdirection: DeltaDirection\n\t/**\n\t * Signed percentage change vs the previous value, or null when the previous value is\n\t * 0 (the baseline is undefined, so a percentage would be infinite/meaningless).\n\t */\n\tpercent: number | null\n}\n\nconst DAY_MS = 86_400_000\n\n/**\n * Comparison windows beyond a year are not meaningful period-over-period reads; the cap\n * also rejects the unbounded `allTime` range, which would otherwise compare against a\n * pre-1970 window.\n */\nconst MAX_COMPARISON_DAYS = 366\n\n/**\n * The comparable window immediately preceding `range`, aligned to whole reporting-timezone\n * days so daily rollup stores (which stamp each day at its local midnight) never lose their\n * first day to a mid-day window start. It spans the same count of calendar days the current\n * range touches (the current partial day counts as one) and ends 1ms before the current\n * range's first day. Returns null when the range spans more than\n * {@link MAX_COMPARISON_DAYS} days, where a previous period is not meaningful.\n */\nexport const previousWindow = (\n\trange: DateRange,\n\ttz: string = DEFAULT_TIMEZONE\n): DateRange | null => {\n\tconst firstDay = startOfDayInTz(range.start, tz)\n\tconst lastDay = startOfDayInTz(range.end, tz)\n\tconst days = Math.round((lastDay.getTime() - firstDay.getTime()) / DAY_MS) + 1\n\tif (!Number.isFinite(days) || days < 1 || days > MAX_COMPARISON_DAYS) {\n\t\treturn null\n\t}\n\treturn { start: addDaysInTz(firstDay, -days, tz), end: new Date(firstDay.getTime() - 1) }\n}\n\n/**\n * Period-over-period delta for one metric value against its previous-window value.\n * Returns null when either side is missing so the caller can omit the comparison.\n */\nexport const computeDelta = (current?: number, previous?: number): MetricDelta | null => {\n\tif (current === undefined || previous === undefined) {\n\t\treturn null\n\t}\n\tconst diff = current - previous\n\tconst direction: DeltaDirection = diff > 0 ? 'up' : diff < 0 ? 'down' : 'none'\n\tconst percent = previous === 0 ? null : (diff / previous) * 100\n\treturn { direction, percent }\n}\n"],"mappings":";;AAcA,MAAM,SAAS;;;;;;AAOf,MAAM,sBAAsB;;;;;;;;;AAU5B,MAAa,kBACZ,OACA,KAAA,UACsB;CACtB,MAAM,WAAW,eAAe,MAAM,OAAO,EAAE;CAC/C,MAAM,UAAU,eAAe,MAAM,KAAK,EAAE;CAC5C,MAAM,OAAO,KAAK,OAAO,QAAQ,QAAQ,IAAI,SAAS,QAAQ,KAAK,MAAM,IAAI;CAC7E,IAAI,CAAC,OAAO,SAAS,IAAI,KAAK,OAAO,KAAK,OAAO,qBAChD,OAAO;CAER,OAAO;EAAE,OAAO,YAAY,UAAU,CAAC,MAAM,EAAE;EAAG,qBAAK,IAAI,KAAK,SAAS,QAAQ,IAAI,CAAC;CAAE;AACzF;;;;;AAMA,MAAa,gBAAgB,SAAkB,aAA0C;CACxF,IAAI,YAAY,KAAA,KAAa,aAAa,KAAA,GACzC,OAAO;CAER,MAAM,OAAO,UAAU;CAGvB,OAAO;EAAE,WAFyB,OAAO,IAAI,OAAO,OAAO,IAAI,SAAS;EAEpD,SADJ,aAAa,IAAI,OAAQ,OAAO,WAAY;CAChC;AAC7B"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { CapabilityRequirement } from "../core/capabilities.js";
|
|
2
|
+
import { Field, Widget, WidgetWidth } from "payload";
|
|
3
|
+
|
|
4
|
+
//#region src/widgets/customWidget.d.ts
|
|
5
|
+
/** App-provided custom dashboard widget. Registered via the `widgets.register` option. */
|
|
6
|
+
interface CustomWidgetDef {
|
|
7
|
+
slug: string;
|
|
8
|
+
/** The app's own RSC import path, e.g. `@/widgets/My#default`. */
|
|
9
|
+
component: string;
|
|
10
|
+
/** A plain label, a locale->label map, or a Payload label function (`Widget['label']`). */
|
|
11
|
+
label: Exclude<Widget['label'], undefined>;
|
|
12
|
+
fields?: Field[];
|
|
13
|
+
/** Capability gate; omit to always register. */
|
|
14
|
+
requires?: CapabilityRequirement;
|
|
15
|
+
minWidth?: WidgetWidth;
|
|
16
|
+
maxWidth?: WidgetWidth;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { CustomWidgetDef };
|
|
20
|
+
//# sourceMappingURL=customWidget.d.ts.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { satisfiesCapabilities } from "../core/capabilities.js";
|
|
2
|
+
//#region src/widgets/customWidget.ts
|
|
3
|
+
const isSupported = (requires, adapters) => !requires || adapters.some((a) => satisfiesCapabilities(a.capabilities, requires));
|
|
4
|
+
/**
|
|
5
|
+
* Turn app-provided custom widget defs into Payload widgets: validate the slug (the
|
|
6
|
+
* `analytics-` prefix is reserved for built-ins), skip a disabled slug, and drop a def
|
|
7
|
+
* whose `requires` no configured adapter satisfies. Throws on a reserved or empty slug
|
|
8
|
+
* so a misconfiguration fails at boot rather than silently dropping the widget.
|
|
9
|
+
*/
|
|
10
|
+
const buildCustomWidgets = (defs, adapters, disabled) => {
|
|
11
|
+
const built = [];
|
|
12
|
+
for (const def of defs) {
|
|
13
|
+
if (!def.slug || def.slug.startsWith("analytics-")) throw new Error(`analytics: custom widget slug "${def.slug}" is invalid; it must be non-empty and must not use the reserved "analytics-" prefix`);
|
|
14
|
+
if (disabled.includes(def.slug)) continue;
|
|
15
|
+
if (!isSupported(def.requires, adapters)) continue;
|
|
16
|
+
built.push({
|
|
17
|
+
slug: def.slug,
|
|
18
|
+
Component: def.component,
|
|
19
|
+
label: def.label,
|
|
20
|
+
fields: def.fields ?? [],
|
|
21
|
+
minWidth: def.minWidth,
|
|
22
|
+
maxWidth: def.maxWidth
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return built;
|
|
26
|
+
};
|
|
27
|
+
//#endregion
|
|
28
|
+
export { buildCustomWidgets };
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=customWidget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customWidget.js","names":[],"sources":["../../src/widgets/customWidget.ts"],"sourcesContent":["import type { Field, Widget, WidgetWidth } from 'payload'\nimport { type CapabilityRequirement, satisfiesCapabilities } from '../core/capabilities'\nimport type { AnalyticsAdapter } from '../core/contract'\n\n/** App-provided custom dashboard widget. Registered via the `widgets.register` option. */\nexport interface CustomWidgetDef {\n\tslug: string\n\t/** The app's own RSC import path, e.g. `@/widgets/My#default`. */\n\tcomponent: string\n\t/** A plain label, a locale->label map, or a Payload label function (`Widget['label']`). */\n\tlabel: Exclude<Widget['label'], undefined>\n\tfields?: Field[]\n\t/** Capability gate; omit to always register. */\n\trequires?: CapabilityRequirement\n\tminWidth?: WidgetWidth\n\tmaxWidth?: WidgetWidth\n}\n\nconst isSupported = (\n\trequires: CapabilityRequirement | undefined,\n\tadapters: AnalyticsAdapter[]\n): boolean => !requires || adapters.some((a) => satisfiesCapabilities(a.capabilities, requires))\n\n/**\n * Turn app-provided custom widget defs into Payload widgets: validate the slug (the\n * `analytics-` prefix is reserved for built-ins), skip a disabled slug, and drop a def\n * whose `requires` no configured adapter satisfies. Throws on a reserved or empty slug\n * so a misconfiguration fails at boot rather than silently dropping the widget.\n */\nexport const buildCustomWidgets = (\n\tdefs: CustomWidgetDef[],\n\tadapters: AnalyticsAdapter[],\n\tdisabled: string[]\n): Widget[] => {\n\tconst built: Widget[] = []\n\tfor (const def of defs) {\n\t\tif (!def.slug || def.slug.startsWith('analytics-')) {\n\t\t\tthrow new Error(\n\t\t\t\t`analytics: custom widget slug \"${def.slug}\" is invalid; it must be non-empty and must not use the reserved \"analytics-\" prefix`\n\t\t\t)\n\t\t}\n\t\tif (disabled.includes(def.slug)) {\n\t\t\tcontinue\n\t\t}\n\t\tif (!isSupported(def.requires, adapters)) {\n\t\t\tcontinue\n\t\t}\n\t\tbuilt.push({\n\t\t\tslug: def.slug,\n\t\t\tComponent: def.component,\n\t\t\tlabel: def.label,\n\t\t\tfields: def.fields ?? [],\n\t\t\tminWidth: def.minWidth,\n\t\t\tmaxWidth: def.maxWidth,\n\t\t})\n\t}\n\treturn built\n}\n"],"mappings":";;AAkBA,MAAM,eACL,UACA,aACa,CAAC,YAAY,SAAS,MAAM,MAAM,sBAAsB,EAAE,cAAc,QAAQ,CAAC;;;;;;;AAQ/F,MAAa,sBACZ,MACA,UACA,aACc;CACd,MAAM,QAAkB,CAAC;CACzB,KAAK,MAAM,OAAO,MAAM;EACvB,IAAI,CAAC,IAAI,QAAQ,IAAI,KAAK,WAAW,YAAY,GAChD,MAAM,IAAI,MACT,kCAAkC,IAAI,KAAK,qFAC5C;EAED,IAAI,SAAS,SAAS,IAAI,IAAI,GAC7B;EAED,IAAI,CAAC,YAAY,IAAI,UAAU,QAAQ,GACtC;EAED,MAAM,KAAK;GACV,MAAM,IAAI;GACV,WAAW,IAAI;GACf,OAAO,IAAI;GACX,QAAQ,IAAI,UAAU,CAAC;GACvB,UAAU,IAAI;GACd,UAAU,IAAI;EACf,CAAC;CACF;CACA,OAAO;AACR"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { WidgetInstance } from "payload";
|
|
2
|
+
|
|
3
|
+
//#region src/widgets/defaults.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Default analytics widget instances to spread into an app's own
|
|
6
|
+
* `admin.dashboard.defaultLayout`. A plugin must never set `defaultLayout` itself
|
|
7
|
+
* (Payload applies it with `??=`, so any setter would clobber the app's layout).
|
|
8
|
+
*/
|
|
9
|
+
declare const analyticsDefaultWidgets: () => WidgetInstance[];
|
|
10
|
+
//#endregion
|
|
11
|
+
export { analyticsDefaultWidgets };
|
|
12
|
+
//# sourceMappingURL=defaults.d.ts.map
|