-
Notifications
You must be signed in to change notification settings - Fork 25
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
Conversation
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 I would say this edge-case is expected behavior because when the preview site is being created Let's also see what Antonio thinks about this as well. |
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 👍 |
There was a problem hiding this 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?
src/hooks/use-archive-site.ts
Outdated
@@ -158,6 +164,7 @@ export function useArchiveSite() { | |||
nextSequence | |||
), | |||
sequence: nextSequence, | |||
userId: user.id ?? undefined, |
There was a problem hiding this comment.
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.
userId: user.id ?? undefined, | |
userId: user.id, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to #937 (comment).
src/hooks/use-archive-site.ts
Outdated
@@ -74,7 +80,7 @@ export function useArchiveSite() { | |||
const errorMessages = useArchiveErrorMessages(); | |||
const archiveSite = useCallback( | |||
async ( siteId: string ) => { | |||
if ( ! client ) { | |||
if ( ! client || ! user ) { |
There was a problem hiding this comment.
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.
if ( ! client || ! user ) { | |
if ( ! client ) { |
There was a problem hiding this comment.
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
Thank you for your reviews Kat and Antonio!
Sounds good. 👍🏼 I will make the change tomorrow, alongside the other changes you have suggested. 🙂 |
I have made the change in 5ff1f6f and 68855c3. When the appdata loads, it will populate the I have also removed checks that accepted snapshots without The PR is now ready for another review round. 🙂 |
There was a problem hiding this 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.
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; | ||
} ); | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ); |
There was a problem hiding this comment.
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. ✨
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My pleasure! 🙂
Related issues
Proposed Changes
userId
to the snapshot object as the new preview site is createduserId
to all snapshots that are missing whenappdata
is getting loadedclearFloatingSnapshots
that is no longer neededTesting Instructions
To test the changes in this PR, we will need two WPCOM accounts.
appdata-v1.json
. The newly created preview site should have a newuserId
value that equals to your WPCOM user ID:appdata-v1.json
. The newly created preview site should have a newuserId
value - but this time it should equal to your second WPCOM user ID (that you are currently logged into).userId
value from any of your existing preview sites in theappdata-v1.json
, the value should get added automatically on the nextappdata-v1.json
load, e.g. when you restart the app. The userId of a currently-logged-in user will be used.The snapshot sequence number should be calculated for each user and site separately.
Pre-merge Checklist