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 { MemberWithPublicUser } from '../models/Member'
|
|
|
|
import { GuildsChannelsPath } from '../components/Application'
|
|
|
|
import { handleSocketData, SocketData } from '../tools/handleSocketData'
|
|
|
|
import { User } from '../models/User'
|
2022-08-24 17:22:55 +02:00
|
|
|
import { CacheKey, MEMBERS_CACHE_KEY } from '../tools/cache'
|
2022-01-01 20:42:25 +01:00
|
|
|
|
|
|
|
export interface Members {
|
|
|
|
members: MemberWithPublicUser[]
|
|
|
|
hasMore: boolean
|
|
|
|
nextPage: NextPage
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultMembersContext = {} as any
|
|
|
|
const MembersContext = createContext<Members>(defaultMembersContext)
|
|
|
|
|
|
|
|
export interface MembersProviderProps {
|
|
|
|
path: GuildsChannelsPath
|
|
|
|
}
|
|
|
|
|
2022-05-12 20:35:46 +02:00
|
|
|
export const MembersProviders: React.FC<
|
|
|
|
React.PropsWithChildren<MembersProviderProps>
|
|
|
|
> = (props) => {
|
2022-01-01 20:42:25 +01:00
|
|
|
const { children, path } = props
|
|
|
|
|
|
|
|
const { authentication } = useAuthentication()
|
|
|
|
|
2022-08-24 17:22:55 +02:00
|
|
|
const cacheKey: CacheKey = `${path.guildId}-${MEMBERS_CACHE_KEY}`
|
|
|
|
|
2022-01-01 20:42:25 +01:00
|
|
|
const {
|
|
|
|
items: members,
|
|
|
|
hasMore,
|
|
|
|
nextPage,
|
2022-03-05 18:22:30 +01:00
|
|
|
resetPagination,
|
|
|
|
setItems
|
2022-01-01 20:42:25 +01:00
|
|
|
} = usePagination<MemberWithPublicUser>({
|
|
|
|
api: authentication.api,
|
2022-08-24 17:22:55 +02:00
|
|
|
url: `/guilds/${path.guildId}/members`,
|
|
|
|
cacheKey
|
2022-01-01 20:42:25 +01:00
|
|
|
})
|
|
|
|
|
2022-03-05 18:22:30 +01:00
|
|
|
useEffect(() => {
|
2022-08-24 17:22:55 +02:00
|
|
|
authentication?.socket?.on(
|
2022-03-05 18:22:30 +01:00
|
|
|
'members',
|
|
|
|
(data: SocketData<MemberWithPublicUser>) => {
|
2022-08-24 17:22:55 +02:00
|
|
|
handleSocketData({ data, setItems, cacheKey })
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-08-24 17:22:55 +02:00
|
|
|
authentication?.socket?.on('users', (data: SocketData<User>) => {
|
2022-03-05 18:22:30 +01:00
|
|
|
setItems((oldItems) => {
|
|
|
|
const newItems = [...oldItems]
|
|
|
|
switch (data.action) {
|
|
|
|
case 'update': {
|
|
|
|
for (const member of newItems) {
|
|
|
|
if (member.user.id === data.item.id) {
|
|
|
|
member.user = data.item
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newItems
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return () => {
|
2022-08-24 17:22:55 +02:00
|
|
|
authentication?.socket?.off('members')
|
|
|
|
authentication?.socket?.off('users')
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
2022-08-24 17:22:55 +02:00
|
|
|
}, [authentication.socket, setItems, cacheKey])
|
2022-03-05 18:22:30 +01:00
|
|
|
|
2022-01-01 20:42:25 +01:00
|
|
|
useEffect(() => {
|
|
|
|
resetPagination()
|
|
|
|
nextPage()
|
|
|
|
}, [nextPage, resetPagination])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<MembersContext.Provider value={{ members, hasMore, nextPage }}>
|
|
|
|
{children}
|
|
|
|
</MembersContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useMembers = (): Members => {
|
|
|
|
const members = useContext(MembersContext)
|
|
|
|
if (members === defaultMembersContext) {
|
|
|
|
throw new Error('useMembers must be used within MembersProvider')
|
|
|
|
}
|
|
|
|
return members
|
|
|
|
}
|