From 213703a70236e81082cd72b8bc320591989857ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20Lef=C3=A9v=C3=A8re-Laoide?= <90181748+FredLL-Avaiga@users.noreply.github.com> Date: Wed, 21 Aug 2024 09:17:21 +0200 Subject: [PATCH] add sender icon and name if sender is described in users dict (#1686) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit resolves #1670 Co-authored-by: Fred Lefévère-Laoide --- .../src/components/Taipy/Chat.spec.tsx | 2 +- .../taipy-gui/src/components/Taipy/Chat.tsx | 66 +++++++++++-------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/frontend/taipy-gui/src/components/Taipy/Chat.spec.tsx b/frontend/taipy-gui/src/components/Taipy/Chat.spec.tsx index cba7fe20dd..aaebc2b912 100644 --- a/frontend/taipy-gui/src/components/Taipy/Chat.spec.tsx +++ b/frontend/taipy-gui/src/components/Taipy/Chat.spec.tsx @@ -48,7 +48,7 @@ describe("Chat Component", () => { it("uses the class", async () => { const { getByText } = render(); const elt = getByText(searchMsg); - expect(elt.parentElement?.parentElement?.parentElement?.parentElement).toHaveClass("taipy-chat"); + expect(elt.parentElement?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement).toHaveClass("taipy-chat"); }); it("can display an avatar", async () => { const { getByAltText } = render(); diff --git a/frontend/taipy-gui/src/components/Taipy/Chat.tsx b/frontend/taipy-gui/src/components/Taipy/Chat.tsx index 84f097ea63..1287453b99 100644 --- a/frontend/taipy-gui/src/components/Taipy/Chat.tsx +++ b/frontend/taipy-gui/src/components/Taipy/Chat.tsx @@ -37,6 +37,7 @@ import { LoVElt, useLovListMemo } from "./lovUtils"; import { IconAvatar, avatarSx } from "../../utils/icon"; import { emptyArray, getInitials } from "../../utils"; import { RowType, TableValueType } from "./tableUtils"; +import { Stack } from "@mui/material"; interface ChatProps extends TaipyActiveProps { messages?: TableValueType; @@ -56,7 +57,11 @@ const indicWidth = 0.7; const avatarWidth = 24; const chatAvatarSx = { ...avatarSx, width: avatarWidth, height: avatarWidth }; const avatarColSx = { width: 1.5 * avatarWidth }; -const senderMsgSx = { width: "fit-content", maxWidth: "80%", marginLeft: "auto" }; +const senderMsgSx = { + width: "fit-content", + maxWidth: "80%", + color: (theme: Theme) => theme.palette.text.disabled, +} as SxProps; const gridSx = { pb: "1em", mt: "unset", flex: 1, overflow: "auto" }; const loadMoreSx = { width: "fit-content", marginLeft: "auto", marginRight: "auto" }; const inputSx = { maxWidth: "unset" }; @@ -79,6 +84,7 @@ const senderPaperSx = { top: "0", right: `-${indicWidth}em`, }, + color: (theme: Theme) => theme.palette.text.disabled, } as SxProps; const otherPaperSx = { position: "relative", @@ -118,41 +124,42 @@ interface ChatRowProps { message: string; name: string; className?: string; - getAvatar: (id: string) => ReactNode; + getAvatar: (id: string, sender: boolean) => ReactNode; index: number; } const ChatRow = (props: ChatRowProps) => { const { senderId, message, name, className, getAvatar, index } = props; - return senderId == name ? ( - - - - {message} - - - - ) : ( + const sender = senderId == name; + const avatar = getAvatar(name, sender); + return ( - - - {name} - - - - {getAvatar(name)} - - - - {message} - + + {avatar ? ( + + + + {name} + + + {avatar} + + {message} + + + + ) : ( + + {message} + + )} ); @@ -238,12 +245,13 @@ const Chat = (props: ChatProps) => { }, [users]); const getAvatar = useCallback( - (id: string) => - avatars[id] || ( + (id: string, sender: boolean) => + avatars[id] || + (sender ? null : ( {getInitials(id)} - ), + )), [avatars] );