Skip to content

Commit c1c792b

Browse files
authored
Improve emoji autocomplete matching (#3571)
## Summary - Show all colon emoji autocomplete matches - Rank exact and prefix shortcodes before weaker matches - Add a regression test and screenshot ## Validation - `pnpm test` - `pnpm build` - `pnpm exec playwright test --project=smoke tests/e2e/custom-emoji.spec.ts --grep "exact standard shortcode"` - `just desktop-tauri-clippy` Native Tauri tests were attempted but could not link because the local disk filled during compilation. --------- Signed-off-by: kenny lopez <[email protected]>
1 parent f1a827d commit c1c792b

6 files changed

Lines changed: 193 additions & 53 deletions

File tree

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { init, SearchIndex } from "emoji-mart";
44
import data from "@emoji-mart/data";
55

66
import type { CustomEmoji } from "@/shared/lib/remarkCustomEmoji";
7-
import { fuzzyStandardEmoji, rankByShortcode } from "@/shared/lib/emojiSearch";
7+
import {
8+
fuzzyStandardEmoji,
9+
rankByShortcode,
10+
rankShortcodeMatchesFirst,
11+
} from "@/shared/lib/emojiSearch";
812
import { rewriteRelayUrl } from "@/shared/lib/mediaUrl";
913
import type { AutocompleteEdit } from "./useRichTextEditor";
1014

@@ -18,7 +22,7 @@ export type EmojiSuggestion = {
1822

1923
const EMOJI_DEBOUNCE_MS = 120;
2024
const MIN_QUERY_LENGTH = 2;
21-
const MAX_RESULTS = 8;
25+
const UNLIMITED_RESULTS = Number.POSITIVE_INFINITY;
2226

2327
init({ data });
2428

@@ -81,15 +85,18 @@ export function useEmojiAutocomplete(customEmoji: CustomEmoji[] = []) {
8185
emojiQuery,
8286
customEmojiRef.current,
8387
(e) => e.shortcode,
84-
MAX_RESULTS,
88+
UNLIMITED_RESULTS,
8589
).map((e) => ({
8690
id: e.shortcode,
8791
name: e.shortcode,
8892
native: "",
8993
url: rewriteRelayUrl(e.url),
9094
}));
9195

92-
SearchIndex.search(emojiQuery)
96+
SearchIndex.search(emojiQuery, {
97+
caller: "useEmojiAutocomplete",
98+
maxResults: UNLIMITED_RESULTS,
99+
})
93100
.then(
94101
(
95102
results: Array<{
@@ -106,23 +113,28 @@ export function useEmojiAutocomplete(customEmoji: CustomEmoji[] = []) {
106113
native: emoji.skins[0]?.native ?? "",
107114
}))
108115
.filter((e) => e.native !== "");
109-
// Top up remaining slots with fuzzy shortcode matches emoji-mart
110-
// missed — its token-prefix search can't cross `_` (so `pointup`
111-
// finds nothing). Skip ids already shown to avoid duplicates.
116+
// Add fuzzy shortcode matches emoji-mart missed — its token-prefix
117+
// search can't cross `_` (so `pointup` finds nothing). Skip ids
118+
// already shown to avoid duplicates.
112119
const shown = new Set<string>(
113120
[...customMatches, ...standard].map((e) => e.id),
114121
);
115122
const fuzzy: EmojiSuggestion[] = fuzzyStandardEmoji(
116123
emojiQuery,
117-
MAX_RESULTS - customMatches.length - standard.length,
124+
UNLIMITED_RESULTS,
118125
shown,
119126
).map((e) => ({ id: e.id, name: e.name, native: e.native }));
120-
// Custom emoji first (community-specific), then standard, then fuzzy.
121-
const merged = [...customMatches, ...standard, ...fuzzy].slice(
122-
0,
123-
MAX_RESULTS,
127+
// Rank exact/prefix shortcode matches across custom and standard emoji
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.
131+
setSuggestions(
132+
rankShortcodeMatchesFirst(
133+
emojiQuery,
134+
[...standard, ...customMatches, ...fuzzy],
135+
(emoji) => emoji.id,
136+
),
124137
);
125-
setSuggestions(merged);
126138
setEmojiSelectedIndex(0);
127139
},
128140
)

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

Lines changed: 56 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,47 +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
)}
53-
ref={listRef}
63+
data-testid="emoji-autocomplete"
5464
style={POPOVER_SHADOW_STYLE}
5565
>
56-
{suggestions.map((suggestion, index) => (
57-
<button
58-
className={cn(
59-
"flex w-full cursor-pointer items-center gap-2 rounded-lg px-3 py-1.5 text-left text-sm",
60-
index === selectedIndex
61-
? "bg-accent text-accent-foreground"
62-
: "text-popover-foreground hover:bg-accent/50",
63-
)}
64-
key={suggestion.id}
65-
onMouseDown={(event) => {
66-
event.preventDefault();
67-
onSelect(suggestion);
68-
}}
69-
tabIndex={-1}
70-
type="button"
71-
>
72-
{suggestion.url ? (
73-
<img
74-
alt={`:${suggestion.id}:`}
75-
src={suggestion.url}
76-
className="h-5 w-5 object-contain"
77-
draggable={false}
78-
/>
79-
) : (
80-
<span className="text-lg leading-none">{suggestion.native}</span>
81-
)}
82-
<span className="truncate text-muted-foreground">
83-
:{suggestion.id}:
84-
</span>
85-
</button>
86-
))}
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+
/>
87105
</div>
88106
</div>
89107
);

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
fuzzyStandardEmoji,
66
normalizeShortcode,
77
rankByShortcode,
8+
rankShortcodeMatchesFirst,
89
scoreShortcodeMatch,
910
} from "./emojiSearch.ts";
1011

@@ -77,6 +78,35 @@ test("rankByShortcode respects the limit", () => {
7778
assert.equal(ranked.length, 2);
7879
});
7980

81+
test("exact shortcode matches rank ahead of weaker custom shortcode matches", () => {
82+
const items = [
83+
{ code: "bufo_joy", source: "custom" },
84+
{ code: "joy", source: "standard" },
85+
{ code: "joy_cat", source: "custom" },
86+
{ code: "face_with_tears_of_joy", source: "standard" },
87+
];
88+
const ranked = rankShortcodeMatchesFirst("joy", items, (item) => item.code);
89+
90+
assert.deepEqual(
91+
ranked.map((item) => item.code),
92+
["joy", "joy_cat", "bufo_joy", "face_with_tears_of_joy"],
93+
);
94+
});
95+
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+
80110
test("fuzzyStandardEmoji surfaces point_up for `pointup`", () => {
81111
const hits = fuzzyStandardEmoji("pointup", 8, new Set());
82112
const ids = hits.map((e) => e.id);

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,33 @@ export function rankByShortcode<T>(
114114
return scored.slice(0, limit).map((s) => s.item);
115115
}
116116

117+
/**
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.
122+
*/
123+
export function rankShortcodeMatchesFirst<T>(
124+
query: string,
125+
items: readonly T[],
126+
shortcodeOf: (item: T) => string,
127+
): T[] {
128+
const strongShortcodeMatches = rankByShortcode(
129+
query,
130+
items,
131+
shortcodeOf,
132+
Number.POSITIVE_INFINITY,
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);
138+
return [
139+
...strongShortcodeMatches,
140+
...items.filter((item) => !matchedItems.has(item)),
141+
];
142+
}
143+
117144
export interface StandardEmoji {
118145
id: string;
119146
name: string;

‎desktop/src/testing/e2eBridge.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,8 @@ function createMockRelayMembershipEvent(): RelayEvent {
913913
* sets from distinct pubkeys so the e2e exercises the union/collapse path, not
914914
* a single relay-owned set. `:buzz:` is the stable shortcode exercised by
915915
* custom-emoji.spec.ts (claimed by BOTH members with different URLs, so the
916-
* palette must collapse it to one deterministic winner); `:narf:` proves a
917-
* second member's distinct emoji unions in.
916+
* palette must collapse it to one deterministic winner); `:narf:` and
917+
* `:bufo_joy:` prove a second member's distinct emoji unions in.
918918
*/
919919
function createMockCustomEmojiSetEvents(): RelayEvent[] {
920920
return [
@@ -941,6 +941,7 @@ function createMockCustomEmojiSetEvents(): RelayEvent[] {
941941
// member B claims :buzz: with a DIFFERENT url — unionCustomEmoji must
942942
// collapse it to one deterministic winner, never expose two URLs.
943943
["emoji", "buzz", "https://example.com/e2e/buzz-b.png"],
944+
["emoji", "bufo_joy", "https://example.com/e2e/bufo-joy.png"],
944945
],
945946
"b".repeat(64),
946947
),

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { expect, test } from "@playwright/test";
2+
import * as fs from "node:fs";
3+
import * as path from "node:path";
24

35
import { installMockBridge } from "../helpers/bridge";
6+
import { waitForAnimations } from "../helpers/animations";
47

58
// Custom-emoji end-to-end guard.
69
//
@@ -77,6 +80,55 @@ test("typing a known :shortcode: renders an inline emoji node in the composer",
7780
await expect(input).not.toContainText(`:${SHORTCODE}:`);
7881
});
7982

83+
test("emoji autocomplete ranks an exact standard shortcode before a custom substring", async ({
84+
page,
85+
}, testInfo) => {
86+
await openGeneral(page);
87+
88+
const input = page.getByTestId("message-input");
89+
await input.click();
90+
await input.pressSequentially(":joy");
91+
92+
const autocomplete = page.getByTestId("emoji-autocomplete");
93+
await expect(autocomplete).toBeVisible();
94+
const labels = await autocomplete.locator("button").allTextContents();
95+
expect(labels[0]).toContain(":joy:");
96+
expect(
97+
labels.findIndex((label) => label.includes(":bufo_joy:")),
98+
).toBeGreaterThan(0);
99+
100+
const screenshotDir = path.resolve(
101+
"test-results/emoji-autocomplete-screenshots",
102+
);
103+
fs.mkdirSync(screenshotDir, { recursive: true });
104+
const screenshotPath = path.join(
105+
screenshotDir,
106+
"joy-exact-before-custom-substring.png",
107+
);
108+
await waitForAnimations(page);
109+
await autocomplete.screenshot({ path: screenshotPath });
110+
await testInfo.attach("joy-exact-before-custom-substring", {
111+
path: screenshotPath,
112+
contentType: "image/png",
113+
});
114+
});
115+
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+
80132
test("custom emoji deletes as a single unit (like a built-in emoji)", async ({
81133
page,
82134
}) => {

0 commit comments

Comments
 (0)