@@ -615,70 +615,65 @@ def store_feedback(
615615
616616
617617def open_citation (channel : str , timestamp : str , message : Any , params : Dict [str , Any ], client : WebClient ) -> None :
618- """
619- Open citation - update/ replace message to include citation content
620- """
618+ """Open citation - update/replace message to include citation content"""
621619 logger .info ("Opening citation" , extra = {"channel" : channel , "timestamp" : timestamp })
622- # Get Message
623620 try :
624- # Get citation details
621+ # Citation details
625622 title : str = params .get ("title" , "No title available." )
626623 body : str = params .get ("body" , "No citation text available." )
627624 source_number : str = params .get ("source_number" )
625+ link : str = params .get ("link" , "" )
628626
629- # Remove any citation block (and divider), if it exists
627+ # Remove any existing citation block/ divider
630628 blocks = message .get ("blocks" , [])
631- blocks = [block for block in blocks if block .get ("block_id" ) not in ["citation_block" , "citation_divider" ]]
632-
633- # Add formatting
634- title = f"*{ title .replace ("\n " , "" )} *"
635- body = f"> { body .replace ("\n " , "\n > " )} "
636- link : str = params .get ("link" , "" )
629+ blocks = [b for b in blocks if b .get ("block_id" ) not in ["citation_block" , "citation_divider" ]]
637630
638- # Highlight selected citation by updating the button style
639- logger .info ("Searching for Citation" , extra = {"cit_n" : source_number , "cit_title" : title })
631+ # Format text
632+ title = f"*{ title .replace ('\n ' , '' )} *"
633+ body = f"> { body .replace ('\n ' , '\n > ' )} "
640634
641- active_id = None
642635 current_id = f"cite_{ source_number } "
636+ selected = False
637+
638+ # Reset all button styles, then set the clicked one
643639 for block in blocks :
644640 if block .get ("type" ) == "actions" :
645641 for element in block .get ("elements" , []):
646642 if element .get ("type" ) == "button" :
647643 action_id = element .get ("action_id" )
648-
649- if element .get ("style" ):
650- active_id = action_id
651-
652- if action_id == current_id and active_id != current_id :
653- logger .info (
654- "Citation found - set active" , extra = {"cit_n" : source_number , "cit_title" : title }
655- )
656- element ["style" ] = "primary"
644+ if action_id == current_id :
645+ # Toggle: if already styled, unselect; else select
646+ if element .get ("style" ) == "primary" :
647+ element .pop ("style" , None )
648+ selected = False
649+ else :
650+ element ["style" ] = "primary"
651+ selected = True
657652 else :
653+ # Unselect all other buttons
658654 element .pop ("style" , None )
659655
660- if active_id :
661- # Add citation content before feedback block
656+ # If selected, insert citation block before feedback
657+ if selected :
662658 citation_block = {
663659 "type" : "section" ,
664660 "text" : {
665661 "type" : "mrkdwn" ,
666662 "text" : (
667663 f"{ title } \n \n { body } \n \n <{ link } |View Source>"
668664 if link and link != "none"
669- else f"* { title } * \n \n { body } "
665+ else f"{ title } \n \n { body } "
670666 ),
671667 },
672668 "block_id" : "citation_block" ,
673669 }
674-
675- # Find index of feedback block to insert before it
676- feedback_block_index = next (
677- (i for i , block in enumerate (blocks ) if block .get ("block_id" ) == "feedback-divider" ), len (blocks )
670+ feedback_index = next (
671+ (i for i , b in enumerate (blocks ) if b .get ("block_id" ) == "feedback-divider" ),
672+ len (blocks ),
678673 )
679- blocks .insert (feedback_block_index , citation_block )
674+ blocks .insert (feedback_index , citation_block )
680675
681- # Update message with new blocks
676+ # Update Slack message
682677 logger .info ("Updated message body" , extra = {"blocks" : blocks })
683678 client .chat_update (channel = channel , ts = timestamp , blocks = blocks )
684679
0 commit comments