|
| 1 | +import { useTranslation } from 'react-i18next'; |
| 2 | + |
| 3 | +import { |
| 4 | + Button, |
| 5 | + Container, |
| 6 | + IconButton, |
| 7 | + Stack, |
| 8 | + Table, |
| 9 | + TableBody, |
| 10 | + TableCell, |
| 11 | + TableContainer, |
| 12 | + TableRow, |
| 13 | + Typography, |
| 14 | +} from '@mui/material'; |
| 15 | + |
| 16 | +import { intlFormat } from 'date-fns'; |
| 17 | +import groupBy from 'lodash.groupby'; |
| 18 | +import { MessagesSquareIcon, PlusIcon } from 'lucide-react'; |
| 19 | +import { v4 } from 'uuid'; |
| 20 | + |
| 21 | +import type { CommentData } from '@/config/appData'; |
| 22 | +import { hooks } from '@/config/queryClient'; |
| 23 | +import { |
| 24 | + TABLE_VIEW_BODY_USERS_CYPRESS, |
| 25 | + TABLE_VIEW_TABLE_CYPRESS, |
| 26 | +} from '@/config/selectors'; |
| 27 | + |
| 28 | +import { ChatbotContainer } from './ChatbotContainer'; |
| 29 | +import ChatbotHeader from './ChatbotHeader'; |
| 30 | + |
| 31 | +const useConversations = () => { |
| 32 | + const { i18n } = useTranslation(); |
| 33 | + const { data: messages = [] } = hooks.useAppData<CommentData>(); |
| 34 | + |
| 35 | + if (messages) { |
| 36 | + const conversations = groupBy(messages, (c) => c.data.conversationId); |
| 37 | + return Object.entries(conversations) |
| 38 | + .toSorted((a, b) => (a[0] > b[0] ? 1 : -1)) |
| 39 | + .map(([id, m]) => { |
| 40 | + const sortedMessages = m.toSorted((a, b) => |
| 41 | + a.createdAt > b.createdAt ? 1 : -1, |
| 42 | + ); |
| 43 | + const creationDate = sortedMessages.at(-1)?.createdAt; |
| 44 | + |
| 45 | + return { |
| 46 | + id, |
| 47 | + name: sortedMessages[0].data.content, |
| 48 | + messages: m, |
| 49 | + lastMessageDate: creationDate |
| 50 | + ? intlFormat( |
| 51 | + creationDate, |
| 52 | + { |
| 53 | + year: 'numeric', |
| 54 | + month: 'long', |
| 55 | + day: 'numeric', |
| 56 | + hour: 'numeric', |
| 57 | + minute: 'numeric', |
| 58 | + }, |
| 59 | + { locale: i18n.language }, |
| 60 | + ) |
| 61 | + : '', |
| 62 | + }; |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + return []; |
| 67 | +}; |
| 68 | + |
| 69 | +export function Conversations({ |
| 70 | + chatbotAvatar, |
| 71 | + chatbotName, |
| 72 | + onSelect, |
| 73 | +}: Readonly<{ |
| 74 | + chatbotAvatar?: Blob; |
| 75 | + chatbotName: string; |
| 76 | + onSelect: (id: string) => void; |
| 77 | +}>) { |
| 78 | + const { t } = useTranslation(); |
| 79 | + const conversations = useConversations(); |
| 80 | + |
| 81 | + const startNewConversation = () => { |
| 82 | + onSelect(v4()); |
| 83 | + }; |
| 84 | + |
| 85 | + return ( |
| 86 | + <Container> |
| 87 | + <ChatbotContainer> |
| 88 | + <Stack gap={3}> |
| 89 | + <ChatbotHeader avatar={chatbotAvatar} name={chatbotName} /> |
| 90 | + <TableContainer data-cy={TABLE_VIEW_TABLE_CYPRESS}> |
| 91 | + <Table aria-label="conversations table"> |
| 92 | + <TableBody data-cy={TABLE_VIEW_BODY_USERS_CYPRESS}> |
| 93 | + {conversations.map((c) => ( |
| 94 | + <TableRow key={c.id}> |
| 95 | + <TableCell> |
| 96 | + <Typography |
| 97 | + variant="body1" |
| 98 | + fontWeight="bold" |
| 99 | + noWrap |
| 100 | + maxWidth={200} |
| 101 | + title={c.name} |
| 102 | + > |
| 103 | + {c.name} |
| 104 | + </Typography> |
| 105 | + </TableCell> |
| 106 | + <TableCell> |
| 107 | + <Typography variant="body2"> |
| 108 | + {c.lastMessageDate} |
| 109 | + </Typography> |
| 110 | + </TableCell> |
| 111 | + <TableCell align="right"> |
| 112 | + <IconButton |
| 113 | + onClick={() => { |
| 114 | + console.debug(`Open conversation ${c.id}`); |
| 115 | + onSelect(c.id); |
| 116 | + }} |
| 117 | + > |
| 118 | + <MessagesSquareIcon /> |
| 119 | + </IconButton> |
| 120 | + </TableCell> |
| 121 | + </TableRow> |
| 122 | + ))} |
| 123 | + </TableBody> |
| 124 | + </Table> |
| 125 | + </TableContainer> |
| 126 | + <Stack alignItems="center"> |
| 127 | + <Button |
| 128 | + variant="contained" |
| 129 | + startIcon={<PlusIcon />} |
| 130 | + onClick={startNewConversation} |
| 131 | + > |
| 132 | + {t('New conversation')} |
| 133 | + </Button> |
| 134 | + </Stack> |
| 135 | + </Stack> |
| 136 | + </ChatbotContainer> |
| 137 | + </Container> |
| 138 | + ); |
| 139 | +} |
0 commit comments