Skip to content

Commit 246d933

Browse files
committed
Address emoji autocomplete review feedback
Signed-off-by: kenny lopez <[email protected]>
1 parent f5cf258 commit 246d933

5 files changed

Lines changed: 100 additions & 48 deletions

File tree

‎desktop/src/features/messages/lib/useEmojiAutocomplete.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,13 @@ export function useEmojiAutocomplete(customEmoji: CustomEmoji[] = []) {
125125
shown,
126126
).map((e) => ({ id: e.id, name: e.name, native: e.native }));
127127
// Rank exact/prefix shortcode matches across custom and standard emoji
128-
// before weaker matches (for example, `joy` before `bufo_joy`).
128+
// before semantic and weaker matches (for example, `joy` before
129+
// `bufo_joy`). Keep emoji-mart's name/keyword results ahead of loose
130+
// substring and subsequence matches.
129131
setSuggestions(
130132
rankShortcodeMatchesFirst(
131133
emojiQuery,
132-
[...customMatches, ...standard, ...fuzzy],
134+
[...standard, ...customMatches, ...fuzzy],
133135
(emoji) => emoji.id,
134136
),
135137
);

‎desktop/src/features/messages/ui/EmojiAutocomplete.tsx‎

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import * as React from "react";
22

33
import type { EmojiSuggestion } from "@/features/messages/lib/useEmojiAutocomplete";
44
import { cn } from "@/shared/lib/cn";
5+
import {
6+
type ListVirtualizer,
7+
VirtualizedList,
8+
} from "@/shared/ui/VirtualizedList";
59
import {
610
POPOVER_CUSTOM_ENTER_MOTION_CLASS,
711
POPOVER_SHADOW_STYLE,
@@ -21,15 +25,21 @@ export const EmojiAutocomplete = React.memo(function EmojiAutocomplete({
2125
onSelect,
2226
position = "above",
2327
}: EmojiAutocompleteProps) {
24-
const listRef = React.useRef<HTMLDivElement>(null);
28+
const listVirtualizerRef = React.useRef<ListVirtualizer | null>(null);
2529

2630
React.useEffect(() => {
27-
const activeItem = listRef.current?.children[selectedIndex] as
28-
| HTMLElement
29-
| undefined;
30-
activeItem?.scrollIntoView({ block: "nearest" });
31+
listVirtualizerRef.current?.scrollToIndex(selectedIndex, {
32+
align: "auto",
33+
});
3134
}, [selectedIndex]);
3235

36+
const handleVirtualizer = React.useCallback(
37+
(virtualizer: ListVirtualizer) => {
38+
listVirtualizerRef.current = virtualizer;
39+
},
40+
[],
41+
);
42+
3343
if (suggestions.length === 0) {
3444
return null;
3545
}
@@ -43,48 +53,55 @@ export const EmojiAutocomplete = React.memo(function EmojiAutocomplete({
4353
>
4454
<div
4555
className={cn(
46-
"max-h-48 overflow-y-auto rounded-xl p-1",
56+
"rounded-xl p-1",
4757
POPOVER_CUSTOM_ENTER_MOTION_CLASS,
4858
position === "below"
4959
? "origin-top slide-in-from-top-1"
5060
: "origin-bottom slide-in-from-bottom-1",
5161
POPOVER_SURFACE_CLASS,
5262
)}
5363
data-testid="emoji-autocomplete"
54-
ref={listRef}
5564
style={POPOVER_SHADOW_STYLE}
5665
>
57-
{suggestions.map((suggestion, index) => (
58-
<button
59-
className={cn(
60-
"flex w-full cursor-pointer items-center gap-2 rounded-lg px-3 py-1.5 text-left text-sm",
61-
index === selectedIndex
62-
? "bg-accent text-accent-foreground"
63-
: "text-popover-foreground hover:bg-accent/50",
64-
)}
65-
key={suggestion.id}
66-
onMouseDown={(event) => {
67-
event.preventDefault();
68-
onSelect(suggestion);
69-
}}
70-
tabIndex={-1}
71-
type="button"
72-
>
73-
{suggestion.url ? (
74-
<img
75-
alt={`:${suggestion.id}:`}
76-
src={suggestion.url}
77-
className="h-5 w-5 object-contain"
78-
draggable={false}
79-
/>
80-
) : (
81-
<span className="text-lg leading-none">{suggestion.native}</span>
82-
)}
83-
<span className="truncate text-muted-foreground">
84-
:{suggestion.id}:
85-
</span>
86-
</button>
87-
))}
66+
<VirtualizedList
67+
className="max-h-48"
68+
estimateSize={36}
69+
getItemKey={(suggestion) => suggestion.id}
70+
items={suggestions}
71+
onVirtualizer={handleVirtualizer}
72+
renderItem={(suggestion, index) => (
73+
<button
74+
className={cn(
75+
"flex w-full cursor-pointer items-center gap-2 rounded-lg px-3 py-1.5 text-left text-sm",
76+
index === selectedIndex
77+
? "bg-accent text-accent-foreground"
78+
: "text-popover-foreground hover:bg-accent/50",
79+
)}
80+
onMouseDown={(event) => {
81+
event.preventDefault();
82+
onSelect(suggestion);
83+
}}
84+
tabIndex={-1}
85+
type="button"
86+
>
87+
{suggestion.url ? (
88+
<img
89+
alt={`:${suggestion.id}:`}
90+
src={suggestion.url}
91+
className="h-5 w-5 object-contain"
92+
draggable={false}
93+
/>
94+
) : (
95+
<span className="text-lg leading-none">
96+
{suggestion.native}
97+
</span>
98+
)}
99+
<span className="truncate text-muted-foreground">
100+
:{suggestion.id}:
101+
</span>
102+
</button>
103+
)}
104+
/>
88105
</div>
89106
</div>
90107
);

‎desktop/src/shared/lib/emojiSearch.test.mjs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ test("exact shortcode matches rank ahead of weaker custom shortcode matches", ()
9393
);
9494
});
9595

96+
test("semantic results stay ahead of loose shortcode matches", () => {
97+
const items = [
98+
{ code: "frowning_face", source: "semantic" },
99+
{ code: "sandwich", source: "custom" },
100+
{ code: "sad", source: "standard" },
101+
];
102+
const ranked = rankShortcodeMatchesFirst("sad", items, (item) => item.code);
103+
104+
assert.deepEqual(
105+
ranked.map((item) => item.code),
106+
["sad", "frowning_face", "sandwich"],
107+
);
108+
});
109+
96110
test("fuzzyStandardEmoji surfaces point_up for `pointup`", () => {
97111
const hits = fuzzyStandardEmoji("pointup", 8, new Set());
98112
const ids = hits.map((e) => e.id);

‎desktop/src/shared/lib/emojiSearch.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,28 @@ export function rankByShortcode<T>(
115115
}
116116

117117
/**
118-
* Place shortcode matches ahead of items that only matched an emoji name or
119-
* keyword. This lets an exact standard emoji like `joy` beat a weaker custom
120-
* shortcode match such as `bufo_joy`, while retaining emoji-mart's order for
121-
* name- and keyword-only results.
118+
* Place exact and prefix shortcode matches ahead of items that only matched an
119+
* emoji name or keyword. This lets an exact standard emoji like `joy` beat a
120+
* weaker custom shortcode match such as `bufo_joy`, while retaining emoji-mart's
121+
* order for name- and keyword-only results ahead of loose shortcode matches.
122122
*/
123123
export function rankShortcodeMatchesFirst<T>(
124124
query: string,
125125
items: readonly T[],
126126
shortcodeOf: (item: T) => string,
127127
): T[] {
128-
const shortcodeMatches = rankByShortcode(
128+
const strongShortcodeMatches = rankByShortcode(
129129
query,
130130
items,
131131
shortcodeOf,
132132
Number.POSITIVE_INFINITY,
133-
);
134-
const matchedItems = new Set(shortcodeMatches);
133+
).filter((item) => {
134+
const match = scoreShortcodeMatch(query, shortcodeOf(item));
135+
return match !== null && match.tier <= TIER_PREFIX;
136+
});
137+
const matchedItems = new Set(strongShortcodeMatches);
135138
return [
136-
...shortcodeMatches,
139+
...strongShortcodeMatches,
137140
...items.filter((item) => !matchedItems.has(item)),
138141
];
139142
}

‎desktop/tests/e2e/custom-emoji.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ test("emoji autocomplete ranks an exact standard shortcode before a custom subst
113113
});
114114
});
115115

116+
test("emoji autocomplete keeps semantic matches ahead of loose shortcode fallbacks", async ({
117+
page,
118+
}) => {
119+
await openGeneral(page);
120+
121+
const input = page.getByTestId("message-input");
122+
await input.click();
123+
await input.pressSequentially(":sad");
124+
125+
const autocomplete = page.getByTestId("emoji-autocomplete");
126+
await expect(autocomplete).toBeVisible();
127+
await expect(autocomplete.locator("button").first()).not.toContainText(
128+
":sandwich:",
129+
);
130+
});
131+
116132
test("custom emoji deletes as a single unit (like a built-in emoji)", async ({
117133
page,
118134
}) => {

0 commit comments

Comments
 (0)