From 2d6fe308b8e7e008eb25acf000b8588ea3206623 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Sat, 1 Oct 2022 14:14:03 -0500 Subject: [PATCH] better group sort --- web/components/groups/group-selector.tsx | 40 ++++++++++++------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/web/components/groups/group-selector.tsx b/web/components/groups/group-selector.tsx index a04a91af..caeb5f7d 100644 --- a/web/components/groups/group-selector.tsx +++ b/web/components/groups/group-selector.tsx @@ -32,27 +32,27 @@ export function GroupSelector(props: { const openGroups = useOpenGroups() const memberGroups = useMemberGroups(creator?.id) const memberGroupIds = memberGroups?.map((g) => g.id) ?? [] - const availableGroups = openGroups - .concat( - (memberGroups ?? []).filter( - (g) => !openGroups.map((og) => og.id).includes(g.id) - ) - ) - .filter((group) => !ignoreGroupIds?.includes(group.id)) - .sort((a, b) => b.totalContracts - a.totalContracts) - // put the groups the user is a member of first - .sort((a, b) => { - if (memberGroupIds.includes(a.id)) { - return -1 - } - if (memberGroupIds.includes(b.id)) { - return 1 - } - return 0 - }) - const filteredGroups = availableGroups.filter((group) => - searchInAny(query, group.name) + const sortGroups = (groups: Group[]) => + groups.sort( + (a, b) => + // weight group higher if user is a member + (memberGroupIds.includes(b.id) ? 5 : 1) * b.totalContracts - + (memberGroupIds.includes(a.id) ? 5 : 1) * a.totalContracts + ) + + const availableGroups = sortGroups( + openGroups + .concat( + (memberGroups ?? []).filter( + (g) => !openGroups.map((og) => og.id).includes(g.id) + ) + ) + .filter((group) => !ignoreGroupIds?.includes(group.id)) + ) + + const filteredGroups = sortGroups( + availableGroups.filter((group) => searchInAny(query, group.name)) ) if (!showSelector || !creator) {