Skip to content

fix(site): maintain initial workspace list order #7590

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

Closed
wants to merge 1 commit into from

Conversation

rodrimaia
Copy link
Contributor

addresses the issue of annoying visual reorders in the workspace list by maintaining the initial order of workspaces fetched from the backend while handling updates, additions, and removals of workspaces in the list.

Changes:

  1. Added a new state displayedWorkspaces to store the workspaces to be displayed on the screen.
  2. Implemented a useEffect hook to update the displayedWorkspaces state based on the latest data fetched from the backend.
  3. Updated the merging logic to:
    • Keep the order of the initially fetched workspaces.
    • Update the workspaces' data without changing their position in the list.
    • Add new workspaces to the beginning of the list.
    • Remove workspaces from the list if they are not present in the fetched data.

I usually try to avoid using useEffectas much as I can, but I believe this is an ok use-case for it.

Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

I'm not quite sure if it's more a hack or a good fix, so I will leave the final decision to Bruno or Ben.

EDIT:

Please link the original issue in the PR description.

if (fetchedWorkspaces) {
if (displayedWorkspaces.length === 0) {
setDisplayedWorkspaces(fetchedWorkspaces)
} else {
Copy link
Member

Choose a reason for hiding this comment

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

nit: I have a feeling that this can be refactored into more like a fluent approach.

Comment on lines +56 to +60
fetchedWorkspaces.forEach((fetchedItem) => {
if (!mergedItems.some((item) => item?.id === fetchedItem.id)) {
mergedItems.unshift(fetchedItem)
}
})
Copy link
Member

Choose a reason for hiding this comment

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

Ok, I understand the "hack", but I'm curious how it behaves while changing pages or playing with the filter bar.

// If the fetched item already exists, update its data without changing its position
return { ...item, ...fetchedItem }
})
.filter((item) => item !== null) // Remove the removed items (null values)
Copy link
Member

Choose a reason for hiding this comment

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

nit: "Remove the removed" sounds weird :)

if (displayedWorkspaces.length === 0) {
setDisplayedWorkspaces(fetchedWorkspaces)
} else {
// Merge the fetched workspaces with the displayed onws, without changing the order of the existing items
Copy link
Member

Choose a reason for hiding this comment

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

nit: displayed ones

@BrunoQuaresma
Copy link
Collaborator

I'm not sure how this would fix the issue since the BE paginate the results so the order only would work per page which is strange IMO. For me, the fix on FE looks very fragile.

@mtojek
Copy link
Member

mtojek commented May 18, 2023

It would be valuable to pull @kylecarbs into this chat. It looks like we're fixing "regression" (not exactly regression) introduced in #6984.

@rodrimaia
Copy link
Contributor Author

I'm not sure how this would fix the issue since the BE paginate the results so the order only would work per page which is strange IMO. For me, the fix on FE looks very fragile.

@BrunoQuaresma Yeah, what we want here is to remove the flickering of workspaces reordering every 5 seconds because the LastUsedAt workspace is always on top. the subsequent fetches will not change the order of the visible elements. I am not attempting to sort it again or apply a different other.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

Couple of things:

  1. Munging the list is a bit of a hacky workaround

  2. I'd be happier if the sorting logic could be extracted to a pure function, but this doesn't seem easy with useEffect().

  3. The original issue does not really change the sorting logic as far as I can tell.

The actual issue as far as I can see is that each actively-used workspaces has its last_used_at continuously updated. Because we sort by last_used_at by default, this means that for looking at workspaces belonging to a single u ser, the ordering issue is less noticeable (users will probably only use one space at a time). However, showing active workspaces from all users (or all workspaces) will definitely result in this ordering behaviour.

What if, instead, we modify the ordering on the backend to order by user_id first (guaranteed stable ordering) and last_used_at second (not stable, but is likely to remain so with the ordering)?

That way, we don't need a complicated frontend hack. There may be some bouncing if a single user has multiple active workspaces.

Alternatively, we order by created_at, end of story.

@BrunoQuaresma
Copy link
Collaborator

I see but we can properly fix that by just changing the sorting in the BE to not use the "latest used at" field because it changes very frequently. IMO, we just sort by name which is very predictable and does not change frequently.

@rodrimaia
Copy link
Contributor Author

I am in favor of showing the last used at first because if I just create a workspace I would not expect to go to other pages (or scrolling) to find it in the workspace list. But I agree that for admins (who I believe would like to watch the list for all workspaces more often), the current order can be bothersome.

IMO the ranking of possible solutions (from best to worst) would be:
1 - Sort in the BE by (in this order): Status (running first), then user_id, then last_used_at.
2 - Decrease the refetch frequency,
3 - Stop the bleeding: considering all recent LastUsedAt as the same avoiding reordering the page (this PR)
4 - implement every column as sortable (asc and desc), allowing multiple sortable parameters.
5 - Having a fixed order that does not allow last_used_at to be on the first results.

@mtojek
Copy link
Member

mtojek commented May 18, 2023

Thanks for the summary, Rodrigo. I wouldn't spend much time debating on options. It looks like option 1. has the majority :) Would you mind jumping into backend area?

@rodrimaia
Copy link
Contributor Author

rodrimaia commented May 18, 2023

Thanks for the summary, Rodrigo. I wouldn't spend much time debating on options. It looks like option 1. has the majority :) Would you mind jumping into backend area?

@mtojek getting my backend gloves on. brb with a PR :)

@rodrimaia rodrimaia closed this May 18, 2023
@github-actions github-actions bot locked and limited conversation to collaborators May 18, 2023
@github-actions github-actions bot deleted the maintain_workspace_order_list branch November 19, 2023 00:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants