Skip to content

fix(composer): flush debounced autocomplete detection on Tab/Enter#1541

Open
wesbillman wants to merge 1 commit into
mainfrom
fix/mention-tab-stale-commit
Open

fix(composer): flush debounced autocomplete detection on Tab/Enter#1541
wesbillman wants to merge 1 commit into
mainfrom
fix/mention-tab-stale-commit

Conversation

@wesbillman

Copy link
Copy Markdown
Collaborator

Problem

Reported by DK: typing @cl and quickly hitting Tab right after the composer regains focus commits "@fizz" (the first entry of the unfiltered list) instead of the intended match, forcing him to back it out and retype.

Root cause

Composer autocomplete detection is debounced 120 ms (MENTION_DEBOUNCE_MS, introduced in #222 to reduce input lag): keystrokes only stash the latest text in refs and schedule setMentionQuery for later. But handleMentionKeyDown commits Tab/Enter synchronously against suggestions[selectedIndex] — a list ranked for the previous query. Type inside the debounce window and Tab commits the stale list's top entry. The #channel and :emoji: autocompletes share the identical pattern.

Fix

On Tab/Enter, if a detection is pending, flush it synchronously:

  • Mentions / channel links — re-run detection on the latest text (already in refs) and, when the query changed, re-rank synchronously and commit the fresh top match. A single fast Tab now inserts what you actually typed. If the fresh query matches nothing, the keypress is swallowed instead of committing garbage.
  • Emoji — results come from an async search, so a stale Tab refreshes the dropdown for the new query instead of committing a wrong pick.
  • All three hooks also move the insert start-index from state to a ref, so the replaced text range can't lag behind the committed query either.

useMentions.ts crossed the 1000-line guard with the flush branch, so the pure candidate helpers (MentionCandidate type, label/owner formatters) moved to a new mentionCandidates.ts — no logic changes there.

Testing

  • New Playwright regression test: open dropdown with bare @, type bo + Tab inside the debounce window, assert @bob is inserted. Verified it fails against the unpatched code and passes with the fix.
  • Full mentions.spec.ts e2e suite: 31/31 passing.
  • pnpm test (1625 unit tests), pnpm typecheck, pnpm check (biome + file sizes) all green.

Fixes the race DK reported in typing-improvements.

Composer autocomplete detection is debounced 120ms, but Tab/Enter
committed suggestions synchronously from the previous query's list. A
fast "type @cl, hit Tab" inside that window inserted the top of the
stale unfiltered list (e.g. @fizz) instead of the typed match.

On Tab/Enter, flush the pending detection: re-detect the query from the
latest text and commit the fresh top match (mentions, channel links),
or refresh the dropdown instead of committing when results are async
(emoji). Start indices move to refs so the replaced range can't go
stale the same way. Extracts mention candidate helpers to
mentionCandidates.ts to keep useMentions.ts under the file-size limit.

Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
Signed-off-by: Wes <wesbillman@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant