forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui/search): option to sort browse paths (datahub-project#10268)
- Loading branch information
1 parent
870cce2
commit d33faed
Showing
6 changed files
with
115 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export type SortBy = 'AtoZ' | 'ZtoA'; | ||
|
||
export const useSort = (initialData, sortBy: SortBy) => { | ||
const [sortedData, setSortedData] = useState([...initialData]); | ||
|
||
useEffect(() => { | ||
const sortData = () => { | ||
const newData = [...initialData]; | ||
if (sortBy === 'AtoZ') { | ||
newData?.sort((a, b) => { | ||
const nameA = a?.entity?.properties?.name || a.name; | ||
const nameB = b?.entity?.properties?.name || b.name; | ||
return nameA?.localeCompare(nameB); | ||
}); | ||
} else if (sortBy === 'ZtoA') { | ||
newData?.sort((a, b) => { | ||
const nameA = a?.entity?.properties?.name || a.name; | ||
const nameB = b?.entity?.properties?.name || b.name; | ||
return nameB?.localeCompare(nameA); | ||
}); | ||
} | ||
setSortedData(newData); | ||
}; | ||
sortData(); | ||
}, [initialData, sortBy]); | ||
|
||
return sortedData; | ||
}; |