Skip to content

Commit

Permalink
Revert sorting order (#11756)
Browse files Browse the repository at this point in the history
* Revert  sorting order

* Address issues/ remove the optimistic updates

* Add more tests

* Fix tests
  • Loading branch information
MrFlashAccount authored Dec 5, 2024
1 parent 497884a commit 984aeb9
Show file tree
Hide file tree
Showing 3 changed files with 401 additions and 31 deletions.
26 changes: 20 additions & 6 deletions app/common/src/services/Backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1433,13 +1433,27 @@ export function compareAssets(a: AnyAsset, b: AnyAsset) {

if (relativeTypeOrder !== 0) {
return relativeTypeOrder
} else {
// We sort by modified date, because the running/recent projects should be at the top,
// but below the folders.
const aModified = Number(new Date(a.modifiedAt))
const bModified = Number(new Date(b.modifiedAt))
const modifiedDelta = aModified - bModified

const aTitle = a.title.toLowerCase()
const bTitle = b.title.toLowerCase()

if (modifiedDelta !== 0) {
// Sort by date descending, rather than ascending.
return -modifiedDelta
} else {
return (
aTitle > bTitle ? 1
: aTitle < bTitle ? -1
: 0
)
}
}

return (
a.title > b.title ? 1
: a.title < b.title ? -1
: 0
)
}

// ==================
Expand Down
25 changes: 0 additions & 25 deletions app/gui/src/dashboard/hooks/backendHooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -572,25 +572,6 @@ export function useRootDirectoryId(backend: Backend, category: Category) {
}, [category, backend, user, organization, localRootDirectory])
}

/** A function to optimistically insert assets into the React Query cache listing for a folder. */
function useInsertAssets(backend: Backend, category: Category) {
const queryClient = useQueryClient()
const rootDirectoryId = useRootDirectoryId(backend, category)

return useEventCallback((assets: readonly AnyAsset[], parentId: DirectoryId | null) => {
const actualParentId = parentId ?? rootDirectoryId

const listDirectoryQuery = queryClient.getQueryCache().find<DirectoryQuery>({
queryKey: [backend.type, 'listDirectory', actualParentId],
exact: false,
})

if (listDirectoryQuery?.state.data) {
listDirectoryQuery.setData([...listDirectoryQuery.state.data, ...assets])
}
})
}

/** Return query data for the children of a directory, fetching it if it does not exist. */
function useEnsureListDirectory(backend: Backend, category: Category) {
const queryClient = useQueryClient()
Expand Down Expand Up @@ -772,7 +753,6 @@ export function useNewProject(backend: Backend, category: Category) {

/** A function to create a new secret. */
export function useNewSecret(backend: Backend, category: Category) {
const insertAssets = useInsertAssets(backend, category)
const toggleDirectoryExpansion = useToggleDirectoryExpansion()
const { user } = useFullUserSession()
const { data: users } = useBackendQuery(backend, 'listUsers', [])
Expand All @@ -799,8 +779,6 @@ export function useNewSecret(backend: Backend, category: Category) {
),
)

insertAssets([placeholderItem], parentId)

return await createSecretMutation.mutateAsync([
{
parentDirectoryId: placeholderItem.parentId,
Expand All @@ -814,7 +792,6 @@ export function useNewSecret(backend: Backend, category: Category) {

/** A function to create a new Datalink. */
export function useNewDatalink(backend: Backend, category: Category) {
const insertAssets = useInsertAssets(backend, category)
const toggleDirectoryExpansion = useToggleDirectoryExpansion()
const { user } = useFullUserSession()
const { data: users } = useBackendQuery(backend, 'listUsers', [])
Expand All @@ -841,8 +818,6 @@ export function useNewDatalink(backend: Backend, category: Category) {
),
)

insertAssets([placeholderItem], parentId)

return await createDatalinkMutation.mutateAsync([
{
parentDirectoryId: placeholderItem.parentId,
Expand Down
Loading

0 comments on commit 984aeb9

Please sign in to comment.