Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview Sites: Avoid removing snapshots when switching between users #937

Merged
merged 13 commits into from
Feb 18, 2025

Conversation

ivan-ottinger
Copy link
Contributor

@ivan-ottinger ivan-ottinger commented Feb 14, 2025

Related issues

Proposed Changes

  • add userId to the snapshot object as the new preview site is created
  • automatically add current userId to all snapshots that are missing when appdata is getting loaded
  • add filter that will ensure only snapshots linked with the logged-in user get loaded on the Previews tab
  • remove clearFloatingSnapshots that is no longer needed

Testing Instructions

To test the changes in this PR, we will need two WPCOM accounts.

  1. While logged-in to one of your WPCOM account in Studio, review your current preview sites.
  2. Create a new preview site.
  3. Take a look into appdata-v1.json. The newly created preview site should have a new userId value that equals to your WPCOM user ID:

Markup on 2025-02-14 at 11:26:59

  1. Log out from your WPCOM account in Studio and log in to your another WPCOM account.
  2. When you review the existing preview sites in Studio, the preview site that you created in the step 2 should not load in the Preview tab of the related local site.
  3. Create another preview site while still logged-in to your second WPCOM account.
  4. Take a look into appdata-v1.json. The newly created preview site should have a new userId value - but this time it should equal to your second WPCOM user ID (that you are currently logged into).
  5. Log out from your second WPCOM account in Studio and log in to your first WPCOM account.
  6. When you review the existing preview sites in Studio, you should see the preview site that you have created in the step 2. The preview site that you created with your second account in the step 6 should not load in the Preview tab of the related local site.
  7. If you manually remove userId value from any of your existing preview sites in the appdata-v1.json, the value should get added automatically on the next appdata-v1.json load, e.g. when you restart the app. The userId of a currently-logged-in user will be used.
  8. You can try other cases / variations as well.

The snapshot sequence number should be calculated for each user and site separately.

Pre-merge Checklist

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

@ivan-ottinger ivan-ottinger self-assigned this Feb 14, 2025
@ivan-ottinger ivan-ottinger requested a review from a team February 14, 2025 10:47
@katinthehatsite
Copy link
Contributor

The switching between accounts worked for me! I noticed one other small thing - if I start creating a site with the user account A and then switch to user account B, I would still see the loading state. I have not investigated in detail though, it might be a different issue. I can give it another test on Monday.

@ivan-ottinger
Copy link
Contributor Author

The switching between accounts worked for me! I noticed one other small thing - if I start creating a site with the user account A and then switch to user account B, I would still see the loading state. I have not investigated in detail though, it might be a different issue. I can give it another test on Monday.

Great observation!

I can see why this is happening.

When the first user initiates the preview site creation and then quickly logs out and then logs in with another user before the preview site creation is finished, the progress bar will still display itself until the site is created.

This is because the isUploading gets its value from isUploadingSiteId callback that is tied with the local site ID instead of the snapshot being created.

I would say this edge-case is expected behavior because when the preview site is being created isUploading should return true for any user that is logged in at the moment - since we want to block creating new preview sites at that time. If the user switches their accounts so quickly before the preview site creation is finished, it should be clear that the process is still finishing - since it is tied to that same local site.

Let's also see what Antonio thinks about this as well.

@katinthehatsite
Copy link
Contributor

I would say this edge-case is expected behavior because when the preview site is being created isUploading should return true for any user that is logged in at the moment - since we want to block creating new preview sites at that time. If the user switches their accounts so quickly before the preview site creation is finished, it should be clear that the process is still finishing - since it is tied to that same local site.

Yeah, makes sense. Also, I do not think the user would be switching when the process is running as this would not be a logical user behaviour so it would indeed be more of an edge case 👍

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.

@ivan-ottinger , thanks for creating the PR. It works as expected and now I can see the preview sites for my user.
The expired sites are not automatically removed.
The sequence number takes into account the userId ✨ .

My only concern is something I suggested in the issue.

If snapshots don't have userId because were created before this PR, then I think it's ok to include them in the list of previews, as they'll eventually expire.

After testing the PR, I think it would be better if we assign the current userId to old snapshots in appData when Studio starts. What do you think?

@@ -158,6 +164,7 @@ export function useArchiveSite() {
nextSequence
),
sequence: nextSequence,
userId: user.id ?? undefined,
Copy link
Member

Choose a reason for hiding this comment

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

Let's make sure user.id always exists when archiving a site.

Suggested change
userId: user.id ?? undefined,
userId: user.id,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Related to #937 (comment).

@@ -74,7 +80,7 @@ export function useArchiveSite() {
const errorMessages = useArchiveErrorMessages();
const archiveSite = useCallback(
async ( siteId: string ) => {
if ( ! client ) {
if ( ! client || ! user ) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's check if user?.id has a value and if not, we can display a generic error to the user, suggesting to re-authenticate.

Suggested change
if ( ! client || ! user ) {
if ( ! client ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice idea! Thanks! What do you think about a change like this? ff18ea9

@ivan-ottinger
Copy link
Contributor Author

Thank you for your reviews Kat and Antonio!

After testing the PR, I think it would be better if we assign the current userId to old snapshots in appData when Studio starts. What do you think?

Sounds good. 👍🏼 I will make the change tomorrow, alongside the other changes you have suggested. 🙂

@ivan-ottinger
Copy link
Contributor Author

After testing the PR, I think it would be better if we assign the current userId to old snapshots in appData when Studio starts. What do you think?

I have made the change in 5ff1f6f and 68855c3. When the appdata loads, it will populate the userId value for all snapshots that are missing it.

I have also removed checks that accepted snapshots without userId: 9b31ef9.

The PR is now ready for another review round. 🙂

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.

@ivan-ottinger , great work!
I've tested it and I confirm that now the preview sites appear under the user who created it.
We've removed clearFloatingSnapshots, so no preview sites are removed from appdata unless the user delete the preview site or they expire and the user clears the row.
For former demo sites without userId assigned we run a function when Studio starts that populates that value with the current authenticated user.

Comment on lines +49 to +60
function populateSnapshotUserIds( data: UserData ): void {
const userId = data.authToken?.id;

if ( userId && data.snapshots ) {
data.snapshots = data.snapshots.map( ( snapshot ) => {
if ( ! snapshot.userId ) {
return { ...snapshot, userId };
}
return snapshot;
} );
}
}
Copy link
Member

Choose a reason for hiding this comment

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

We are adding this function that assigns a userId to pre-existing snapshots so they appear to in the previews tab.
We can consider removing this code after a few weeks/months as those preview sites would expire.
Before this PR, if a user logged out the snapshots disappeared from Studio.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

→ I have created a new task so we don't forget about the removal: https://github.com/Automattic/dotcom-forge/issues/10484.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wojtek suggested that we could rename the function and add a little bit of context instead of leaving the task hanging, so I updated the task and created related PR: #954.

@@ -55,6 +68,8 @@ export async function loadUserData(): Promise< UserData > {
try {
const parsed = JSON.parse( asString );
const data = fromDiskFormat( parsed );

populateSnapshotUserIds( data );
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for isolating the code into this function. ✨

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My pleasure! 🙂

@sejas sejas merged commit 77e11a2 into trunk Feb 18, 2025
10 checks passed
@sejas sejas deleted the update/keep-snapshots-between-users branch February 18, 2025 14:02
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