Hardening a Shared Support Inbox

How a React and Supabase support inbox keeps messages in the right ticket, authorizes receipts, and reconciles missed updates after reconnecting.

Client
Portfolio client and staff support inbox
Focus
Web Application Engineering
Year
2026
Reading time
5 min read • 864 words

Key outcomes

Authorized receipts, preserved active threads, and verified message recovery.

  • Scoped receipt writes to authorized recipients and enforced forward-only status changes.
  • Merged reply responses and realtime events by ID across client and staff views.
  • Refreshed active threads after resubscription or window focus to recover missing messages.
  • Realtime
  • Support inbox
  • React
  • Supabase
  • Message receipts
  • State coordination

I designed and implemented the shared support inbox used by clients and staff in my client portal. Clients follow individual ticket threads, while staff work across tickets from multiple companies. The two views share a realtime hook and a message model, with separate APIs for posting replies and retrieving ticket history.

A reply can reach the browser twice, first as a response to the send request and again as a realtime event. A receipt can arrive out of order. A detail request can finish after the user has opened a different ticket. The hardening work addressed each of those cases so the thread could keep its messages, retain the latest receipt, and stay tied to the ticket the user selected.

Merge the reply and its realtime echo

The reply API saves a row and returns its message ID. The database publishes an insert event for that same row. The response lets the sender see the saved reply promptly, and the event lets the other side receive it without refreshing.

The client previously built its next message list from the array it held when the send began. If staff replied while that request was pending, the new message could appear and then disappear when the response arrived. The row was still saved. The problem was that the browser replaced its newer list with an older one.

Both views now merge messages into the current thread by ID. That gives a saved reply and its realtime echo one place in the list and preserves other messages received during the request. The staff view can merge the saved reply directly into the thread. The staff ticket list still refreshes separately because it serves a different purpose from the open conversation.

sequenceDiagram
    accTitle: A support reply and its receipt
    accDescr: A reply response and a realtime insert can reach the sender in either order. Each view merges by message ID. The recipient requests a receipt update, which returns to the sender as a realtime event.
    participant S as Sender
    participant A as API
    participant D as Database
    participant R as Recipient
    S->>A: Post reply
    A->>D: Save message as sent
    par Reply response
        A-->>S: Return saved message
        S->>S: Merge by message ID
    and Realtime insert
        D-->>S: Saved message event
        S->>S: Merge by message ID
        D-->>R: Saved message event
        R->>R: Merge into active ticket
    end
    R->>A: Request receipt update
    A->>A: Check recipient access
    A->>D: Advance status if newer
    D-->>S: Receipt update event

Authorize receipts and keep them moving forward

A message starts at sent. A matching realtime event can trigger delivered on the receiving side. Loading the incoming message in the active thread can trigger seen. The receipt belongs to the message and records the receiving side’s acknowledgement. It represents application state rather than an individual reader’s attention.

Because the receipt route uses a privileged database client, it verifies the signed-in user and checks whether they can acknowledge that message. A client can acknowledge a staff reply on a ticket belonging to their company. A staff user with ticket access can acknowledge a client reply. The browser’s database read access still supports the subscription, and the migration moves receipt updates behind the API.

The state check is part of the database update. A delivered request can advance a message from sent, and seen can advance it from either earlier state. If seen arrives first, a later delivered request leaves it in place. Retries return successfully without writing the same receipt again. The browser uses the same ordering when it merges a fresh snapshot with messages already in view.

Keep asynchronous work attached to its ticket

Switching tickets changes more than the realtime subscription. It changes which requests and reply responses can still affect the visible thread. For example, a slow response for ticket A might arrive after ticket B has already loaded.

Both inboxes now track the current selection when requesting ticket details and discard superseded responses. Realtime events and completed replies also check which ticket they belong to before changing the thread. Subscription cleanup stops old callbacks from updating the view after the channel has been replaced. These checks cover the separate paths by which an earlier ticket could affect the one now on screen.

Reconcile after reconnecting

The hook refreshes the open ticket when it subscribes again or the window regains focus. The refresh fetches the current ticket history and merges it with the messages already present. That recovers rows missing from the local list while preserving events received during the fetch.

The merge matters even when a message appears in both sources. A snapshot can contain delivered for a row whose seen event has already reached the browser. Keeping the furthest receipt state lets the refresh fill in missing messages without rolling their indicators backward.

Verification

I completed verification of the fixes. The checks covered unauthorized receipt requests, repeated and reversed receipt updates, delayed ticket responses, reply echoes, and recovery snapshots. In Chromium, the staff inbox kept the newer ticket selected and retained live messages while merging a simulated reconnect snapshot. In an isolated PostgreSQL check, overlapping receipt updates finished at seen, and the migration blocked direct browser updates while preserving same-company reads.