Skip to content

Search Dashboard: harden plan-info renders against missing API data#48660

Open
CGastrell wants to merge 1 commit intotrunkfrom
fix/search-dashboard-harden-plan-info-renders
Open

Search Dashboard: harden plan-info renders against missing API data#48660
CGastrell wants to merge 1 commit intotrunkfrom
fix/search-dashboard-harden-plan-info-renders

Conversation

@CGastrell
Copy link
Copy Markdown
Contributor

Fixes pre-existing Cannot read properties of undefined crashes in the Search dashboard when state.sitePlan.plan_usage / plan_current haven't fully populated yet.

Proposed changes

  • Selector: projects/packages/search/src/dashboard/store/selectors/site-plan.jsgetLatestMonthRequests had state.sitePlan.plan_usage?.num_requests_3m[ 0 ]. The optional chain on plan_usage is undermined by the unguarded [ 0 ] — when plan_usage (or num_requests_3m) is undefined, accessing undefined[ 0 ] throws "Cannot read properties of undefined (reading '0')". Add the missing ?. between num_requests_3m and [ 0 ].
  • <PlanSummary>: displayPeriodFromAPIData reads apiData.latestMonthRequests.start_date and .end_date unconditionally. Bail to null if either date is missing; the <h2> keeps rendering, just without the date span.
  • <PlanUsageSection>: usageInfoFromAPIData had apiData?.currentPlan.monthly_search_request_limit — missing ?. on .currentPlan (the three sibling lines had it). Same crash class. Add the ?..

Why this matters

The Search dashboard renders <PlanInfo><FirstRunSection><PlanSummary> and <PlanUsageSection> whenever isNewPricing && supportsInstantSearch. These components reach into the redux sitePlan state without fully guarding against pieces being undefined while the resolver fetches the wpcom plan response. When any of the three landmines fires, React unmounts the entire dashboard subtree, the <h1> heading disappears, and the page renders blank — which is exactly what made the Search e2e dashboard test ("Title should be visible") flaky against the partner-provisioned 'free' plan that the e2e infra uses.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No. Defensive guards only.

Testing instructions

  1. On a connected Jetpack site, deactivate any paid Search subscription and visit /wp-admin/admin.php?page=jetpack-search.
  2. Before this PR: with ?new_pricing_202208=1 (or wherever isNewPricing && supportsInstantSearch is true), if the wpcom plan endpoint hasn't fully resolved yet, the dashboard renders blank — main content area is empty, only WP-admin chrome shows. Console error: Uncaught TypeError: Cannot read properties of undefined (reading '0') traced to <PlanInfo>.
  3. After this PR: the dashboard renders with <PlanSummary>'s <h2> showing just the plan name (e.g., "(Free plan)") when the date period isn't available, the donut meters show 0 / 0, and no React-tree crash. The <h1> from <MockedSearchContent> is visible — Search e2e's getByRole('heading', { name: 'Help your visitors find' }) finds it.

A console probe to confirm:

console.log( document.querySelector('#jp-search-dashboard')?.children?.length );
// Before fix: 0 (React unmounted)
// After fix:  1 (React tree mounted)

No visual change for sites where the wpcom plan is fully populated — the guards just keep the existing render path.

The Search dashboard renders `<PlanInfo>` → `<FirstRunSection>` →
`<PlanSummary>` and `<PlanUsageSection>` whenever `isNewPricing &&
supportsInstantSearch`, and these components reach into the redux
`sitePlan` state without fully guarding against pieces being
undefined while the data resolves. Three pre-existing landmines:

- `getLatestMonthRequests` selector:
  `state.sitePlan.plan_usage?.num_requests_3m[ 0 ]` had an unguarded
  `[0]` after the optional chain — when `plan_usage` (or
  `num_requests_3m`) is undefined, accessing `undefined[0]` throws
  "Cannot read properties of undefined (reading '0')". Add the
  missing `?.` between `num_requests_3m` and `[0]`.

- `displayPeriodFromAPIData` in plan-summary.jsx:
  `apiData.latestMonthRequests.start_date` blew up the same way when
  `latestMonthRequests` was undefined. Bail to `null` if either
  date is missing; the `<PlanSummary>` `<h2>` now renders without the
  date span when data isn't available yet.

- `usageInfoFromAPIData` in plan-usage-section.jsx:
  `apiData?.currentPlan.monthly_search_request_limit` was missing the
  `?.` on `.currentPlan` (the other three fields above it had it).
  Same crash class. Add the `?.`.

These bugs predate this PR and are independent of the notice
migration. They surface intermittently in CI because the JN test
site's API population timing varies — the dashboard mounts and the
selector evaluates before the API data resolves. Without the fix,
the React tree blows up and the dashboard renders blank, which is
why Search e2e (`Title should be visible` looking for `<h1>` in
`MockedSearchContent`) was failing. Verified locally: with these
guards, the dashboard renders cleanly with no console errors even
when `plan_usage` is empty.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 8, 2026

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/search-dashboard-harden-plan-info-renders branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/search-dashboard-harden-plan-info-renders

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions github-actions Bot added the [Package] Search Contains core Search functionality for Jetpack and Search plugins label May 8, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 8, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • 🔴 Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions Bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label May 8, 2026
@jp-launch-control
Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/search/src/dashboard/components/pages/sections/plan-summary.jsx 3/16 (18.75%) -4.33% 3 ❤️‍🩹

Full summary · PHP report · JS report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Search Contains core Search functionality for Jetpack and Search plugins [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant