Chat improvements

This commit is contained in:
mrfry 2022-03-20 09:42:06 +01:00
parent dd7001023e
commit c2f991254a
3 changed files with 28 additions and 16 deletions

View file

@ -66,7 +66,6 @@ textarea {
color: var(--text-color); color: var(--text-color);
background-color: var(--background-color); background-color: var(--background-color);
box-sizing: border-box; box-sizing: border-box;
height: 140px;
width: 90%; width: 90%;
border: 0px solid #666; border: 0px solid #666;
border-radius: 3px; border-radius: 3px;

View file

@ -122,6 +122,7 @@ export default class Chat extends React.Component {
this.connect(this.props.globalData.userId) this.connect(this.props.globalData.userId)
} }
this.router = props.router this.router = props.router
this.chatInputRef = React.createRef()
} }
componentDidUpdate(prevProps, prevState) { componentDidUpdate(prevProps, prevState) {
@ -165,6 +166,13 @@ export default class Chat extends React.Component {
} }
} }
resizeInputBar() {
if (this.chatInputRef.current) {
const input = this.chatInputRef.current
input.style.height = input.scrollHeight + 'px'
}
}
scrollToChatBottom() { scrollToChatBottom() {
const objDiv = document.getElementById('messages') const objDiv = document.getElementById('messages')
if (objDiv) { if (objDiv) {
@ -263,6 +271,7 @@ export default class Chat extends React.Component {
} }
sendMsg(currMsg, type) { sendMsg(currMsg, type) {
this.chatInputRef.current.style.height = '40px'
const { msgs, selectedUser, user } = this.state const { msgs, selectedUser, user } = this.state
if (!currMsg) { if (!currMsg) {
return return
@ -389,10 +398,7 @@ export default class Chat extends React.Component {
uploadFile(file).then((res) => { uploadFile(file).then((res) => {
const { path, success } = res const { path, success } = res
if (success) { if (success) {
this.sendMsg( this.sendMsg(`${path}`, isImage ? 'img' : 'file')
`${constants.apiUrl}${path}`,
isImage ? 'img' : 'file'
)
} else { } else {
alert('Error uploading image :/') alert('Error uploading image :/')
console.error(res) console.error(res)
@ -411,12 +417,17 @@ export default class Chat extends React.Component {
📂 📂
</label> </label>
</div> </div>
<input <textarea
ref={this.chatInputRef}
className={styles.msgInput}
autoFocus autoFocus
placeholder={'Message ...'} placeholder={'Message ...'}
type={'text'} type={'text'}
value={currMsg} value={currMsg}
tabIndex={0} tabIndex={0}
onKeyDown={() => {
this.resizeInputBar()
}}
onKeyUp={(e) => { onKeyUp={(e) => {
const lastMessage = msgs[selectedUser] const lastMessage = msgs[selectedUser]
? msgs[selectedUser].lastMessage ? msgs[selectedUser].lastMessage
@ -429,8 +440,9 @@ export default class Chat extends React.Component {
this.chatMessageSeen(selectedUser) this.chatMessageSeen(selectedUser)
} }
if (e.key === 'Enter') { if (e.key === 'Enter') {
this.sendMsg(currMsg) if (!e.shiftKey) this.sendMsg(currMsg)
} }
this.resizeInputBar()
}} }}
onChange={(e) => { onChange={(e) => {
this.setState({ this.setState({
@ -534,13 +546,6 @@ export default class Chat extends React.Component {
renderMsg(message, key) { renderMsg(message, key) {
const { msg, type } = message const { msg, type } = message
console.log(msg)
console.log(
linkifyString(msg, {
defaultProtocol: 'https',
target: '_blank',
})
)
if (type === 'text') { if (type === 'text') {
return ( return (
@ -557,7 +562,7 @@ export default class Chat extends React.Component {
} else if (type === 'img') { } else if (type === 'img') {
return ( return (
<a key={key} href={msg} rel="noreferrer" target="_blank"> <a key={key} href={msg} rel="noreferrer" target="_blank">
<img src={msg} /> <img src={`${constants.apiUrl}${msg}`} />
</a> </a>
) )
} else if (type === 'file') { } else if (type === 'file') {
@ -565,7 +570,7 @@ export default class Chat extends React.Component {
<a <a
className={`${styles.messageEntry}`} className={`${styles.messageEntry}`}
key={key} key={key}
href={msg} href={`${constants.apiUrl}${msg}`}
rel="noreferrer" rel="noreferrer"
target="_blank" target="_blank"
> >

View file

@ -173,10 +173,12 @@
border-radius: 4px; border-radius: 4px;
color: black; color: black;
max-width: 400px; max-width: 400px;
white-space: pre-wrap;
} }
.sendButton { .sendButton {
width: 90px; width: 90px;
padding: 4px;
} }
.home { .home {
@ -221,3 +223,9 @@
margin: 0px 5px; margin: 0px 5px;
cursor: pointer; cursor: pointer;
} }
.msgInput {
resize: vertical;
padding: 10px;
height: 40px;
}