Skip to content

Commit

Permalink
Improve ordering of mention matches
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Dec 21, 2023
1 parent 7c3d74e commit a85e703
Showing 1 changed file with 46 additions and 23 deletions.
69 changes: 46 additions & 23 deletions frontend/app/src/components/home/MentionPicker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,44 @@
$: prefixLower = prefix?.toLowerCase();
$: filtered = usersAndGroups.filter((userOrGroup) => {
switch (userOrGroup.kind) {
case "user_group":
return (
!usersOnly &&
(prefixLower === undefined ||
(supportsUserGroups &&
userOrGroup.name.toLowerCase().startsWith(prefixLower)))
);
case "everyone": {
return (
!usersOnly &&
(prefixLower === undefined || userOrGroup.kind.startsWith(prefixLower))
);
$: filtered = usersAndGroups
.filter((userOrGroup) => {
switch (userOrGroup.kind) {
case "user_group":
return (
!usersOnly &&
(prefixLower === undefined ||
(supportsUserGroups &&
userOrGroup.name.toLowerCase().startsWith(prefixLower)))
);
case "everyone": {
return (
!usersOnly &&
(prefixLower === undefined || userOrGroup.kind.startsWith(prefixLower))
);
}
default:
return (
(mentionSelf || userOrGroup.userId !== $currentUser.userId) &&
(prefixLower === undefined ||
userOrGroup.username.toLowerCase().startsWith(prefixLower) ||
userOrGroup.displayName?.toLowerCase().startsWith(prefixLower))
);
}
default:
return (
(mentionSelf || userOrGroup.userId !== $currentUser.userId) &&
(prefixLower === undefined ||
userOrGroup.username.toLowerCase().startsWith(prefixLower) ||
userOrGroup.displayName?.toLowerCase().startsWith(prefixLower))
);
}
});
})
.sort((a, b) => {
// 'everyone' first, then user groups, then users
if (a.kind === "everyone") return -1;
if (b.kind === "everyone") return 1;
if (a.kind === "user_group" && b.kind === "user_group") {
return compareMatchNames(a.name, b.name);
}
if (a.kind === "user" && b.kind === "user") {
return compareMatchNames(a.username, b.username);
}
return a.kind === "user_group" ? -1 : 1;
});
$: style =
direction === "up"
Expand Down Expand Up @@ -110,6 +124,15 @@
break;
}
}
function compareMatchNames(a: string, b: string): number {
// Order by length, then alphabetically
if (a === b) return 0;
if (a.length === b.length) {
return a < b ? -1 : 1;
}
return a.length < b.length ? -1 : 1;
}
</script>

{#if filtered.length > 0}
Expand Down

0 comments on commit a85e703

Please sign in to comment.