Skip to content

Commit 0a6429f

Browse files
committed
Make Enter send chat message
1 parent fdc684f commit 0a6429f

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

src/components/ticket-page/Chat.tsx

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'react';
1+
import { useState, useEffect, useMemo } from 'react';
22
import { useChannel } from '@ably-labs/react-hooks';
33
import { Button, Box, Flex, Text, Spinner, useToast, Textarea } from '@chakra-ui/react';
44
import { trpc } from '../../utils/trpc';
@@ -130,39 +130,50 @@ const Chat = (props: ChatProps) => {
130130
});
131131
};
132132

133-
const allMessages = messages.map((message, index: number) => {
134-
const { content, sentByName, sentByUserId, sentByUserRole } = message;
135-
const amISender = sentByUserId === (session?.user?.id ?? 'Unknown');
136-
137-
return (
138-
<Flex
139-
key={index}
140-
data-author={sentByName}
141-
backgroundColor={amISender ? 'blue.600' : 'gray'}
142-
p={3}
143-
alignSelf={amISender ? 'flex-end' : 'flex-start'}
144-
borderRadius={5}
145-
borderBottomRightRadius={amISender ? 0 : 5}
146-
borderBottomLeftRadius={amISender ? 5 : 0}
147-
color='white'
148-
>
149-
<Text mr={2} fontWeight='bold' hidden={amISender}>
150-
{canSeeName || sentByUserRole !== UserRole.STUDENT
151-
? sentByName
152-
: 'Anonymous' + ' (' + uppercaseFirstLetter(sentByUserRole) + ')'}
153-
</Text>
154-
{content}
155-
</Flex>
156-
);
157-
});
133+
const allMessages = useMemo(
134+
() =>
135+
messages.map((message, index) => {
136+
const { content, sentByName, sentByUserId, sentByUserRole } = message;
137+
const amISender = sentByUserId === (session?.user?.id ?? 'Unknown');
138+
139+
return (
140+
<Flex
141+
key={index}
142+
data-author={sentByName}
143+
backgroundColor={amISender ? 'blue.600' : 'gray'}
144+
p={3}
145+
alignSelf={amISender ? 'flex-end' : 'flex-start'}
146+
borderRadius={5}
147+
borderBottomRightRadius={amISender ? 0 : 5}
148+
borderBottomLeftRadius={amISender ? 5 : 0}
149+
color='white'
150+
>
151+
<Text mr={2} fontWeight='bold' hidden={amISender}>
152+
{canSeeName || sentByUserRole !== UserRole.STUDENT
153+
? sentByName
154+
: 'Anonymous' + ' (' + uppercaseFirstLetter(sentByUserRole) + ')'}
155+
</Text>
156+
{content}
157+
</Flex>
158+
);
159+
}),
160+
[messages],
161+
);
158162

159163
useEffect(() => {
160164
if (messageEnd) {
161-
// Bug: This causes scroll to bottom of page
162-
// messageEnd.scrollIntoView({ behaviour: 'smooth' });
165+
messageEnd.scrollIntoView({ behaviour: 'smooth' });
163166
}
164167
}, [messages, messageEnd]);
165168

169+
/** Pressing Enter should call handleFormSubmission */
170+
const handleKeyPress = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
171+
if (event.key === 'Enter' && !event.shiftKey) {
172+
event.preventDefault();
173+
handleFormSubmission(event as any);
174+
}
175+
};
176+
166177
return (
167178
<>
168179
{!isChatLoaded && <Spinner />}
@@ -180,6 +191,7 @@ const Chat = (props: ChatProps) => {
180191
value={messageText}
181192
placeholder='Type a message...'
182193
onChange={e => setMessageText(e.target.value)}
194+
onKeyDown={handleKeyPress}
183195
mr={4}
184196
maxLength={1000}
185197
/>

0 commit comments

Comments
 (0)