feat: auto focus message input on chat open#526
Conversation
📝 WalkthroughWalkthroughChatWindow now includes a ChangesMessage Input Auto-Focus on User Selection
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
frontend/components/chat/ChatWindow.jsx (2)
145-145: ⚡ Quick winOptimize dependency: use
selectedUser?._idinstead ofselectedUser.The current dependency on the entire
selectedUserobject will trigger the effect whenever the object reference changes, even if it's the same user. This can cause unnecessary focus calls.⚡ Suggested improvement
-}, [selectedUser]); +}, [selectedUser?._id]);This ensures the focus only triggers when switching between different users, not on object reference changes.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@frontend/components/chat/ChatWindow.jsx` at line 145, The effect in ChatWindow.jsx currently lists the whole selectedUser object in the dependency array, causing unnecessary runs when the object reference changes; update the useEffect dependency to depend on selectedUser?._id (e.g., replace selectedUser with selectedUser?._id) so the effect (the focus call on the input ref inside the useEffect) only runs when the selected user's id changes.
141-145: ⚡ Quick winConsider guarding against focus conflicts when search is open.
When the search panel is open (line 490 has
autoFocuson the search input), this effect will steal focus back to the textarea ifselectedUserchanges. This could disrupt users who are actively searching messages.🛡️ Suggested guard condition
useEffect(() => { - if (selectedUser && textareaRef.current) { + if (selectedUser && textareaRef.current && !searchOpen) { textareaRef.current.focus(); } -}, [selectedUser]); +}, [selectedUser, searchOpen]);This prevents focus-stealing when the user is actively searching.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@frontend/components/chat/ChatWindow.jsx` around lines 141 - 145, The effect that focuses textareaRef on selectedUser change can steal focus from the search input; update the useEffect (referencing useEffect, selectedUser, and textareaRef) to first check that the search panel is not active (e.g., a boolean like isSearchOpen or isSearching, or that searchInputRef.current is not the activeElement) before calling textareaRef.current.focus(), and include that guard in the dependency array so focus only occurs when selectedUser changes and search is closed.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@frontend/components/chat/ChatWindow.jsx`:
- Line 145: The effect in ChatWindow.jsx currently lists the whole selectedUser
object in the dependency array, causing unnecessary runs when the object
reference changes; update the useEffect dependency to depend on
selectedUser?._id (e.g., replace selectedUser with selectedUser?._id) so the
effect (the focus call on the input ref inside the useEffect) only runs when the
selected user's id changes.
- Around line 141-145: The effect that focuses textareaRef on selectedUser
change can steal focus from the search input; update the useEffect (referencing
useEffect, selectedUser, and textareaRef) to first check that the search panel
is not active (e.g., a boolean like isSearchOpen or isSearching, or that
searchInputRef.current is not the activeElement) before calling
textareaRef.current.focus(), and include that guard in the dependency array so
focus only occurs when selectedUser changes and search is closed.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6046c6bc-b3bc-4e4d-a90b-9f2bb360f1b6
📒 Files selected for processing (1)
frontend/components/chat/ChatWindow.jsx
|
This PR adds automatic focus behavior for the chat input field when opening a conversation. Improvements
@UTKARSHH20 Would appreciate a review. Thanks! |
Related Issue
Closes #525
Description
This PR improves chat usability by automatically focusing the message input field whenever a conversation is opened.
Changes Made
useEffectselectedUserchangesBenefits
Summary by CodeRabbit