WOOA7S-1495: add Authors widget backed by Jetpack Stats#49570
WOOA7S-1495: add Authors widget backed by Jetpack Stats#49570Nikschavan wants to merge 10 commits into
Conversation
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Premium Analytics plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
| * TODO: Remove the default range once we have a way to pass the launched date to the widget. | ||
| */ | ||
| const getDefaultReportParams = () => ( { | ||
| from: '2000-01-01T00:00:00', |
There was a problem hiding this comment.
Temporary hardcoded long date range to view the data in the widget, until we have date range picker in the dashboard
Code Coverage SummaryThis PR did not change code coverage! That could be good or bad, depending on the situation. Everything covered before, and still is? Great! Nothing was covered before? Not so great. 🤷 |
909996e to
d060b2d
Compare
5bb6dd9 to
7ff7f71
Compare
7ff7f71 to
22e10a7
Compare
e1f31ef to
49f10a6
Compare
|
@claude Review the PR please |
|
Claude finished @Nikschavan's task in 3m 43s —— View job PR Review — Authors widgetReviewed the 11 files in this commit. Note: the data package ( 🐛 Correctness / i18n1. "Untracked authors" never shows the translated string — label: item.name || 'Untracked Authors',So by the time data reaches 2. Authors are merged by display label — 🔧 Type safety3. 📝 Minor / consistency4. 5. 💬 Answers to your inline questions
✅ Nice touches
Note: the widget-dashboard CSS / data-package / devtools changes from the PR description aren't in this commit, so they weren't reviewed here. |
There was a problem hiding this comment.
Pull request overview
Adds a new Authors widget to the Jetpack Premium Analytics dashboard, using the Jetpack Stats “top-authors” endpoint and a new widget/toolkit implementation to render a leaderboard with comparison support.
Changes:
- Register new
jpa/authorswidget metadata (type/module JSON) with a configurablemaxattribute and a render entry usingWidgetRoot. - Add
AuthorsWidgetto the widgets-toolkit plus abuildTopAuthorsDatahelper to transform Stats responses into leaderboard chart data. - Export the new widget/helper from the widgets-toolkit public entrypoints and add a changelog entry.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| projects/packages/premium-analytics/widgets/authors/widget.ts | Defines the widget type (name/title/icon) and its max attribute. |
| projects/packages/premium-analytics/widgets/authors/widget.json | Widget picker metadata (title/description/category). |
| projects/packages/premium-analytics/widgets/authors/render.tsx | Render entry wiring WidgetRoot + AuthorsWidget, including a wide default report range. |
| projects/packages/premium-analytics/widgets/authors/package.json | Declares per-widget package dependencies. |
| projects/packages/premium-analytics/packages/widgets-toolkit/src/widgets/index.ts | Exports AuthorsWidget from the widgets toolkit widget barrel. |
| projects/packages/premium-analytics/packages/widgets-toolkit/src/widgets/authors/index.ts | Authors widget module re-export. |
| projects/packages/premium-analytics/packages/widgets-toolkit/src/widgets/authors/authors-widget.tsx | Main widget component: fetches top authors and renders a leaderboard with comparison/empty/loading/error handling. |
| projects/packages/premium-analytics/packages/widgets-toolkit/src/index.ts | Exposes AuthorsWidget from the toolkit public API. |
| projects/packages/premium-analytics/packages/widgets-toolkit/src/helpers/index.ts | Exports buildTopAuthorsData helper. |
| projects/packages/premium-analytics/packages/widgets-toolkit/src/helpers/build-top-authors-data.ts | Converts Stats “top-authors” report(s) into LeaderboardChart-ready data with deltas/shares. |
| projects/packages/premium-analytics/changelog/update-pa-authors-widget | Changelog entry for introducing the Authors widget. |
4eb1e79 to
36d5586
Compare
fe70374 to
017a5ac
Compare
chihsuan
left a comment
There was a problem hiding this comment.
Nice work on the port! @Nikschavan
A couple of things before merge:
- This PR doesn't include a Storybook story —
AGENTS.md asksevery widget to ship one. Could you add it? - The description mentions a
withDevtoolsprop and a picker-preview change, but I don't see those in the diff. Did they land separately on rebase? Mind updating the description to match?
Also left a couple of inline comments.
| * <AuthorsWidget /> | ||
| * </WidgetRoot> | ||
| */ | ||
| export function AuthorsWidget( { max }: AuthorsWidgetProps ) { |
There was a problem hiding this comment.
Could we move AuthorsWidget into widgets/authors/render.tsx? widgets-toolkit/src/widgets/* is for the legacy (unmigrated) widgets and not to use them as templates. The newer stats ports keep the body in widgets/<name>/render.tsx e.g. locations defines a LocationsInner there or #49568 we defines a TopPostsLeaderboard.
There was a problem hiding this comment.
Moved it. The body now lives in widgets/authors/render.tsx as a local AuthorsLeaderboard component, mirroring LocationsInner and the TopPostsLeaderboard in #49568, and I dropped the old widgets-toolkit/src/widgets/authors/ entirely. The buildTopAuthorsData helper (and its test) moved into the widget folder too, so the toolkit barrels no longer carry anything authors-specific. render.tsx#L70
afac0cf to
3f8c8f0
Compare
| @@ -0,0 +1,109 @@ | |||
| /** | |||
There was a problem hiding this comment.
Maybe we could coordinate those changes into https://github.com/Automattic/jetpack/blob/trunk/projects/packages/premium-analytics/packages/data/src/processing/stats/top-authors.ts, so that there's no need for more mappings?
| return Number.isFinite( parsed ) && parsed > 0 ? parsed : fallback; | ||
| }; | ||
|
|
||
| const toDateString = ( date: Date ) => { |
There was a problem hiding this comment.
Could toDateString defer to date-fns? It hand-rolls format(date, 'yyyy-MM-dd') I think date-fns or the datetime package should provide similar feature.
| const hasReportParams = | ||
| !! attributes.reportParams && Object.keys( attributes.reportParams ).length > 0; | ||
|
|
||
| return hasReportParams ? attributes : { ...attributes, reportParams: getDefaultReportParams() }; |
There was a problem hiding this comment.
Non-blocking - since this is probably not an issue once we pass the launch date to the widget.
It looks like getDefaultReportParams returns from: 2000-01-01, to: today, interval: 'day', which maps to period=day with days≈9673 plus summarize:1. That would ask WPCOM to summarize roughly 9,600 daily buckets. It should likely be fine since our dev sites probably do not have much data, but I wanted to flag it anyway.
- widgets-toolkit: add AuthorsWidget (consumes the useStatsTopAuthors hook from the data package) and the buildTopAuthorsData leaderboard helper - widgets/authors: register the jpa/authors dashboard widget; render.tsx fetches via the existing /jetpack-premium-analytics/v1/proxy Stats proxy (v1.1 stats/top-authors) and wraps the widget in WidgetRoot - query client provider: add withDevtools prop so per-widget providers don't each render React Query devtools - widget-dashboard: inset picker previews with padding instead of scaling so content clears the selection checkbox
- Forward `max` to the Stats top-authors query so the API limit matches the rendered leaderboard instead of relying on the endpoint default. - Dedup and align authors by stable `author_id` (falling back to the display label) so distinct authors sharing a name aren't merged. - Translate the "Untracked authors" label in a single place: the processing layer leaves an empty label and the widget supplies the translated fallback. - Drop `StatsProxyParams`' index signature from `StatsReportParams` so `reportParams` no longer needs casting; make `useStatsReport` generic so `useStatsTopAuthors` returns a typed report (removes data casts). - Declare the widget's runtime deps (widgets-toolkit, @wordpress/i18n, @wordpress/icons) in its package.json. - Forward `setError` from the render entry to WidgetRoot.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
The empty-state icon was dropped earlier but the import was left behind, failing ESLint and the exclude-list check in CI.
- Declare presentation (framed) in widget.json.
- Type render props via WidgetRenderProps<AuthorsAttributes> from
@wordpress/widget-primitives and default attributes to {}.
- Add @wordpress/widget-primitives as a devDependency (type-only import,
erased at build, keeps runtime deps host-agnostic).
Relocate buildTopAuthorsData and the authors render logic out of the shared widgets-toolkit package into widgets/authors, colocating the widget's tests. Wire the widget to its own dependencies, change the max authors field to integer, and stub CSS imports in jest so widgets-toolkit CSS doesn't get parsed as JS.
Extract AuthorsLeaderboard as an exported presentational component taking pre-built rows, and rename the data-connected wrapper to AuthorsReport so Storybook can exercise loading/empty/populated states. Trust the Stats API's server-side ranking and limiting: replace summarizeAuthors' cross-interval aggregation and sorting with a flat toAuthorItems, dropping the maxEntries param. Update tests accordingly.
3f8c8f0 to
be5e7a9
Compare
Proposed changes
Ports the Authors widget from the Jetpack Stats module into the Premium Analytics dashboard, backed by the Jetpack Stats API.
stats-fetchAPI client that goes through the stats-admin proxy (/jetpack/v4/stats-app/sites/{blog_id}/stats/{resource}), plusstatsQuery/statsTopAuthorsQueryquery builders anduseStatsTopAuthors/useStatsBlogIdhooks. The blog ID is resolved once from/jetpack/v4/connection/dataand cached for the page lifetime; queries useskipTokenwhile the blog ID or report date is unresolved.AuthorsWidget(leaderboard chart of top authors by views with comparison support) and abuildTopAuthorsDatahelper, with loading/refetching overlays and empty/error states.jpa/authorsdashboard widget (render entry,widget.json,widget.tswith a configurablemaxattribute). The render entry defaults to an all-time report range until the host can pass the site launch date.withDevtoolsprop so per-widget providers don't each render React Query devtools.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
/jetpack/v4/stats-app/sites/{blog_id}/stats/top-authors).Maximum authorsattribute and verify the list respects the limit.