|
1 | 1 | import { useRef } from 'react'; |
2 | | -import { useTranslation } from 'react-i18next'; |
3 | 2 |
|
4 | | -import type { CardProps } from '@mui/material'; |
5 | | -import { Card, CardContent, CardHeader, styled } from '@mui/material'; |
| 3 | +import { Stack, Typography } from '@mui/material'; |
6 | 4 |
|
7 | | -import { useLocalContext } from '@graasp/apps-query-client'; |
8 | | -import { formatDate } from '@graasp/sdk'; |
9 | | - |
10 | | -import type { CommentAppData } from '@/config/appData'; |
11 | | -import { AppDataTypes } from '@/config/appData'; |
12 | | -import type { ChatbotPromptSettings } from '@/config/appSetting'; |
13 | | -import { ChatbotPromptSettingsKeys, SettingsKeys } from '@/config/appSetting'; |
14 | | -import { hooks } from '@/config/queryClient'; |
15 | 5 | import { buildCommentContainerDataCy } from '@/config/selectors'; |
16 | | -import { BIG_BORDER_RADIUS, DEFAULT_BOT_USERNAME } from '@/constants'; |
17 | 6 |
|
18 | 7 | import ChatbotAvatar from './ChatbotAvatar'; |
19 | 8 | import CommentBody from './CommentBody'; |
20 | 9 | import CustomAvatar from './CustomAvatar'; |
21 | 10 |
|
22 | | -const CustomCard = styled(Card)<CardProps>({ |
23 | | - borderRadius: BIG_BORDER_RADIUS, |
24 | | -}); |
25 | | - |
26 | 11 | type Props = { |
27 | | - comment: CommentAppData; |
| 12 | + body: string; |
| 13 | + isBot: boolean; |
| 14 | + id: string; |
| 15 | + username: string; |
28 | 16 | }; |
29 | 17 |
|
30 | | -function Comment({ comment }: Props): JSX.Element { |
31 | | - const { i18n } = useTranslation(); |
32 | | - |
33 | | - const { accountId } = useLocalContext(); |
34 | | - const { data: appContext } = hooks.useAppContext(); |
35 | | - const currentMember = appContext?.members.find((m) => m.id === accountId); |
36 | | - const { data: chatbotPrompts } = hooks.useAppSettings<ChatbotPromptSettings>({ |
37 | | - name: SettingsKeys.ChatbotPrompt, |
38 | | - }); |
39 | | - const chatbotPrompt = chatbotPrompts?.[0]; |
| 18 | +function Comment({ id, isBot, body, username }: Readonly<Props>): JSX.Element { |
40 | 19 | const commentRef = useRef<HTMLDivElement>(null); |
41 | 20 |
|
42 | | - const isBot = comment.type === AppDataTypes.BotComment; |
| 21 | + const avatar = isBot ? ( |
| 22 | + <ChatbotAvatar /> |
| 23 | + ) : ( |
| 24 | + <CustomAvatar username={username} /> |
| 25 | + ); |
43 | 26 |
|
44 | 27 | return ( |
45 | | - <CustomCard |
46 | | - data-cy={buildCommentContainerDataCy(comment.id)} |
47 | | - elevation={0} |
48 | | - ref={commentRef} |
49 | | - > |
50 | | - <CardHeader |
51 | | - title={ |
52 | | - isBot |
53 | | - ? (chatbotPrompt?.data[ChatbotPromptSettingsKeys.ChatbotName] ?? |
54 | | - DEFAULT_BOT_USERNAME) |
55 | | - : currentMember?.name |
56 | | - } |
57 | | - subheader={formatDate(comment.updatedAt, { locale: i18n.language })} |
58 | | - avatar={ |
59 | | - isBot ? <ChatbotAvatar /> : <CustomAvatar member={currentMember} /> |
60 | | - } |
61 | | - /> |
62 | | - <CardContent sx={{ p: 2, py: 0, '&:last-child': { pb: 0 } }}> |
63 | | - <CommentBody>{comment.data.content}</CommentBody> |
64 | | - </CardContent> |
65 | | - </CustomCard> |
| 28 | + <Stack data-cy={buildCommentContainerDataCy(id)} ref={commentRef}> |
| 29 | + <Stack |
| 30 | + direction={isBot ? 'row' : 'row-reverse'} |
| 31 | + alignItems="end" |
| 32 | + justifyContent={isBot ? 'start' : 'end'} |
| 33 | + > |
| 34 | + <Stack>{avatar}</Stack> |
| 35 | + <Stack |
| 36 | + sx={{ p: 2, py: 0, '&:last-child': { pb: 0 } }} |
| 37 | + alignItems={isBot ? 'start' : 'end'} |
| 38 | + gap={1} |
| 39 | + > |
| 40 | + <Typography variant="subtitle2">{username}</Typography> |
| 41 | + <CommentBody>{body}</CommentBody> |
| 42 | + </Stack> |
| 43 | + </Stack> |
| 44 | + </Stack> |
66 | 45 | ); |
67 | 46 | } |
68 | 47 |
|
|
0 commit comments