Skip to content

Guard async input focus after unmount#1004

Merged
PeterDaveHello merged 1 commit into
ChatGPTBox-dev:masterfrom
PeterDaveHello:guard-input-focus-after-unmount
Jul 13, 2026
Merged

Guard async input focus after unmount#1004
PeterDaveHello merged 1 commit into
ChatGPTBox-dev:masterfrom
PeterDaveHello:guard-input-focus-after-unmount

Conversation

@PeterDaveHello

@PeterDaveHello PeterDaveHello commented Jul 13, 2026

Copy link
Copy Markdown
Member

Summary

Guard the deferred input focus when InputBox unmounts before the user configuration finishes loading.

Context

getUserConfig() reads extension storage asynchronously. If the component unmounts before that read completes, the textarea ref is cleared and calling focus() throws. Optional chaining preserves the mounted behavior while making the unmounted path a no-op.

Summary by CodeRabbit

  • Bug Fixes
    • Prevented a runtime error when automatically focusing the input after an answer if the input is not yet available.
    • Improved stability for users with post-answer focus enabled.

Skip focusing when the config read completes after InputBox has
unmounted. This prevents a null-ref exception without changing mounted
behavior.
@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 162b4200-7833-4a1d-9b59-75a1f8ab99b4

📥 Commits

Reviewing files that changed from the base of the PR and between c15d470 and aeb137a.

📒 Files selected for processing (1)
  • src/components/InputBox/index.jsx

📝 Walkthrough

Walkthrough

Updated InputBox post-answer focus handling to safely skip focusing when the input reference is unavailable.

Changes

Input focus safety

Layer / File(s) Summary
Guard post-answer focus
src/components/InputBox/index.jsx
Uses optional chaining before calling focus() when focusAfterAnswer is enabled.

Estimated code review effort: 1 (Trivial) | ~2 minutes

Suggested reviewers: josstorer, snoopy83

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: guarding deferred input focus when async config completes after unmount.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@qodo-code-review

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Guard InputBox focus when async config resolves after unmount

🐞 Bug fix 🕐 Less than 10 minutes

Grey Divider

AI Description

• Prevent null-ref errors when deferred focus runs after InputBox unmounts
• Preserve existing focus behavior when the component remains mounted
Diagram

graph TD
  A["InputBox"] --> B["useEffect enabled"] --> C["getUserConfig()"] --> D{focusAfterAnswer?} --> E["inputRef.current?.focus()"]
  subgraph Legend
    direction LR
    _cmp[Component] ~~~ _dec{Decision} ~~~ _act[Action]
  end
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Effect cleanup with isMounted flag
  • ➕ Makes the async-to-unmount boundary explicit and easy to extend for other side effects
  • ➕ Allows early-return for multiple statements, not just focus()
  • ➖ More code and state to maintain for a single guarded call
  • ➖ Easy to get wrong if reused broadly (stale closures, missed resets)
2. Cancelable config request (AbortController or cancellable promise)
  • ➕ Avoids doing unnecessary work after unmount
  • ➕ Scales well if config loading triggers additional downstream effects
  • ➖ Requires changing getUserConfig() API and storage access patterns
  • ➖ Overkill for a simple ref-usage guard

Recommendation: The optional chaining guard is the best fit here: it preserves the mounted behavior and safely no-ops after unmount with minimal surface-area change. More explicit cancellation/cleanup patterns are reasonable if additional async side effects are added later.

Files changed (1) +1 / -1

Bug fix (1) +1 / -1
index.jsxGuard deferred focus call when input ref is cleared +1/-1

Guard deferred focus call when input ref is cleared

• Changes the async config-driven focus path to use optional chaining on the textarea ref. Prevents a runtime exception when getUserConfig() resolves after InputBox unmounts.

src/components/InputBox/index.jsx

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces optional chaining to safely focus the input box, preventing errors if the ref is not yet set. The review feedback suggests further improving the robustness of this asynchronous effect by handling potential promise rejections and implementing a cleanup flag to prevent executing callbacks after the component has unmounted.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/components/InputBox/index.jsx
@qodo-code-review

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Qodo Logo

@kilo-code-bot

kilo-code-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Overview

The single-line change in src/components/InputBox/index.jsx:90 converts inputRef.current.focus() to inputRef.current?.focus(), correctly guarding the deferred focus against a null ref after InputBox unmounts. This precisely matches the PR's stated intent and is minimal, changing no behavior when the component remains mounted.

Adjacent robustness concerns raised by gemini-code-assist[bot] on line 92 (unhandled getUserConfig() rejection and a missing isMounted cleanup flag) are valid but are already tracked in an active review comment, so they are not duplicated here.

Files Reviewed (1 file)
  • src/components/InputBox/index.jsx

Reviewed by hy3:free · Input: 29.6K · Output: 4.5K · Cached: 96.5K

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents a runtime error in the InputBox component by safely guarding a deferred focus() call that can occur after the component has unmounted (e.g., while getUserConfig() is still resolving from extension storage).

Changes:

  • Replaced a direct inputRef.current.focus() call with optional chaining (inputRef.current?.focus()) to avoid throwing when the ref is cleared on unmount.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@PeterDaveHello PeterDaveHello merged commit 9c92aa1 into ChatGPTBox-dev:master Jul 13, 2026
4 checks passed
@PeterDaveHello PeterDaveHello deleted the guard-input-focus-after-unmount branch July 13, 2026 19:32
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.

2 participants