diff --git a/static-site/src/components/ListResources/useSortAndFilter.ts b/static-site/src/components/ListResources/useSortAndFilter.ts index 67c7bdaa1..b58ad6185 100644 --- a/static-site/src/components/ListResources/useSortAndFilter.ts +++ b/static-site/src/components/ListResources/useSortAndFilter.ts @@ -23,6 +23,22 @@ export const useSortAndFilter = ( /* Following console log is really useful for development */ // console.log(`useSortAndFilter() sortMethod "${sortMethod}" ` + (selectedFilterOptions.length ? `filtering to ${selectedFilterOptions.map((el) => el.value).join(", ")}` : '(no filtering)')) + function sortAndFilter( + groups: T[], + sortGroups: (a: T, b: T) => number, + sortResources: (a: Resource, b: Resource) => number, + ): T[] { + return groups + .map((group) => ({ + ...group, + resources: group.resources + .filter(_filterResources) + .sort(sortResources) + })) + .filter((group) => !!group.resources.length) + .sort(sortGroups); + } + if (sortMethod === "lastUpdated") { const groups = originalData.map((group: Group) => convertVersionedGroup(group)) const _sortGroups = (groupA: VersionedGroup, groupB: VersionedGroup) => _newestFirstSort(groupA.lastUpdated, groupB.lastUpdated) @@ -45,23 +61,7 @@ export const useSortAndFilter = ( const resourceGroups = sortAndFilter(groups, _sortGroups, _sortResources) setState(resourceGroups) } - }, [sortMethod, originalData, setState, sortAndFilter]) - - function sortAndFilter( - groups: T[], - sortGroups: (a: T, b: T) => number, - sortResources: (a: Resource, b: Resource) => number, - ): T[] { - return groups - .map((group) => ({ - ...group, - resources: group.resources - .filter(_filterResources) - .sort(sortResources) - })) - .filter((group) => !!group.resources.length) - .sort(sortGroups); - } + }, [sortMethod, originalData, setState]) }