2022-01-01 20:42:25 +01:00
|
|
|
import { createContext, useContext, useEffect } from 'react'
|
|
|
|
|
2022-03-16 12:18:09 +01:00
|
|
|
import { NextPage, usePagination } from '../hooks/usePagination'
|
|
|
|
import { useAuthentication } from '../tools/authentication'
|
|
|
|
import { MessageWithMember } from '../models/Message'
|
|
|
|
import { GuildsChannelsPath } from '../components/Application'
|
|
|
|
import { handleSocketData, SocketData } from '../tools/handleSocketData'
|
2022-01-01 20:42:25 +01:00
|
|
|
|
|
|
|
export interface Messages {
|
|
|
|
messages: MessageWithMember[]
|
|
|
|
hasMore: boolean
|
|
|
|
nextPage: NextPage
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultMessagesContext = {} as any
|
|
|
|
const MessagesContext = createContext<Messages>(defaultMessagesContext)
|
|
|
|
|
|
|
|
export interface MessagesProviderProps {
|
|
|
|
path: GuildsChannelsPath
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MessagesProvider: React.FC<MessagesProviderProps> = (props) => {
|
|
|
|
const { path, children } = props
|
|
|
|
const { authentication } = useAuthentication()
|
|
|
|
|
|
|
|
const {
|
|
|
|
items: messages,
|
|
|
|
hasMore,
|
|
|
|
nextPage,
|
2022-01-13 18:21:45 +01:00
|
|
|
resetPagination,
|
|
|
|
setItems
|
2022-01-01 20:42:25 +01:00
|
|
|
} = usePagination<MessageWithMember>({
|
|
|
|
api: authentication.api,
|
|
|
|
url: `/channels/${path.channelId}/messages`,
|
|
|
|
inverse: true
|
|
|
|
})
|
|
|
|
|
2022-01-13 18:21:45 +01:00
|
|
|
useEffect(() => {
|
|
|
|
authentication.socket.on(
|
|
|
|
'messages',
|
|
|
|
(data: SocketData<MessageWithMember>) => {
|
|
|
|
if (data.item.channelId === path.channelId) {
|
|
|
|
const messagesDiv = window.document.getElementById(
|
|
|
|
'messages'
|
|
|
|
) as HTMLDivElement
|
|
|
|
const isAtBottom =
|
|
|
|
messagesDiv.scrollHeight - messagesDiv.scrollTop <=
|
|
|
|
messagesDiv.clientHeight
|
|
|
|
handleSocketData({ data, setItems })
|
|
|
|
if (data.action === 'create' && isAtBottom) {
|
|
|
|
messagesDiv.scrollTo(0, messagesDiv.scrollHeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
authentication.socket.off('messages')
|
|
|
|
}
|
|
|
|
}, [authentication.socket, setItems, path])
|
|
|
|
|
2022-01-01 20:42:25 +01:00
|
|
|
useEffect(() => {
|
|
|
|
resetPagination()
|
|
|
|
nextPage(undefined, () => {
|
|
|
|
const messagesDiv = window.document.getElementById(
|
|
|
|
'messages'
|
|
|
|
) as HTMLDivElement
|
|
|
|
messagesDiv.scrollTo(0, messagesDiv.scrollHeight)
|
|
|
|
})
|
|
|
|
}, [nextPage, resetPagination])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<MessagesContext.Provider value={{ messages, hasMore, nextPage }}>
|
|
|
|
{children}
|
|
|
|
</MessagesContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useMessages = (): Messages => {
|
|
|
|
const messages = useContext(MessagesContext)
|
|
|
|
if (messages === defaultMessagesContext) {
|
|
|
|
throw new Error('useMessages must be used within a MessagesProvider')
|
|
|
|
}
|
|
|
|
return messages
|
|
|
|
}
|