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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions site/src/pages/WorkspacesPage/WorkspacesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useFilter } from "hooks/useFilter"
import { usePagination } from "hooks/usePagination"
import { FC } from "react"
import { FC, useEffect, useState } from "react"
import { Helmet } from "react-helmet-async"
import { workspaceFilterQuery } from "utils/filters"
import { pageTitle } from "utils/page"
import { useWorkspacesData, useWorkspaceUpdate } from "./data"
import { WorkspacesPageView } from "./WorkspacesPageView"
import { useDashboard } from "components/Dashboard/DashboardProvider"
import { Workspace } from "api/typesGenerated"

const WorkspacesPage: FC = () => {
const filter = useFilter(workspaceFilterQuery.me)
Expand All @@ -22,20 +23,61 @@ const WorkspacesPage: FC = () => {
...pagination,
...filter,
})

const updateWorkspace = useWorkspaceUpdate(queryKey)

const [displayedWorkspaces, setDisplayedWorkspaces] = useState<Workspace[]>(
[],
)

useEffect(() => {
const fetchedWorkspaces = data?.workspaces || []
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.

// 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

const mergedItems = displayedWorkspaces
.map((item) => {
const fetchedItem = fetchedWorkspaces.find(
(fItem) => fItem.id === item.id,
)

if (!fetchedItem) {
return null
}

// 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 :)


// Add new items to the beginning of the list
fetchedWorkspaces.forEach((fetchedItem) => {
if (!mergedItems.some((item) => item?.id === fetchedItem.id)) {
mergedItems.unshift(fetchedItem)
}
})
Comment on lines +56 to +60
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.


setDisplayedWorkspaces(
mergedItems.filter((item) => item !== null) as Workspace[],
)
}
}
}, [data?.workspaces, displayedWorkspaces])

return (
<>
<Helmet>
<title>{pageTitle("Workspaces")}</title>
</Helmet>

<WorkspacesPageView
workspaces={data?.workspaces}
workspaces={data && displayedWorkspaces}
error={error}
filter={filter.query}
onFilter={filter.setFilter}
count={data?.count}
count={displayedWorkspaces.length}
page={pagination.page}
limit={pagination.limit}
onPageChange={pagination.goToPage}
Expand Down