Skip to content

fix(chat): guard undefined event.key in type-to-focus handler - #639

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/guard-undefined-key-type-to-focus
Draft

fix(chat): guard undefined event.key in type-to-focus handler#639
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/guard-undefined-key-type-to-focus

Conversation

@posthog

@posthog posthog Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Summary

The document-level type-to-focus keydown handler read event.key.length without checking that event.key exists (shouldRouteTypingToTextInput in src/hooks/use-type-to-focus-text-input.ts). On some keydown events — notably Android Chrome soft keyboards, which fire keydown with keyCode 229 and no key during IME composition — event.key is undefined, so reading .length threw an uncaught TypeError: Cannot read properties of undefined (reading 'length').

This adds a nullish guard (if (!event.key || event.key.length !== 1) return false;) before the length check. The existing event.isComposing guard doesn't cover all the Android cases where key comes through undefined. Also adds a regression test for the predicate.

Why

Users typing into the AI chat composer were hitting a recurring uncaught production TypeError on Android soft keyboards. It's low volume but confirmed firing in the live code (resurfaced ~2.5 months after an earlier occurrence in the pre-snapshot code). The core flow still worked (users can tap to focus), so this is a low-risk, contained fix.


Created with PostHog Code from an inbox report.


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.

Review in cubic

The document-level keydown handler read `event.key.length` without checking that `event.key` exists. On some keydown events — notably Android Chrome soft keyboards, which fire keydown with keyCode 229 and no `key` during IME composition — `event.key` is undefined, throwing an uncaught `TypeError: Cannot read properties of undefined (reading 'length')`.

Add a nullish guard before the length check. The existing `event.isComposing` guard does not cover all Android cases where `key` comes through undefined.

Generated-By: PostHog Code
Task-Id: f9a1223a-c963-4b1c-bca6-a47a81c3c8b2
@github-actions

Copy link
Copy Markdown

React Doctor found no new issues. 🎉

Reviewed by React Doctor for commit b41b8e3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

0 participants