@10x-media/analytics 1.0.0-beta.12 → 1.0.0-beta.13
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 +67 -0
- package/dist/adapters/ga4/ga4.d.ts +6 -0
- package/dist/adapters/ga4/ga4.js +3 -2
- package/dist/adapters/ga4/ga4.js.map +1 -1
- package/dist/adapters/plausible/plausible.d.ts +6 -0
- package/dist/adapters/plausible/plausible.js +2 -1
- package/dist/adapters/plausible/plausible.js.map +1 -1
- package/dist/adapters/posthog/posthog.d.ts +6 -0
- package/dist/adapters/posthog/posthog.js +21 -8
- package/dist/adapters/posthog/posthog.js.map +1 -1
- package/dist/adapters/umami/umami.d.ts +6 -0
- package/dist/adapters/umami/umami.js +2 -1
- package/dist/adapters/umami/umami.js.map +1 -1
- package/dist/capture/exclusionEndpoint.js +1 -1
- package/dist/capture/exclusionEndpoint.js.map +1 -1
- package/dist/capture/slots.js +3 -3
- package/dist/capture/slots.js.map +1 -1
- package/dist/capture/trackerConfig.js +11 -3
- package/dist/capture/trackerConfig.js.map +1 -1
- package/dist/charts/series.js +1 -1
- package/dist/core/adminOptions.js +6 -1
- package/dist/core/adminOptions.js.map +1 -1
- package/dist/core/capture.d.ts +3 -1
- package/dist/core/contract.d.ts +13 -1
- package/dist/core/contract.js.map +1 -1
- package/dist/core/options.d.ts +37 -5
- package/dist/core/options.js +6 -2
- package/dist/core/options.js.map +1 -1
- package/dist/core/registry.js.map +1 -1
- package/dist/core/scopeOptions.js +2 -0
- package/dist/core/scopeOptions.js.map +1 -1
- package/dist/core/scopedRead.js +170 -21
- package/dist/core/scopedRead.js.map +1 -1
- package/dist/core/sourceLabel.d.ts +10 -0
- package/dist/core/sourceLabel.js +63 -0
- package/dist/core/sourceLabel.js.map +1 -0
- package/dist/core/sourceOptions.js +18 -6
- package/dist/core/sourceOptions.js.map +1 -1
- package/dist/core/sourcePolicy.d.ts +55 -0
- package/dist/core/sourcePolicy.js +51 -0
- package/dist/core/sourcePolicy.js.map +1 -0
- package/dist/exports/adapters/contract.d.ts +2 -1
- package/dist/exports/adapters/ga4.d.ts +3 -2
- package/dist/exports/adapters/native.d.ts +3 -2
- package/dist/exports/adapters/plausible.d.ts +3 -2
- package/dist/exports/adapters/posthog.d.ts +3 -2
- package/dist/exports/adapters/umami.d.ts +3 -2
- package/dist/exports/client.d.ts +7 -5
- package/dist/exports/client.js +2 -1
- package/dist/exports/next.d.ts +3 -2
- package/dist/exports/rsc.d.ts +7 -6
- package/dist/exports/testing.d.ts +3 -2
- package/dist/exports/types.d.ts +12 -8
- package/dist/fields/AnalyticsPanelClient.js.map +1 -1
- package/dist/fields/AnalyticsPanelField.d.ts +1 -1
- package/dist/fields/AnalyticsPanelField.js +3 -1
- package/dist/fields/AnalyticsPanelField.js.map +1 -1
- package/dist/fields/AnalyticsStatField.d.ts +1 -1
- package/dist/fields/AnalyticsStatField.js +1 -1
- package/dist/fields/PanelContent.js +6 -1
- package/dist/fields/PanelContent.js.map +1 -1
- package/dist/fields/config/fetchSources.d.ts +3 -0
- package/dist/fields/config/fetchSources.js +5 -3
- package/dist/fields/config/fetchSources.js.map +1 -1
- package/dist/fields/readForDocument.d.ts +5 -4
- package/dist/fields/readForDocument.js +6 -2
- package/dist/fields/readForDocument.js.map +1 -1
- package/dist/goals/collection.js +4 -31
- package/dist/goals/collection.js.map +1 -1
- package/dist/goals/resolver.js +2 -2
- package/dist/goals/trackGoalAction.js +1 -1
- package/dist/index.d.ts +14 -11
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/native/geo/maxmindResolver.js +1 -1
- package/dist/native/ingest/normalizeEvent.js +1 -1
- package/dist/native/nativeAdapter.d.ts +6 -0
- package/dist/native/nativeAdapter.js +3 -2
- package/dist/native/nativeAdapter.js.map +1 -1
- package/dist/plugin/createRuntime.js +6 -3
- package/dist/plugin/createRuntime.js.map +1 -1
- package/dist/plugin/documentEndpoint.js +1 -0
- package/dist/plugin/documentEndpoint.js.map +1 -1
- package/dist/plugin/pluginCollections.js +11 -5
- package/dist/plugin/pluginCollections.js.map +1 -1
- package/dist/plugin/queryEndpoint.js +1 -1
- package/dist/plugin/queryEndpoint.js.map +1 -1
- package/dist/plugin/readContextForRequest.d.ts +11 -0
- package/dist/plugin/readContextForRequest.js +25 -15
- package/dist/plugin/readContextForRequest.js.map +1 -1
- package/dist/plugin/runtime.js +13 -5
- package/dist/plugin/runtime.js.map +1 -1
- package/dist/plugin/sourcesEndpoint.js +8 -9
- package/dist/plugin/sourcesEndpoint.js.map +1 -1
- package/dist/providers/ProvidersListNotice.d.ts +11 -0
- package/dist/providers/ProvidersListNotice.js +36 -0
- package/dist/providers/ProvidersListNotice.js.map +1 -0
- package/dist/providers/collection.d.ts +9 -1
- package/dist/providers/collection.js +107 -42
- package/dist/providers/collection.js.map +1 -1
- package/dist/providers/factory.d.ts +1 -1
- package/dist/providers/factory.js +8 -11
- package/dist/providers/factory.js.map +1 -1
- package/dist/providers/probe.js +5 -1
- package/dist/providers/probe.js.map +1 -1
- package/dist/providers/registry.js +49 -39
- package/dist/providers/registry.js.map +1 -1
- package/dist/providers/resolver.js +6 -3
- package/dist/providers/resolver.js.map +1 -1
- package/dist/providers/scopeUnique.js +41 -0
- package/dist/providers/scopeUnique.js.map +1 -0
- package/dist/query/fetchQuery.d.ts +1 -1
- package/dist/surfacing/rebucket.js +1 -1
- package/dist/tracker/config.d.ts +5 -0
- package/dist/tracker/config.js.map +1 -1
- package/dist/tracker/sinks/createSink.js +4 -1
- package/dist/tracker/sinks/createSink.js.map +1 -1
- package/dist/tracker/sinks/ga4.js +2 -2
- package/dist/tracker/sinks/posthog.js +30 -2
- package/dist/tracker/sinks/posthog.js.map +1 -1
- package/dist/tracker/sinks/vendor.js +2 -2
- package/dist/tracker/sinks/vendor.js.map +1 -1
- package/dist/translations/ar.js +6 -0
- package/dist/translations/ar.js.map +1 -1
- package/dist/translations/de.js +6 -0
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +6 -0
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +6 -0
- package/dist/translations/es.js.map +1 -1
- package/dist/translations/fr.js +6 -0
- package/dist/translations/fr.js.map +1 -1
- package/dist/translations/id.js +6 -0
- package/dist/translations/id.js.map +1 -1
- package/dist/translations/keys.d.ts +6 -0
- package/dist/translations/keys.js +7 -1
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +6 -0
- package/dist/translations/ko.js.map +1 -1
- package/dist/translations/pt.js +6 -0
- package/dist/translations/pt.js.map +1 -1
- package/dist/translations/ru.js +6 -0
- package/dist/translations/ru.js.map +1 -1
- package/dist/translations/uk.js +6 -0
- package/dist/translations/uk.js.map +1 -1
- package/dist/translations/zh.js +6 -0
- package/dist/translations/zh.js.map +1 -1
- package/dist/ui/analytics.css +5 -0
- package/dist/ui/components/SourcesNoticeBanner.js +20 -0
- package/dist/ui/components/SourcesNoticeBanner.js.map +1 -0
- package/dist/view/ViewRoot.js +2 -0
- package/dist/view/ViewRoot.js.map +1 -1
- package/dist/view/registerView.js +5 -4
- package/dist/view/registerView.js.map +1 -1
- package/dist/view/viewContext.d.ts +1 -1
- package/dist/view/viewProps.d.ts +1 -1
- package/dist/view/viewProps.js +3 -2
- package/dist/view/viewProps.js.map +1 -1
- package/dist/widgets/AnalyticsBreakdownWidget.js +1 -0
- package/dist/widgets/AnalyticsBreakdownWidget.js.map +1 -1
- package/dist/widgets/AnalyticsGoalsWidget.js +1 -0
- package/dist/widgets/AnalyticsGoalsWidget.js.map +1 -1
- package/dist/widgets/AnalyticsMetricWidget.js +1 -0
- package/dist/widgets/AnalyticsMetricWidget.js.map +1 -1
- package/dist/widgets/AnalyticsRealtimeWidget.js +15 -7
- package/dist/widgets/AnalyticsRealtimeWidget.js.map +1 -1
- package/dist/widgets/AnalyticsTrendWidget.js +1 -0
- package/dist/widgets/AnalyticsTrendWidget.js.map +1 -1
- package/dist/widgets/defaultLayout.d.ts +26 -0
- package/dist/widgets/defaultLayout.js +51 -0
- package/dist/widgets/defaultLayout.js.map +1 -0
- package/dist/widgets/notices.js +1 -0
- package/dist/widgets/notices.js.map +1 -1
- package/dist/widgets/prepareWidgetRead.d.ts +14 -1
- package/dist/widgets/prepareWidgetRead.js +6 -2
- package/dist/widgets/prepareWidgetRead.js.map +1 -1
- package/dist/widgets/readForWidget.d.ts +4 -4
- package/dist/widgets/readForWidget.js +3 -1
- package/dist/widgets/readForWidget.js.map +1 -1
- package/dist/widgets/readForWidgetBreakdown.d.ts +4 -4
- package/dist/widgets/readForWidgetBreakdown.js +3 -1
- package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
- package/dist/widgets/readForWidgetGoals.d.ts +4 -4
- package/dist/widgets/readForWidgetGoals.js +3 -1
- package/dist/widgets/readForWidgetGoals.js.map +1 -1
- package/dist/widgets/readForWidgetRealtime.d.ts +4 -3
- package/dist/widgets/readForWidgetRealtime.js +3 -1
- package/dist/widgets/readForWidgetRealtime.js.map +1 -1
- package/dist/widgets/readForWidgetSeries.d.ts +4 -4
- package/dist/widgets/readForWidgetSeries.js +3 -1
- package/dist/widgets/readForWidgetSeries.js.map +1 -1
- package/dist/widgets/registerWidgets.js +3 -1
- package/dist/widgets/registerWidgets.js.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"viewProps.js","names":[],"sources":["../../src/view/viewProps.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { readGate } from '../access/readGate'\nimport { type MetricKey, PLATFORM_SCOPE } from '../core/contract'\nimport { DEFAULT_VIEW, type ResolvedOptions } from '../core/options'\nimport { type RequestedScope, resolveRequestedScope } from '../core/scopedRead'\nimport type { SourcesResponse } from '../fields/config/fetchSources'\nimport { apiPath } from '../plugin/apiPath'\nimport { resolveSourcesForRequest } from '../plugin/readContextForRequest'\nimport { analyticsRoutes } from '../plugin/routes'\nimport {\n\ttype AnalyticsRuntime,\n\tgetRuntime,\n\tgoalNamesFor,\n\tresolveCurrencyFor,\n\tresolveScopeFor,\n\tresolveTimezoneFor,\n\ttype ScopeGates,\n\tscopeGates,\n} from '../plugin/runtime'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { DEFAULT_TIMEZONE } from '../timeframe/tz'\nimport { intlLocale } from '../translations/intl'\nimport type { CountryFlags } from './CountryFlag'\nimport { aggregateFrom, isAggregateToken } from './state'\n\n/**\n * A goal as the view lists it: the picker needs no match or value details.\n *\n * @beta\n */\nexport interface ViewGoal {\n\tslug: string\n\tname: string\n}\n\n/**\n * Which scope the view reads; scoped installs only. The host owns the scope and the view only\n * toggles aggregation, so `own` sits beside `current` for the Site select to switch back to.\n *\n * @beta\n */\nexport interface ViewScopes {\n\t/** The scope the reads carry: `'*'` for every scope at once, `''` for none. */\n\tcurrent: string\n\t/** The scope this request resolves to on its own, whatever the URL asked for. */\n\town: string\n\t/** What `scope.label` calls `own`, when it names it. */\n\townLabel?: string\n\t/** `access.crossScope.aggregate` for this request; gates the Site select. */\n\tcanAggregate: boolean\n}\n\n/**\n * The endpoints the view reads through, each under the app's `routes.api` and the plugin's base.\n *\n * @beta\n */\nexport interface ViewEndpoints {\n\tquery: string\n\trefresh: string\n\tlive: string\n}\n\n/**\n * Everything the client shell needs to render without a second round trip. Serializable\n * by construction: the RSC boundary drops functions, so only wire shapes appear here.\n *\n * @beta\n */\nexport interface AnalyticsViewClientProps {\n\t/** Same payload the sources endpoint answers, resolved for this request's scope. */\n\tsources: SourcesResponse\n\tgoals: ViewGoal[]\n\tdefaults: { range: TimeframePreset; metric: MetricKey }\n\tendpoints: ViewEndpoints\n\tadminRoute: string\n\t/** IANA reporting timezone the day boundaries align to. */\n\ttimezone: string\n\t/** ISO 4217 code revenue is formatted in, from `revenue.currency`; absent, a plain number. */\n\tcurrency?: string\n\t/** Admin UI language (`req.i18n.language`), for number and date formatting. */\n\tlocale: string\n\t/**\n\t * The flag beside a country name: `true` the plugin's own, or the host's `countryFlags.Component`\n\t * as the server shell looked it up, which every row renders with its `code`.\n\t */\n\tcountryFlags: CountryFlags\n\t/**\n\t * Scope this render resolved to, `''` for none. Keys the client body: a host tenant switch's\n\t * `router.refresh()` keeps client state, and two tenants' reads can be identical strings.\n\t */\n\tscopeKey: string\n\t/** Scope caption and Site select; absent on an unscoped install. */\n\tscopes?: ViewScopes\n\t/**\n\t * The `scope` the URL may keep: the aggregate token this request honoured, else absent. The\n\t * client rewrites the address to match what was read.\n\t */\n\tscopeParam?: string\n\t/**\n\t * Where the view asks for the exclusion link of the reader's own scope, while\n\t * `capture.exclusion` is on; absent, the view offers no exclusion at all.\n\t */\n\texclusion?: { endpoint: string }\n\t/**\n\t * The reader works per site and has none selected (a scoped install that resolved no scope\n\t * for them), so the view asks for one instead of reporting that no source is connected.\n\t */\n\tneedsScope?: boolean\n}\n\n/**\n * `denied` is a value, not a thrown error, so the view renders its no-access message inside the\n * admin template instead of redirecting a signed-in reader.\n */\nexport type ViewPropsResult = { denied: true } | { denied: false; props: AnalyticsViewClientProps }\n\n/**\n * Goals the request's scope may see, mirroring the goals endpoint; a failed resolution falls back\n * to config goals only when unscoped.\n */\nconst resolveGoalsList = async (\n\truntime: AnalyticsRuntime,\n\treq: PayloadRequest,\n\tscope: string | null\n): Promise<ViewGoal[]> => {\n\ttry {\n\t\treturn await goalNamesFor({ runtime, req, scope })\n\t} catch (err) {\n\t\treq.payload.logger?.warn(`analytics: view goals listing failed: ${String(err)}`)\n\t\treturn runtime.scoped\n\t\t\t? []\n\t\t\t: (runtime.goals ?? []).map((goal) => ({ slug: goal.slug, name: goal.name }))\n\t}\n}\n\nconst emptySources = (): SourcesResponse => ({ defaultId: null, sources: [] })\n\n/**\n * Caption scope and Site select state. `own` resolves apart from `current` so the select keeps\n * naming the reader's own tenant while reads aggregate.\n */\nconst resolveScopes = async (args: {\n\truntime: AnalyticsRuntime\n\treq: PayloadRequest\n\tgates: ScopeGates\n\tcurrent: string\n\town: string\n}): Promise<ViewScopes> => {\n\tconst { runtime, req, gates, current, own } = args\n\tconst ownLabel = await resolveOwnLabel(runtime, req, own)\n\treturn {\n\t\tcurrent,\n\t\town,\n\t\tcanAggregate: await gates.aggregate(),\n\t\t...(ownLabel === undefined ? {} : { ownLabel }),\n\t}\n}\n\n/** What to call the request's own scope; the id stands wherever the host names nothing. */\nconst resolveOwnLabel = async (\n\truntime: AnalyticsRuntime,\n\treq: PayloadRequest,\n\tscope: string\n): Promise<string | undefined> => {\n\tif (!runtime.scopeLabel || scope === '' || scope === PLATFORM_SCOPE) {\n\t\treturn undefined\n\t}\n\ttry {\n\t\treturn (await runtime.scopeLabel({ req, scope })) ?? undefined\n\t} catch (err) {\n\t\treq.payload.logger?.warn(`analytics: scope.label failed: ${String(err)}`)\n\t\treturn undefined\n\t}\n}\n\n/** The `scope` a URL is allowed to carry into the view: an aggregate token, and nothing else. */\nconst requestedAggregate = (searchParams: ViewSearchParams | undefined): string | undefined => {\n\tconst raw = searchParams?.scope\n\tconst value = Array.isArray(raw) ? raw[0] : raw\n\treturn typeof value === 'string' && isAggregateToken(value) ? value : undefined\n}\n\n/**\n * The scope this request resolves to on its own; `''` for none, for a resolver that threw, and\n * for a tenant the user selected without belonging to it, whose name the view never shows.\n */\nconst ownScopeFor = async (\n\truntime: AnalyticsRuntime,\n\treq: PayloadRequest,\n\town: RequestedScope\n): Promise<string> => {\n\tif (own.ok) {\n\t\treturn own.scope ?? ''\n\t}\n\tif (own.reason !== 'unresolved') {\n\t\treturn ''\n\t}\n\ttry {\n\t\treturn (await resolveScopeFor(runtime, req)) ?? ''\n\t} catch {\n\t\treturn ''\n\t}\n}\n\n/** The admin view's `searchParams`, as Payload hands them to a server component. */\nexport type ViewSearchParams = { [key: string]: string | string[] | undefined }\n\n/**\n * Client shell props for one request. Host's `scope.resolve` is the one scope authority: the URL\n * may only ask to aggregate (granted by `access.crossScope.aggregate`); other `scope` values drop.\n */\nexport const resolveViewProps = async (\n\treq: PayloadRequest,\n\tresolved: ResolvedOptions,\n\tsearchParams?: ViewSearchParams\n): Promise<ViewPropsResult> => {\n\tif (!(await resolved.access.view({ req }))) {\n\t\treturn { denied: true }\n\t}\n\t// `false` cannot reach a render (the view is then never registered); it defaults anyway.\n\tconst view = resolved.view === false ? DEFAULT_VIEW : resolved.view\n\tconst runtime = getRuntime(req.payload)\n\tconst config = req.payload.config\n\tconst routes = analyticsRoutes(config)\n\tconst base = {\n\t\tdefaults: { range: view.defaultRange, metric: view.defaultMetric },\n\t\tendpoints: {\n\t\t\tquery: apiPath(config, routes.query),\n\t\t\trefresh: apiPath(config, routes.refresh),\n\t\t\tlive: apiPath(config, routes.live),\n\t\t},\n\t\tadminRoute: config.routes?.admin ?? '/admin',\n\t\tlocale: intlLocale(req.i18n.language),\n\t\t// The host's component is looked up by the view's body (`renderViewBody`), which hands it on.\n\t\tcountryFlags: resolved.countryFlags === true,\n\t\t...(resolved.capture.exclusion\n\t\t\t? { exclusion: { endpoint: apiPath(config, routes.exclusion) } }\n\t\t\t: {}),\n\t}\n\t// No runtime means the plugin is not installed on this instance: nothing is readable.\n\tif (!runtime) {\n\t\treturn {\n\t\t\tdenied: false,\n\t\t\tprops: {\n\t\t\t\t...base,\n\t\t\t\tsources: emptySources(),\n\t\t\t\tgoals: [],\n\t\t\t\ttimezone: DEFAULT_TIMEZONE,\n\t\t\t\tscopeKey: '',\n\t\t\t},\n\t\t}\n\t}\n\tconst gates = scopeGates(runtime, req)\n\tconst token = requestedAggregate(searchParams)\n\tconst ownRequested = await resolveRequestedScope({ runtime, req, gates })\n\tconst own = await ownScopeFor(runtime, req, ownRequested)\n\t// The aggregate belongs to the tenant it was chosen from; the host's selector can move while\n\t// the address stays open, so a token tagged with another falls back to the request's scope.\n\tconst aggregate = token !== undefined && aggregateFrom(token) === own\n\t// A wildcard this reader may not have degrades to their own scope rather than to nothing:\n\t// they are entitled to that read, and the client rewrites the address to match.\n\tlet requested = ownRequested\n\tif (aggregate) {\n\t\tconst all = await resolveRequestedScope({ runtime, req, gates, raw: PLATFORM_SCOPE })\n\t\tif (all.ok) {\n\t\t\trequested = all\n\t\t}\n\t}\n\tconst scope = requested.ok ? requested.scope : null\n\tconst scopeKey = scope ?? ''\n\tconst registryScope = scope === PLATFORM_SCOPE ? null : scope\n\t// `access.view` may be wider than `access.read`: goal names and scope names are read data too.\n\tconst readable = await readGate(runtime, req).catch(() => false)\n\tconst [sources, goals, timezone, currency, scopes] = await Promise.all([\n\t\trequested.ok\n\t\t\t? resolveSourcesForRequest(req, { gates, scope: requested.gated })\n\t\t\t: emptySources(),\n\t\trequested.ok && readable ? resolveGoalsList(runtime, req, scope) : [],\n\t\tresolveTimezoneFor(runtime, req, registryScope).catch(() => DEFAULT_TIMEZONE),\n\t\tresolveCurrencyFor(runtime, req, registryScope),\n\t\truntime.scoped === true && readable\n\t\t\t? resolveScopes({ runtime, req, gates, current: scopeKey, own })\n\t\t\t: undefined,\n\t])\n\treturn {\n\t\tdenied: false,\n\t\tprops: {\n\t\t\t...base,\n\t\t\tsources: { defaultId: sources.defaultId, sources: sources.sources },\n\t\t\tgoals,\n\t\t\ttimezone,\n\t\t\tscopeKey,\n\t\t\t...(scope === PLATFORM_SCOPE && token !== undefined ? { scopeParam: token } : {}),\n\t\t\t...(!requested.ok && requested.reason === 'unresolved' ? { needsScope: true } : {}),\n\t\t\t...(currency ? { currency } : {}),\n\t\t\t...(scopes ? { scopes } : {}),\n\t\t},\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;AAyHA,MAAM,mBAAmB,OACxB,SACA,KACA,UACyB;CACzB,IAAI;EACH,OAAO,MAAM,aAAa;GAAE;GAAS;GAAK;EAAM,CAAC;CAClD,SAAS,KAAK;EACb,IAAI,QAAQ,QAAQ,KAAK,yCAAyC,OAAO,GAAG,GAAG;EAC/E,OAAO,QAAQ,SACZ,CAAC,KACA,QAAQ,SAAS,CAAC,GAAG,KAAK,UAAU;GAAE,MAAM,KAAK;GAAM,MAAM,KAAK;EAAK,EAAE;CAC9E;AACD;AAEA,MAAM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;GAAO,OAAO,UAAU;EAAM,CAAC,IAC/D,aAAa;EAChB,UAAU,MAAM,WAAW,iBAAiB,SAAS,KAAK,KAAK,IAAI,CAAC;EACpE,mBAAmB,SAAS,KAAK,aAAa,EAAE,YAAA,KAA4B;EAC5E,mBAAmB,SAAS,KAAK,aAAa;EAC9C,QAAQ,WAAW,QAAQ,WACxB,cAAc;GAAE;GAAS;GAAK;GAAO,SAAS;GAAU;EAAI,CAAC,IAC7D,KAAA;CACJ,CAAC;CACD,OAAO;EACN,QAAQ;EACR,OAAO;GACN,GAAG;GACH,SAAS;IAAE,WAAW,QAAQ;IAAW,SAAS,QAAQ;GAAQ;GAClE;GACA;GACA;GACA,GAAI,UAAA,OAA4B,UAAU,KAAA,IAAY,EAAE,YAAY,MAAM,IAAI,CAAC;GAC/E,GAAI,CAAC,UAAU,MAAM,UAAU,WAAW,eAAe,EAAE,YAAY,KAAK,IAAI,CAAC;GACjF,GAAI,WAAW,EAAE,SAAS,IAAI,CAAC;GAC/B,GAAI,SAAS,EAAE,OAAO,IAAI,CAAC;EAC5B;CACD;AACD"}
|
|
1
|
+
{"version":3,"file":"viewProps.js","names":[],"sources":["../../src/view/viewProps.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { readGate } from '../access/readGate'\nimport { type MetricKey, PLATFORM_SCOPE } from '../core/contract'\nimport { DEFAULT_VIEW, type ResolvedOptions } from '../core/options'\nimport { type RequestedScope, resolveRequestedScope } from '../core/scopedRead'\nimport type { SourcesResponse } from '../fields/config/fetchSources'\nimport { apiPath } from '../plugin/apiPath'\nimport { resolveSourcesForRequest } from '../plugin/readContextForRequest'\nimport { analyticsRoutes } from '../plugin/routes'\nimport {\n\ttype AnalyticsRuntime,\n\tgetRuntime,\n\tgoalNamesFor,\n\tresolveCurrencyFor,\n\tresolveScopeFor,\n\tresolveTimezoneFor,\n\ttype ScopeGates,\n\tscopeGates,\n} from '../plugin/runtime'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { DEFAULT_TIMEZONE } from '../timeframe/tz'\nimport { intlLocale } from '../translations/intl'\nimport type { CountryFlags } from './CountryFlag'\nimport { aggregateFrom, isAggregateToken } from './state'\n\n/**\n * A goal as the view lists it: the picker needs no match or value details.\n *\n * @beta\n */\nexport interface ViewGoal {\n\tslug: string\n\tname: string\n}\n\n/**\n * Which scope the view reads; scoped installs only. The host owns the scope and the view only\n * toggles aggregation, so `own` sits beside `current` for the Site select to switch back to.\n *\n * @beta\n */\nexport interface ViewScopes {\n\t/** The scope the reads carry: `'*'` for every scope at once, `''` for none. */\n\tcurrent: string\n\t/** The scope this request resolves to on its own, whatever the URL asked for. */\n\town: string\n\t/** What `scope.label` calls `own`, when it names it. */\n\townLabel?: string\n\t/** `access.crossScope.aggregate` for this request; gates the Site select. */\n\tcanAggregate: boolean\n}\n\n/**\n * The endpoints the view reads through, each under the app's `routes.api` and the plugin's base.\n *\n * @beta\n */\nexport interface ViewEndpoints {\n\tquery: string\n\trefresh: string\n\tlive: string\n}\n\n/**\n * Everything the client shell needs to render without a second round trip. Serializable\n * by construction: the RSC boundary drops functions, so only wire shapes appear here.\n *\n * @beta\n */\nexport interface AnalyticsViewClientProps {\n\t/** Same payload the sources endpoint answers, resolved for this request's scope. */\n\tsources: SourcesResponse\n\tgoals: ViewGoal[]\n\tdefaults: { range: TimeframePreset; metric: MetricKey }\n\tendpoints: ViewEndpoints\n\tadminRoute: string\n\t/** IANA reporting timezone the day boundaries align to. */\n\ttimezone: string\n\t/** ISO 4217 code revenue is formatted in, from `revenue.currency`; absent, a plain number. */\n\tcurrency?: string\n\t/** Admin UI language (`req.i18n.language`), for number and date formatting. */\n\tlocale: string\n\t/**\n\t * The flag beside a country name: `true` the plugin's own, or the host's `countryFlags.Component`\n\t * as the server shell looked it up, which every row renders with its `code`.\n\t */\n\tcountryFlags: CountryFlags\n\t/**\n\t * Scope this render resolved to, `''` for none. Keys the client body: a host tenant switch's\n\t * `router.refresh()` keeps client state, and two tenants' reads can be identical strings.\n\t */\n\tscopeKey: string\n\t/** Scope caption and Site select; absent on an unscoped install. */\n\tscopes?: ViewScopes\n\t/**\n\t * The `scope` the URL may keep: the aggregate token this request honoured, else absent. The\n\t * client rewrites the address to match what was read.\n\t */\n\tscopeParam?: string\n\t/**\n\t * Where the view asks for the exclusion link of the reader's own scope, while\n\t * `capture.exclusion` is on; absent, the view offers no exclusion at all.\n\t */\n\texclusion?: { endpoint: string }\n\t/**\n\t * The reader works per site and has none selected (a scoped install that resolved no scope\n\t * for them), so the view asks for one instead of reporting that no source is connected.\n\t */\n\tneedsScope?: boolean\n}\n\n/**\n * `denied` is a value, not a thrown error, so the view renders its no-access message inside the\n * admin template instead of redirecting a signed-in reader.\n */\nexport type ViewPropsResult = { denied: true } | { denied: false; props: AnalyticsViewClientProps }\n\n/**\n * Goals the request's scope may see, mirroring the goals endpoint; a failed resolution falls back\n * to config goals only when unscoped.\n */\nconst resolveGoalsList = async (\n\truntime: AnalyticsRuntime,\n\treq: PayloadRequest,\n\tscope: string | null\n): Promise<ViewGoal[]> => {\n\ttry {\n\t\treturn await goalNamesFor({ runtime, req, scope })\n\t} catch (err) {\n\t\treq.payload.logger?.warn(`analytics: view goals listing failed: ${String(err)}`)\n\t\treturn runtime.scoped\n\t\t\t? []\n\t\t\t: (runtime.goals ?? []).map((goal) => ({ slug: goal.slug, name: goal.name }))\n\t}\n}\n\nconst emptySources = (): SourcesResponse => ({ defaultId: null, sources: [] })\n\n/**\n * Caption scope and Site select state. `own` resolves apart from `current` so the select keeps\n * naming the reader's own tenant while reads aggregate.\n */\nconst resolveScopes = async (args: {\n\truntime: AnalyticsRuntime\n\treq: PayloadRequest\n\tgates: ScopeGates\n\tcurrent: string\n\town: string\n}): Promise<ViewScopes> => {\n\tconst { runtime, req, gates, current, own } = args\n\tconst ownLabel = await resolveOwnLabel(runtime, req, own)\n\treturn {\n\t\tcurrent,\n\t\town,\n\t\tcanAggregate: await gates.aggregate({ scope: PLATFORM_SCOPE }),\n\t\t...(ownLabel === undefined ? {} : { ownLabel }),\n\t}\n}\n\n/** What to call the request's own scope; the id stands wherever the host names nothing. */\nconst resolveOwnLabel = async (\n\truntime: AnalyticsRuntime,\n\treq: PayloadRequest,\n\tscope: string\n): Promise<string | undefined> => {\n\tif (!runtime.scopeLabel || scope === '' || scope === PLATFORM_SCOPE) {\n\t\treturn undefined\n\t}\n\ttry {\n\t\treturn (await runtime.scopeLabel({ req, scope })) ?? undefined\n\t} catch (err) {\n\t\treq.payload.logger?.warn(`analytics: scope.label failed: ${String(err)}`)\n\t\treturn undefined\n\t}\n}\n\n/** The `scope` a URL is allowed to carry into the view: an aggregate token, and nothing else. */\nconst requestedAggregate = (searchParams: ViewSearchParams | undefined): string | undefined => {\n\tconst raw = searchParams?.scope\n\tconst value = Array.isArray(raw) ? raw[0] : raw\n\treturn typeof value === 'string' && isAggregateToken(value) ? value : undefined\n}\n\n/**\n * The scope this request resolves to on its own; `''` for none, for a resolver that threw, and\n * for a tenant the user selected without belonging to it, whose name the view never shows.\n */\nconst ownScopeFor = async (\n\truntime: AnalyticsRuntime,\n\treq: PayloadRequest,\n\town: RequestedScope\n): Promise<string> => {\n\tif (own.ok) {\n\t\treturn own.scope ?? ''\n\t}\n\tif (own.reason !== 'unresolved') {\n\t\treturn ''\n\t}\n\ttry {\n\t\treturn (await resolveScopeFor(runtime, req)) ?? ''\n\t} catch {\n\t\treturn ''\n\t}\n}\n\n/** The admin view's `searchParams`, as Payload hands them to a server component. */\nexport type ViewSearchParams = { [key: string]: string | string[] | undefined }\n\n/**\n * Client shell props for one request. Host's `scope.resolve` is the one scope authority: the URL\n * may only ask to aggregate (granted by `access.crossScope.aggregate`); other `scope` values drop.\n */\nexport const resolveViewProps = async (\n\treq: PayloadRequest,\n\tresolved: ResolvedOptions,\n\tsearchParams?: ViewSearchParams\n): Promise<ViewPropsResult> => {\n\tif (!(await resolved.access.view({ req }))) {\n\t\treturn { denied: true }\n\t}\n\t// `false` cannot reach a render (the view is then never registered); it defaults anyway.\n\tconst view = resolved.view === false ? DEFAULT_VIEW : resolved.view\n\tconst runtime = getRuntime(req.payload)\n\tconst config = req.payload.config\n\tconst routes = analyticsRoutes(config)\n\tconst base = {\n\t\tdefaults: { range: view.defaultRange, metric: view.defaultMetric },\n\t\tendpoints: {\n\t\t\tquery: apiPath(config, routes.query),\n\t\t\trefresh: apiPath(config, routes.refresh),\n\t\t\tlive: apiPath(config, routes.live),\n\t\t},\n\t\tadminRoute: config.routes?.admin ?? '/admin',\n\t\tlocale: intlLocale(req.i18n.language),\n\t\t// The host's component is looked up by the view's body (`renderViewBody`), which hands it on.\n\t\tcountryFlags: resolved.countryFlags === true,\n\t\t...(resolved.capture.exclusion\n\t\t\t? { exclusion: { endpoint: apiPath(config, routes.exclusion) } }\n\t\t\t: {}),\n\t}\n\t// No runtime means the plugin is not installed on this instance: nothing is readable.\n\tif (!runtime) {\n\t\treturn {\n\t\t\tdenied: false,\n\t\t\tprops: {\n\t\t\t\t...base,\n\t\t\t\tsources: emptySources(),\n\t\t\t\tgoals: [],\n\t\t\t\ttimezone: DEFAULT_TIMEZONE,\n\t\t\t\tscopeKey: '',\n\t\t\t},\n\t\t}\n\t}\n\tconst gates = scopeGates(runtime, req)\n\tconst token = requestedAggregate(searchParams)\n\tconst ownRequested = await resolveRequestedScope({ runtime, req, gates })\n\tconst own = await ownScopeFor(runtime, req, ownRequested)\n\t// The aggregate belongs to the tenant it was chosen from; the host's selector can move while\n\t// the address stays open, so a token tagged with another falls back to the request's scope.\n\tconst aggregate = token !== undefined && aggregateFrom(token) === own\n\t// A wildcard this reader may not have degrades to their own scope rather than to nothing:\n\t// they are entitled to that read, and the client rewrites the address to match.\n\tlet requested = ownRequested\n\tif (aggregate) {\n\t\tconst all = await resolveRequestedScope({ runtime, req, gates, raw: PLATFORM_SCOPE })\n\t\tif (all.ok) {\n\t\t\trequested = all\n\t\t}\n\t}\n\tconst scope = requested.ok ? requested.scope : null\n\tconst scopeKey = scope ?? ''\n\tconst registryScope = scope === PLATFORM_SCOPE ? null : scope\n\t// `access.view` may be wider than `access.read`: goal names and scope names are read data too.\n\tconst readable = await readGate(runtime, req).catch(() => false)\n\tconst [sources, goals, timezone, currency, scopes] = await Promise.all([\n\t\trequested.ok\n\t\t\t? resolveSourcesForRequest(req, { gates, scope: requested.gated })\n\t\t\t: emptySources(),\n\t\trequested.ok && readable ? resolveGoalsList(runtime, req, scope) : [],\n\t\tresolveTimezoneFor(runtime, req, registryScope).catch(() => DEFAULT_TIMEZONE),\n\t\tresolveCurrencyFor(runtime, req, registryScope),\n\t\truntime.scoped === true && readable\n\t\t\t? resolveScopes({ runtime, req, gates, current: scopeKey, own })\n\t\t\t: undefined,\n\t])\n\treturn {\n\t\tdenied: false,\n\t\tprops: {\n\t\t\t...base,\n\t\t\tsources: {\n\t\t\t\tdefaultId: sources.defaultId,\n\t\t\t\tsources: sources.sources,\n\t\t\t\t...(sources.notice ? { notice: sources.notice } : {}),\n\t\t\t},\n\t\t\tgoals,\n\t\t\ttimezone,\n\t\t\tscopeKey,\n\t\t\t...(scope === PLATFORM_SCOPE && token !== undefined ? { scopeParam: token } : {}),\n\t\t\t...(!requested.ok && requested.reason === 'unresolved' ? { needsScope: true } : {}),\n\t\t\t...(currency ? { currency } : {}),\n\t\t\t...(scopes ? { scopes } : {}),\n\t\t},\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;AAyHA,MAAM,mBAAmB,OACxB,SACA,KACA,UACyB;CACzB,IAAI;EACH,OAAO,MAAM,aAAa;GAAE;GAAS;GAAK;EAAM,CAAC;CAClD,SAAS,KAAK;EACb,IAAI,QAAQ,QAAQ,KAAK,yCAAyC,OAAO,GAAG,GAAG;EAC/E,OAAO,QAAQ,SACZ,CAAC,KACA,QAAQ,SAAS,CAAC,GAAG,KAAK,UAAU;GAAE,MAAM,KAAK;GAAM,MAAM,KAAK;EAAK,EAAE;CAC9E;AACD;AAEA,MAAM,sBAAuC;CAAE,WAAW;CAAM,SAAS,CAAC;AAAE;;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AAM,CAAC,IAC/D,aAAa;EAChB,UAAU,MAAM,WAAW,iBAAiB,SAAS,KAAK,KAAK,IAAI,CAAC;EACpE,mBAAmB,SAAS,KAAK,aAAa,EAAE,YAAA,KAA4B;EAC5E,mBAAmB,SAAS,KAAK,aAAa;EAC9C,QAAQ,WAAW,QAAQ,WACxB,cAAc;GAAE;GAAS;GAAK;GAAO,SAAS;GAAU;EAAI,CAAC,IAC7D,KAAA;CACJ,CAAC;CACD,OAAO;EACN,QAAQ;EACR,OAAO;GACN,GAAG;GACH,SAAS;IACR,WAAW,QAAQ;IACnB,SAAS,QAAQ;IACjB,GAAI,QAAQ,SAAS,EAAE,QAAQ,QAAQ,OAAO,IAAI,CAAC;GACpD;GACA;GACA;GACA;GACA,GAAI,UAAA,OAA4B,UAAU,KAAA,IAAY,EAAE,YAAY,MAAM,IAAI,CAAC;GAC/E,GAAI,CAAC,UAAU,MAAM,UAAU,WAAW,eAAe,EAAE,YAAY,KAAK,IAAI,CAAC;GACjF,GAAI,WAAW,EAAE,SAAS,IAAI,CAAC;GAC/B,GAAI,SAAS,EAAE,OAAO,IAAI,CAAC;EAC5B;CACD;AACD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsBreakdownWidget.js","names":[],"sources":["../../src/widgets/AnalyticsBreakdownWidget.tsx"],"sourcesContent":["import type { ReactNode } from 'react'\nimport { barShares } from '../charts/bars'\nimport { currentDate } from '../core/clock'\nimport type { MetricKey } from '../core/contract'\nimport { readingOf } from '../core/metricValues'\nimport { formatMetricValue } from '../fields/format'\nimport { requestCurrency } from '../plugin/runtime'\nimport { intlLocale } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport { metricLabelKey } from '../translations/metricKeys'\nimport { asTranslate, type Translate } from '../translations/server'\nimport { DataTable } from '../ui/components/DataTable'\nimport { toDisplayRows } from '../ui/rows/toDisplayRows'\nimport { CountryFlagImg } from '../view/CountryFlag'\nimport { countryFlagSrc, countryFlagsMount } from '../view/flagSrc'\nimport { renderRowFlag } from '../view/hostCountryFlag'\nimport { DIMENSION_LABELS } from '../view/labels'\nimport { type BreakdownSpec, type BreakdownWidgetData, breakdownSpecBySlug } from './breakdownTypes'\nimport { widgetFilters } from './filterField'\nimport { widgetNotice } from './notices'\nimport { readForWidgetBreakdown } from './readForWidgetBreakdown'\nimport { emptyGoalsKey } from './stateKeys'\nimport { type AnalyticsWidgetProps, viewTabForDimension, widgetLink } from './viewLink'\nimport { WidgetBody, WidgetCard, WidgetEmpty, WidgetNote } from './WidgetCard'\nimport { WidgetFrame, WidgetStateCard } from './WidgetFrame'\nimport { storedWidgetData } from './widgetData'\nimport { resolveWidgetWindow, widgetCaption, windowReadArgs } from './widgetWindow'\n\nconst CLASS_NAME = 'analytics-breakdown-widget'\n\nconst DEFAULT_LIMIT = 5\n\n/** A list fills its row limit more often than not; past this many rows the guess stops paying. */\nconst MAX_SHIMMER_ROWS = 10\n\nconst dataOf = (props: AnalyticsWidgetProps): BreakdownWidgetData => {\n\tconst { limit, ...stored } = storedWidgetData(props.widgetData)\n\treturn { ...stored, ...(typeof limit === 'number' ? { limit } : {}) }\n}\n\nconst titleOf = (\n\tdata: BreakdownWidgetData,\n\tspec: BreakdownSpec | undefined,\n\tt: Translate\n): string => data.title?.trim() || (spec ? t(spec.label) : '')\n\nconst shimmerRows = (limit: number | undefined): number =>\n\tMath.min(\n\t\tMath.max(Math.round(Number(limit ?? DEFAULT_LIMIT)) || DEFAULT_LIMIT, 1),\n\t\tMAX_SHIMMER_ROWS\n\t)\n\n/**\n * The card's frame paints at once; its list streams in when the read answers.\n *\n * @beta\n */\nexport default function AnalyticsBreakdownWidget(props: AnalyticsWidgetProps) {\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst data = dataOf(props)\n\treturn (\n\t\t<WidgetFrame\n\t\t\tclassName={CLASS_NAME}\n\t\t\tkind=\"breakdown\"\n\t\t\trows={shimmerRows(data.limit)}\n\t\t\ttitle={titleOf(data, breakdownSpecBySlug(props.widgetSlug), t)}\n\t\t\twidget={props}\n\t\t>\n\t\t\t<BreakdownWidgetBody {...props} />\n\t\t</WidgetFrame>\n\t)\n}\n\nasync function BreakdownWidgetBody(props: AnalyticsWidgetProps) {\n\tconst spec = breakdownSpecBySlug(props.widgetSlug)\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = intlLocale(props.req.i18n.language)\n\tconst data = dataOf(props)\n\tconst title = titleOf(data, spec, t)\n\tif (!spec) {\n\t\treturn (\n\t\t\t<WidgetCard className={CLASS_NAME} title={title}>\n\t\t\t\t<WidgetEmpty>{t(keys.stateUnavailable)}</WidgetEmpty>\n\t\t\t</WidgetCard>\n\t\t)\n\t}\n\t// The spec's own default, not pageviews: an events or goals widget saved before its\n\t// picker had a default would otherwise rank by a metric its dimension cannot answer.\n\tconst metric: MetricKey = data.metric ?? spec.preferredDefault ?? 'pageviews'\n\tconst window = await resolveWidgetWindow(props.req, data)\n\tif (window.unreadable) {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlocale={locale}\n\t\t\t\tstatus=\"invalid\"\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\tconst reporting = await requestCurrency(props.req)\n\tconst tab = viewTabForDimension(spec.dimension)\n\tconst filters = widgetFilters(data)\n\tconst now = currentDate()\n\tconst result = await readForWidgetBreakdown({\n\t\treq: props.req,\n\t\tmetric,\n\t\tdimension: spec.dimension,\n\t\tlimit: data.limit ?? DEFAULT_LIMIT,\n\t\tadapterId: data.dataSource,\n\t\tnow,\n\t\tfilters,\n\t\t...windowReadArgs(window),\n\t})\n\tconst link = widgetLink(props, t, {\n\t\twindow,\n\t\tadapterId: result.adapterId,\n\t\t...(tab ? { tab, dim: spec.dimension } : {}),\n\t\tmetric,\n\t\tfilters,\n\t})\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlink={link}\n\t\t\t\tlocale={locale}\n\t\t\t\tretryAfterMs={result.retryAfterMs}\n\t\t\t\tstatus={result.status}\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\n\t// Built here rather than in the row map: the same config reads answer every row.\n\tconst flagsMount = countryFlagsMount(props.req.payload.config)\n\tconst flags = props.countryFlags ?? true\n\tconst flagFor = (code: string): ReactNode =>\n\t\ttypeof flags === 'object' ? (\n\t\t\trenderRowFlag(flags, props.req.payload.importMap, code)\n\t\t) : (\n\t\t\t<CountryFlagImg src={countryFlagSrc(flagsMount, code)} />\n\t\t)\n\tconst currency = result.currency ?? reporting\n\tconst caption = await widgetCaption(props.req, { window, filters, read: result, locale, t })\n\t// The goal-resolution states are the list's own empty label here, so the line below it\n\t// carries only what the read says about its numbers.\n\tconst notice = widgetNotice(\n\t\t{ ...result, goalsUnresolved: false, noGoals: false },\n\t\t{ t, locale, now, timeZone: result.timezone }\n\t)\n\tconst displayed = toDisplayRows(\n\t\tresult.rows.map((row) => ({ value: row.dimensionValue, metrics: { [metric]: row.value } })),\n\t\t{\n\t\t\tdimension: spec.dimension,\n\t\t\tprovider: result.provider ?? '',\n\t\t\tt,\n\t\t\tlocale,\n\t\t\t...(result.goals ? { goals: result.goals } : {}),\n\t\t\tcountryFlags: flags !== false,\n\t\t}\n\t)\n\tconst values = displayed.map((row) => readingOf(row.metrics, metric))\n\tconst shares = barShares(values)\n\treturn (\n\t\t<WidgetCard className={CLASS_NAME} caption={caption} link={link} title={title}>\n\t\t\t{displayed.length === 0 ? (\n\t\t\t\t<WidgetEmpty>\n\t\t\t\t\t{t(spec.dimension === 'goal' ? emptyGoalsKey(result) : keys.stateNoBreakdown)}\n\t\t\t\t</WidgetEmpty>\n\t\t\t) : (\n\t\t\t\t<WidgetBody>\n\t\t\t\t\t<DataTable\n\t\t\t\t\t\tbars=\"soft\"\n\t\t\t\t\t\tcolumns={[\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tkey: metric,\n\t\t\t\t\t\t\t\theader: t(metricLabelKey(metric, result.visitorsSummed)),\n\t\t\t\t\t\t\t\tclassName: 'analytics-bars__value',\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t]}\n\t\t\t\t\t\thead=\"hidden\"\n\t\t\t\t\t\tlabel={title}\n\t\t\t\t\t\tlabelHeader={t(DIMENSION_LABELS[spec.dimension])}\n\t\t\t\t\t\tnoValueLabel={t(keys.stateNoValue)}\n\t\t\t\t\t\trows={displayed.map((row, i) => ({\n\t\t\t\t\t\t\tkey: row.key,\n\t\t\t\t\t\t\tlabel: row.label,\n\t\t\t\t\t\t\tbar: shares[i] ?? 0,\n\t\t\t\t\t\t\t...(row.flag === null ? {} : { decoration: flagFor(row.flag) }),\n\t\t\t\t\t\t\tcells: [formatMetricValue(metric, values[i] ?? null, { locale, currency })],\n\t\t\t\t\t\t}))}\n\t\t\t\t\t/>\n\t\t\t\t</WidgetBody>\n\t\t\t)}\n\t\t\t<WidgetNote>{notice}</WidgetNote>\n\t\t</WidgetCard>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,MAAM,aAAa;AAEnB,MAAM,gBAAgB;;AAGtB,MAAM,mBAAmB;AAEzB,MAAM,UAAU,UAAqD;CACpE,MAAM,EAAE,OAAO,GAAG,WAAW,iBAAiB,MAAM,UAAU;CAC9D,OAAO;EAAE,GAAG;EAAQ,GAAI,OAAO,UAAU,WAAW,EAAE,MAAM,IAAI,CAAC;CAAG;AACrE;AAEA,MAAM,WACL,MACA,MACA,MACY,KAAK,OAAO,KAAK,MAAM,OAAO,EAAE,KAAK,KAAK,IAAI;AAE3D,MAAM,eAAe,UACpB,KAAK,IACJ,KAAK,IAAI,KAAK,MAAM,OAAO,SAAS,aAAa,CAAC,KAAK,eAAe,CAAC,GACvE,gBACD;;;;;;AAOD,SAAwB,yBAAyB,OAA6B;CAC7E,MAAM,IAAI,YAAY,MA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;GACC,MAAK;GACL,SAAS,CACR;IACC,KAAK;IACL,QAAQ,EAAE,eAAe,QAAQ,OAAO,cAAc,CAAC;IACvD,WAAW;GACZ,CACD;GACA,MAAK;GACL,OAAO;GACP,aAAa,EAAE,iBAAiB,KAAK,UAAU;GAC/C,cAAc,EAAE,KAAK,YAAY;GACjC,MAAM,UAAU,KAAK,KAAK,OAAO;IAChC,KAAK,IAAI;IACT,OAAO,IAAI;IACX,KAAK,OAAO,MAAM;IAClB,GAAI,IAAI,SAAS,OAAO,CAAC,IAAI,EAAE,YAAY,QAAQ,IAAI,IAAI,EAAE;IAC7D,OAAO,CAAC,kBAAkB,QAAQ,OAAO,MAAM,MAAM;KAAE;KAAQ;IAAS,CAAC,CAAC;GAC3E,EAAE;EACF,CAAA,EACU,CAAA,GAEb,oBAAC,YAAD,EAAA,UAAa,OAAmB,CAAA,CACrB;;AAEd"}
|
|
1
|
+
{"version":3,"file":"AnalyticsBreakdownWidget.js","names":[],"sources":["../../src/widgets/AnalyticsBreakdownWidget.tsx"],"sourcesContent":["import type { ReactNode } from 'react'\nimport { barShares } from '../charts/bars'\nimport { currentDate } from '../core/clock'\nimport type { MetricKey } from '../core/contract'\nimport { readingOf } from '../core/metricValues'\nimport { formatMetricValue } from '../fields/format'\nimport { requestCurrency } from '../plugin/runtime'\nimport { intlLocale } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport { metricLabelKey } from '../translations/metricKeys'\nimport { asTranslate, type Translate } from '../translations/server'\nimport { DataTable } from '../ui/components/DataTable'\nimport { toDisplayRows } from '../ui/rows/toDisplayRows'\nimport { CountryFlagImg } from '../view/CountryFlag'\nimport { countryFlagSrc, countryFlagsMount } from '../view/flagSrc'\nimport { renderRowFlag } from '../view/hostCountryFlag'\nimport { DIMENSION_LABELS } from '../view/labels'\nimport { type BreakdownSpec, type BreakdownWidgetData, breakdownSpecBySlug } from './breakdownTypes'\nimport { widgetFilters } from './filterField'\nimport { widgetNotice } from './notices'\nimport { readForWidgetBreakdown } from './readForWidgetBreakdown'\nimport { emptyGoalsKey } from './stateKeys'\nimport { type AnalyticsWidgetProps, viewTabForDimension, widgetLink } from './viewLink'\nimport { WidgetBody, WidgetCard, WidgetEmpty, WidgetNote } from './WidgetCard'\nimport { WidgetFrame, WidgetStateCard } from './WidgetFrame'\nimport { storedWidgetData } from './widgetData'\nimport { resolveWidgetWindow, widgetCaption, windowReadArgs } from './widgetWindow'\n\nconst CLASS_NAME = 'analytics-breakdown-widget'\n\nconst DEFAULT_LIMIT = 5\n\n/** A list fills its row limit more often than not; past this many rows the guess stops paying. */\nconst MAX_SHIMMER_ROWS = 10\n\nconst dataOf = (props: AnalyticsWidgetProps): BreakdownWidgetData => {\n\tconst { limit, ...stored } = storedWidgetData(props.widgetData)\n\treturn { ...stored, ...(typeof limit === 'number' ? { limit } : {}) }\n}\n\nconst titleOf = (\n\tdata: BreakdownWidgetData,\n\tspec: BreakdownSpec | undefined,\n\tt: Translate\n): string => data.title?.trim() || (spec ? t(spec.label) : '')\n\nconst shimmerRows = (limit: number | undefined): number =>\n\tMath.min(\n\t\tMath.max(Math.round(Number(limit ?? DEFAULT_LIMIT)) || DEFAULT_LIMIT, 1),\n\t\tMAX_SHIMMER_ROWS\n\t)\n\n/**\n * The card's frame paints at once; its list streams in when the read answers.\n *\n * @beta\n */\nexport default function AnalyticsBreakdownWidget(props: AnalyticsWidgetProps) {\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst data = dataOf(props)\n\treturn (\n\t\t<WidgetFrame\n\t\t\tclassName={CLASS_NAME}\n\t\t\tkind=\"breakdown\"\n\t\t\trows={shimmerRows(data.limit)}\n\t\t\ttitle={titleOf(data, breakdownSpecBySlug(props.widgetSlug), t)}\n\t\t\twidget={props}\n\t\t>\n\t\t\t<BreakdownWidgetBody {...props} />\n\t\t</WidgetFrame>\n\t)\n}\n\nasync function BreakdownWidgetBody(props: AnalyticsWidgetProps) {\n\tconst spec = breakdownSpecBySlug(props.widgetSlug)\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = intlLocale(props.req.i18n.language)\n\tconst data = dataOf(props)\n\tconst title = titleOf(data, spec, t)\n\tif (!spec) {\n\t\treturn (\n\t\t\t<WidgetCard className={CLASS_NAME} title={title}>\n\t\t\t\t<WidgetEmpty>{t(keys.stateUnavailable)}</WidgetEmpty>\n\t\t\t</WidgetCard>\n\t\t)\n\t}\n\t// The spec's own default, not pageviews: an events or goals widget saved before its\n\t// picker had a default would otherwise rank by a metric its dimension cannot answer.\n\tconst metric: MetricKey = data.metric ?? spec.preferredDefault ?? 'pageviews'\n\tconst window = await resolveWidgetWindow(props.req, data)\n\tif (window.unreadable) {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlocale={locale}\n\t\t\t\tstatus=\"invalid\"\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\tconst reporting = await requestCurrency(props.req)\n\tconst tab = viewTabForDimension(spec.dimension)\n\tconst filters = widgetFilters(data)\n\tconst now = currentDate()\n\tconst result = await readForWidgetBreakdown({\n\t\treq: props.req,\n\t\tmetric,\n\t\tdimension: spec.dimension,\n\t\tlimit: data.limit ?? DEFAULT_LIMIT,\n\t\tadapterId: data.dataSource,\n\t\tfallbackWhenUnreadable: true,\n\t\tnow,\n\t\tfilters,\n\t\t...windowReadArgs(window),\n\t})\n\tconst link = widgetLink(props, t, {\n\t\twindow,\n\t\tadapterId: result.adapterId,\n\t\t...(tab ? { tab, dim: spec.dimension } : {}),\n\t\tmetric,\n\t\tfilters,\n\t})\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlink={link}\n\t\t\t\tlocale={locale}\n\t\t\t\tretryAfterMs={result.retryAfterMs}\n\t\t\t\tstatus={result.status}\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\n\t// Built here rather than in the row map: the same config reads answer every row.\n\tconst flagsMount = countryFlagsMount(props.req.payload.config)\n\tconst flags = props.countryFlags ?? true\n\tconst flagFor = (code: string): ReactNode =>\n\t\ttypeof flags === 'object' ? (\n\t\t\trenderRowFlag(flags, props.req.payload.importMap, code)\n\t\t) : (\n\t\t\t<CountryFlagImg src={countryFlagSrc(flagsMount, code)} />\n\t\t)\n\tconst currency = result.currency ?? reporting\n\tconst caption = await widgetCaption(props.req, { window, filters, read: result, locale, t })\n\t// The goal-resolution states are the list's own empty label here, so the line below it\n\t// carries only what the read says about its numbers.\n\tconst notice = widgetNotice(\n\t\t{ ...result, goalsUnresolved: false, noGoals: false },\n\t\t{ t, locale, now, timeZone: result.timezone }\n\t)\n\tconst displayed = toDisplayRows(\n\t\tresult.rows.map((row) => ({ value: row.dimensionValue, metrics: { [metric]: row.value } })),\n\t\t{\n\t\t\tdimension: spec.dimension,\n\t\t\tprovider: result.provider ?? '',\n\t\t\tt,\n\t\t\tlocale,\n\t\t\t...(result.goals ? { goals: result.goals } : {}),\n\t\t\tcountryFlags: flags !== false,\n\t\t}\n\t)\n\tconst values = displayed.map((row) => readingOf(row.metrics, metric))\n\tconst shares = barShares(values)\n\treturn (\n\t\t<WidgetCard className={CLASS_NAME} caption={caption} link={link} title={title}>\n\t\t\t{displayed.length === 0 ? (\n\t\t\t\t<WidgetEmpty>\n\t\t\t\t\t{t(spec.dimension === 'goal' ? emptyGoalsKey(result) : keys.stateNoBreakdown)}\n\t\t\t\t</WidgetEmpty>\n\t\t\t) : (\n\t\t\t\t<WidgetBody>\n\t\t\t\t\t<DataTable\n\t\t\t\t\t\tbars=\"soft\"\n\t\t\t\t\t\tcolumns={[\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tkey: metric,\n\t\t\t\t\t\t\t\theader: t(metricLabelKey(metric, result.visitorsSummed)),\n\t\t\t\t\t\t\t\tclassName: 'analytics-bars__value',\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t]}\n\t\t\t\t\t\thead=\"hidden\"\n\t\t\t\t\t\tlabel={title}\n\t\t\t\t\t\tlabelHeader={t(DIMENSION_LABELS[spec.dimension])}\n\t\t\t\t\t\tnoValueLabel={t(keys.stateNoValue)}\n\t\t\t\t\t\trows={displayed.map((row, i) => ({\n\t\t\t\t\t\t\tkey: row.key,\n\t\t\t\t\t\t\tlabel: row.label,\n\t\t\t\t\t\t\tbar: shares[i] ?? 0,\n\t\t\t\t\t\t\t...(row.flag === null ? {} : { decoration: flagFor(row.flag) }),\n\t\t\t\t\t\t\tcells: [formatMetricValue(metric, values[i] ?? null, { locale, currency })],\n\t\t\t\t\t\t}))}\n\t\t\t\t\t/>\n\t\t\t\t</WidgetBody>\n\t\t\t)}\n\t\t\t<WidgetNote>{notice}</WidgetNote>\n\t\t</WidgetCard>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,MAAM,aAAa;AAEnB,MAAM,gBAAgB;;AAGtB,MAAM,mBAAmB;AAEzB,MAAM,UAAU,UAAqD;CACpE,MAAM,EAAE,OAAO,GAAG,WAAW,iBAAiB,MAAM,UAAU;CAC9D,OAAO;EAAE,GAAG;EAAQ,GAAI,OAAO,UAAU,WAAW,EAAE,MAAM,IAAI,CAAC;CAAG;AACrE;AAEA,MAAM,WACL,MACA,MACA,MACY,KAAK,OAAO,KAAK,MAAM,OAAO,EAAE,KAAK,KAAK,IAAI;AAE3D,MAAM,eAAe,UACpB,KAAK,IACJ,KAAK,IAAI,KAAK,MAAM,OAAO,SAAS,aAAa,CAAC,KAAK,eAAe,CAAC,GACvE,gBACD;;;;;;AAOD,SAAwB,yBAAyB,OAA6B;CAC7E,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,MAAM,OAAO,OAAO,KAAK;CACzB,OACC,oBAAC,aAAD;EACC,WAAW;EACX,MAAK;EACL,MAAM,YAAY,KAAK,KAAK;EAC5B,OAAO,QAAQ,MAAM,oBAAoB,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E,iBAAiB,KAAK,UAAU;GAC/C,cAAc,EAAE,KAAK,YAAY;GACjC,MAAM,UAAU,KAAK,KAAK,OAAO;IAChC,KAAK,IAAI;IACT,OAAO,IAAI;IACX,KAAK,OAAO,MAAM;IAClB,GAAI,IAAI,SAAS,OAAO,CAAC,IAAI,EAAE,YAAY,QAAQ,IAAI,IAAI,EAAE;IAC7D,OAAO,CAAC,kBAAkB,QAAQ,OAAO,MAAM,MAAM;KAAE;KAAQ;IAAS,CAAC,CAAC;GAC3E,EAAE;EACF,CAAA,EACU,CAAA,GAEb,oBAAC,YAAD,EAAA,UAAa,OAAmB,CAAA,CACrB;;AAEd"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsGoalsWidget.js","names":[],"sources":["../../src/widgets/AnalyticsGoalsWidget.tsx"],"sourcesContent":["import { currentDate } from '../core/clock'\nimport { formatMetricValue, formatRate } from '../fields/format'\nimport { requestCurrency } from '../plugin/runtime'\nimport { computeDelta } from '../timeframe/comparison'\nimport { intlLocale } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport { METRIC_KEYS } from '../translations/metricKeys'\nimport { asTranslate, type Translate } from '../translations/server'\nimport { DataTable } from '../ui/components/DataTable'\nimport { ComparisonDelta } from './ComparisonDelta'\nimport { widgetNotice } from './notices'\nimport { readForWidgetGoals } from './readForWidgetGoals'\nimport { emptyGoalsKey } from './stateKeys'\nimport { type GoalsWidgetData, resolveGoalRowLimit } from './types'\nimport { type AnalyticsWidgetProps, widgetLink } from './viewLink'\nimport { WidgetBody, WidgetCard, WidgetEmpty, WidgetNote } from './WidgetCard'\nimport { WidgetFrame, WidgetStateCard } from './WidgetFrame'\nimport { storedWidgetData } from './widgetData'\nimport { resolveWidgetWindow, windowCaption, windowReadArgs } from './widgetWindow'\n\nconst CLASS_NAME = 'analytics-goals-widget'\n\n/** A scope configures a handful of goals far more often than its row limit's worth. */\nconst SHIMMER_ROWS = 5\n\nconst dataOf = (props: AnalyticsWidgetProps): GoalsWidgetData => storedWidgetData(props.widgetData)\n\nconst titleOf = (data: GoalsWidgetData, t: Translate): string =>\n\tdata.title?.trim() || t(keys.widgetGoals)\n\n/**\n * Conversions per goal over the widget's window, with delta, conversion rate and revenue.\n * Columns the source cannot fill are left out rather than rendered empty.\n *\n * @beta\n */\nexport default function AnalyticsGoalsWidget(props: AnalyticsWidgetProps) {\n\tconst t = asTranslate(props.req.i18n.t)\n\treturn (\n\t\t<WidgetFrame\n\t\t\tclassName={CLASS_NAME}\n\t\t\tkind=\"goals\"\n\t\t\trows={SHIMMER_ROWS}\n\t\t\ttitle={titleOf(dataOf(props), t)}\n\t\t\twidget={props}\n\t\t>\n\t\t\t<GoalsWidgetBody {...props} />\n\t\t</WidgetFrame>\n\t)\n}\n\nasync function GoalsWidgetBody(props: AnalyticsWidgetProps) {\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = intlLocale(props.req.i18n.language)\n\tconst data = dataOf(props)\n\tconst title = titleOf(data, t)\n\tconst window = await resolveWidgetWindow(props.req, data)\n\tif (window.unreadable) {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlocale={locale}\n\t\t\t\tstatus=\"invalid\"\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\tconst reporting = await requestCurrency(props.req)\n\tconst compare = data.compare === true\n\tconst now = currentDate()\n\tconst result = await readForWidgetGoals({\n\t\treq: props.req,\n\t\tlimit: resolveGoalRowLimit(data.limit),\n\t\tcompare,\n\t\tadapterId: data.dataSource,\n\t\tnow,\n\t\t...windowReadArgs(window),\n\t})\n\t// The metric rides along so the view's goals tab opens ranked by conversions, not its default.\n\tconst link = widgetLink(props, t, {\n\t\twindow,\n\t\tadapterId: result.adapterId,\n\t\tmetric: 'conversions',\n\t\tcompare,\n\t\ttab: 'goals',\n\t})\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlink={link}\n\t\t\t\tlocale={locale}\n\t\t\t\tretryAfterMs={result.retryAfterMs}\n\t\t\t\tstatus={result.status}\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\n\t// The goal-resolution states are the table's own empty states here, so the line below it\n\t// carries only what the read says about its numbers.\n\tconst notice = widgetNotice(\n\t\t{ ...result, goalsUnresolved: false, noGoals: false },\n\t\t{ t, locale, now, timeZone: result.timezone }\n\t)\n\tconst showsRate = result.rows.some((row) => row.rate !== undefined)\n\tconst showsRevenue = result.rows.some((row) => row.revenue !== undefined)\n\tconst showsDelta = result.rows.some(\n\t\t(row) => computeDelta(row.conversions, row.previousConversions) !== null\n\t)\n\tconst currency = result.currency ?? reporting\n\treturn (\n\t\t<WidgetCard\n\t\t\tclassName={CLASS_NAME}\n\t\t\tcaption={windowCaption(window, locale, t)}\n\t\t\tlink={link}\n\t\t\ttitle={title}\n\t\t>\n\t\t\t{/* Rows counted against goals the source could not resolve mean nothing, so none show. */}\n\t\t\t{result.goalsUnresolved || result.rows.length === 0 ? (\n\t\t\t\t<WidgetEmpty>{t(emptyGoalsKey(result))}</WidgetEmpty>\n\t\t\t) : (\n\t\t\t\t<WidgetBody>\n\t\t\t\t\t<DataTable\n\t\t\t\t\t\tclassName=\"analytics-goals-widget__table\"\n\t\t\t\t\t\tcolumns={[\n\t\t\t\t\t\t\t{ key: 'conversions', header: t(METRIC_KEYS.conversions) },\n\t\t\t\t\t\t\t...(showsRevenue ? [{ key: 'revenue', header: t(METRIC_KEYS.revenue) }] : []),\n\t\t\t\t\t\t\t...(showsRate ? [{ key: 'rate', header: t(keys.viewConversionRate) }] : []),\n\t\t\t\t\t\t]}\n\t\t\t\t\t\tlabel={title}\n\t\t\t\t\t\tlabelHeader={t(keys.fieldGoalLabel)}\n\t\t\t\t\t\tnoValueLabel={t(keys.stateNoValue)}\n\t\t\t\t\t\trows={result.rows.map((row) => ({\n\t\t\t\t\t\t\tkey: row.slug,\n\t\t\t\t\t\t\tlabel: row.name,\n\t\t\t\t\t\t\tcells: [\n\t\t\t\t\t\t\t\t<span className=\"analytics-table__value\" key=\"conversions\">\n\t\t\t\t\t\t\t\t\t{formatMetricValue('conversions', row.conversions, locale)}\n\t\t\t\t\t\t\t\t\t<ComparisonDelta\n\t\t\t\t\t\t\t\t\t\tcaption={false}\n\t\t\t\t\t\t\t\t\t\tcurrent={row.conversions}\n\t\t\t\t\t\t\t\t\t\tprevious={row.previousConversions}\n\t\t\t\t\t\t\t\t\t\tmetric=\"conversions\"\n\t\t\t\t\t\t\t\t\t\tlocale={locale}\n\t\t\t\t\t\t\t\t\t\tt={t}\n\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t</span>,\n\t\t\t\t\t\t\t\t...(showsRevenue\n\t\t\t\t\t\t\t\t\t? [formatMetricValue('revenue', row.revenue, { locale, currency })]\n\t\t\t\t\t\t\t\t\t: []),\n\t\t\t\t\t\t\t\t...(showsRate ? [formatRate(row.rate, locale)] : []),\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t}))}\n\t\t\t\t\t/>\n\t\t\t\t\t{showsDelta ? (\n\t\t\t\t\t\t<span className=\"analytics-delta__caption\">{t(keys.comparisonVsPrevious)}</span>\n\t\t\t\t\t) : null}\n\t\t\t\t</WidgetBody>\n\t\t\t)}\n\t\t\t<WidgetNote>{notice}</WidgetNote>\n\t\t</WidgetCard>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoBA,MAAM,aAAa;;AAGnB,MAAM,eAAe;AAErB,MAAM,UAAU,UAAiD,iBAAiB,MAAM,UAAU;AAElG,MAAM,WAAW,MAAuB,MACvC,KAAK,OAAO,KAAK,KAAK,EAAE,KAAK,WAAW;;;;;;;AAQzC,SAAwB,qBAAqB,OAA6B;CACzE,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,OACC,oBAAC,aAA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,IACO,EAAA,CAAA,GAEb,oBAAC,YAAD,EAAA,UAAa,OAAmB,CAAA,CACrB;;AAEd"}
|
|
1
|
+
{"version":3,"file":"AnalyticsGoalsWidget.js","names":[],"sources":["../../src/widgets/AnalyticsGoalsWidget.tsx"],"sourcesContent":["import { currentDate } from '../core/clock'\nimport { formatMetricValue, formatRate } from '../fields/format'\nimport { requestCurrency } from '../plugin/runtime'\nimport { computeDelta } from '../timeframe/comparison'\nimport { intlLocale } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport { METRIC_KEYS } from '../translations/metricKeys'\nimport { asTranslate, type Translate } from '../translations/server'\nimport { DataTable } from '../ui/components/DataTable'\nimport { ComparisonDelta } from './ComparisonDelta'\nimport { widgetNotice } from './notices'\nimport { readForWidgetGoals } from './readForWidgetGoals'\nimport { emptyGoalsKey } from './stateKeys'\nimport { type GoalsWidgetData, resolveGoalRowLimit } from './types'\nimport { type AnalyticsWidgetProps, widgetLink } from './viewLink'\nimport { WidgetBody, WidgetCard, WidgetEmpty, WidgetNote } from './WidgetCard'\nimport { WidgetFrame, WidgetStateCard } from './WidgetFrame'\nimport { storedWidgetData } from './widgetData'\nimport { resolveWidgetWindow, windowCaption, windowReadArgs } from './widgetWindow'\n\nconst CLASS_NAME = 'analytics-goals-widget'\n\n/** A scope configures a handful of goals far more often than its row limit's worth. */\nconst SHIMMER_ROWS = 5\n\nconst dataOf = (props: AnalyticsWidgetProps): GoalsWidgetData => storedWidgetData(props.widgetData)\n\nconst titleOf = (data: GoalsWidgetData, t: Translate): string =>\n\tdata.title?.trim() || t(keys.widgetGoals)\n\n/**\n * Conversions per goal over the widget's window, with delta, conversion rate and revenue.\n * Columns the source cannot fill are left out rather than rendered empty.\n *\n * @beta\n */\nexport default function AnalyticsGoalsWidget(props: AnalyticsWidgetProps) {\n\tconst t = asTranslate(props.req.i18n.t)\n\treturn (\n\t\t<WidgetFrame\n\t\t\tclassName={CLASS_NAME}\n\t\t\tkind=\"goals\"\n\t\t\trows={SHIMMER_ROWS}\n\t\t\ttitle={titleOf(dataOf(props), t)}\n\t\t\twidget={props}\n\t\t>\n\t\t\t<GoalsWidgetBody {...props} />\n\t\t</WidgetFrame>\n\t)\n}\n\nasync function GoalsWidgetBody(props: AnalyticsWidgetProps) {\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = intlLocale(props.req.i18n.language)\n\tconst data = dataOf(props)\n\tconst title = titleOf(data, t)\n\tconst window = await resolveWidgetWindow(props.req, data)\n\tif (window.unreadable) {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlocale={locale}\n\t\t\t\tstatus=\"invalid\"\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\tconst reporting = await requestCurrency(props.req)\n\tconst compare = data.compare === true\n\tconst now = currentDate()\n\tconst result = await readForWidgetGoals({\n\t\treq: props.req,\n\t\tlimit: resolveGoalRowLimit(data.limit),\n\t\tcompare,\n\t\tadapterId: data.dataSource,\n\t\tfallbackWhenUnreadable: true,\n\t\tnow,\n\t\t...windowReadArgs(window),\n\t})\n\t// The metric rides along so the view's goals tab opens ranked by conversions, not its default.\n\tconst link = widgetLink(props, t, {\n\t\twindow,\n\t\tadapterId: result.adapterId,\n\t\tmetric: 'conversions',\n\t\tcompare,\n\t\ttab: 'goals',\n\t})\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlink={link}\n\t\t\t\tlocale={locale}\n\t\t\t\tretryAfterMs={result.retryAfterMs}\n\t\t\t\tstatus={result.status}\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\n\t// The goal-resolution states are the table's own empty states here, so the line below it\n\t// carries only what the read says about its numbers.\n\tconst notice = widgetNotice(\n\t\t{ ...result, goalsUnresolved: false, noGoals: false },\n\t\t{ t, locale, now, timeZone: result.timezone }\n\t)\n\tconst showsRate = result.rows.some((row) => row.rate !== undefined)\n\tconst showsRevenue = result.rows.some((row) => row.revenue !== undefined)\n\tconst showsDelta = result.rows.some(\n\t\t(row) => computeDelta(row.conversions, row.previousConversions) !== null\n\t)\n\tconst currency = result.currency ?? reporting\n\treturn (\n\t\t<WidgetCard\n\t\t\tclassName={CLASS_NAME}\n\t\t\tcaption={windowCaption(window, locale, t)}\n\t\t\tlink={link}\n\t\t\ttitle={title}\n\t\t>\n\t\t\t{/* Rows counted against goals the source could not resolve mean nothing, so none show. */}\n\t\t\t{result.goalsUnresolved || result.rows.length === 0 ? (\n\t\t\t\t<WidgetEmpty>{t(emptyGoalsKey(result))}</WidgetEmpty>\n\t\t\t) : (\n\t\t\t\t<WidgetBody>\n\t\t\t\t\t<DataTable\n\t\t\t\t\t\tclassName=\"analytics-goals-widget__table\"\n\t\t\t\t\t\tcolumns={[\n\t\t\t\t\t\t\t{ key: 'conversions', header: t(METRIC_KEYS.conversions) },\n\t\t\t\t\t\t\t...(showsRevenue ? [{ key: 'revenue', header: t(METRIC_KEYS.revenue) }] : []),\n\t\t\t\t\t\t\t...(showsRate ? [{ key: 'rate', header: t(keys.viewConversionRate) }] : []),\n\t\t\t\t\t\t]}\n\t\t\t\t\t\tlabel={title}\n\t\t\t\t\t\tlabelHeader={t(keys.fieldGoalLabel)}\n\t\t\t\t\t\tnoValueLabel={t(keys.stateNoValue)}\n\t\t\t\t\t\trows={result.rows.map((row) => ({\n\t\t\t\t\t\t\tkey: row.slug,\n\t\t\t\t\t\t\tlabel: row.name,\n\t\t\t\t\t\t\tcells: [\n\t\t\t\t\t\t\t\t<span className=\"analytics-table__value\" key=\"conversions\">\n\t\t\t\t\t\t\t\t\t{formatMetricValue('conversions', row.conversions, locale)}\n\t\t\t\t\t\t\t\t\t<ComparisonDelta\n\t\t\t\t\t\t\t\t\t\tcaption={false}\n\t\t\t\t\t\t\t\t\t\tcurrent={row.conversions}\n\t\t\t\t\t\t\t\t\t\tprevious={row.previousConversions}\n\t\t\t\t\t\t\t\t\t\tmetric=\"conversions\"\n\t\t\t\t\t\t\t\t\t\tlocale={locale}\n\t\t\t\t\t\t\t\t\t\tt={t}\n\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t</span>,\n\t\t\t\t\t\t\t\t...(showsRevenue\n\t\t\t\t\t\t\t\t\t? [formatMetricValue('revenue', row.revenue, { locale, currency })]\n\t\t\t\t\t\t\t\t\t: []),\n\t\t\t\t\t\t\t\t...(showsRate ? [formatRate(row.rate, locale)] : []),\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t}))}\n\t\t\t\t\t/>\n\t\t\t\t\t{showsDelta ? (\n\t\t\t\t\t\t<span className=\"analytics-delta__caption\">{t(keys.comparisonVsPrevious)}</span>\n\t\t\t\t\t) : null}\n\t\t\t\t</WidgetBody>\n\t\t\t)}\n\t\t\t<WidgetNote>{notice}</WidgetNote>\n\t\t</WidgetCard>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoBA,MAAM,aAAa;;AAGnB,MAAM,eAAe;AAErB,MAAM,UAAU,UAAiD,iBAAiB,MAAM,UAAU;AAElG,MAAM,WAAW,MAAuB,MACvC,KAAK,OAAO,KAAK,KAAK,EAAE,KAAK,WAAW;;;;;;;AAQzC,SAAwB,qBAAqB,OAA6B;CACzE,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,OACC,oBAAC,aAAD;EACC,WAAW;EACX,MAAK;EACL,MAAM;EACN,OAAO,QAAQ,OAAO,KAAK,GAAG,CAAC;EAC/B,QAAQ;YAER,oBAAC,iBAAD,EAAiB,GAAI,MAAQ,CAAA;CACjB,CAAA;AAEf;AAEA,eAAe,gBAAgB,OAA6B;CAC3D,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,MAAM,SAAS,WAAW,MAAM,IAAI,KAAK,QAAQ;CACjD,MAAM,OAAO,OAAO,KAAK;CACzB,MAAM,Q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AC,YAAD,EAAA,UAAA,CACC,oBAAC,WAAD;GACC,WAAU;GACV,SAAS;IACR;KAAE,KAAK;KAAe,QAAQ,EAAE,YAAY,WAAW;IAAE;IACzD,GAAI,eAAe,CAAC;KAAE,KAAK;KAAW,QAAQ,EAAE,YAAY,OAAO;IAAE,CAAC,IAAI,CAAC;IAC3E,GAAI,YAAY,CAAC;KAAE,KAAK;KAAQ,QAAQ,EAAE,KAAK,kBAAkB;IAAE,CAAC,IAAI,CAAC;GAC1E;GACA,OAAO;GACP,aAAa,EAAE,KAAK,cAAc;GAClC,cAAc,EAAE,KAAK,YAAY;GACjC,MAAM,OAAO,KAAK,KAAK,SAAS;IAC/B,KAAK,IAAI;IACT,OAAO,IAAI;IACX,OAAO;KACN,qBAAC,QAAD;MAAM,WAAU;gBAAhB,CACE,kBAAkB,eAAe,IAAI,aAAa,MAAM,GACzD,oBAAC,iBAAD;OACC,SAAS;OACT,SAAS,IAAI;OACb,UAAU,IAAI;OACd,QAAO;OACC;OACL;MACH,CAAA,CACI;QAVuC,aAUvC;KACN,GAAI,eACD,CAAC,kBAAkB,WAAW,IAAI,SAAS;MAAE;MAAQ;KAAS,CAAC,CAAC,IAChE,CAAC;KACJ,GAAI,YAAY,CAAC,WAAW,IAAI,MAAM,MAAM,CAAC,IAAI,CAAC;IACnD;GACD,EAAE;EACF,CAAA,GACA,aACA,oBAAC,QAAD;GAAM,WAAU;aAA4B,EAAE,KAAK,oBAAoB;EAAQ,CAAA,IAC5E,IACO,EAAA,CAAA,GAEb,oBAAC,YAAD,EAAA,UAAa,OAAmB,CAAA,CACrB;;AAEd"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsMetricWidget.js","names":[],"sources":["../../src/widgets/AnalyticsMetricWidget.tsx"],"sourcesContent":["import { currentDate } from '../core/clock'\nimport { formatMetricValue } from '../fields/format'\nimport { revenueCurrencyOf } from '../goals/currency'\nimport { requestCurrency } from '../plugin/runtime'\nimport { intlLocale } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport { asTranslate } from '../translations/server'\nimport { ComparisonDelta } from './ComparisonDelta'\nimport { widgetFilters } from './filterField'\nimport { widgetNotice } from './notices'\nimport { readForWidget } from './readForWidget'\nimport { metricWidgetTitle, placeholderTitle } from './titleForecast'\nimport type { MetricWidgetData } from './types'\nimport { type AnalyticsWidgetProps, widgetLink } from './viewLink'\nimport { WidgetCard, WidgetNote, WidgetValue } from './WidgetCard'\nimport { WidgetFrame, WidgetStateCard } from './WidgetFrame'\nimport { storedWidgetData } from './widgetData'\nimport { resolveWidgetWindow, widgetCaption, windowReadArgs } from './widgetWindow'\n\nconst CLASS_NAME = 'analytics-metric-widget'\n\nconst dataOf = (props: AnalyticsWidgetProps): MetricWidgetData => storedWidgetData(props.widgetData)\n\n/**\n * The card's frame paints at once; its number streams in when the read answers.\n *\n * @beta\n */\nexport default function AnalyticsMetricWidget(props: AnalyticsWidgetProps) {\n\treturn (\n\t\t<WidgetFrame\n\t\t\tclassName={CLASS_NAME}\n\t\t\tkind=\"metric\"\n\t\t\twidget={props}\n\t\t\t{...placeholderTitle(props.req, dataOf(props), { series: false })}\n\t\t>\n\t\t\t<MetricWidgetBody {...props} />\n\t\t</WidgetFrame>\n\t)\n}\n\nasync function MetricWidgetBody(props: AnalyticsWidgetProps) {\n\tconst data = dataOf(props)\n\tconst metric = data.metric ?? 'pageviews'\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = intlLocale(props.req.i18n.language)\n\tconst window = await resolveWidgetWindow(props.req, data)\n\tif (window.unreadable) {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlocale={locale}\n\t\t\t\tstatus=\"invalid\"\n\t\t\t\tt={t}\n\t\t\t\ttitle={metricWidgetTitle(data, t)}\n\t\t\t/>\n\t\t)\n\t}\n\tconst reporting = await requestCurrency(props.req)\n\tconst filters = widgetFilters(data)\n\tconst now = currentDate()\n\tconst result = await readForWidget({\n\t\treq: props.req,\n\t\tmetrics: [metric],\n\t\tadapterId: data.dataSource,\n\t\tnow,\n\t\tfilters,\n\t\t...windowReadArgs(window),\n\t})\n\tconst link = widgetLink(props, t, { window, adapterId: result.adapterId, metric, filters })\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlink={link}\n\t\t\t\tlocale={locale}\n\t\t\t\tretryAfterMs={result.retryAfterMs}\n\t\t\t\tstatus={result.status}\n\t\t\t\tt={t}\n\t\t\t\ttitle={metricWidgetTitle(data, t)}\n\t\t\t/>\n\t\t)\n\t}\n\n\tconst caption = await widgetCaption(props.req, { window, filters, read: result, locale, t })\n\tconst value = result.metrics[metric]\n\tconst currency = result.currency ?? revenueCurrencyOf(filters, reporting)\n\treturn (\n\t\t<WidgetCard\n\t\t\tclassName={CLASS_NAME}\n\t\t\tcaption={caption}\n\t\t\tlink={link}\n\t\t\ttitle={metricWidgetTitle(data, t, result.visitorsSummed)}\n\t\t>\n\t\t\t<WidgetValue\n\t\t\t\tnoValueLabel={t(keys.stateNoValue)}\n\t\t\t\tvalue={formatMetricValue(metric, value, { locale, currency })}\n\t\t\t>\n\t\t\t\t{result.comparisonRange ? (\n\t\t\t\t\t<ComparisonDelta\n\t\t\t\t\t\tcurrent={value}\n\t\t\t\t\t\tprevious={result.previousMetrics?.[metric]}\n\t\t\t\t\t\tmetric={metric}\n\t\t\t\t\t\tlocale={locale}\n\t\t\t\t\t\tt={t}\n\t\t\t\t\t/>\n\t\t\t\t) : null}\n\t\t\t</WidgetValue>\n\t\t\t<WidgetNote>{widgetNotice(result, { t, locale, now, timeZone: result.timezone })}</WidgetNote>\n\t\t</WidgetCard>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,MAAM,aAAa;AAEnB,MAAM,UAAU,UAAkD,iBAAiB,MAAM,UAAU;;;;;;AAOnG,SAAwB,sBAAsB,OAA6B;CAC1E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C,WAAW;EACF;EACH;EACN,OAAO,kBAAkB,MAAM,GAAG,OAAO,cAAc;YAJxD,CAMC,oBAAC,aAAD;GACC,cAAc,EAAE,KAAK,YAAY;GACjC,OAAO,kBAAkB,QAAQ,OAAO;IAAE;IAAQ;GAAS,CAAC;aAE3D,OAAO,kBACP,oBAAC,iBAAD;IACC,SAAS;IACT,UAAU,OAAO,kBAAkB;IAC3B;IACA;IACL;GACH,CAAA,IACE;EACQ,CAAA,GACb,oBAAC,YAAD,EAAA,UAAa,aAAa,QAAQ;GAAE;GAAG;GAAQ;GAAK,UAAU,OAAO;EAAS,CAAC,EAAc,CAAA,CAClF;;AAEd"}
|
|
1
|
+
{"version":3,"file":"AnalyticsMetricWidget.js","names":[],"sources":["../../src/widgets/AnalyticsMetricWidget.tsx"],"sourcesContent":["import { currentDate } from '../core/clock'\nimport { formatMetricValue } from '../fields/format'\nimport { revenueCurrencyOf } from '../goals/currency'\nimport { requestCurrency } from '../plugin/runtime'\nimport { intlLocale } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport { asTranslate } from '../translations/server'\nimport { ComparisonDelta } from './ComparisonDelta'\nimport { widgetFilters } from './filterField'\nimport { widgetNotice } from './notices'\nimport { readForWidget } from './readForWidget'\nimport { metricWidgetTitle, placeholderTitle } from './titleForecast'\nimport type { MetricWidgetData } from './types'\nimport { type AnalyticsWidgetProps, widgetLink } from './viewLink'\nimport { WidgetCard, WidgetNote, WidgetValue } from './WidgetCard'\nimport { WidgetFrame, WidgetStateCard } from './WidgetFrame'\nimport { storedWidgetData } from './widgetData'\nimport { resolveWidgetWindow, widgetCaption, windowReadArgs } from './widgetWindow'\n\nconst CLASS_NAME = 'analytics-metric-widget'\n\nconst dataOf = (props: AnalyticsWidgetProps): MetricWidgetData => storedWidgetData(props.widgetData)\n\n/**\n * The card's frame paints at once; its number streams in when the read answers.\n *\n * @beta\n */\nexport default function AnalyticsMetricWidget(props: AnalyticsWidgetProps) {\n\treturn (\n\t\t<WidgetFrame\n\t\t\tclassName={CLASS_NAME}\n\t\t\tkind=\"metric\"\n\t\t\twidget={props}\n\t\t\t{...placeholderTitle(props.req, dataOf(props), { series: false })}\n\t\t>\n\t\t\t<MetricWidgetBody {...props} />\n\t\t</WidgetFrame>\n\t)\n}\n\nasync function MetricWidgetBody(props: AnalyticsWidgetProps) {\n\tconst data = dataOf(props)\n\tconst metric = data.metric ?? 'pageviews'\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = intlLocale(props.req.i18n.language)\n\tconst window = await resolveWidgetWindow(props.req, data)\n\tif (window.unreadable) {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlocale={locale}\n\t\t\t\tstatus=\"invalid\"\n\t\t\t\tt={t}\n\t\t\t\ttitle={metricWidgetTitle(data, t)}\n\t\t\t/>\n\t\t)\n\t}\n\tconst reporting = await requestCurrency(props.req)\n\tconst filters = widgetFilters(data)\n\tconst now = currentDate()\n\tconst result = await readForWidget({\n\t\treq: props.req,\n\t\tmetrics: [metric],\n\t\tadapterId: data.dataSource,\n\t\tfallbackWhenUnreadable: true,\n\t\tnow,\n\t\tfilters,\n\t\t...windowReadArgs(window),\n\t})\n\tconst link = widgetLink(props, t, { window, adapterId: result.adapterId, metric, filters })\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlink={link}\n\t\t\t\tlocale={locale}\n\t\t\t\tretryAfterMs={result.retryAfterMs}\n\t\t\t\tstatus={result.status}\n\t\t\t\tt={t}\n\t\t\t\ttitle={metricWidgetTitle(data, t)}\n\t\t\t/>\n\t\t)\n\t}\n\n\tconst caption = await widgetCaption(props.req, { window, filters, read: result, locale, t })\n\tconst value = result.metrics[metric]\n\tconst currency = result.currency ?? revenueCurrencyOf(filters, reporting)\n\treturn (\n\t\t<WidgetCard\n\t\t\tclassName={CLASS_NAME}\n\t\t\tcaption={caption}\n\t\t\tlink={link}\n\t\t\ttitle={metricWidgetTitle(data, t, result.visitorsSummed)}\n\t\t>\n\t\t\t<WidgetValue\n\t\t\t\tnoValueLabel={t(keys.stateNoValue)}\n\t\t\t\tvalue={formatMetricValue(metric, value, { locale, currency })}\n\t\t\t>\n\t\t\t\t{result.comparisonRange ? (\n\t\t\t\t\t<ComparisonDelta\n\t\t\t\t\t\tcurrent={value}\n\t\t\t\t\t\tprevious={result.previousMetrics?.[metric]}\n\t\t\t\t\t\tmetric={metric}\n\t\t\t\t\t\tlocale={locale}\n\t\t\t\t\t\tt={t}\n\t\t\t\t\t/>\n\t\t\t\t) : null}\n\t\t\t</WidgetValue>\n\t\t\t<WidgetNote>{widgetNotice(result, { t, locale, now, timeZone: result.timezone })}</WidgetNote>\n\t\t</WidgetCard>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,MAAM,aAAa;AAEnB,MAAM,UAAU,UAAkD,iBAAiB,MAAM,UAAU;;;;;;AAOnG,SAAwB,sBAAsB,OAA6B;CAC1E,OACC,oBAAC,aAAD;EACC,WAAW;EACX,MAAK;EACL,QAAQ;EACR,GAAI,iBAAiB,MAAM,KAAK,OAAO,KAAK,GAAG,EAAE,QAAQ,MAAM,CAAC;YAEhE,oBAAC,kBAAD,EAAkB,GAAI,MAAQ,CAAA;CAClB,CAAA;AAEf;AAEA,eAAe,iBAAiB,OAA6B;CAC5D,MAAM,OAAO,OAAO,KAAK;CACzB,MAAM,SAAS,KAAK,UAAU;CAC9B,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,MAAM,SAAS,WAAW,MAAM,IAAI,KAAK,QAAQ;CACjD,MAAM,SAAS,MAAM,oBAAoB,MAAM,KAAK,IAAI;CACxD,IAAI,OAAO,YACV,OACC,oBAAC,iBAAD;EACC,WAAW;EACH;EACR,QAAO;EACJ;EACH,OAAO,kBAAkB,MAAM,CAAC;CAChC,CAAA;CAGH,MAAM,YAAY,MAAM,gBAAgB,MAAM,GAAG;CACjD,MAAM,UAAU,cAAc,IAAI;CAClC,MAAM,MAAM,YAAY;CACxB,MAAM,SAAS,MAAM,cAAc;EAClC,KAAK,MAAM;EACX,SAAS,CAAC,MAAM;EAChB,WAAW,KAAK;EAChB,wBAAwB;EACxB;EACA;EACA,GAAG,eAAe,MAAM;CACzB,CAAC;CACD,MAAM,OAAO,WAAW,OAAO,GAAG;EAAE;EAAQ,WAAW,OAAO;EAAW;EAAQ;CAAQ,CAAC;CAC1F,IAAI,OAAO,WAAW,MACrB,OACC,oBAAC,iBAAD;EACC,WAAW;EACL;EACE;EACR,cAAc,OAAO;EACrB,QAAQ,OAAO;EACZ;EACH,OAAO,kBAAkB,MAAM,CAAC;CAChC,CAAA;CAIH,MAAM,UAAU,MAAM,cAAc,MAAM,KAAK;EAAE;EAAQ;EAAS,MAAM;EAAQ;EAAQ;CAAE,CAAC;CAC3F,MAAM,QAAQ,OAAO,QAAQ;CAC7B,MAAM,WAAW,OAAO,YAAY,kBAAkB,SAAS,SAAS;CACxE,OACC,qBAAC,YAAD;EACC,WAAW;EACF;EACH;EACN,OAAO,kBAAkB,MAAM,GAAG,OAAO,cAAc;YAJxD,CAMC,oBAAC,aAAD;GACC,cAAc,EAAE,KAAK,YAAY;GACjC,OAAO,kBAAkB,QAAQ,OAAO;IAAE;IAAQ;GAAS,CAAC;aAE3D,OAAO,kBACP,oBAAC,iBAAD;IACC,SAAS;IACT,UAAU,OAAO,kBAAkB;IAC3B;IACA;IACL;GACH,CAAA,IACE;EACQ,CAAA,GACb,oBAAC,YAAD,EAAA,UAAa,aAAa,QAAQ;GAAE;GAAG;GAAQ;GAAK,UAAU,OAAO;EAAS,CAAC,EAAc,CAAA,CAClF;;AAEd"}
|
|
@@ -6,13 +6,14 @@ import { asTranslate } from "../translations/server.js";
|
|
|
6
6
|
import { intlLocale } from "../translations/intl.js";
|
|
7
7
|
import { readForWidgetRealtime } from "./readForWidgetRealtime.js";
|
|
8
8
|
import { formatMinutes } from "../fields/format.js";
|
|
9
|
+
import { widgetNotice } from "./notices.js";
|
|
9
10
|
import { buildRealtimeEndpoint } from "./realtimePoll.js";
|
|
10
|
-
import { WidgetCard } from "./WidgetCard.js";
|
|
11
|
+
import { WidgetCard, WidgetNote } from "./WidgetCard.js";
|
|
11
12
|
import { RealtimeCounter } from "./RealtimeCounter.js";
|
|
12
13
|
import { widgetLink } from "./viewLink.js";
|
|
13
14
|
import { WidgetFrame, WidgetStateCard } from "./WidgetFrame.js";
|
|
14
15
|
import { storedWidgetData } from "./widgetData.js";
|
|
15
|
-
import { jsx } from "react/jsx-runtime";
|
|
16
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
16
17
|
//#region src/widgets/AnalyticsRealtimeWidget.tsx
|
|
17
18
|
const CLASS_NAME = "analytics-realtime-widget";
|
|
18
19
|
const POLL_INTERVAL_MS = 15e3;
|
|
@@ -48,12 +49,14 @@ async function RealtimeWidgetBody(props) {
|
|
|
48
49
|
const locale = intlLocale(props.req.i18n.language);
|
|
49
50
|
const title = titleOf(data, t);
|
|
50
51
|
const timezone = await requestTimezone(props.req);
|
|
52
|
+
const now = currentDate();
|
|
51
53
|
const result = await readForWidgetRealtime({
|
|
52
54
|
req: props.req,
|
|
53
55
|
metric,
|
|
54
56
|
windowMinutes,
|
|
55
57
|
adapterId: data.dataSource,
|
|
56
|
-
|
|
58
|
+
fallbackWhenUnreadable: true,
|
|
59
|
+
now
|
|
57
60
|
});
|
|
58
61
|
const link = widgetLink(props, t, { adapterId: result.adapterId });
|
|
59
62
|
if (result.status !== "ok") return /* @__PURE__ */ jsx(WidgetStateCard, {
|
|
@@ -66,17 +69,17 @@ async function RealtimeWidgetBody(props) {
|
|
|
66
69
|
title
|
|
67
70
|
});
|
|
68
71
|
const duration = formatMinutes(windowMinutes, locale);
|
|
69
|
-
return /* @__PURE__ */
|
|
72
|
+
return /* @__PURE__ */ jsxs(WidgetCard, {
|
|
70
73
|
className: CLASS_NAME,
|
|
71
74
|
caption: t(keys.widgetRealtimeWindow, { duration }),
|
|
72
75
|
link,
|
|
73
76
|
title,
|
|
74
|
-
children: /* @__PURE__ */ jsx(RealtimeCounter, {
|
|
77
|
+
children: [/* @__PURE__ */ jsx(RealtimeCounter, {
|
|
75
78
|
endpoint: buildRealtimeEndpoint(props.req.payload.config),
|
|
76
79
|
intervalMs: POLL_INTERVAL_MS,
|
|
77
80
|
metric,
|
|
78
81
|
windowMinutes,
|
|
79
|
-
dataSource: data.dataSource,
|
|
82
|
+
dataSource: result.substitutedFrom === void 0 ? data.dataSource : result.adapterId,
|
|
80
83
|
initialActiveNow: result.activeNow,
|
|
81
84
|
initialSeries: result.series,
|
|
82
85
|
initialSampled: result.sampled,
|
|
@@ -87,7 +90,12 @@ async function RealtimeWidgetBody(props) {
|
|
|
87
90
|
periodLabel: t(keys.viewTrendPeriod),
|
|
88
91
|
sampledLabel: t(keys.stateSampled),
|
|
89
92
|
fill: true
|
|
90
|
-
})
|
|
93
|
+
}), /* @__PURE__ */ jsx(WidgetNote, { children: widgetNotice({ substitutedFrom: result.substitutedFrom }, {
|
|
94
|
+
t,
|
|
95
|
+
locale,
|
|
96
|
+
now,
|
|
97
|
+
timeZone: timezone
|
|
98
|
+
}) })]
|
|
91
99
|
});
|
|
92
100
|
}
|
|
93
101
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsRealtimeWidget.js","names":[],"sources":["../../src/widgets/AnalyticsRealtimeWidget.tsx"],"sourcesContent":["import { currentDate } from '../core/clock'\nimport { formatMinutes } from '../fields/format'\nimport { requestTimezone } from '../plugin/runtime'\nimport { intlLocale } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport { METRIC_KEYS } from '../translations/metricKeys'\nimport { asTranslate, type Translate } from '../translations/server'\nimport { RealtimeCounter } from './RealtimeCounter'\nimport { readForWidgetRealtime } from './readForWidgetRealtime'\nimport { buildRealtimeEndpoint } from './realtimePoll'\nimport { type AnalyticsWidgetProps, widgetLink } from './viewLink'\nimport { WidgetCard } from './WidgetCard'\nimport { WidgetFrame, WidgetStateCard } from './WidgetFrame'\nimport { storedWidgetData } from './widgetData'\n\nconst CLASS_NAME = 'analytics-realtime-widget'\n\nconst POLL_INTERVAL_MS = 15_000\n\nconst DEFAULT_WINDOW_MINUTES = 30\n\ninterface RealtimeWidgetData {\n\ttitle?: string\n\tmetric?: 'visitors' | 'pageviews'\n\twindowMinutes?: number | string\n\tdataSource?: string\n}\n\nconst dataOf = (props: AnalyticsWidgetProps): RealtimeWidgetData => {\n\tconst { metric, ...stored } = storedWidgetData(props.widgetData)\n\treturn { ...stored, ...(metric === 'visitors' || metric === 'pageviews' ? { metric } : {}) }\n}\n\nconst titleOf = (data: RealtimeWidgetData, t: Translate): string =>\n\tdata.title?.trim() || t(keys.widgetRealtimeLabel)\n\n/**\n * The card's frame paints at once; the live count streams in when the read answers.\n *\n * @beta\n */\nexport default function AnalyticsRealtimeWidget(props: AnalyticsWidgetProps) {\n\tconst t = asTranslate(props.req.i18n.t)\n\treturn (\n\t\t<WidgetFrame\n\t\t\tclassName={CLASS_NAME}\n\t\t\tkind=\"realtime\"\n\t\t\ttitle={titleOf(dataOf(props), t)}\n\t\t\twidget={props}\n\t\t>\n\t\t\t<RealtimeWidgetBody {...props} />\n\t\t</WidgetFrame>\n\t)\n}\n\nasync function RealtimeWidgetBody(props: AnalyticsWidgetProps) {\n\tconst data = dataOf(props)\n\tconst metric = data.metric ?? 'visitors'\n\tconst windowMinutes = Number(data.windowMinutes) || DEFAULT_WINDOW_MINUTES\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = intlLocale(props.req.i18n.language)\n\tconst title = titleOf(data, t)\n\t// The counter's minute labels are server-rendered and rehydrated, so both sides have to\n\t// read them in the install's reporting zone rather than in whatever zone they run in.\n\tconst timezone = await requestTimezone(props.req)\n\tconst result = await readForWidgetRealtime({\n\t\treq: props.req,\n\t\tmetric,\n\t\twindowMinutes,\n\t\tadapterId: data.dataSource,\n\t\
|
|
1
|
+
{"version":3,"file":"AnalyticsRealtimeWidget.js","names":[],"sources":["../../src/widgets/AnalyticsRealtimeWidget.tsx"],"sourcesContent":["import { currentDate } from '../core/clock'\nimport { formatMinutes } from '../fields/format'\nimport { requestTimezone } from '../plugin/runtime'\nimport { intlLocale } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport { METRIC_KEYS } from '../translations/metricKeys'\nimport { asTranslate, type Translate } from '../translations/server'\nimport { widgetNotice } from './notices'\nimport { RealtimeCounter } from './RealtimeCounter'\nimport { readForWidgetRealtime } from './readForWidgetRealtime'\nimport { buildRealtimeEndpoint } from './realtimePoll'\nimport { type AnalyticsWidgetProps, widgetLink } from './viewLink'\nimport { WidgetCard, WidgetNote } from './WidgetCard'\nimport { WidgetFrame, WidgetStateCard } from './WidgetFrame'\nimport { storedWidgetData } from './widgetData'\n\nconst CLASS_NAME = 'analytics-realtime-widget'\n\nconst POLL_INTERVAL_MS = 15_000\n\nconst DEFAULT_WINDOW_MINUTES = 30\n\ninterface RealtimeWidgetData {\n\ttitle?: string\n\tmetric?: 'visitors' | 'pageviews'\n\twindowMinutes?: number | string\n\tdataSource?: string\n}\n\nconst dataOf = (props: AnalyticsWidgetProps): RealtimeWidgetData => {\n\tconst { metric, ...stored } = storedWidgetData(props.widgetData)\n\treturn { ...stored, ...(metric === 'visitors' || metric === 'pageviews' ? { metric } : {}) }\n}\n\nconst titleOf = (data: RealtimeWidgetData, t: Translate): string =>\n\tdata.title?.trim() || t(keys.widgetRealtimeLabel)\n\n/**\n * The card's frame paints at once; the live count streams in when the read answers.\n *\n * @beta\n */\nexport default function AnalyticsRealtimeWidget(props: AnalyticsWidgetProps) {\n\tconst t = asTranslate(props.req.i18n.t)\n\treturn (\n\t\t<WidgetFrame\n\t\t\tclassName={CLASS_NAME}\n\t\t\tkind=\"realtime\"\n\t\t\ttitle={titleOf(dataOf(props), t)}\n\t\t\twidget={props}\n\t\t>\n\t\t\t<RealtimeWidgetBody {...props} />\n\t\t</WidgetFrame>\n\t)\n}\n\nasync function RealtimeWidgetBody(props: AnalyticsWidgetProps) {\n\tconst data = dataOf(props)\n\tconst metric = data.metric ?? 'visitors'\n\tconst windowMinutes = Number(data.windowMinutes) || DEFAULT_WINDOW_MINUTES\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = intlLocale(props.req.i18n.language)\n\tconst title = titleOf(data, t)\n\t// The counter's minute labels are server-rendered and rehydrated, so both sides have to\n\t// read them in the install's reporting zone rather than in whatever zone they run in.\n\tconst timezone = await requestTimezone(props.req)\n\tconst now = currentDate()\n\tconst result = await readForWidgetRealtime({\n\t\treq: props.req,\n\t\tmetric,\n\t\twindowMinutes,\n\t\tadapterId: data.dataSource,\n\t\tfallbackWhenUnreadable: true,\n\t\tnow,\n\t})\n\tconst link = widgetLink(props, t, { adapterId: result.adapterId })\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlink={link}\n\t\t\t\tlocale={locale}\n\t\t\t\tretryAfterMs={result.retryAfterMs}\n\t\t\t\tstatus={result.status}\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\n\t// The number formats its own unit and plural, so the template only places it.\n\tconst duration = formatMinutes(windowMinutes, locale)\n\treturn (\n\t\t<WidgetCard\n\t\t\tclassName={CLASS_NAME}\n\t\t\tcaption={t(keys.widgetRealtimeWindow, { duration })}\n\t\t\tlink={link}\n\t\t\ttitle={title}\n\t\t>\n\t\t\t<RealtimeCounter\n\t\t\t\tendpoint={buildRealtimeEndpoint(props.req.payload.config)}\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={result.substitutedFrom === undefined ? data.dataSource : result.adapterId}\n\t\t\t\tinitialActiveNow={result.activeNow}\n\t\t\t\tinitialSeries={result.series}\n\t\t\t\tinitialSampled={result.sampled}\n\t\t\t\tlocale={locale}\n\t\t\t\ttimezone={timezone}\n\t\t\t\tlabel={t(METRIC_KEYS[metric])}\n\t\t\t\tpausedLabel={t(keys.widgetRealtimePaused)}\n\t\t\t\tperiodLabel={t(keys.viewTrendPeriod)}\n\t\t\t\tsampledLabel={t(keys.stateSampled)}\n\t\t\t\tfill\n\t\t\t/>\n\t\t\t<WidgetNote>\n\t\t\t\t{widgetNotice(\n\t\t\t\t\t{ substitutedFrom: result.substitutedFrom },\n\t\t\t\t\t{ t, locale, now, timeZone: timezone }\n\t\t\t\t)}\n\t\t\t</WidgetNote>\n\t\t</WidgetCard>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAgBA,MAAM,aAAa;AAEnB,MAAM,mBAAmB;AAEzB,MAAM,yBAAyB;AAS/B,MAAM,UAAU,UAAoD;CACnE,MAAM,EAAE,QAAQ,GAAG,WAAW,iBAAiB,MAAM,UAAU;CAC/D,OAAO;EAAE,GAAG;EAAQ,GAAI,WAAW,cAAc,WAAW,cAAc,EAAE,OAAO,IAAI,CAAC;CAAG;AAC5F;AAEA,MAAM,WAAW,MAA0B,MAC1C,KAAK,OAAO,KAAK,KAAK,EAAE,KAAK,mBAAmB;;;;;;AAOjD,SAAwB,wBAAwB,OAA6B;CAC5E,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,OACC,oBAAC,aAAD;EACC,WAAW;EACX,MAAK;EACL,OAAO,QAAQ,OAAO,KAAK,GAAG,CAAC;EAC/B,QAAQ;YAER,oBAAC,oBAAD,EAAoB,GAAI,MAAQ,CAAA;CACpB,CAAA;AAEf;AAEA,eAAe,mBAAmB,OAA6B;CAC9D,MAAM,OAAO,OAAO,KAAK;CACzB,MAAM,SAAS,KAAK,UAAU;CAC9B,MAAM,gBAAgB,OAAO,KAAK,aAAa,KAAK;CACpD,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,MAAM,SAAS,WAAW,MAAM,IAAI,KAAK,QAAQ;CACjD,MAAM,QAAQ,QAAQ,MAAM,CAAC;CAG7B,MAAM,WAAW,MAAM,gBAAgB,MAAM,GAAG;CAChD,MAAM,MAAM,YAAY;CACxB,MAAM,SAAS,MAAM,sBAAsB;EAC1C,KAAK,MAAM;EACX;EACA;EACA,WAAW,KAAK;EAChB,wBAAwB;EACxB;CACD,CAAC;CACD,MAAM,OAAO,WAAW,OAAO,GAAG,EAAE,WAAW,OAAO,UAAU,CAAC;CACjE,IAAI,OAAO,WAAW,MACrB,OACC,oBAAC,iBAAD;EACC,WAAW;EACL;EACE;EACR,cAAc,OAAO;EACrB,QAAQ,OAAO;EACZ;EACI;CACP,CAAA;CAKH,MAAM,WAAW,cAAc,eAAe,MAAM;CACpD,OACC,qBAAC,YAAD;EACC,WAAW;EACX,SAAS,EAAE,KAAK,sBAAsB,EAAE,SAAS,CAAC;EAC5C;EACC;YAJR,CAMC,oBAAC,iBAAD;GACC,UAAU,sBAAsB,MAAM,IAAI,QAAQ,MAAM;GACxD,YAAY;GACJ;GACO;GACf,YAAY,OAAO,oBAAoB,KAAA,IAAY,KAAK,aAAa,OAAO;GAC5E,kBAAkB,OAAO;GACzB,eAAe,OAAO;GACtB,gBAAgB,OAAO;GACf;GACE;GACV,OAAO,EAAE,YAAY,OAAO;GAC5B,aAAa,EAAE,KAAK,oBAAoB;GACxC,aAAa,EAAE,KAAK,eAAe;GACnC,cAAc,EAAE,KAAK,YAAY;GACjC,MAAA;EACA,CAAA,GACD,oBAAC,YAAD,EAAA,UACE,aACA,EAAE,iBAAiB,OAAO,gBAAgB,GAC1C;GAAE;GAAG;GAAQ;GAAK,UAAU;EAAS,CACtC,EACW,CAAA,CACD;;AAEd"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsTrendWidget.js","names":[],"sources":["../../src/widgets/AnalyticsTrendWidget.tsx"],"sourcesContent":["import { labelSeries, unitForRange } from '../charts/bucket'\nimport { markInProgress } from '../charts/inProgress'\nimport { TrendChart, type TrendPoint } from '../charts/TrendChart'\nimport { currentDate } from '../core/clock'\nimport { formatMetricValue } from '../fields/format'\nimport { revenueCurrencyOf } from '../goals/currency'\nimport { requestCurrency } from '../plugin/runtime'\nimport { intlLocale } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport { asTranslate } from '../translations/server'\nimport { ComparisonDelta } from './ComparisonDelta'\nimport { widgetFilters } from './filterField'\nimport { widgetNotice } from './notices'\nimport { formatRangeCaption } from './range'\nimport { readForWidgetSeries } from './readForWidgetSeries'\nimport { metricWidgetTitle, placeholderTitle } from './titleForecast'\nimport type { MetricWidgetData } from './types'\nimport { type AnalyticsWidgetProps, widgetLink } from './viewLink'\nimport { WidgetBody, WidgetCard, WidgetNote, WidgetValue } from './WidgetCard'\nimport { WidgetFrame, WidgetStateCard } from './WidgetFrame'\nimport { storedWidgetData } from './widgetData'\nimport { resolveWidgetWindow, widgetCaption, windowReadArgs } from './widgetWindow'\n\nconst CLASS_NAME = 'analytics-trend-widget'\n\nconst CHART_MIN_HEIGHT = 140\n\nconst dataOf = (props: AnalyticsWidgetProps): MetricWidgetData => storedWidgetData(props.widgetData)\n\n/**\n * The card's frame paints at once; its total and chart stream in when the read answers.\n *\n * @beta\n */\nexport default function AnalyticsTrendWidget(props: AnalyticsWidgetProps) {\n\treturn (\n\t\t<WidgetFrame\n\t\t\tclassName={CLASS_NAME}\n\t\t\tkind=\"trend\"\n\t\t\twidget={props}\n\t\t\t{...placeholderTitle(props.req, dataOf(props), { series: true })}\n\t\t>\n\t\t\t<TrendWidgetBody {...props} />\n\t\t</WidgetFrame>\n\t)\n}\n\nasync function TrendWidgetBody(props: AnalyticsWidgetProps) {\n\tconst data = dataOf(props)\n\tconst metric = data.metric ?? 'pageviews'\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = intlLocale(props.req.i18n.language)\n\tconst title = metricWidgetTitle(data, t)\n\tconst window = await resolveWidgetWindow(props.req, data)\n\tif (window.unreadable) {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlocale={locale}\n\t\t\t\tstatus=\"invalid\"\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\tconst reporting = await requestCurrency(props.req)\n\tconst compare = data.compare === true\n\tconst filters = widgetFilters(data)\n\tconst now = currentDate()\n\tconst result = await readForWidgetSeries({\n\t\treq: props.req,\n\t\tmetric,\n\t\tadapterId: data.dataSource,\n\t\tnow,\n\t\tcompare,\n\t\tfilters,\n\t\t...windowReadArgs(window),\n\t})\n\tconst link = widgetLink(props, t, {\n\t\twindow,\n\t\tadapterId: result.adapterId,\n\t\tmetric,\n\t\tcompare,\n\t\tfilters,\n\t})\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlink={link}\n\t\t\t\tlocale={locale}\n\t\t\t\tretryAfterMs={result.retryAfterMs}\n\t\t\t\tstatus={result.status}\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\n\tconst currency = result.currency ?? revenueCurrencyOf(filters, reporting)\n\tconst caption = await widgetCaption(props.req, { window, filters, read: result, locale, t })\n\tconst notice = widgetNotice(result, { t, locale, now, timeZone: result.timezone })\n\t// A window charted by hour needs hourly rows; without them the headline total above is the\n\t// whole answer, and a one-point line under it would only look like a broken chart.\n\tconst chartable = unitForRange(result.dateRange) !== 'hour' || result.seriesGranularity === 'hour'\n\tconst format = (value: TrendPoint['value']): string =>\n\t\tformatMetricValue(metric, value, { locale, currency })\n\tconst trendPoints: TrendPoint[] = markInProgress(\n\t\tlabelSeries(result.points, {\n\t\t\tgranularity: result.seriesGranularity ?? 'day',\n\t\t\ttz: result.timezone,\n\t\t\tlocale,\n\t\t\tweekday: !window.range && window.timeframe === 'last7days',\n\t\t\tfrom: result.dateRange.start,\n\t\t}).map((bucket) => ({ ...bucket, display: format(bucket.value) })),\n\t\t{ now, end: result.dateRange.end, soFar: (period) => t(keys.chartSoFar, { period }) }\n\t)\n\t// The read aligned the previous window to these buckets by index, so it borrows their labels.\n\tconst comparisonPoints: TrendPoint[] | undefined =\n\t\tcompare && result.comparisonPoints\n\t\t\t? trendPoints.map((bucket, i) => {\n\t\t\t\t\tconst value = result.comparisonPoints?.[i]?.value ?? null\n\t\t\t\t\treturn { ...bucket, value, display: format(value) }\n\t\t\t\t})\n\t\t\t: undefined\n\t// An average with no reading on any day has no line to draw; the dash above says so.\n\tconst drawable = trendPoints.some((point) => point.value !== null)\n\treturn (\n\t\t<WidgetCard\n\t\t\tclassName={CLASS_NAME}\n\t\t\tcaption={caption}\n\t\t\tlink={link}\n\t\t\ttitle={metricWidgetTitle(data, t, result.visitorsSummed)}\n\t\t>\n\t\t\t<WidgetValue\n\t\t\t\tnoValueLabel={t(keys.stateNoValue)}\n\t\t\t\tvalue={formatMetricValue(metric, result.total, { locale, currency })}\n\t\t\t>\n\t\t\t\t{result.comparisonRange ? (\n\t\t\t\t\t<ComparisonDelta\n\t\t\t\t\t\tcurrent={result.total}\n\t\t\t\t\t\tprevious={result.previousTotal}\n\t\t\t\t\t\tmetric={metric}\n\t\t\t\t\t\tlocale={locale}\n\t\t\t\t\t\tt={t}\n\t\t\t\t\t/>\n\t\t\t\t) : null}\n\t\t\t</WidgetValue>\n\t\t\t{chartable && drawable ? (\n\t\t\t\t<WidgetBody>\n\t\t\t\t\t<TrendChart\n\t\t\t\t\t\tbuckets={trendPoints}\n\t\t\t\t\t\t{...(comparisonPoints && result.comparisonRange\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tcomparison: comparisonPoints,\n\t\t\t\t\t\t\t\t\tcomparisonLabel: t(keys.viewTrendPrevious),\n\t\t\t\t\t\t\t\t\tcomparisonRange: formatRangeCaption(\n\t\t\t\t\t\t\t\t\t\tresult.comparisonRange,\n\t\t\t\t\t\t\t\t\t\tlocale,\n\t\t\t\t\t\t\t\t\t\tresult.timezone\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {})}\n\t\t\t\t\t\tariaLabel={`${title} ${caption}`}\n\t\t\t\t\t\tfill\n\t\t\t\t\t\tkeyboardHint={t(keys.chartKeyboardHint)}\n\t\t\t\t\t\tlabel={title}\n\t\t\t\t\t\tminHeight={CHART_MIN_HEIGHT}\n\t\t\t\t\t\tnoValueLabel={t(keys.stateNoValue)}\n\t\t\t\t\t\tperiodLabel={t(keys.viewTrendPeriod)}\n\t\t\t\t\t/>\n\t\t\t\t</WidgetBody>\n\t\t\t) : null}\n\t\t\t<WidgetNote>{notice}</WidgetNote>\n\t\t</WidgetCard>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAM,aAAa;AAEnB,MAAM,mBAAmB;AAEzB,MAAM,UAAU,UAAkD,iBAAiB,MAAM,UAAU;;;;;;AAOnG,SAAwB,qBAAqB,OAA6B;CACzE,OACC,oBAAC,aAAD;EACC,WAAW;EACX,MAAK;EACL,QAAQ;EACR,GAAI,iBAAiB,MAAM,KAAK,OAAO,KAAK,GAAG,EAAE,QAAQ,KAAK,CAAC;YAE/D,oBAAC,iBAAD,EAAiB,GAAI,MAAQ,CAAA;CACjB,CAAA;AAEf;AAEA,eAAe,gBAAgB,OAA6B;CAC3D,MAAM,OAAO,OAAO,KAAK;CACzB,MAAM,SAAS,KAAK,UAAU;CAC9B,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,MAAM,SAAS,WAAW,MAAM,IAAI,KAAK,QAAQ;CACjD,MAAM,QAAQ,kBAAkB,MAAM,CAAC;CACvC,MAAM,SAAS,MAAM,oBAAoB,MAAM,KAAK,IAAI;CACxD,IAAI,OAAO,YACV,OACC,oBAAC,iBAAD;EACC,WAAW;EACH;EACR,QAAO;EACJ;EACI;CACP,CAAA;CAGH,MAAM,YAAY,MAAM,gBAAgB,MAA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,IAAI,SAAS;EACrD,OAAO;GAAE,GAAG;GAAQ;GAAO,SAAS,OAAO,KAAK;EAAE;CACnD,CAAC,IACA,KAAA;CAEJ,MAAM,WAAW,YAAY,MAAM,UAAU,MAAM,UAAU,IAAI;CACjE,OACC,qBAAC,YAAD;EACC,WAAW;EACF;EACH;EACN,OAAO,kBAAkB,MAAM,GAAG,OAAO,cAAc;YAJxD;GAMC,oBAAC,aAAD;IACC,cAAc,EAAE,KAAK,YAAY;IACjC,OAAO,kBAAkB,QAAQ,OAAO,OAAO;KAAE;KAAQ;IAAS,CAAC;cAElE,OAAO,kBACP,oBAAC,iBAAD;KACC,SAAS,OAAO;KAChB,UAAU,OAAO;KACT;KACA;KACL;IACH,CAAA,IACE;GACQ,CAAA;GACZ,aAAa,WACb,oBAAC,YAAD,EAAA,UACC,oBAAC,YAAD;IACC,SAAS;IACT,GAAK,oBAAoB,OAAO,kBAC7B;KACA,YAAY;KACZ,iBAAiB,EAAE,KAAK,iBAAiB;KACzC,iBAAiB,mBAChB,OAAO,iBACP,QACA,OAAO,QACR;IACD,IACC,CAAC;IACJ,WAAW,GAAG,MAAM,GAAG;IACvB,MAAA;IACA,cAAc,EAAE,KAAK,iBAAiB;IACtC,OAAO;IACP,WAAW;IACX,cAAc,EAAE,KAAK,YAAY;IACjC,aAAa,EAAE,KAAK,eAAe;GACnC,CAAA,EACU,CAAA,IACT;GACJ,oBAAC,YAAD,EAAA,UAAa,OAAmB,CAAA;EACrB;;AAEd"}
|
|
1
|
+
{"version":3,"file":"AnalyticsTrendWidget.js","names":[],"sources":["../../src/widgets/AnalyticsTrendWidget.tsx"],"sourcesContent":["import { labelSeries, unitForRange } from '../charts/bucket'\nimport { markInProgress } from '../charts/inProgress'\nimport { TrendChart, type TrendPoint } from '../charts/TrendChart'\nimport { currentDate } from '../core/clock'\nimport { formatMetricValue } from '../fields/format'\nimport { revenueCurrencyOf } from '../goals/currency'\nimport { requestCurrency } from '../plugin/runtime'\nimport { intlLocale } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport { asTranslate } from '../translations/server'\nimport { ComparisonDelta } from './ComparisonDelta'\nimport { widgetFilters } from './filterField'\nimport { widgetNotice } from './notices'\nimport { formatRangeCaption } from './range'\nimport { readForWidgetSeries } from './readForWidgetSeries'\nimport { metricWidgetTitle, placeholderTitle } from './titleForecast'\nimport type { MetricWidgetData } from './types'\nimport { type AnalyticsWidgetProps, widgetLink } from './viewLink'\nimport { WidgetBody, WidgetCard, WidgetNote, WidgetValue } from './WidgetCard'\nimport { WidgetFrame, WidgetStateCard } from './WidgetFrame'\nimport { storedWidgetData } from './widgetData'\nimport { resolveWidgetWindow, widgetCaption, windowReadArgs } from './widgetWindow'\n\nconst CLASS_NAME = 'analytics-trend-widget'\n\nconst CHART_MIN_HEIGHT = 140\n\nconst dataOf = (props: AnalyticsWidgetProps): MetricWidgetData => storedWidgetData(props.widgetData)\n\n/**\n * The card's frame paints at once; its total and chart stream in when the read answers.\n *\n * @beta\n */\nexport default function AnalyticsTrendWidget(props: AnalyticsWidgetProps) {\n\treturn (\n\t\t<WidgetFrame\n\t\t\tclassName={CLASS_NAME}\n\t\t\tkind=\"trend\"\n\t\t\twidget={props}\n\t\t\t{...placeholderTitle(props.req, dataOf(props), { series: true })}\n\t\t>\n\t\t\t<TrendWidgetBody {...props} />\n\t\t</WidgetFrame>\n\t)\n}\n\nasync function TrendWidgetBody(props: AnalyticsWidgetProps) {\n\tconst data = dataOf(props)\n\tconst metric = data.metric ?? 'pageviews'\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = intlLocale(props.req.i18n.language)\n\tconst title = metricWidgetTitle(data, t)\n\tconst window = await resolveWidgetWindow(props.req, data)\n\tif (window.unreadable) {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlocale={locale}\n\t\t\t\tstatus=\"invalid\"\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\tconst reporting = await requestCurrency(props.req)\n\tconst compare = data.compare === true\n\tconst filters = widgetFilters(data)\n\tconst now = currentDate()\n\tconst result = await readForWidgetSeries({\n\t\treq: props.req,\n\t\tmetric,\n\t\tadapterId: data.dataSource,\n\t\tfallbackWhenUnreadable: true,\n\t\tnow,\n\t\tcompare,\n\t\tfilters,\n\t\t...windowReadArgs(window),\n\t})\n\tconst link = widgetLink(props, t, {\n\t\twindow,\n\t\tadapterId: result.adapterId,\n\t\tmetric,\n\t\tcompare,\n\t\tfilters,\n\t})\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<WidgetStateCard\n\t\t\t\tclassName={CLASS_NAME}\n\t\t\t\tlink={link}\n\t\t\t\tlocale={locale}\n\t\t\t\tretryAfterMs={result.retryAfterMs}\n\t\t\t\tstatus={result.status}\n\t\t\t\tt={t}\n\t\t\t\ttitle={title}\n\t\t\t/>\n\t\t)\n\t}\n\n\tconst currency = result.currency ?? revenueCurrencyOf(filters, reporting)\n\tconst caption = await widgetCaption(props.req, { window, filters, read: result, locale, t })\n\tconst notice = widgetNotice(result, { t, locale, now, timeZone: result.timezone })\n\t// A window charted by hour needs hourly rows; without them the headline total above is the\n\t// whole answer, and a one-point line under it would only look like a broken chart.\n\tconst chartable = unitForRange(result.dateRange) !== 'hour' || result.seriesGranularity === 'hour'\n\tconst format = (value: TrendPoint['value']): string =>\n\t\tformatMetricValue(metric, value, { locale, currency })\n\tconst trendPoints: TrendPoint[] = markInProgress(\n\t\tlabelSeries(result.points, {\n\t\t\tgranularity: result.seriesGranularity ?? 'day',\n\t\t\ttz: result.timezone,\n\t\t\tlocale,\n\t\t\tweekday: !window.range && window.timeframe === 'last7days',\n\t\t\tfrom: result.dateRange.start,\n\t\t}).map((bucket) => ({ ...bucket, display: format(bucket.value) })),\n\t\t{ now, end: result.dateRange.end, soFar: (period) => t(keys.chartSoFar, { period }) }\n\t)\n\t// The read aligned the previous window to these buckets by index, so it borrows their labels.\n\tconst comparisonPoints: TrendPoint[] | undefined =\n\t\tcompare && result.comparisonPoints\n\t\t\t? trendPoints.map((bucket, i) => {\n\t\t\t\t\tconst value = result.comparisonPoints?.[i]?.value ?? null\n\t\t\t\t\treturn { ...bucket, value, display: format(value) }\n\t\t\t\t})\n\t\t\t: undefined\n\t// An average with no reading on any day has no line to draw; the dash above says so.\n\tconst drawable = trendPoints.some((point) => point.value !== null)\n\treturn (\n\t\t<WidgetCard\n\t\t\tclassName={CLASS_NAME}\n\t\t\tcaption={caption}\n\t\t\tlink={link}\n\t\t\ttitle={metricWidgetTitle(data, t, result.visitorsSummed)}\n\t\t>\n\t\t\t<WidgetValue\n\t\t\t\tnoValueLabel={t(keys.stateNoValue)}\n\t\t\t\tvalue={formatMetricValue(metric, result.total, { locale, currency })}\n\t\t\t>\n\t\t\t\t{result.comparisonRange ? (\n\t\t\t\t\t<ComparisonDelta\n\t\t\t\t\t\tcurrent={result.total}\n\t\t\t\t\t\tprevious={result.previousTotal}\n\t\t\t\t\t\tmetric={metric}\n\t\t\t\t\t\tlocale={locale}\n\t\t\t\t\t\tt={t}\n\t\t\t\t\t/>\n\t\t\t\t) : null}\n\t\t\t</WidgetValue>\n\t\t\t{chartable && drawable ? (\n\t\t\t\t<WidgetBody>\n\t\t\t\t\t<TrendChart\n\t\t\t\t\t\tbuckets={trendPoints}\n\t\t\t\t\t\t{...(comparisonPoints && result.comparisonRange\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tcomparison: comparisonPoints,\n\t\t\t\t\t\t\t\t\tcomparisonLabel: t(keys.viewTrendPrevious),\n\t\t\t\t\t\t\t\t\tcomparisonRange: formatRangeCaption(\n\t\t\t\t\t\t\t\t\t\tresult.comparisonRange,\n\t\t\t\t\t\t\t\t\t\tlocale,\n\t\t\t\t\t\t\t\t\t\tresult.timezone\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {})}\n\t\t\t\t\t\tariaLabel={`${title} ${caption}`}\n\t\t\t\t\t\tfill\n\t\t\t\t\t\tkeyboardHint={t(keys.chartKeyboardHint)}\n\t\t\t\t\t\tlabel={title}\n\t\t\t\t\t\tminHeight={CHART_MIN_HEIGHT}\n\t\t\t\t\t\tnoValueLabel={t(keys.stateNoValue)}\n\t\t\t\t\t\tperiodLabel={t(keys.viewTrendPeriod)}\n\t\t\t\t\t/>\n\t\t\t\t</WidgetBody>\n\t\t\t) : null}\n\t\t\t<WidgetNote>{notice}</WidgetNote>\n\t\t</WidgetCard>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAM,aAAa;AAEnB,MAAM,mBAAmB;AAEzB,MAAM,UAAU,UAAkD,iBAAiB,MAAM,UAAU;;;;;;AAOnG,SAAwB,qBAAqB,OAA6B;CACzE,OACC,oBAAC,aAAD;EACC,WAAW;EACX,MAAK;EACL,QAAQ;EACR,GAAI,iBAAiB,MAAM,KAAK,OAAO,KAAK,GAAG,EAAE,QAAQ,KAAK,CAAC;YAE/D,oBAAC,iBAAD,EAAiB,GAAI,MAAQ,CAAA;CACjB,CAAA;AAEf;AAEA,eAAe,gBAAgB,OAA6B;CAC3D,MAAM,OAAO,OAAO,KAAK;CACzB,MAAM,SAAS,KAAK,UAAU;CAC9B,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,MAAM,SAAS,WAAW,MAAM,IAAI,KAAK,QAAQ;CACjD,MAAM,QAAQ,kBAAkB,MAAM,CAAC;CACvC,MAAM,SAAS,MAAM,oBAAoB,MAAM,KAAK,IAAI;CACxD,IAAI,OAAO,YACV,OACC,oBAAC,iBAAD;EACC,WAAW;EACH;EACR,QAAO;EACJ;EACI;CACP,CAAA;CAGH,MAAM,YAAY,MAAM,gBAAgB,MAAM,GAAG;CACjD,MAAM,UAAU,KAAK,YAAY;CACjC,MAAM,UAAU,cAAc,IAAI;CAClC,MAAM,MAAM,YAAY;CACxB,MAAM,SAAS,MAAM,oBAAoB;EACxC,KA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AY,MAAM,UAAU,MAAM,UAAU,IAAI;CACjE,OACC,qBAAC,YAAD;EACC,WAAW;EACF;EACH;EACN,OAAO,kBAAkB,MAAM,GAAG,OAAO,cAAc;YAJxD;GAMC,oBAAC,aAAD;IACC,cAAc,EAAE,KAAK,YAAY;IACjC,OAAO,kBAAkB,QAAQ,OAAO,OAAO;KAAE;KAAQ;IAAS,CAAC;cAElE,OAAO,kBACP,oBAAC,iBAAD;KACC,SAAS,OAAO;KAChB,UAAU,OAAO;KACT;KACA;KACL;IACH,CAAA,IACE;GACQ,CAAA;GACZ,aAAa,WACb,oBAAC,YAAD,EAAA,UACC,oBAAC,YAAD;IACC,SAAS;IACT,GAAK,oBAAoB,OAAO,kBAC7B;KACA,YAAY;KACZ,iBAAiB,EAAE,KAAK,iBAAiB;KACzC,iBAAiB,mBAChB,OAAO,iBACP,QACA,OAAO,QACR;IACD,IACC,CAAC;IACJ,WAAW,GAAG,MAAM,GAAG;IACvB,MAAA;IACA,cAAc,EAAE,KAAK,iBAAiB;IACtC,OAAO;IACP,WAAW;IACX,cAAc,EAAE,KAAK,YAAY;IACjC,aAAa,EAAE,KAAK,eAAe;GACnC,CAAA,EACU,CAAA,IACT;GACJ,oBAAC,YAAD,EAAA,UAAa,OAAmB,CAAA;EACrB;;AAEd"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PayloadRequest, WidgetInstance } from "payload";
|
|
2
|
+
|
|
3
|
+
//#region src/widgets/defaultLayout.d.ts
|
|
4
|
+
/** @beta */
|
|
5
|
+
interface AnalyticsDefaultLayoutOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The layout to narrow. Default `analyticsDefaultWidgets()`. A widget that is not the plugin's,
|
|
8
|
+
* or one naming its own `dataSource`, is kept as placed.
|
|
9
|
+
*/
|
|
10
|
+
widgets?: WidgetInstance[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* `admin.dashboard.defaultLayout` for a reader: the default widgets less those the reader's default
|
|
14
|
+
* source cannot answer (realtime, goals, a breakdown's dimension, a metric it lacks). With no
|
|
15
|
+
* runtime or no source the reader may read, the whole list.
|
|
16
|
+
*
|
|
17
|
+
* @beta
|
|
18
|
+
*/
|
|
19
|
+
declare const analyticsDefaultLayout: (options?: AnalyticsDefaultLayoutOptions) => ({
|
|
20
|
+
req
|
|
21
|
+
}: {
|
|
22
|
+
req: PayloadRequest;
|
|
23
|
+
}) => Promise<WidgetInstance[]>;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { AnalyticsDefaultLayoutOptions, analyticsDefaultLayout };
|
|
26
|
+
//# sourceMappingURL=defaultLayout.d.ts.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { isRecord } from "../core/values.js";
|
|
2
|
+
import { isMetricKey } from "../core/guards.js";
|
|
3
|
+
import { getRuntime } from "../plugin/runtime.js";
|
|
4
|
+
import { resolveReadContext } from "../core/scopedRead.js";
|
|
5
|
+
import { satisfiesCapabilities } from "../core/capabilities.js";
|
|
6
|
+
import { isBuiltInWidgetSlug } from "../core/widgetSlugs.js";
|
|
7
|
+
import { servedName } from "../core/metricAliases.js";
|
|
8
|
+
import { builtInWidgetRequires } from "./registerWidgets.js";
|
|
9
|
+
import { analyticsDefaultWidgets } from "./defaults.js";
|
|
10
|
+
//#region src/widgets/defaultLayout.ts
|
|
11
|
+
/** Typed as `unknown` so an app's generated widget data unions read alike. */
|
|
12
|
+
const dataField = (data, key) => isRecord(data) ? data[key] : void 0;
|
|
13
|
+
/** What one placed built-in widget reads: its widget's own needs plus the metric it opens on. */
|
|
14
|
+
const requirementOf = (instance, caps) => {
|
|
15
|
+
if (!isBuiltInWidgetSlug(instance.widgetSlug)) return;
|
|
16
|
+
const requires = builtInWidgetRequires(instance.widgetSlug) ?? {};
|
|
17
|
+
const metric = dataField(instance.data, "metric");
|
|
18
|
+
const metrics = [...requires.metrics ?? [], ...isMetricKey(metric) ? [metric] : []];
|
|
19
|
+
return {
|
|
20
|
+
...requires,
|
|
21
|
+
metrics: metrics.map((m) => servedName(m, caps.metrics))
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
const namesSource = (instance) => typeof dataField(instance.data, "dataSource") === "string";
|
|
25
|
+
/**
|
|
26
|
+
* `admin.dashboard.defaultLayout` for a reader: the default widgets less those the reader's default
|
|
27
|
+
* source cannot answer (realtime, goals, a breakdown's dimension, a metric it lacks). With no
|
|
28
|
+
* runtime or no source the reader may read, the whole list.
|
|
29
|
+
*
|
|
30
|
+
* @beta
|
|
31
|
+
*/
|
|
32
|
+
const analyticsDefaultLayout = (options = {}) => async ({ req }) => {
|
|
33
|
+
const widgets = options.widgets ?? analyticsDefaultWidgets();
|
|
34
|
+
const runtime = getRuntime(req.payload);
|
|
35
|
+
if (!runtime) return widgets;
|
|
36
|
+
const read = await resolveReadContext({
|
|
37
|
+
runtime,
|
|
38
|
+
req
|
|
39
|
+
});
|
|
40
|
+
if (!read.ok) return widgets;
|
|
41
|
+
const caps = read.adapter.capabilities;
|
|
42
|
+
return widgets.filter((instance) => {
|
|
43
|
+
if (namesSource(instance)) return true;
|
|
44
|
+
const requirement = requirementOf(instance, caps);
|
|
45
|
+
return requirement === void 0 || satisfiesCapabilities(caps, requirement);
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
//#endregion
|
|
49
|
+
export { analyticsDefaultLayout };
|
|
50
|
+
|
|
51
|
+
//# sourceMappingURL=defaultLayout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaultLayout.js","names":[],"sources":["../../src/widgets/defaultLayout.ts"],"sourcesContent":["import type { PayloadRequest, WidgetInstance } from 'payload'\nimport { type CapabilityRequirement, satisfiesCapabilities } from '../core/capabilities'\nimport type { AnalyticsCapabilities } from '../core/contract'\nimport { isMetricKey } from '../core/guards'\nimport { servedName } from '../core/metricAliases'\nimport { resolveReadContext } from '../core/scopedRead'\nimport { isRecord } from '../core/values'\nimport { isBuiltInWidgetSlug } from '../core/widgetSlugs'\nimport { getRuntime } from '../plugin/runtime'\nimport { analyticsDefaultWidgets } from './defaults'\nimport { builtInWidgetRequires } from './registerWidgets'\n\n/** @beta */\nexport interface AnalyticsDefaultLayoutOptions {\n\t/**\n\t * The layout to narrow. Default `analyticsDefaultWidgets()`. A widget that is not the plugin's,\n\t * or one naming its own `dataSource`, is kept as placed.\n\t */\n\twidgets?: WidgetInstance[]\n}\n\n/** Typed as `unknown` so an app's generated widget data unions read alike. */\nconst dataField = (data: unknown, key: string): unknown => (isRecord(data) ? data[key] : undefined)\n\n/** What one placed built-in widget reads: its widget's own needs plus the metric it opens on. */\nconst requirementOf = (\n\tinstance: WidgetInstance,\n\tcaps: AnalyticsCapabilities\n): CapabilityRequirement | undefined => {\n\tif (!isBuiltInWidgetSlug(instance.widgetSlug)) {\n\t\treturn undefined\n\t}\n\tconst requires = builtInWidgetRequires(instance.widgetSlug) ?? {}\n\tconst metric = dataField(instance.data, 'metric')\n\tconst metrics = [...(requires.metrics ?? []), ...(isMetricKey(metric) ? [metric] : [])]\n\t// A metric served under another name (`visits` as `sessions`) is read, so asked, under that one.\n\treturn { ...requires, metrics: metrics.map((m) => servedName(m, caps.metrics)) }\n}\n\nconst namesSource = (instance: WidgetInstance): boolean =>\n\ttypeof dataField(instance.data, 'dataSource') === 'string'\n\n/**\n * `admin.dashboard.defaultLayout` for a reader: the default widgets less those the reader's default\n * source cannot answer (realtime, goals, a breakdown's dimension, a metric it lacks). With no\n * runtime or no source the reader may read, the whole list.\n *\n * @beta\n */\nexport const analyticsDefaultLayout =\n\t(options: AnalyticsDefaultLayoutOptions = {}) =>\n\tasync ({ req }: { req: PayloadRequest }): Promise<WidgetInstance[]> => {\n\t\tconst widgets = options.widgets ?? analyticsDefaultWidgets()\n\t\tconst runtime = getRuntime(req.payload)\n\t\tif (!runtime) {\n\t\t\treturn widgets\n\t\t}\n\t\tconst read = await resolveReadContext({ runtime, req })\n\t\tif (!read.ok) {\n\t\t\treturn widgets\n\t\t}\n\t\tconst caps = read.adapter.capabilities\n\t\treturn widgets.filter((instance) => {\n\t\t\tif (namesSource(instance)) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t\tconst requirement = requirementOf(instance, caps)\n\t\t\treturn requirement === undefined || satisfiesCapabilities(caps, requirement)\n\t\t})\n\t}\n"],"mappings":";;;;;;;;;;;AAsBA,MAAM,aAAa,MAAe,QAA0B,SAAS,IAAI,IAAI,KAAK,OAAO,KAAA;;AAGzF,MAAM,iBACL,UACA,SACuC;CACvC,IAAI,CAAC,oBAAoB,SAAS,UAAU,GAC3C;CAED,MAAM,WAAW,sBAAsB,SAAS,UAAU,KAAK,CAAC;CAChE,MAAM,SAAS,UAAU,SAAS,MAAM,QAAQ;CAChD,MAAM,UAAU,CAAC,GAAI,SAAS,WAAW,CAAC,GAAI,GAAI,YAAY,MAAM,IAAI,CAAC,MAAM,IAAI,CAAC,CAAE;CAEtF,OAAO;EAAE,GAAG;EAAU,SAAS,QAAQ,KAAK,MAAM,WAAW,GAAG,KAAK,OAAO,CAAC;CAAE;AAChF;AAEA,MAAM,eAAe,aACpB,OAAO,UAAU,SAAS,MAAM,YAAY,MAAM;;;;;;;;AASnD,MAAa,0BACX,UAAyC,CAAC,MAC3C,OAAO,EAAE,UAA8D;CACtE,MAAM,UAAU,QAAQ,WAAW,wBAAwB;CAC3D,MAAM,UAAU,WAAW,IAAI,OAAO;CACtC,IAAI,CAAC,SACJ,OAAO;CAER,MAAM,OAAO,MAAM,mBAAmB;EAAE;EAAS;CAAI,CAAC;CACtD,IAAI,CAAC,KAAK,IACT,OAAO;CAER,MAAM,OAAO,KAAK,QAAQ;CAC1B,OAAO,QAAQ,QAAQ,aAAa;EACnC,IAAI,YAAY,QAAQ,GACvB,OAAO;EAER,MAAM,cAAc,cAAc,UAAU,IAAI;EAChD,OAAO,gBAAgB,KAAA,KAAa,sBAAsB,MAAM,WAAW;CAC5E,CAAC;AACF"}
|
package/dist/widgets/notices.js
CHANGED
|
@@ -42,6 +42,7 @@ const clampedText = (flags, context) => {
|
|
|
42
42
|
const noticeTexts = (flags, context) => {
|
|
43
43
|
const { t } = context;
|
|
44
44
|
const texts = [];
|
|
45
|
+
if (flags.substitutedFrom !== void 0) texts.push(t(keys.stateSourceSubstituted));
|
|
45
46
|
if (flags.stale) texts.push(t(keys.viewStale));
|
|
46
47
|
if (flags.hostRefused) texts.push(t(keys.viewErrorHostRefused));
|
|
47
48
|
else if (flags.sourceError) texts.push(t(keys.viewErrorMisconfigured));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"notices.js","names":[],"sources":["../../src/widgets/notices.ts"],"sourcesContent":["import { dateTimeFormat, listFormat, relativeTimeFormat } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport type { Translate } from '../translations/server'\nimport type { ReadNotices } from './readMeta'\n\n/** What a read can carry beyond its numbers, each of which earns a word of caveat. */\nexport type WidgetNoticeFlags = Partial<ReadNotices> & {\n\t/** The read asked about goals and the scope declared none of its own. */\n\tnoGoals?: boolean\n}\n\n/** What wording a notice needs beyond its key: the reader's language and the moment it is read. */\nexport interface NoticeContext {\n\tt: Translate\n\tlocale: string\n\tnow: Date\n\t/** The reporting timezone a clamped window's days are named in; UTC when absent. */\n\ttimeZone?: string\n}\n\n/** Between notices on the one line: a list separator, not sentence punctuation. */\nexport const NOTICE_SEPARATOR = ' · '\n\nconst SECOND = 1000\nconst MINUTE = 60 * SECOND\nconst HOUR = 60 * MINUTE\n\nconst relativeFormat = (locale: string): Intl.RelativeTimeFormat =>\n\trelativeTimeFormat(locale, { numeric: 'auto' })\n\n/** How long ago `asOf` was, in the reader's language: \"3 minutes ago\". */\nexport const timeAgo = (asOf: string, now: Date, locale: string): string => {\n\tconst elapsed = Math.max(0, now.getTime() - Date.parse(asOf))\n\tconst format = relativeFormat(locale)\n\tif (elapsed < MINUTE) {\n\t\treturn format.format(-Math.round(elapsed / SECOND), 'second')\n\t}\n\tif (elapsed < HOUR) {\n\t\treturn format.format(-Math.floor(elapsed / MINUTE), 'minute')\n\t}\n\treturn format.format(-Math.floor(elapsed / HOUR), 'hour')\n}\n\n/** The day an instant falls on in the reader's language and the reporting zone: \"Mar 3, 2026\". */\nconst dayOf = (instant: string, context: NoticeContext): string =>\n\tdateTimeFormat(context.locale, {\n\t\tmonth: 'short',\n\t\tday: 'numeric',\n\t\tyear: 'numeric',\n\t\ttimeZone: context.timeZone ?? 'UTC',\n\t}).format(new Date(instant))\n\n/** What a clamped read covers, by the bounds it kept, else the generic wording. */\nconst clampedText = (flags: WidgetNoticeFlags, context: NoticeContext): string => {\n\tconst { t } = context\n\tconst from = flags.dataFrom\n\tconst to = flags.dataTo\n\tif (from && to) {\n\t\treturn t(keys.stateClampedBetween, { from: dayOf(from, context), to: dayOf(to, context) })\n\t}\n\tif (from) return t(keys.stateClampedFrom, { date: dayOf(from, context) })\n\tif (to) return t(keys.stateClampedTo, { date: dayOf(to, context) })\n\treturn t(keys.stateClamped)\n}\n\n/**\n * The caveats a read carries, each a sentence, widest-reaching first, so the one that changes what\n * the number means the most is read first. Empty when the read carries none, the common case.\n */\nexport const noticeTexts = (flags: WidgetNoticeFlags, context: NoticeContext): string[] => {\n\tconst { t } = context\n\tconst texts: string[] = []\n\tif (flags.stale) texts.push(t(keys.viewStale))\n\tif (flags.hostRefused) texts.push(t(keys.viewErrorHostRefused))\n\telse if (flags.sourceError) texts.push(t(keys.viewErrorMisconfigured))\n\tif (flags.timezoneUnresolved) texts.push(t(keys.stateTimezoneUnresolved))\n\tif (flags.partial) texts.push(t(keys.statePartial))\n\tif (flags.capped) texts.push(t(keys.stateCapped))\n\tif (flags.failed) texts.push(t(keys.stateFailed))\n\tif (flags.clamped) texts.push(clampedText(flags, context))\n\tif (flags.filtersUnapplied) texts.push(t(keys.stateFiltersUnapplied))\n\tif (flags.sampled) texts.push(t(keys.stateSampled))\n\tconst currencies = flags.mixedCurrencies ?? []\n\tif (currencies.length > 1) {\n\t\tconst named = listFormat(context.locale, { type: 'conjunction' }).format(currencies)\n\t\ttexts.push(t(keys.stateMixedCurrencies, { currencies: named }))\n\t}\n\t// A conversions number with no goals behind it is a number about nothing, which the plain\n\t// caption would read as \"nobody converted\".\n\tif (flags.goalsUnresolved) texts.push(t(keys.stateGoalsUnresolved))\n\telse if (flags.noGoals) texts.push(t(keys.stateNoGoals))\n\tif (flags.asOf) {\n\t\ttexts.push(t(keys.stateUpdated, { ago: timeAgo(flags.asOf, context.now, context.locale) }))\n\t}\n\treturn texts\n}\n\n/**\n * The caveats as one line: a card is a few centimetres tall, and notices stacked under it would\n * push the number itself out of view.\n */\nexport const widgetNotice = (flags: WidgetNoticeFlags, context: NoticeContext): string =>\n\tnoticeTexts(flags, context).join(NOTICE_SEPARATOR)\n"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"notices.js","names":[],"sources":["../../src/widgets/notices.ts"],"sourcesContent":["import { dateTimeFormat, listFormat, relativeTimeFormat } from '../translations/intl'\nimport { keys } from '../translations/keys'\nimport type { Translate } from '../translations/server'\nimport type { SubstitutedSource } from './prepareWidgetRead'\nimport type { ReadNotices } from './readMeta'\n\n/** What a read can carry beyond its numbers, each of which earns a word of caveat. */\nexport type WidgetNoticeFlags = Partial<ReadNotices> &\n\tSubstitutedSource & {\n\t\t/** The read asked about goals and the scope declared none of its own. */\n\t\tnoGoals?: boolean\n\t}\n\n/** What wording a notice needs beyond its key: the reader's language and the moment it is read. */\nexport interface NoticeContext {\n\tt: Translate\n\tlocale: string\n\tnow: Date\n\t/** The reporting timezone a clamped window's days are named in; UTC when absent. */\n\ttimeZone?: string\n}\n\n/** Between notices on the one line: a list separator, not sentence punctuation. */\nexport const NOTICE_SEPARATOR = ' · '\n\nconst SECOND = 1000\nconst MINUTE = 60 * SECOND\nconst HOUR = 60 * MINUTE\n\nconst relativeFormat = (locale: string): Intl.RelativeTimeFormat =>\n\trelativeTimeFormat(locale, { numeric: 'auto' })\n\n/** How long ago `asOf` was, in the reader's language: \"3 minutes ago\". */\nexport const timeAgo = (asOf: string, now: Date, locale: string): string => {\n\tconst elapsed = Math.max(0, now.getTime() - Date.parse(asOf))\n\tconst format = relativeFormat(locale)\n\tif (elapsed < MINUTE) {\n\t\treturn format.format(-Math.round(elapsed / SECOND), 'second')\n\t}\n\tif (elapsed < HOUR) {\n\t\treturn format.format(-Math.floor(elapsed / MINUTE), 'minute')\n\t}\n\treturn format.format(-Math.floor(elapsed / HOUR), 'hour')\n}\n\n/** The day an instant falls on in the reader's language and the reporting zone: \"Mar 3, 2026\". */\nconst dayOf = (instant: string, context: NoticeContext): string =>\n\tdateTimeFormat(context.locale, {\n\t\tmonth: 'short',\n\t\tday: 'numeric',\n\t\tyear: 'numeric',\n\t\ttimeZone: context.timeZone ?? 'UTC',\n\t}).format(new Date(instant))\n\n/** What a clamped read covers, by the bounds it kept, else the generic wording. */\nconst clampedText = (flags: WidgetNoticeFlags, context: NoticeContext): string => {\n\tconst { t } = context\n\tconst from = flags.dataFrom\n\tconst to = flags.dataTo\n\tif (from && to) {\n\t\treturn t(keys.stateClampedBetween, { from: dayOf(from, context), to: dayOf(to, context) })\n\t}\n\tif (from) return t(keys.stateClampedFrom, { date: dayOf(from, context) })\n\tif (to) return t(keys.stateClampedTo, { date: dayOf(to, context) })\n\treturn t(keys.stateClamped)\n}\n\n/**\n * The caveats a read carries, each a sentence, widest-reaching first, so the one that changes what\n * the number means the most is read first. Empty when the read carries none, the common case.\n */\nexport const noticeTexts = (flags: WidgetNoticeFlags, context: NoticeContext): string[] => {\n\tconst { t } = context\n\tconst texts: string[] = []\n\t// The id is not named: it can be an internal one, such as a provider document's.\n\tif (flags.substitutedFrom !== undefined) texts.push(t(keys.stateSourceSubstituted))\n\tif (flags.stale) texts.push(t(keys.viewStale))\n\tif (flags.hostRefused) texts.push(t(keys.viewErrorHostRefused))\n\telse if (flags.sourceError) texts.push(t(keys.viewErrorMisconfigured))\n\tif (flags.timezoneUnresolved) texts.push(t(keys.stateTimezoneUnresolved))\n\tif (flags.partial) texts.push(t(keys.statePartial))\n\tif (flags.capped) texts.push(t(keys.stateCapped))\n\tif (flags.failed) texts.push(t(keys.stateFailed))\n\tif (flags.clamped) texts.push(clampedText(flags, context))\n\tif (flags.filtersUnapplied) texts.push(t(keys.stateFiltersUnapplied))\n\tif (flags.sampled) texts.push(t(keys.stateSampled))\n\tconst currencies = flags.mixedCurrencies ?? []\n\tif (currencies.length > 1) {\n\t\tconst named = listFormat(context.locale, { type: 'conjunction' }).format(currencies)\n\t\ttexts.push(t(keys.stateMixedCurrencies, { currencies: named }))\n\t}\n\t// A conversions number with no goals behind it is a number about nothing, which the plain\n\t// caption would read as \"nobody converted\".\n\tif (flags.goalsUnresolved) texts.push(t(keys.stateGoalsUnresolved))\n\telse if (flags.noGoals) texts.push(t(keys.stateNoGoals))\n\tif (flags.asOf) {\n\t\ttexts.push(t(keys.stateUpdated, { ago: timeAgo(flags.asOf, context.now, context.locale) }))\n\t}\n\treturn texts\n}\n\n/**\n * The caveats as one line: a card is a few centimetres tall, and notices stacked under it would\n * push the number itself out of view.\n */\nexport const widgetNotice = (flags: WidgetNoticeFlags, context: NoticeContext): string =>\n\tnoticeTexts(flags, context).join(NOTICE_SEPARATOR)\n"],"mappings":";;;;AAuBA,MAAa,mBAAmB;AAEhC,MAAM,SAAS;AACf,MAAM,SAAS,KAAK;AACpB,MAAM,OAAO,KAAK;AAElB,MAAM,kBAAkB,WACvB,mBAAmB,QAAQ,EAAE,SAAS,OAAO,CAAC;;AAG/C,MAAa,WAAW,MAAc,KAAW,WAA2B;CAC3E,MAAM,UAAU,KAAK,IAAI,GAAG,IAAI,QAAQ,IAAI,KAAK,MAAM,IAAI,CAAC;CAC5D,MAAM,SAAS,eAAe,MAAM;CACpC,IAAI,UAAU,QACb,OAAO,OAAO,OAAO,CAAC,KAAK,MAAM,UAAU,MAAM,GAAG,QAAQ;CAE7D,IAAI,UAAU,MACb,OAAO,OAAO,OAAO,CAAC,KAAK,MAAM,UAAU,MAAM,GAAG,QAAQ;CAE7D,OAAO,OAAO,OAAO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,KAAK,WAAW,CAAC;CAChD,IAAI,MAAM,SAAS,MAAM,KAAK,YAAY,OAAO,OAAO,CAAC;CACzD,IAAI,MAAM,kBAAkB,MAAM,KAAK,EAAE,KAAK,qBAAqB,CAAC;CACpE,IAAI,MAAM,SAAS,MAAM,KAAK,EAAE,KAAK,YAAY,CAAC;CAClD,MAAM,aAAa,MAAM,mBAAmB,CAAC;CAC7C,IAAI,WAAW,SAAS,GAAG;EAC1B,MAAM,QAAQ,WAAW,QAAQ,QAAQ,EAAE,MAAM,cAAc,CAAC,EAAE,OAAO,UAAU;EACnF,MAAM,KAAK,EAAE,KAAK,sBAAsB,EAAE,YAAY,MAAM,CAAC,CAAC;CAC/D;CAGA,IAAI,MAAM,iBAAiB,MAAM,KAAK,EAAE,KAAK,oBAAoB,CAAC;MAC7D,IAAI,MAAM,SAAS,MAAM,KAAK,EAAE,KAAK,YAAY,CAAC;CACvD,IAAI,MAAM,MACT,MAAM,KAAK,EAAE,KAAK,cAAc,EAAE,KAAK,QAAQ,MAAM,MAAM,QAAQ,KAAK,QAAQ,MAAM,EAAE,CAAC,CAAC;CAE3F,OAAO;AACR;;;;;AAMA,MAAa,gBAAgB,OAA0B,YACtD,YAAY,OAAO,OAAO,EAAE,KAAK,gBAAgB"}
|
|
@@ -6,6 +6,19 @@ import { PayloadRequest } from "payload";
|
|
|
6
6
|
* (a retry can fix it), `invalid` a read refused as asked, `range-too-long` over the query cap.
|
|
7
7
|
*/
|
|
8
8
|
type WidgetReadStatus = 'ok' | 'not-configured' | 'unavailable' | 'busy' | 'invalid' | 'range-too-long' | 'filter-unsupported' | 'no-access' | 'no-scope' | 'misconfigured' | 'host-refused';
|
|
9
|
+
/** What a read of a stored source (a widget's `dataSource`) may take. */
|
|
10
|
+
interface StoredSourceRead {
|
|
11
|
+
/**
|
|
12
|
+
* Read the scope's default when `adapterId` names a source this reader cannot read, saying so
|
|
13
|
+
* in `substitutedFrom`. For a stored choice only, never a source a caller just picked.
|
|
14
|
+
*/
|
|
15
|
+
fallbackWhenUnreadable?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** What a read says when it did not read the source it was asked for. */
|
|
18
|
+
interface SubstitutedSource {
|
|
19
|
+
/** The source asked for, which this reader cannot read; the scope default answered instead. */
|
|
20
|
+
substitutedFrom?: string;
|
|
21
|
+
}
|
|
9
22
|
//#endregion
|
|
10
|
-
export { WidgetReadStatus };
|
|
23
|
+
export { StoredSourceRead, SubstitutedSource, WidgetReadStatus };
|
|
11
24
|
//# sourceMappingURL=prepareWidgetRead.d.ts.map
|
|
@@ -14,6 +14,8 @@ import { goalHintFor } from "../plugin/goalHint.js";
|
|
|
14
14
|
* cannot filter says so rather than returning site-wide numbers that pass for filtered ones.
|
|
15
15
|
*/
|
|
16
16
|
const supportsFilters = (caps, filters) => !filters || unsupportedFilter(caps, filters) === void 0;
|
|
17
|
+
/** The substitution a prepared read made, spread into the helper's own result. */
|
|
18
|
+
const substitutionOf = (prepared) => prepared.substitutedFrom === void 0 ? {} : { substitutedFrom: prepared.substitutedFrom };
|
|
17
19
|
/** The engine read of a prepared source, each metric asked under the name the source serves. */
|
|
18
20
|
const engineRead = ({ runtime, adapter }, timeframe) => (query) => readAsAsked((served) => runtime.engine.read(adapter, served, { timeframe }), query, adapter.capabilities.metrics);
|
|
19
21
|
/**
|
|
@@ -46,7 +48,8 @@ const prepareWidgetRead = async (args) => {
|
|
|
46
48
|
runtime,
|
|
47
49
|
req,
|
|
48
50
|
adapterId: args.adapterId,
|
|
49
|
-
...readPrincipalOf(args)
|
|
51
|
+
...readPrincipalOf(args),
|
|
52
|
+
...args.fallbackWhenUnreadable ? { fallbackWhenUnreadable: true } : {}
|
|
50
53
|
}) : { ok: false };
|
|
51
54
|
if (!runtime || !ctx.ok) {
|
|
52
55
|
const reason = ctx.ok ? void 0 : ctx.reason;
|
|
@@ -96,11 +99,12 @@ const prepareWidgetRead = async (args) => {
|
|
|
96
99
|
pin: ctx.pin,
|
|
97
100
|
builtinGoalSlugs: args.builtinGoalSlugs ?? resolved?.builtin ?? [],
|
|
98
101
|
goalNames: resolved?.names ?? [],
|
|
102
|
+
...substitutionOf(ctx),
|
|
99
103
|
...ctx.queryScope === void 0 ? {} : { queryScope: ctx.queryScope },
|
|
100
104
|
...goalSlugs === void 0 ? {} : { goalSlugs }
|
|
101
105
|
};
|
|
102
106
|
};
|
|
103
107
|
//#endregion
|
|
104
|
-
export { comparisonWindow, engineRead, pinnedScope, prepareWidgetRead };
|
|
108
|
+
export { comparisonWindow, engineRead, pinnedScope, prepareWidgetRead, substitutionOf };
|
|
105
109
|
|
|
106
110
|
//# sourceMappingURL=prepareWidgetRead.js.map
|