Skip to content

Commit

Permalink
fix dynamic chat reaction icons (#6815)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 authored May 9, 2024
1 parent d5d4237 commit 41aa5f8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
27 changes: 19 additions & 8 deletions panel/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,16 @@ def __init__(self, object=None, **params):
elif state.browser_info and state.browser_info.timezone:
tz = ZoneInfo(state.browser_info.timezone)
params["timestamp"] = datetime.datetime.now(tz=tz)

reaction_icons = params.get("reaction_icons", {"favorite": "heart"})
if isinstance(reaction_icons, dict):
params["reaction_icons"] = ChatReactionIcons(
options=reaction_icons, width=15, height=15,
value=params.get('reactions', []),
)
params["reaction_icons"] = ChatReactionIcons(options=reaction_icons)
self._internal = True
super().__init__(object=object, **params)
self.chat_copy_icon = ChatCopyIcon(
visible=False, width=15, height=15, css_classes=["copy-icon"],
stylesheets=self._stylesheets + self.param.stylesheets.rx(),
)
self.reaction_icons.stylesheets = self._stylesheets + self.param.stylesheets.rx()
self.reaction_icons.link(self, value="reactions", bidirectional=True)
self.reaction_icons.visible = self.param.show_reaction_icons
if not self.avatar:
self._update_avatar()
self._build_layout()
Expand All @@ -287,12 +282,13 @@ def _build_layout(self):
self._update_chat_copy_icon()
self._center_row = Row(
self._object_panel,
self.param.reaction_icons.rx(),
self._render_reaction_icons(),
css_classes=["center"],
stylesheets=self._stylesheets + self.param.stylesheets.rx(),
sizing_mode=None
)
self.param.watch(self._update_object_pane, "object")
self.param.watch(self._update_reaction_icons, "reaction_icons")

self._user_html = HTML(
self.param.user, height=20, css_classes=["name"],
Expand Down Expand Up @@ -630,6 +626,21 @@ def _update_avatar_pane(self, event=None):
del params['name']
self._left_col[0].param.update(**params)

def _render_reaction_icons(self):
reaction_icons = self.reaction_icons
reaction_icons.param.update(
width=15,
height=15,
value=self.param.reactions,
stylesheets=self._stylesheets + self.param.stylesheets.rx(),
visible=self.param.show_reaction_icons
)
reaction_icons.link(self, value="reactions", bidirectional=True)
return reaction_icons

def _update_reaction_icons(self, _):
self._center_row[1] = self._render_reaction_icons()

def _update(self, ref, old_models):
"""
Internals will be updated inplace.
Expand Down
16 changes: 15 additions & 1 deletion panel/tests/chat/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_layout(self):
assert isinstance(object_pane, Markdown)
assert object_pane.object == "ABC"

icons = center_row[1][0][0].object()
icons = center_row[1]
assert isinstance(icons, ChatReactionIcons)

footer_col = columns[1][2]
Expand All @@ -63,6 +63,20 @@ def test_layout(self):
timestamp_pane = footer_col[2]
assert isinstance(timestamp_pane, HTML)

def test_reactions_dynamic(self):
message = ChatMessage(reactions=["favorite"])
assert message.reaction_icons.value == ["favorite"]

message.reactions = ["thumbs-up"]
assert message.reaction_icons.value == ["thumbs-up"]

def test_reaction_icons_dynamic(self):
message = ChatMessage(reaction_icons={"favorite": "heart"})
assert message.reaction_icons.options == {"favorite": "heart"}

message.reaction_icons = ChatReactionIcons(options={"like": "thumb-up"})
assert message._center_row[1] == message.reaction_icons

def test_reactions_link(self):
# on init
message = ChatMessage(reactions=["favorite"])
Expand Down

0 comments on commit 41aa5f8

Please sign in to comment.