Skip to content

Conversation

@CaioBolognesi
Copy link

@CaioBolognesi CaioBolognesi commented Jan 8, 2026

Pull Request

📋 Description

This PR fixes a frontend crash in the chat UI caused by malformed or incomplete message payloads coming from the backend.

In production, some messages arrive with message set to null (or missing nested fields), while the UI assumed the presence of properties like contactMessage, resulting in a runtime error:

Cannot read properties of null (reading 'contactMessage')

The fix adds proper null-guards and safe fallbacks when rendering messages, preventing the entire conversation view from breaking.

Note: This is my first contribution fixing a bug in this repository.
While I cannot guarantee that this covers all possible edge cases, the change successfully reproduces and fixes the reported crash in my local environment. The goal of this PR is to prevent the UI from breaking when receiving malformed or null message payloads, as observed in production.
Feedback and suggestions are very welcome.


🔗 Related Issues

  • Fixes crash when opening conversations containing contact messages with null payloads
  • Related to production runtime error: Cannot read properties of null (reading 'contactMessage')

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

🧪 Testing

Test Environment

  • Local development

Test Cases

  • Unit tests pass (no tests defined in the project)
  • Integration tests pass (not available)
  • Manual testing completed
  • Cross-browser testing (not applicable)
  • Mobile testing (not applicable)

Test Instructions

  1. Run the project locally.
  2. Open a conversation containing a contact message with malformed or null payload.
  3. Verify that the chat renders without crashing and shows a safe fallback for the message.

📸 Screenshots

Captura de Tela 2026-01-08 às 15 31 04

Before

  • Application crashes with:

    Cannot read properties of null (reading 'contactMessage')
    

After

  • Conversation opens normally.
  • Message renders using a safe fallback without breaking the UI.

✅ Checklist

Code Quality

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • My changes generate no new errors
  • I have run npm run lint and fixed blocking issues (warnings only, pre-existing)

Testing

  • I have manually tested the fix locally
  • The fix prevents the reported crash
  • Automated tests added (not applicable — no test setup exists)

Documentation

  • Documentation update not required (no public API changes)

Internationalization

  • Not applicable

Breaking Changes

  • This PR introduces no breaking changes

🔄 Migration Guide

Not applicable — no API or behavioral changes for consumers.


📝 Additional Notes

This issue only surfaced in production due to inconsistent message payloads.
The fix ensures the UI is resilient to malformed messages without assuming backend correctness.


🤝 Reviewer Guidelines

Focus Areas

  • Code logic and correctness
  • User experience (no more hard crashes)
  • Edge case handling for malformed messages

Testing Checklist for Reviewers

  • Pull and run the project locally
  • Open a conversation with contact messages
  • Verify the UI no longer crashes

Summary by Sourcery

Handle null or malformed chat messages without crashing the conversation view.

Bug Fixes:

  • Prevent chat UI from crashing when rendering messages with null or missing message payloads by handling invalid message objects early.

Enhancements:

  • Display a generic fallback text for invalid or unsupported chat messages using localized translations across supported languages.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 8, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds a defensive early-return in the chat message renderer for null/invalid message payloads and introduces a localized fallback string for invalid or unsupported messages across all supported languages.

Sequence diagram for rendering a chat message with null-guard fallback

sequenceDiagram
  actor User
  participant ChatUI
  participant MessageList
  participant MessageContent
  participant I18n

  User->>ChatUI: OpenConversation(conversationId)
  ChatUI->>MessageList: FetchAndRenderMessages(conversationId)
  loop For each message
    MessageList->>MessageContent: Render(message)
    alt message.message is null or undefined
      MessageContent->>I18n: t(chat.message.invalidOrUnsupported)
      I18n-->>MessageContent: localizedFallbackText
      MessageContent-->>MessageList: RenderFallbackDiv(localizedFallbackText)
    else message.message is valid
      MessageContent-->>MessageList: RenderByMessageType(message.messageType)
    end
  end
  MessageList-->>ChatUI: ConversationDOM
  ChatUI-->>User: DisplayConversation()
Loading

Flow diagram for MessageContent rendering with null guard

flowchart TD
  A[Start render MessageContent] --> B{Is message present?}
  B -->|No or message is null| C["Call t(chat.message.invalidOrUnsupported)"]
  C --> D[Render fallback div with muted styling]
  D --> E[End render]

  B -->|Yes| F{Is message.message present?}
  F -->|No| C
  F -->|Yes| G[Read messageType]
  G --> H[Switch on messageType and render specific message UI]
  H --> E
Loading

File-Level Changes

Change Details Files
Add null-guard and fallback UI in MessageContent to prevent crashes on malformed messages.
  • Inject useTranslation hook into MessageContent to access i18n strings.
  • Add an early-return branch when message or message.message is falsy to render a small muted fallback message instead of accessing nested fields.
  • Keep existing messageType-based switch logic unchanged for valid messages.
src/pages/instance/Chat/messages.tsx
Add i18n copy for the invalid/unsupported message fallback in all supported locales.
  • Introduce the chat.message.invalidOrUnsupported translation key in the English locale.
  • Add equivalent translation entries in Spanish, French, and Brazilian Portuguese locale files, aligned with existing chat-related keys.
src/translate/languages/en-US.json
src/translate/languages/es-ES.json
src/translate/languages/fr-FR.json
src/translate/languages/pt-BR.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The early if (!message?.message) return in MessageContent changes behavior for all message types; if the crash only affects contact messages, consider narrowing this guard (e.g. by checking messageType first) to avoid hiding other valid-but-empty message contents.
  • When encountering invalid message payloads, it may be helpful to add some lightweight logging/telemetry (even just console.warn behind a dev flag) so malformed messages can be diagnosed rather than only showing a generic fallback in the UI.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The early `if (!message?.message)` return in `MessageContent` changes behavior for all message types; if the crash only affects contact messages, consider narrowing this guard (e.g. by checking `messageType` first) to avoid hiding other valid-but-empty message contents.
- When encountering invalid message payloads, it may be helpful to add some lightweight logging/telemetry (even just `console.warn` behind a dev flag) so malformed messages can be diagnosed rather than only showing a generic fallback in the UI.

## Individual Comments

### Comment 1
<location> `src/pages/instance/Chat/messages.tsx:146` </location>
<code_context>
+  const { t } = useTranslation();
+
+  // Early return for invalid messages
+  if (!message?.message) {
+    return (
+      <div className="text-xs text-muted-foreground bg-muted p-2 rounded max-w-xs">
</code_context>

<issue_to_address>
**issue (bug_risk):** Narrow the invalid-message check to avoid treating valid falsy values as unsupported.

`!message?.message` will mark any falsy value (empty string, `0`, `false`) as invalid/unsupported. If empty strings or other falsy values can be valid, prefer a nullish check like `if (message?.message == null)` or explicitly whitelist the valid values so you don’t misclassify legitimate content as invalid.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

1 participant