refactor: enhance InlineMentionEditor and Composer auto-focus functionality

- Refactored InlineMentionEditor to improve caret position handling and mention triggering logic.
- Removed unnecessary caret position saving and restoring functions for cleaner code.
- Updated mention handling to ensure proper closing of mention popover based on cursor position.
- Added auto-focus feature in Composer for new chat threads, enhancing user experience.
- Implemented checks to reset auto-focus when the thread is no longer empty.
This commit is contained in:
Anish Sarkar 2025-12-25 14:19:22 +05:30
parent 533084b433
commit 7c8e269190
2 changed files with 74 additions and 44 deletions

View file

@ -421,6 +421,29 @@ const Composer: FC = () => {
const { search_space_id } = useParams();
const setMentionedDocumentIds = useSetAtom(mentionedDocumentIdsAtom);
const composerRuntime = useComposerRuntime();
const hasAutoFocusedRef = useRef(false);
// Check if thread is empty (new chat)
const isThreadEmpty = useAssistantState(({ thread }) => thread.isEmpty);
// Auto-focus editor when on new chat page
useEffect(() => {
if (isThreadEmpty && !hasAutoFocusedRef.current && editorRef.current) {
// Small delay to ensure the editor is fully mounted
const timeoutId = setTimeout(() => {
editorRef.current?.focus();
hasAutoFocusedRef.current = true;
}, 100);
return () => clearTimeout(timeoutId);
}
}, [isThreadEmpty]);
// Reset auto-focus flag when thread becomes non-empty (user sent a message)
useEffect(() => {
if (!isThreadEmpty) {
hasAutoFocusedRef.current = false;
}
}, [isThreadEmpty]);
// Sync mentioned document IDs to atom for use in chat request
useEffect(() => {