this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Ensure mute state stays in sync after mutation (#10067)

authored by

DS Boyce and committed by
GitHub
876e2016 2a87f6f6

+56 -8
+10
src/state/messages/convo/agent.ts
··· 850 850 this.commit() 851 851 } 852 852 853 + updateMuted(muted: boolean) { 854 + if (this.convo) { 855 + this.convo = { 856 + ...this.convo, 857 + muted, 858 + } 859 + } 860 + this.commit() 861 + } 862 + 853 863 async processPendingMessages() { 854 864 logger.debug( 855 865 `processing messages (${this.pendingMessages.size} remaining)`,
+15
src/state/messages/convo/index.tsx
··· 119 119 }) 120 120 }, [convo, queryClient]) 121 121 122 + useEffect(() => { 123 + const [root, id] = getConvoKey(convoId) 124 + return queryClient.getQueryCache().subscribe(event => { 125 + const queryKey = event.query.queryKey as string[] 126 + if (queryKey[0] === root && queryKey[1] === id) { 127 + const data = event.query.state.data as 128 + | ChatBskyConvoDefs.ConvoView 129 + | undefined 130 + if (data && convo.convo && data.muted !== convo.convo.muted) { 131 + convo.updateMuted(data.muted) 132 + } 133 + } 134 + }) 135 + }, [convo, convoId, queryClient]) 136 + 122 137 return <ChatContext.Provider value={service}>{children}</ChatContext.Provider> 123 138 }
+31 -8
src/state/queries/messages/mute-conversation.ts
··· 44 44 return data 45 45 } 46 46 }, 47 - onSuccess: (data, params) => { 47 + onMutate: ({mute}) => { 48 + if (!convoId) return 49 + 50 + const prevConvo = queryClient.getQueryData<ChatBskyConvoDefs.ConvoView>( 51 + CONVO_KEY(convoId), 52 + ) 53 + const prevListEntries = queryClient.getQueriesData< 54 + InfiniteData<ChatBskyConvoListConvos.OutputSchema> 55 + >({queryKey: [CONVO_LIST_KEY]}) 56 + 57 + // Update for a single chat thread 48 58 queryClient.setQueryData<ChatBskyConvoDefs.ConvoView>( 49 - CONVO_KEY(data.convo.id), 59 + CONVO_KEY(convoId), 50 60 prev => { 51 61 if (!prev) return 52 62 return { 53 63 ...prev, 54 - muted: params.mute, 64 + muted: mute, 55 65 } 56 66 }, 57 67 ) 58 - queryClient.setQueryData< 68 + 69 + // Update for the chat list 70 + queryClient.setQueriesData< 59 71 InfiniteData<ChatBskyConvoListConvos.OutputSchema> 60 - >([CONVO_LIST_KEY], prev => { 72 + >({queryKey: [CONVO_LIST_KEY]}, prev => { 61 73 if (!prev?.pages) return 62 74 return { 63 75 ...prev, 64 76 pages: prev.pages.map(page => ({ 65 77 ...page, 66 78 convos: page.convos.map(convo => { 67 - if (convo.id !== data.convo.id) return convo 79 + if (convo.id !== convoId) return convo 68 80 return { 69 81 ...convo, 70 - muted: params.mute, 82 + muted: mute, 71 83 } 72 84 }), 73 85 })), 74 86 } 75 87 }) 76 88 89 + return {prevConvo, prevListEntries} 90 + }, 91 + onSuccess: data => { 77 92 onSuccess?.(data) 78 93 }, 79 - onError: e => { 94 + onError: (e, _variables, context) => { 95 + if (context?.prevConvo && convoId) { 96 + queryClient.setQueryData(CONVO_KEY(convoId), context.prevConvo) 97 + } 98 + if (context?.prevListEntries) { 99 + for (const [key, data] of context.prevListEntries) { 100 + queryClient.setQueryData(key, data) 101 + } 102 + } 80 103 onError?.(e) 81 104 }, 82 105 })