fix: autoscroll the user when posting a message even if is not at bottom

This commit is contained in:
Divlo 2022-06-29 06:23:23 +02:00
parent 10b1e568d2
commit 21ee6f1d66
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9

View File

@ -23,7 +23,7 @@ export const MessagesProvider: React.FC<
React.PropsWithChildren<MessagesProviderProps> React.PropsWithChildren<MessagesProviderProps>
> = (props) => { > = (props) => {
const { path, children } = props const { path, children } = props
const { authentication } = useAuthentication() const { authentication, user } = useAuthentication()
const { const {
items: messages, items: messages,
@ -49,7 +49,10 @@ export const MessagesProvider: React.FC<
messagesDiv.scrollHeight - messagesDiv.scrollTop <= messagesDiv.scrollHeight - messagesDiv.scrollTop <=
messagesDiv.clientHeight messagesDiv.clientHeight
handleSocketData({ data, setItems }) handleSocketData({ data, setItems })
if (data.action === 'create' && isAtBottom) { if (
data.action === 'create' &&
(isAtBottom || data.item.member.userId === user.id)
) {
messagesDiv.scrollTo(0, messagesDiv.scrollHeight) messagesDiv.scrollTo(0, messagesDiv.scrollHeight)
} }
} }
@ -59,7 +62,7 @@ export const MessagesProvider: React.FC<
return () => { return () => {
authentication.socket.off('messages') authentication.socket.off('messages')
} }
}, [authentication.socket, setItems, path]) }, [authentication.socket, setItems, path, user.id])
useEffect(() => { useEffect(() => {
resetPagination() resetPagination()