Skip to content

Commit

Permalink
🔧 fix: android keyboard @ popover issue (#2647)
Browse files Browse the repository at this point in the history
  • Loading branch information
berry-13 authored May 9, 2024
1 parent 6ba7f60 commit 83bae9e
Showing 1 changed file with 13 additions and 31 deletions.
44 changes: 13 additions & 31 deletions client/src/hooks/Input/useTextarea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import store from '~/store';

type KeyEvent = KeyboardEvent<HTMLTextAreaElement>;

const keyMap = {
50: '2',
192: '@',
};

export default function useTextarea({
textAreaRef,
submitButtonRef,
Expand Down Expand Up @@ -141,35 +136,22 @@ export default function useTextarea({
assistantMap,
]);

const handleKeyUp = useCallback(
(e: KeyEvent) => {
let normalizedKey = e.key;

if (!normalizedKey || normalizedKey === 'Unidentified') {
normalizedKey = keyMap[e.keyCode] || normalizedKey;
}

if (normalizedKey !== '@' && normalizedKey !== '2') {
return;
}

const text = textAreaRef.current?.value;
if (!(text && text[text.length - 1] === '@')) {
return;
}
const handleKeyUp = useCallback(() => {
const text = textAreaRef.current?.value;
if (!(text && text[text.length - 1] === '@')) {
return;
}

const startPos = textAreaRef.current?.selectionStart;
if (!startPos) {
return;
}
const startPos = textAreaRef.current?.selectionStart;
if (!startPos) {
return;
}

const isAtStart = startPos === 1;
const isPrecededBySpace = textAreaRef.current?.value.charAt(startPos - 2) === ' ';
const isAtStart = startPos === 1;
const isPrecededBySpace = textAreaRef.current?.value.charAt(startPos - 2) === ' ';

setShowMentionPopover(isAtStart || isPrecededBySpace);
},
[textAreaRef, setShowMentionPopover],
);
setShowMentionPopover(isAtStart || isPrecededBySpace);
}, [textAreaRef, setShowMentionPopover]);

const handleKeyDown = useCallback(
(e: KeyEvent) => {
Expand Down

0 comments on commit 83bae9e

Please sign in to comment.