|
|
@@ -17,6 +17,8 @@ import { appendMessage, prependMessages } from '../utils/message-list-appender'
|
|
|
import useBrowserWindow from '../../../shared/hooks/use-browser-window'
|
|
|
import { useLayoutContext } from '../../../shared/context/layout-context'
|
|
|
import { useIdeContext } from '@/shared/context/ide-context'
|
|
|
+import getMeta from '@/utils/meta'
|
|
|
+import { debugConsole } from '@/utils/debugging'
|
|
|
|
|
|
const PAGE_SIZE = 50
|
|
|
|
|
|
@@ -187,6 +189,8 @@ export const ChatContext = createContext<
|
|
|
>(undefined)
|
|
|
|
|
|
export const ChatProvider: FC = ({ children }) => {
|
|
|
+ const chatEnabled = getMeta('ol-chatEnabled')
|
|
|
+
|
|
|
const clientId = useRef<string>()
|
|
|
if (clientId.current === undefined) {
|
|
|
clientId.current = chatClientIdGenerator.generate()
|
|
|
@@ -235,6 +239,10 @@ export const ChatProvider: FC = ({ children }) => {
|
|
|
}
|
|
|
|
|
|
function loadInitialMessages() {
|
|
|
+ if (!chatEnabled) {
|
|
|
+ debugConsole.warn(`chat is disabled, won't load initial messages`)
|
|
|
+ return
|
|
|
+ }
|
|
|
if (state.initialMessagesLoaded) return
|
|
|
|
|
|
dispatch({ type: 'INITIAL_FETCH_MESSAGES' })
|
|
|
@@ -242,11 +250,19 @@ export const ChatProvider: FC = ({ children }) => {
|
|
|
}
|
|
|
|
|
|
function loadMoreMessages() {
|
|
|
+ if (!chatEnabled) {
|
|
|
+ debugConsole.warn(`chat is disabled, won't load messages`)
|
|
|
+ return
|
|
|
+ }
|
|
|
dispatch({ type: 'FETCH_MESSAGES' })
|
|
|
fetchMessages()
|
|
|
}
|
|
|
|
|
|
function reset() {
|
|
|
+ if (!chatEnabled) {
|
|
|
+ debugConsole.warn(`chat is disabled, won't reset chat`)
|
|
|
+ return
|
|
|
+ }
|
|
|
dispatch({ type: 'CLEAR' })
|
|
|
fetchMessages()
|
|
|
}
|
|
|
@@ -256,10 +272,20 @@ export const ChatProvider: FC = ({ children }) => {
|
|
|
loadMoreMessages,
|
|
|
reset,
|
|
|
}
|
|
|
- }, [projectId, state.atEnd, state.initialMessagesLoaded, state.lastTimestamp])
|
|
|
+ }, [
|
|
|
+ chatEnabled,
|
|
|
+ projectId,
|
|
|
+ state.atEnd,
|
|
|
+ state.initialMessagesLoaded,
|
|
|
+ state.lastTimestamp,
|
|
|
+ ])
|
|
|
|
|
|
const sendMessage = useCallback(
|
|
|
content => {
|
|
|
+ if (!chatEnabled) {
|
|
|
+ debugConsole.warn(`chat is disabled, won't send message`)
|
|
|
+ return
|
|
|
+ }
|
|
|
if (!content) return
|
|
|
|
|
|
dispatch({
|
|
|
@@ -278,17 +304,21 @@ export const ChatProvider: FC = ({ children }) => {
|
|
|
})
|
|
|
})
|
|
|
},
|
|
|
- [projectId, user]
|
|
|
+ [chatEnabled, projectId, user]
|
|
|
)
|
|
|
|
|
|
const markMessagesAsRead = useCallback(() => {
|
|
|
+ if (!chatEnabled) {
|
|
|
+ debugConsole.warn(`chat is disabled, won't mark messages as read`)
|
|
|
+ return
|
|
|
+ }
|
|
|
dispatch({ type: 'MARK_MESSAGES_AS_READ' })
|
|
|
- }, [])
|
|
|
+ }, [chatEnabled])
|
|
|
|
|
|
// Handling receiving messages over the socket
|
|
|
const { socket } = useIdeContext()
|
|
|
useEffect(() => {
|
|
|
- if (!socket) return
|
|
|
+ if (!chatEnabled || !socket) return
|
|
|
|
|
|
function receivedMessage(message: any) {
|
|
|
// If the message is from the current client id, then we are receiving the sent message back from the socket.
|
|
|
@@ -304,7 +334,7 @@ export const ChatProvider: FC = ({ children }) => {
|
|
|
|
|
|
socket.removeListener('new-chat-message', receivedMessage)
|
|
|
}
|
|
|
- }, [socket])
|
|
|
+ }, [chatEnabled, socket])
|
|
|
|
|
|
// Handle unread messages
|
|
|
useEffect(() => {
|