@@ -64,19 +64,48 @@ export const useFetchReactions = ({
6464 } , [ fetchReactions , messageId , reactionType , sortString ] ) ;
6565
6666 useEffect ( ( ) => {
67- const listener = client . on ( 'offline_reactions.queried' , ( event ) => {
68- const { offlineReactions } = event ;
69- if ( offlineReactions ) {
70- setReactions ( offlineReactions ) ;
71- setLoading ( false ) ;
72- setNext ( undefined ) ;
73- }
67+ const listeners : ReturnType < typeof client . on > [ ] = [ ] ;
68+ listeners . push (
69+ client . on ( 'offline_reactions.queried' , ( event ) => {
70+ const { offlineReactions } = event ;
71+ if ( offlineReactions ) {
72+ setReactions ( offlineReactions ) ;
73+ setLoading ( false ) ;
74+ setNext ( undefined ) ;
75+ }
76+ } ) ,
77+ ) ;
78+
79+ [ 'reaction.new' , 'reaction.updated' ] . forEach ( ( eventType ) => {
80+ listeners . push (
81+ client . on ( eventType , ( event ) => {
82+ const { reaction } = event ;
83+
84+ if ( reaction && reaction . type === reactionType ) {
85+ setReactions ( ( prevReactions ) => [ reaction , ...prevReactions ] ) ;
86+ }
87+ } ) ,
88+ ) ;
7489 } ) ;
7590
91+ listeners . push (
92+ client . on ( 'reaction.deleted' , ( event ) => {
93+ const { reaction } = event ;
94+
95+ if ( reaction && reaction . type === reactionType ) {
96+ setReactions ( ( prevReactions ) =>
97+ prevReactions . filter ( ( r ) => r . user_id !== reaction . user_id ) ,
98+ ) ;
99+ }
100+ } ) ,
101+ ) ;
102+
76103 return ( ) => {
77- listener ?. unsubscribe ( ) ;
104+ listeners . forEach ( ( listener ) => {
105+ listener ?. unsubscribe ( ) ;
106+ } ) ;
78107 } ;
79- } , [ client ] ) ;
108+ } , [ client , reactionType ] ) ;
80109
81110 return { loading, loadNextPage, reactions } ;
82111} ;
0 commit comments