forked from Shiloh/remnantchat
feat: show full message transcript
This commit is contained in:
parent
d4bc49f096
commit
499a0ef193
17
src/components/ChatTranscript/ChatTranscript.tsx
Normal file
17
src/components/ChatTranscript/ChatTranscript.tsx
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import Typography from '@mui/material/Typography'
|
||||||
|
|
||||||
|
import { UnsentMessage, ReceivedMessage } from 'models/chat'
|
||||||
|
|
||||||
|
export interface ChatTranscriptProps {
|
||||||
|
messageLog: Array<UnsentMessage | ReceivedMessage>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ChatTranscript = ({ messageLog }: ChatTranscriptProps) => {
|
||||||
|
return (
|
||||||
|
<div className="ChatTranscript">
|
||||||
|
{messageLog.map((message, idx) => (
|
||||||
|
<Typography key={`${idx}_${message}`}>{message.text}</Typography>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
1
src/components/ChatTranscript/index.ts
Normal file
1
src/components/ChatTranscript/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './ChatTranscript'
|
@ -9,6 +9,7 @@ import TextField from '@mui/material/TextField'
|
|||||||
import { usePeerRoom, usePeerRoomAction } from 'hooks/usePeerRoom'
|
import { usePeerRoom, usePeerRoomAction } from 'hooks/usePeerRoom'
|
||||||
import { PeerActions } from 'models/network'
|
import { PeerActions } from 'models/network'
|
||||||
import { UnsentMessage, ReceivedMessage } from 'models/chat'
|
import { UnsentMessage, ReceivedMessage } from 'models/chat'
|
||||||
|
import { ChatTranscript } from 'components/ChatTranscript'
|
||||||
|
|
||||||
export interface RoomProps {
|
export interface RoomProps {
|
||||||
appId?: string
|
appId?: string
|
||||||
@ -24,9 +25,9 @@ export function Room({
|
|||||||
const { roomId = '' } = useParams()
|
const { roomId = '' } = useParams()
|
||||||
|
|
||||||
const [textMessage, setTextMessage] = useState('')
|
const [textMessage, setTextMessage] = useState('')
|
||||||
const [receivedMessages, setReceivedMessages] = useState<ReceivedMessage[]>(
|
const [messageLog, setMessageLog] = useState<
|
||||||
[]
|
Array<ReceivedMessage | UnsentMessage>
|
||||||
)
|
>([])
|
||||||
|
|
||||||
const peerRoom = usePeerRoom(
|
const peerRoom = usePeerRoom(
|
||||||
{
|
{
|
||||||
@ -49,20 +50,22 @@ export function Room({
|
|||||||
event: React.SyntheticEvent<HTMLFormElement>
|
event: React.SyntheticEvent<HTMLFormElement>
|
||||||
) => {
|
) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
sendMessage({
|
|
||||||
|
const unsentMessage: UnsentMessage = {
|
||||||
authorId: userId,
|
authorId: userId,
|
||||||
text: textMessage,
|
text: textMessage,
|
||||||
timeSent: Date.now(),
|
timeSent: Date.now(),
|
||||||
id: getUuid(),
|
id: getUuid(),
|
||||||
})
|
}
|
||||||
|
|
||||||
|
sendMessage(unsentMessage)
|
||||||
|
|
||||||
setTextMessage('')
|
setTextMessage('')
|
||||||
|
setMessageLog([...messageLog, unsentMessage])
|
||||||
}
|
}
|
||||||
|
|
||||||
receiveMessage(message => {
|
receiveMessage(message => {
|
||||||
setReceivedMessages([
|
setMessageLog([...messageLog, { ...message, timeReceived: Date.now() }])
|
||||||
...receivedMessages,
|
|
||||||
{ ...message, timeReceived: Date.now() },
|
|
||||||
])
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -90,11 +93,7 @@ export function Room({
|
|||||||
Send
|
Send
|
||||||
</Button>
|
</Button>
|
||||||
</form>
|
</form>
|
||||||
<div>
|
<ChatTranscript messageLog={messageLog} />
|
||||||
{receivedMessages.map((message, idx) => (
|
|
||||||
<Typography key={`${idx}_${message}`}>{message.text}</Typography>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user