fix: clear stale mention state when @ button insertion is invalid

When the @ button is clicked at a position where the regex does not
match (e.g., immediately after a word or on a second click after the
button just inserted @), the handler now explicitly clears any existing
mention state instead of leaving it intact.

This prevents the popover from remaining bound to a previous cursor
position, which would cause picking a file to replace the old mention
location and leave the newly inserted @ behind.

Mirrors the handleChange behavior which already clears mention when
the regex stops matching.
This commit is contained in:
xxiaoxiong 2026-05-28 22:07:18 +08:00
parent 7b64564b5c
commit 79554198ec

View file

@ -774,8 +774,12 @@ export const ChatComposer = forwardRef<ChatComposerHandle, Props>(
// does when it detects a fresh @ in the typed input. // does when it detects a fresh @ in the typed input.
const pos = cursor + 1; const pos = cursor + 1;
const textBefore = next.slice(0, pos); const textBefore = next.slice(0, pos);
const m = /(^|\s)@([^\s@]*)$/.exec(textBefore); const m = /(^|\\s)@([^\\s@]*)$/.exec(textBefore);
if (m) setMention({ q: m[2] ?? '', cursor: pos }); if (m) {
setMention({ q: m[2] ?? '', cursor: pos });
} else {
setMention(null);
}
requestAnimationFrame(() => { requestAnimationFrame(() => {
ta.focus(); ta.focus();
ta.setSelectionRange(pos, pos); ta.setSelectionRange(pos, pos);