wacrm/src/app/(dashboard)
Arnas Donauskas 138ae8d6f4
fix(inbox): replace broken updater-closure flag, add reconnect/visibility resync
Addresses #106 (issue stays open until the reporter confirms in prod).

#105's `let foundInList = false; setState(p => { foundInList = ...; ... })`
pattern is broken: setState updaters run during reconciliation, *after*
the synchronous handler returns, so `foundInList` reads `false` in the
same tick and `hydrateConversation()` fires on every code path the
flag was meant to gate. Every incoming message and every conversation
UPDATE (read receipts, status flips, agent assignment) triggered a
redundant DB fetch, queuing HTTP behind realtime WS frames on the same
supabase client and starving the channel — which surfaced as exactly
what #106 reports: messages that never arrive, threads stuck on "No
messages yet", events landing out of order.

The fix:

1. Synchronous known-IDs ref. `knownConvIdsRef` mirrors the conv ids
   currently in state, kept in sync via a useEffect on `conversations`.
   Event handlers check it synchronously to decide between a cheap
   in-place patch and a hydrate refetch — no more closure-flag pattern.

2. Reconnect + visibility resync. Realtime is best-effort: events sent
   while the WS was disconnected or while the tab was background-throttled
   are gone. A `resyncToken` state bumped on (a) `isConnected` false→true
   transitions after the initial connect and (b) tab visibility →
   visible drives `ConversationList` and `MessageThread` to refire their
   fetch effects. The reactions effect in MessageThread is split into
   fetch + channel-subscribe so a resync only refetches the rows
   without tearing down and rebuilding the realtime channel.

3. Keep the conv-INSERT hydrate. Still the right fix for #104 (realtime
   payloads never carry the `contact` join); now it only fires when the
   conversation is genuinely unknown to state.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 19:30:48 +03:00
..
automations Merge remote-tracking branch 'origin/main' into chore/whatsapp-trademark-symbol 2026-05-06 13:50:03 +03:00
broadcasts chore: switch accent palette to Hostinger violet, drop emerald green 2026-05-06 11:57:12 +03:00
contacts chore: switch accent palette to Hostinger violet, drop emerald green 2026-05-06 11:57:12 +03:00
dashboard Merge pull request #44 from ArnasDon/fix/dashboard-activity-feed-pagination 2026-04-17 22:39:18 +03:00
inbox fix(inbox): replace broken updater-closure flag, add reconnect/visibility resync 2026-05-21 19:30:48 +03:00
pipelines chore: switch accent palette to Hostinger violet, drop emerald green 2026-05-06 11:57:12 +03:00
settings Merge remote-tracking branch 'origin/main' into chore/whatsapp-trademark-symbol 2026-05-06 13:50:03 +03:00
dashboard-shell.tsx chore: switch accent palette to Hostinger violet, drop emerald green 2026-05-06 11:57:12 +03:00
layout.tsx feat(seo): full SEO pass (reapply with OG-image build fix) 2026-04-19 12:25:59 +03:00