@@ -660,6 +660,20 @@ function applyUnreadFlag(
660660 )
661661}
662662
663+ /**
664+ * Stops the list reads that could overwrite an optimistic flag. Answers whether one was owed by an
665+ * invalidation: the flag's write clears that mark, so the caller has to read the list again.
666+ */
667+ async function holdChatListReads (
668+ queryClient : ReturnType < typeof useQueryClient > ,
669+ owner : MothershipChatOwner | undefined
670+ ) : Promise < boolean > {
671+ const queryKey = mothershipChatKeys . ownerList ( owner )
672+ const owesRead = queryClient . getQueryState ( queryKey ) ?. isInvalidated === true
673+ await queryClient . cancelQueries ( { queryKey } )
674+ return owesRead
675+ }
676+
663677/**
664678 * Marks a chat as read with optimistic update.
665679 *
@@ -670,7 +684,8 @@ function applyUnreadFlag(
670684 * If there is no cached list yet (initial fetch still in flight, e.g. on
671685 * chat-page refresh), we skip cancellation entirely so the in-flight fetch
672686 * can resolve normally — otherwise it would be orphaned and never refetched.
673- * `onSuccess` then reconciles whichever state the fetch produced.
687+ * `onSuccess` then reconciles whichever state the fetch produced. A read an invalidation
688+ * asked for still runs: cancelling one for good would keep a deleted chat in the sidebar.
674689 */
675690export function useMarkMothershipChatRead ( owner ?: MothershipChatOwner ) {
676691 const queryClient = useQueryClient ( )
@@ -680,12 +695,12 @@ export function useMarkMothershipChatRead(owner?: MothershipChatOwner) {
680695 const previousChats = queryClient . getQueryData < MothershipChatMetadata [ ] > (
681696 mothershipChatKeys . ownerList ( owner )
682697 )
683- if ( ! previousChats ) return { previousChats }
698+ if ( ! previousChats ) return { previousChats, owesRead : false }
684699
685- await queryClient . cancelQueries ( { queryKey : mothershipChatKeys . ownerList ( owner ) } )
700+ const owesRead = await holdChatListReads ( queryClient , owner )
686701 applyUnreadFlag ( queryClient , owner , chatId , false )
687702
688- return { previousChats }
703+ return { previousChats, owesRead }
689704 } ,
690705 onSuccess : ( _data , chatId ) => {
691706 applyUnreadFlag ( queryClient , owner , chatId , false )
@@ -695,6 +710,10 @@ export function useMarkMothershipChatRead(owner?: MothershipChatOwner) {
695710 queryClient . setQueryData ( mothershipChatKeys . ownerList ( owner ) , context . previousChats )
696711 }
697712 } ,
713+ onSettled : ( _data , _error , _variables , context ) => {
714+ if ( context ?. owesRead )
715+ queryClient . invalidateQueries ( { queryKey : mothershipChatKeys . ownerList ( owner ) } )
716+ } ,
698717 } )
699718}
700719
@@ -712,12 +731,12 @@ export function useMarkMothershipChatUnread(owner?: MothershipChatOwner) {
712731 const previousChats = queryClient . getQueryData < MothershipChatMetadata [ ] > (
713732 mothershipChatKeys . ownerList ( owner )
714733 )
715- if ( ! previousChats ) return { previousChats }
734+ if ( ! previousChats ) return { previousChats, owesRead : false }
716735
717- await queryClient . cancelQueries ( { queryKey : mothershipChatKeys . ownerList ( owner ) } )
736+ const owesRead = await holdChatListReads ( queryClient , owner )
718737 applyUnreadFlag ( queryClient , owner , chatId , true )
719738
720- return { previousChats }
739+ return { previousChats, owesRead }
721740 } ,
722741 onSuccess : ( _data , chatId ) => {
723742 applyUnreadFlag ( queryClient , owner , chatId , true )
@@ -727,6 +746,10 @@ export function useMarkMothershipChatUnread(owner?: MothershipChatOwner) {
727746 queryClient . setQueryData ( mothershipChatKeys . ownerList ( owner ) , context . previousChats )
728747 }
729748 } ,
749+ onSettled : ( _data , _error , _variables , context ) => {
750+ if ( context ?. owesRead )
751+ queryClient . invalidateQueries ( { queryKey : mothershipChatKeys . ownerList ( owner ) } )
752+ } ,
730753 } )
731754}
732755
0 commit comments