chore: better Prettier config for easier reviews
This commit is contained in:
@ -1,15 +1,15 @@
|
||||
import { createContext, useContext, useEffect, useMemo } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { createContext, useContext, useEffect, useMemo } from "react"
|
||||
import { useRouter } from "next/router"
|
||||
|
||||
import type { NextPage } from '../hooks/usePagination'
|
||||
import { usePagination } from '../hooks/usePagination'
|
||||
import { useAuthentication } from '../tools/authentication'
|
||||
import type { Channel, ChannelWithDefaultChannelId } from '../models/Channel'
|
||||
import type { GuildsChannelsPath } from '../components/Application'
|
||||
import type { SocketData } from '../tools/handleSocketData'
|
||||
import { handleSocketData } from '../tools/handleSocketData'
|
||||
import type { CacheKey } from '../tools/cache'
|
||||
import { CHANNELS_CACHE_KEY } from '../tools/cache'
|
||||
import type { NextPage } from "../hooks/usePagination"
|
||||
import { usePagination } from "../hooks/usePagination"
|
||||
import { useAuthentication } from "../tools/authentication"
|
||||
import type { Channel, ChannelWithDefaultChannelId } from "../models/Channel"
|
||||
import type { GuildsChannelsPath } from "../components/Application"
|
||||
import type { SocketData } from "../tools/handleSocketData"
|
||||
import { handleSocketData } from "../tools/handleSocketData"
|
||||
import type { CacheKey } from "../tools/cache"
|
||||
import { CHANNELS_CACHE_KEY } from "../tools/cache"
|
||||
|
||||
export interface Channels {
|
||||
channels: Channel[]
|
||||
@ -40,28 +40,28 @@ export const ChannelsProvider: React.FC<
|
||||
hasMore,
|
||||
nextPage,
|
||||
resetPagination,
|
||||
setItems
|
||||
setItems,
|
||||
} = usePagination<Channel>({
|
||||
api: authentication.api,
|
||||
url: `/guilds/${path.guildId}/channels`,
|
||||
cacheKey
|
||||
cacheKey,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
authentication?.socket?.on(
|
||||
'channels',
|
||||
"channels",
|
||||
async (data: SocketData<ChannelWithDefaultChannelId>) => {
|
||||
handleSocketData({ data, setItems, cacheKey })
|
||||
if (data.action === 'delete') {
|
||||
if (data.action === "delete") {
|
||||
await router.push(
|
||||
`/application/${path.guildId}/${data.item.defaultChannelId}`
|
||||
`/application/${path.guildId}/${data.item.defaultChannelId}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
return () => {
|
||||
authentication?.socket?.off('channels')
|
||||
authentication?.socket?.off("channels")
|
||||
}
|
||||
}, [authentication.socket, path.guildId, router, setItems, cacheKey])
|
||||
|
||||
@ -80,7 +80,7 @@ export const ChannelsProvider: React.FC<
|
||||
export const useChannels = (): Channels => {
|
||||
const channels = useContext(ChannelsContext)
|
||||
if (channels === defaultChannelsContext) {
|
||||
throw new Error('`useChannels` must be used within `ChannelsProvider`')
|
||||
throw new Error("`useChannels` must be used within `ChannelsProvider`")
|
||||
}
|
||||
return channels
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
import { createContext, useContext, useEffect, useState } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { createContext, useContext, useEffect, useState } from "react"
|
||||
import { useRouter } from "next/router"
|
||||
|
||||
import type { GuildWithDefaultChannelId } from '../models/Guild'
|
||||
import type { Member } from '../models/Member'
|
||||
import { useAuthentication } from '../tools/authentication'
|
||||
import type { SocketData } from '../tools/handleSocketData'
|
||||
import type { GuildWithDefaultChannelId } from "../models/Guild"
|
||||
import type { Member } from "../models/Member"
|
||||
import { useAuthentication } from "../tools/authentication"
|
||||
import type { SocketData } from "../tools/handleSocketData"
|
||||
|
||||
export interface GuildMember {
|
||||
guild: GuildWithDefaultChannelId
|
||||
@ -22,7 +22,7 @@ export interface GuildMemberProps {
|
||||
|
||||
const defaultGuildMemberContext = {} as any
|
||||
const GuildMemberContext = createContext<GuildMemberResult>(
|
||||
defaultGuildMemberContext
|
||||
defaultGuildMemberContext,
|
||||
)
|
||||
|
||||
export const GuildMemberProvider: React.FC<
|
||||
@ -46,38 +46,38 @@ export const GuildMemberProvider: React.FC<
|
||||
|
||||
useEffect(() => {
|
||||
authentication?.socket?.on(
|
||||
'guilds',
|
||||
"guilds",
|
||||
async (data: SocketData<GuildWithDefaultChannelId>) => {
|
||||
if (data.item.id === path.guildId) {
|
||||
switch (data.action) {
|
||||
case 'delete':
|
||||
await router.push('/application')
|
||||
case "delete":
|
||||
await router.push("/application")
|
||||
break
|
||||
case 'update':
|
||||
case "update":
|
||||
setGuildMember((oldGuildMember) => {
|
||||
return {
|
||||
...oldGuildMember,
|
||||
guild: {
|
||||
...oldGuildMember.guild,
|
||||
...data.item
|
||||
}
|
||||
...data.item,
|
||||
},
|
||||
}
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
return () => {
|
||||
authentication?.socket?.off('guilds')
|
||||
authentication?.socket?.off("guilds")
|
||||
}
|
||||
}, [authentication.socket, path.guildId, router])
|
||||
|
||||
return (
|
||||
<GuildMemberContext.Provider
|
||||
value={{
|
||||
...guildMember
|
||||
...guildMember,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
@ -89,7 +89,7 @@ export const useGuildMember = (): GuildMemberResult => {
|
||||
const guildMember = useContext(GuildMemberContext)
|
||||
if (guildMember === defaultGuildMemberContext) {
|
||||
throw new Error(
|
||||
'`useGuildMember` must be used within `GuildMemberProvider`'
|
||||
"`useGuildMember` must be used within `GuildMemberProvider`",
|
||||
)
|
||||
}
|
||||
return guildMember
|
||||
|
@ -1,12 +1,12 @@
|
||||
import { createContext, useContext, useEffect } from 'react'
|
||||
import { createContext, useContext, useEffect } from "react"
|
||||
|
||||
import type { NextPage } from '../hooks/usePagination'
|
||||
import { usePagination } from '../hooks/usePagination'
|
||||
import { useAuthentication } from '../tools/authentication'
|
||||
import type { GuildWithDefaultChannelId } from '../models/Guild'
|
||||
import type { SocketData } from '../tools/handleSocketData'
|
||||
import { handleSocketData } from '../tools/handleSocketData'
|
||||
import { GUILDS_CACHE_KEY } from '../tools/cache'
|
||||
import type { NextPage } from "../hooks/usePagination"
|
||||
import { usePagination } from "../hooks/usePagination"
|
||||
import { useAuthentication } from "../tools/authentication"
|
||||
import type { GuildWithDefaultChannelId } from "../models/Guild"
|
||||
import type { SocketData } from "../tools/handleSocketData"
|
||||
import { handleSocketData } from "../tools/handleSocketData"
|
||||
import { GUILDS_CACHE_KEY } from "../tools/cache"
|
||||
|
||||
export interface Guilds {
|
||||
guilds: GuildWithDefaultChannelId[]
|
||||
@ -27,23 +27,23 @@ export const GuildsProvider: React.FC<React.PropsWithChildren> = (props) => {
|
||||
hasMore,
|
||||
nextPage,
|
||||
resetPagination,
|
||||
setItems
|
||||
setItems,
|
||||
} = usePagination<GuildWithDefaultChannelId>({
|
||||
api: authentication.api,
|
||||
url: '/guilds',
|
||||
cacheKey: GUILDS_CACHE_KEY
|
||||
url: "/guilds",
|
||||
cacheKey: GUILDS_CACHE_KEY,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
authentication?.socket?.on(
|
||||
'guilds',
|
||||
"guilds",
|
||||
(data: SocketData<GuildWithDefaultChannelId>) => {
|
||||
handleSocketData({ data, setItems, cacheKey: GUILDS_CACHE_KEY })
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
return () => {
|
||||
authentication?.socket?.off('guilds')
|
||||
authentication?.socket?.off("guilds")
|
||||
}
|
||||
}, [authentication.socket, setItems])
|
||||
|
||||
@ -62,7 +62,7 @@ export const GuildsProvider: React.FC<React.PropsWithChildren> = (props) => {
|
||||
export const useGuilds = (): Guilds => {
|
||||
const guilds = useContext(GuildsContext)
|
||||
if (guilds === defaultGuildsContext) {
|
||||
throw new Error('`useGuilds` must be used within `GuildsProvider`')
|
||||
throw new Error("`useGuilds` must be used within `GuildsProvider`")
|
||||
}
|
||||
return guilds
|
||||
}
|
||||
|
@ -1,15 +1,15 @@
|
||||
import { createContext, useContext, useEffect, useMemo } from 'react'
|
||||
import { createContext, useContext, useEffect, useMemo } from "react"
|
||||
|
||||
import type { NextPage } from '../hooks/usePagination'
|
||||
import { usePagination } from '../hooks/usePagination'
|
||||
import { useAuthentication } from '../tools/authentication'
|
||||
import type { MemberWithPublicUser } from '../models/Member'
|
||||
import type { GuildsChannelsPath } from '../components/Application'
|
||||
import type { SocketData } from '../tools/handleSocketData'
|
||||
import { handleSocketData } from '../tools/handleSocketData'
|
||||
import type { User } from '../models/User'
|
||||
import type { CacheKey } from '../tools/cache'
|
||||
import { MEMBERS_CACHE_KEY } from '../tools/cache'
|
||||
import type { NextPage } from "../hooks/usePagination"
|
||||
import { usePagination } from "../hooks/usePagination"
|
||||
import { useAuthentication } from "../tools/authentication"
|
||||
import type { MemberWithPublicUser } from "../models/Member"
|
||||
import type { GuildsChannelsPath } from "../components/Application"
|
||||
import type { SocketData } from "../tools/handleSocketData"
|
||||
import { handleSocketData } from "../tools/handleSocketData"
|
||||
import type { User } from "../models/User"
|
||||
import type { CacheKey } from "../tools/cache"
|
||||
import { MEMBERS_CACHE_KEY } from "../tools/cache"
|
||||
|
||||
export interface Members {
|
||||
members: MemberWithPublicUser[]
|
||||
@ -40,26 +40,26 @@ export const MembersProviders: React.FC<
|
||||
hasMore,
|
||||
nextPage,
|
||||
resetPagination,
|
||||
setItems
|
||||
setItems,
|
||||
} = usePagination<MemberWithPublicUser>({
|
||||
api: authentication.api,
|
||||
url: `/guilds/${path.guildId}/members`,
|
||||
cacheKey
|
||||
cacheKey,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
authentication?.socket?.on(
|
||||
'members',
|
||||
"members",
|
||||
(data: SocketData<MemberWithPublicUser>) => {
|
||||
handleSocketData({ data, setItems, cacheKey })
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
authentication?.socket?.on('users', (data: SocketData<User>) => {
|
||||
authentication?.socket?.on("users", (data: SocketData<User>) => {
|
||||
setItems((oldItems) => {
|
||||
const newItems = [...oldItems]
|
||||
switch (data.action) {
|
||||
case 'update': {
|
||||
case "update": {
|
||||
for (const member of newItems) {
|
||||
if (member.user.id === data.item.id) {
|
||||
member.user = data.item
|
||||
@ -74,8 +74,8 @@ export const MembersProviders: React.FC<
|
||||
})
|
||||
|
||||
return () => {
|
||||
authentication?.socket?.off('members')
|
||||
authentication?.socket?.off('users')
|
||||
authentication?.socket?.off("members")
|
||||
authentication?.socket?.off("users")
|
||||
}
|
||||
}, [authentication.socket, setItems, cacheKey])
|
||||
|
||||
@ -94,7 +94,7 @@ export const MembersProviders: React.FC<
|
||||
export const useMembers = (): Members => {
|
||||
const members = useContext(MembersContext)
|
||||
if (members === defaultMembersContext) {
|
||||
throw new Error('`useMembers` must be used within `MembersProvider`')
|
||||
throw new Error("`useMembers` must be used within `MembersProvider`")
|
||||
}
|
||||
return members
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
import { createContext, useContext, useEffect, useMemo } from 'react'
|
||||
import { createContext, useContext, useEffect, useMemo } from "react"
|
||||
|
||||
import type { NextPage } from '../hooks/usePagination'
|
||||
import { usePagination } from '../hooks/usePagination'
|
||||
import { useAuthentication } from '../tools/authentication'
|
||||
import type { MessageWithMember } from '../models/Message'
|
||||
import type { GuildsChannelsPath } from '../components/Application'
|
||||
import type { SocketData } from '../tools/handleSocketData'
|
||||
import { handleSocketData } from '../tools/handleSocketData'
|
||||
import type { CacheKey } from '../tools/cache'
|
||||
import { MESSAGES_CACHE_KEY } from '../tools/cache'
|
||||
import type { NextPage } from "../hooks/usePagination"
|
||||
import { usePagination } from "../hooks/usePagination"
|
||||
import { useAuthentication } from "../tools/authentication"
|
||||
import type { MessageWithMember } from "../models/Message"
|
||||
import type { GuildsChannelsPath } from "../components/Application"
|
||||
import type { SocketData } from "../tools/handleSocketData"
|
||||
import { handleSocketData } from "../tools/handleSocketData"
|
||||
import type { CacheKey } from "../tools/cache"
|
||||
import { MESSAGES_CACHE_KEY } from "../tools/cache"
|
||||
|
||||
export interface Messages {
|
||||
messages: MessageWithMember[]
|
||||
@ -38,38 +38,38 @@ export const MessagesProvider: React.FC<
|
||||
hasMore,
|
||||
nextPage,
|
||||
resetPagination,
|
||||
setItems
|
||||
setItems,
|
||||
} = usePagination<MessageWithMember>({
|
||||
api: authentication.api,
|
||||
url: `/channels/${path.channelId}/messages`,
|
||||
inverse: true,
|
||||
cacheKey
|
||||
cacheKey,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
authentication?.socket?.on(
|
||||
'messages',
|
||||
"messages",
|
||||
(data: SocketData<MessageWithMember>) => {
|
||||
if (data.item.channelId === path.channelId) {
|
||||
const messagesDiv = window.document.getElementById(
|
||||
'messages'
|
||||
"messages",
|
||||
) as HTMLDivElement
|
||||
const isAtBottom =
|
||||
messagesDiv.scrollHeight - messagesDiv.scrollTop <=
|
||||
messagesDiv.clientHeight
|
||||
handleSocketData({ data, setItems, cacheKey })
|
||||
if (
|
||||
data.action === 'create' &&
|
||||
data.action === "create" &&
|
||||
(isAtBottom || data.item.member.userId === user.id)
|
||||
) {
|
||||
messagesDiv.scrollTo(0, messagesDiv.scrollHeight)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
return () => {
|
||||
authentication?.socket?.off('messages')
|
||||
authentication?.socket?.off("messages")
|
||||
}
|
||||
}, [authentication.socket, setItems, path, user.id, cacheKey])
|
||||
|
||||
@ -77,7 +77,7 @@ export const MessagesProvider: React.FC<
|
||||
resetPagination()
|
||||
nextPage(undefined, () => {
|
||||
const messagesDiv = window.document.getElementById(
|
||||
'messages'
|
||||
"messages",
|
||||
) as HTMLDivElement
|
||||
messagesDiv.scrollTo(0, messagesDiv.scrollHeight)
|
||||
})
|
||||
@ -93,7 +93,7 @@ export const MessagesProvider: React.FC<
|
||||
export const useMessages = (): Messages => {
|
||||
const messages = useContext(MessagesContext)
|
||||
if (messages === defaultMessagesContext) {
|
||||
throw new Error('`useMessages` must be used within a `MessagesProvider`')
|
||||
throw new Error("`useMessages` must be used within a `MessagesProvider`")
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
Reference in New Issue
Block a user