1+ use crate :: client:: DiscordClient ;
12use components:: theme:: ActiveTheme ;
23use gpui:: prelude:: FluentBuilder ;
3- use gpui:: { div, img, px, AnyElement , App , IntoElement , ParentElement , RenderOnce , Rgba , Styled } ;
4+ use gpui:: { div, img, px, AnyElement , App , InteractiveElement , IntoElement , ParentElement , RenderOnce , Rgba , StatefulInteractiveElement , Styled } ;
45use scope_chat:: reaction:: MessageReactionType :: Normal ;
56use scope_chat:: reaction:: { MessageReaction , MessageReactionType , ReactionEmoji } ;
6- use serenity:: all:: ReactionType ;
7+ use serenity:: all:: { ChannelId , MessageId , ReactionType } ;
78use std:: fmt:: Debug ;
9+ use std:: sync:: Arc ;
810use MessageReactionType :: Burst ;
911
1012#[ derive( Clone , Debug ) ]
@@ -35,15 +37,21 @@ impl ReactionData {
3537 }
3638}
3739
38- #[ derive( Clone , Debug , IntoElement ) ]
40+ #[ derive( Clone , IntoElement ) ]
3941pub struct DiscordMessageReaction {
4042 pub data : ReactionData ,
43+ pub ( crate ) client : Arc < DiscordClient > ,
44+ pub ( crate ) message_id : MessageId ,
45+ pub ( crate ) channel_id : ChannelId ,
4146}
4247
4348impl DiscordMessageReaction {
44- pub fn from_message ( reaction : & serenity:: all:: MessageReaction ) -> Self {
49+ pub fn new ( reaction : & serenity:: all:: MessageReaction , client : Arc < DiscordClient > , message_id : MessageId , channel_id : ChannelId ) -> Self {
4550 DiscordMessageReaction {
4651 data : ReactionData :: Message ( reaction. clone ( ) ) ,
52+ client,
53+ message_id,
54+ channel_id,
4755 }
4856 }
4957
@@ -71,6 +79,18 @@ impl DiscordMessageReaction {
7179 ReactionEmoji :: Custom { url, .. } => img ( url. clone ( ) ) . w ( px ( 16f32 ) ) . h ( px ( 16f32 ) ) . into_any_element ( ) ,
7280 }
7381 }
82+
83+ fn handle_click ( & self , app : & App ) {
84+ let reaction = self . clone ( ) ;
85+ let had_reaction = reaction. get_self_reaction ( ) . is_some ( ) ;
86+ app. spawn ( |_| async move {
87+ if had_reaction {
88+ reaction. client . remove_reaction ( reaction. channel_id , reaction. message_id , reaction. get_emoji ( ) ) . await ;
89+ } else {
90+ reaction. client . add_reaction ( reaction. channel_id , reaction. message_id , reaction. get_emoji ( ) ) . await ;
91+ }
92+ } ) . detach ( ) ;
93+ }
7494}
7595
7696impl MessageReaction for DiscordMessageReaction {
@@ -165,6 +185,10 @@ impl RenderOnce for DiscordMessageReaction {
165185 . gap_1 ( )
166186 . child ( Self :: render_emoji ( & emoji) )
167187 . child ( self . get_count ( None ) . to_string ( ) )
188+ . id ( "reaction" )
189+ . on_click ( move |_, _, app| {
190+ self . handle_click ( app) ;
191+ } )
168192 }
169193}
170194
@@ -183,3 +207,9 @@ pub fn discord_reaction_to_emoji(reaction: &ReactionType) -> ReactionEmoji {
183207 }
184208 }
185209}
210+
211+ impl Debug for DiscordMessageReaction {
212+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
213+ f. debug_struct ( "DiscordMessageReaction" ) . field ( "data" , & self . data ) . finish ( )
214+ }
215+ }
0 commit comments