Skip to content

Conversation

@ajotka
Copy link
Contributor

@ajotka ajotka commented Oct 16, 2024

Motivation for the change, related issues

Issue #1902

Implementation details

Import & Export from Github causes reloading the playground even before accept this step.
I know that it is because of adding new params in the URL.
So I kept functionality to fire those modal via URL, but I also added logic to open those modals without adding params.

Testing Instructions (or ideally a Blueprint)

  1. open http://127.0.0.1:5400/website-server/
  2. Go to playground settings
  3. Click 3 dots next to "Homepage" button
  4. Click export or import from GitHub
  5. Modal should appear without reloading

Also you can check, that opening on slug is still working (but with reloading on closing popup):
http://127.0.0.1:5400/website-server/?modal=github-import

@ajotka ajotka requested a review from a team as a code owner October 22, 2024 11:34
@ajotka
Copy link
Contributor Author

ajotka commented Oct 22, 2024

@adamziel ready to check

@adamziel adamziel mentioned this pull request Oct 30, 2024
@ajotka
Copy link
Contributor Author

ajotka commented Oct 30, 2024

@adamziel I have kept the functionality of opening the modal on the URL parameter, but I have also added a lock to not reload WordPress if only the 'modal' parameter changes in the URL parameters. And also, there is no signal() anymore.

So you can use this functionality for other modals as well, or extend it with other searchParams keys.

Ready to re-check.

Comment on lines 88 to 94
const notRefreshingParam = 'modal';
const oldParams = new URLSearchParams(prevUrl?.search);
const newParams = new URLSearchParams(url?.search);

if ((!oldParams.has(notRefreshingParam) && newParams.has(notRefreshingParam)) || (oldParams.has(notRefreshingParam) && !newParams.has(notRefreshingParam))) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

This confirms that the modal parameter was added or removed from the params, but it doesn't prove that modal is the only param that changed.

What if we say, "Don't reload the page if nothing changed other than the modal param"? Because URLSearchParams should preserve parameter order and render the query strings consistently, I think we can say that like this:

Suggested change
const notRefreshingParam = 'modal';
const oldParams = new URLSearchParams(prevUrl?.search);
const newParams = new URLSearchParams(url?.search);
if ((!oldParams.has(notRefreshingParam) && newParams.has(notRefreshingParam)) || (oldParams.has(notRefreshingParam) && !newParams.has(notRefreshingParam))) {
return;
}
const oldParams = new URLSearchParams(prevUrl?.search);
const newParams = new URLSearchParams(url?.search);
oldParams.delete('modal');
newParams.delete('modal');
if (oldParams.toString() === newParams.toString()) {
return;
}

That way, we ignore changes to the modal param while reloading for all other param changes.

Copy link
Member

@brandonpayton brandonpayton Oct 30, 2024

Choose a reason for hiding this comment

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

What if we say, "Don't reload the page if nothing changed other than the modal param"?

😄 sorry, that was always the intent here. It didn't need restated, but it was intended to be in contrast to the old logic, which actually said "Don't reload the page if modal was in one set but not the other."

Copy link
Contributor Author

@ajotka ajotka Oct 31, 2024

Choose a reason for hiding this comment

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

If you received the previous comment via email, please ignore it.
I had a coffee and realized that this is a good fix/improvement. 😅

Changes ready for review.

Copy link
Member

Choose a reason for hiding this comment

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

😄 all good!

Copy link
Member

@brandonpayton brandonpayton left a comment

Choose a reason for hiding this comment

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

// Lean on the Query API parameters and the Blueprint API to
// create the new site.
const url = new URL(window.location.href);
const newUrl = new URL(window.location.href);
Copy link
Member

Choose a reason for hiding this comment

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

Nice

);
closeModal();
onImported?.(details);
closeModal();
Copy link
Member

Choose a reason for hiding this comment

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

This seems like a good change because we'll have a chance to notice the modal remaining open if an error is thrown.


export const usePrevious = <T>(value: T): T | undefined => {
const ref = useRef<T>();
useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

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

This is a fun use of useEffect to get the previous value. Nice!

const newParams = new URLSearchParams(url?.search);
oldParams.delete(notRefreshingParam);
newParams.delete(notRefreshingParam);
if (oldParams.toString() === newParams.toString()) {
Copy link
Member

Choose a reason for hiding this comment

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

It turns out that this prevents us from automatically creating a temporary site when no other site is selected.
Screenshot 2024-10-31 at 11 27 10 AM

So it looks like, we need to add another condition:
Only avoid reloading the page when there is already an active Playground.

I suppose this makes sense. :)

Copy link
Member

Choose a reason for hiding this comment

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

Would you mind if I just make that change and land this PR?

Copy link
Member

Choose a reason for hiding this comment

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

Would you mind if I just make that change and land this PR?

I meant to add: Please also feel free to do it. Whatever you want. That would be fine too. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@brandonpayton go ahead! Thanks :)

Copy link
Member

Choose a reason for hiding this comment

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

Done! c06b09b

Once the tests pass, let's go ahead and merge this.

@brandonpayton brandonpayton merged commit 1e51248 into WordPress:trunk Oct 31, 2024
10 checks passed
@brandonpayton
Copy link
Member

Thanks for your contribution, @ajotka!

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

Labels

None yet

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants