2023-04-10 23:28:28 +02:00
|
|
|
import { useState } from 'react'
|
|
|
|
import { FormEventHandler } from 'react'
|
|
|
|
import useAllChat from '../../hooks/useAllChat'
|
|
|
|
import './Chat.css'
|
2023-04-09 10:36:52 +02:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
2023-04-10 23:28:28 +02:00
|
|
|
import { faPaperPlane } from '@fortawesome/free-regular-svg-icons'
|
|
|
|
import { getColorForUserName } from '../../background/style/colorednames'
|
2023-04-08 15:58:11 +02:00
|
|
|
interface Props {
|
2023-04-10 23:28:28 +02:00
|
|
|
sendMessage: Function
|
2023-04-08 15:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Chat({ sendMessage }: Props) {
|
2023-04-10 23:28:28 +02:00
|
|
|
const [chatInput, setChatInput] = useState('')
|
|
|
|
const { chatMesages } = useAllChat()
|
2023-04-08 15:58:11 +02:00
|
|
|
|
2023-04-10 23:28:28 +02:00
|
|
|
const onInput: React.ChangeEventHandler<HTMLInputElement> = (event) => {
|
|
|
|
setChatInput(event.target.value)
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
2023-04-08 15:58:11 +02:00
|
|
|
|
2023-04-10 23:28:28 +02:00
|
|
|
const onSubmit: FormEventHandler<HTMLFormElement> = (event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
sendMessage(JSON.stringify({ content: chatInput }))
|
|
|
|
setChatInput('')
|
|
|
|
}
|
2023-04-08 15:58:11 +02:00
|
|
|
|
2023-04-10 23:28:28 +02:00
|
|
|
return (
|
|
|
|
<div className="chat">
|
|
|
|
<div className="chat-messages">
|
|
|
|
{chatMesages
|
|
|
|
.slice()
|
|
|
|
.reverse() // reverse because of css, don't aks me
|
|
|
|
.map((message) => {
|
|
|
|
const nameStyle = {
|
|
|
|
color: getColorForUserName(message.sender.name),
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div className="chat-bubble" key={message.uuid}>
|
|
|
|
{' '}
|
|
|
|
<span className="chat-sender" style={nameStyle}>
|
|
|
|
{message.sender.name} <br />{' '}
|
|
|
|
</span>{' '}
|
|
|
|
{message.content}{' '}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
<form className="chat-input-form" onSubmit={onSubmit}>
|
|
|
|
<input
|
|
|
|
className="chat-input"
|
|
|
|
placeholder="Say something funny :D"
|
|
|
|
type="text"
|
|
|
|
value={chatInput}
|
|
|
|
onChange={onInput}
|
|
|
|
/>
|
|
|
|
<button type="submit">
|
|
|
|
<FontAwesomeIcon icon={faPaperPlane} />
|
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
)
|
2023-04-08 15:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Chat
|