-
Notifications
You must be signed in to change notification settings - Fork 887
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
|
@@ -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 { | ||
// Merge the fetched workspaces with the displayed onws, without changing the order of the existing items | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
|
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.
nit: I have a feeling that this can be refactored into more like a fluent approach.