From 499a0ef19337def5ca2100a53ba1c46d06f4a7ee Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Sat, 20 Aug 2022 22:23:43 -0500 Subject: [PATCH] feat: show full message transcript --- .../ChatTranscript/ChatTranscript.tsx | 17 ++++++++++++ src/components/ChatTranscript/index.ts | 1 + src/components/Room/Room.tsx | 27 +++++++++---------- 3 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 src/components/ChatTranscript/ChatTranscript.tsx create mode 100644 src/components/ChatTranscript/index.ts diff --git a/src/components/ChatTranscript/ChatTranscript.tsx b/src/components/ChatTranscript/ChatTranscript.tsx new file mode 100644 index 0000000..5e4811c --- /dev/null +++ b/src/components/ChatTranscript/ChatTranscript.tsx @@ -0,0 +1,17 @@ +import Typography from '@mui/material/Typography' + +import { UnsentMessage, ReceivedMessage } from 'models/chat' + +export interface ChatTranscriptProps { + messageLog: Array +} + +export const ChatTranscript = ({ messageLog }: ChatTranscriptProps) => { + return ( +
+ {messageLog.map((message, idx) => ( + {message.text} + ))} +
+ ) +} diff --git a/src/components/ChatTranscript/index.ts b/src/components/ChatTranscript/index.ts new file mode 100644 index 0000000..c32c8cc --- /dev/null +++ b/src/components/ChatTranscript/index.ts @@ -0,0 +1 @@ +export * from './ChatTranscript' diff --git a/src/components/Room/Room.tsx b/src/components/Room/Room.tsx index 2bbf1a4..8c3633f 100644 --- a/src/components/Room/Room.tsx +++ b/src/components/Room/Room.tsx @@ -9,6 +9,7 @@ import TextField from '@mui/material/TextField' import { usePeerRoom, usePeerRoomAction } from 'hooks/usePeerRoom' import { PeerActions } from 'models/network' import { UnsentMessage, ReceivedMessage } from 'models/chat' +import { ChatTranscript } from 'components/ChatTranscript' export interface RoomProps { appId?: string @@ -24,9 +25,9 @@ export function Room({ const { roomId = '' } = useParams() const [textMessage, setTextMessage] = useState('') - const [receivedMessages, setReceivedMessages] = useState( - [] - ) + const [messageLog, setMessageLog] = useState< + Array + >([]) const peerRoom = usePeerRoom( { @@ -49,20 +50,22 @@ export function Room({ event: React.SyntheticEvent ) => { event.preventDefault() - sendMessage({ + + const unsentMessage: UnsentMessage = { authorId: userId, text: textMessage, timeSent: Date.now(), id: getUuid(), - }) + } + + sendMessage(unsentMessage) + setTextMessage('') + setMessageLog([...messageLog, unsentMessage]) } receiveMessage(message => { - setReceivedMessages([ - ...receivedMessages, - { ...message, timeReceived: Date.now() }, - ]) + setMessageLog([...messageLog, { ...message, timeReceived: Date.now() }]) }) return ( @@ -90,11 +93,7 @@ export function Room({ Send -
- {receivedMessages.map((message, idx) => ( - {message.text} - ))} -
+ ) }