Skip to content

Remove user-data-updated listener in useSiteDetails#2164

Merged
fredrikekelund merged 2 commits intotrunkfrom
f26d/remove-user-data-listener-use-site-details
Dec 2, 2025
Merged

Remove user-data-updated listener in useSiteDetails#2164
fredrikekelund merged 2 commits intotrunkfrom
f26d/remove-user-data-listener-use-site-details

Conversation

@fredrikekelund
Copy link
Contributor

@fredrikekelund fredrikekelund commented Dec 2, 2025

Related issues

Proposed Changes

The data flow in and around useSiteDetails when creating a new site is pretty gnarly. Previously, we were unintentionally relying on the user-data-updated listener to refresh the state after writing to the appdata file. When I removed this listener in #2148, it caused a regression in which the isAddingSite property on the new site never cleared (which meant the loading screen would display indefinitely). @sejas merged a hotfix for this in #2161.

The user-data-updated listener is meant to let Studio pick up changes from the CLI. We should not rely on it for flows that run entirely in Studio, where we already have all the state easily accessible in memory.

This PR removes the user-data-updated listener again and fixes the underlying problem by:

  1. Not mutating the selectedSite object in the useMemo callback (this is where we previously set the isAddingSite property, but we didn't have any explicit logic to unset it)
  2. Updating the createSite function in useSiteDetails to accept a phpVersion argument. Previously, we would set this in a callback function in useAddSite that included a state update inuseSiteDetails (which would unset isAddingSite flag).
  3. Removing the addingSiteIds state and instead explicitly setting and unsetting the isAddingSite flag on the site object in the createSite function.
  4. Bonus: not using useMemo to store selectedSite. The way we did this had no effect.
  5. Bonus: renaming the site list state from data to sites in useSiteDetails.

Testing Instructions

  1. Create a new site with all the default settings
  2. Ensure that the flow behaves as before and that it finishes normally
  3. Create another site with a PHP version different than the default
  4. Ensure that the process finishes normally and that the PHP version was persisted correctly (check the Settings tab)
  5. Create a site with a blueprint
  6. Ensure that the process finishes normally
  7. Create a site from an existing remote site (see pdtkmj-4cM-p2)
  8. Ensure that the process finishes normally

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

@fredrikekelund fredrikekelund requested review from a team, bcotrim and sejas December 2, 2025 09:07
@fredrikekelund fredrikekelund self-assigned this Dec 2, 2025
const selectedSite = useMemo( () => {
const site = data.find( ( site ) => site.id === selectedSiteId ) || firstSite;
if ( addingSiteIds.includes( site?.id ) ) {
site.isAddingSite = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we previously mutated the site object to add the isAddingSite property, but we didn't have any explicit logic to unset it.

Comment on lines -130 to -141
let updatedSite = { ...newSite };

if ( newSite.phpVersion !== phpVersion ) {
updatedSite = {
...updatedSite,
phpVersion,
};
}

if ( updatedSite !== newSite ) {
await updateSite( updatedSite );
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we previously set the PHP version and updated the site list state in useSiteDetails by calling updateSite.

I removed this logic and opted for passing the PHP version as an argument to createSite instead.

Comment on lines +307 to +313
setSites( ( prevData ) =>
prevData.map( ( site ) => {
if ( site.id === newSite.id ) {
return { ...newSite, isAddingSite: true };
}
return site;
} )
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call—and the equivalent one just below this in the diff—is what replaces the addingSiteIds state.

Comment on lines -483 to -489
const selectedSite = useMemo( () => {
const site = data.find( ( site ) => site.id === selectedSiteId ) || firstSite;
if ( addingSiteIds.includes( site?.id ) ) {
site.isAddingSite = true;
}
return site;
}, [ addingSiteIds, data, firstSite, selectedSiteId ] );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After removing the site.isAddingSite = true assignment, there was no longer any point in using useMemo here (see this classic blog post)

@github-actions
Copy link
Contributor

github-actions bot commented Dec 2, 2025

📊 Performance Test Results

Comparing 55e7d67 vs trunk

site-editor

Metric trunk 55e7d67 Diff Change
load 8745.00 ms 11062.00 ms +2317.00 ms 🔴 26.5%

site-startup

Metric trunk 55e7d67 Diff Change
siteCreation 18578.00 ms 24975.00 ms +6397.00 ms 🔴 34.4%
siteStartup 9005.00 ms 10055.00 ms +1050.00 ms 🔴 11.7%

Results are median values from multiple test runs.

Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change

Copy link
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for creating the proper solution. I confirm the Add site creation works as expected, I also tested editing a the site name and PHP version. Also, thanks for the bonuses. The code is much cleaner.

@fredrikekelund fredrikekelund merged commit 7b2165b into trunk Dec 2, 2025
14 checks passed
@fredrikekelund fredrikekelund deleted the f26d/remove-user-data-listener-use-site-details branch December 2, 2025 13:01
@bcotrim
Copy link
Contributor

bcotrim commented Dec 2, 2025

Nice cleanup! 👍

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants