diff --git a/src/assistant/AssistantProvider.jsx b/src/assistant/AssistantProvider.jsx index fe648badc..71b31ed03 100644 --- a/src/assistant/AssistantProvider.jsx +++ b/src/assistant/AssistantProvider.jsx @@ -5,18 +5,10 @@ import { useClient } from 'cozy-client' import useRealtime from 'cozy-realtime/dist/useRealtime' import { CHAT_EVENTS_DOCTYPE, CHAT_CONVERSATIONS_DOCTYPE } from './queries' +import { pushMessagesIdInState, isMessageForThisConversation } from './helpers' export const AssistantContext = React.createContext() -export const getInstantMessage = assistantState => - Object.keys(assistantState.message) - .sort((a, b) => a - b) - .map(key => assistantState.message[key]) - .join('') - -export const makeConversationId = () => - `${Date.now()}-${Math.floor(Math.random() * 90000) + 10000}` - export const useAssistant = () => { const context = useContext(AssistantContext) @@ -26,17 +18,6 @@ export const useAssistant = () => { return context } -const pushMessagesIdInState = (res, setState) => { - const messagesId = res.messages.map(message => message.id) - setState(v => ({ - ...v, - messagesId - })) -} - -const isMessageForThisConversation = (res, messagesId) => - messagesId.includes(res._id) - const AssistantProvider = ({ children }) => { const client = useClient() const [assistantState, setAssistantState] = useState({ diff --git a/src/assistant/Conversations/ChatConversation.jsx b/src/assistant/Conversations/ChatConversation.jsx index 0b9ddcc9b..2989422f8 100644 --- a/src/assistant/Conversations/ChatConversation.jsx +++ b/src/assistant/Conversations/ChatConversation.jsx @@ -3,7 +3,8 @@ import React, { useRef, useEffect } from 'react' import { useQuery, isQueryLoading } from 'cozy-client' import { buildChatConversationQueryById } from '../queries' -import { useAssistant, getInstantMessage } from '../AssistantProvider' +import { useAssistant } from '../AssistantProvider' +import { getInstantMessage } from '../helpers' import ChatUserItem from './ChatUserItem' import ChatAssistantItem from './ChatAssistantItem' import ChatRealtimeAnswer from './ChatRealtimeAnswer' diff --git a/src/assistant/Search/SearchBar.jsx b/src/assistant/Search/SearchBar.jsx index 7dccced67..7aebf8fec 100644 --- a/src/assistant/Search/SearchBar.jsx +++ b/src/assistant/Search/SearchBar.jsx @@ -5,7 +5,8 @@ import flag from 'cozy-flags' import { useBreakpoints } from 'cozy-ui/transpiled/react/providers/Breakpoints' import { useSearch } from './SearchProvider' -import { makeConversationId, useAssistant } from '../AssistantProvider' +import { useAssistant } from '../AssistantProvider' +import { makeConversationId } from '../helpers' import SearchBarMobile from './SearchBarMobile' import SearchBarDesktop from './SearchBarDesktop' diff --git a/src/assistant/Views/SearchDialog.jsx b/src/assistant/Views/SearchDialog.jsx index 875289549..958649f36 100644 --- a/src/assistant/Views/SearchDialog.jsx +++ b/src/assistant/Views/SearchDialog.jsx @@ -5,7 +5,8 @@ import flag from 'cozy-flags' import { FixedDialog } from 'cozy-ui/transpiled/react/CozyDialogs' import SearchProvider from '../Search/SearchProvider' -import { makeConversationId, useAssistant } from '../AssistantProvider' +import { useAssistant } from '../AssistantProvider' +import { makeConversationId } from '../helpers' import ResultMenuContent from '../ResultMenu/ResultMenuContent' import { useSearch } from '../Search/SearchProvider' import SearchBar from '../Search/SearchBar' diff --git a/src/assistant/helpers.js b/src/assistant/helpers.js new file mode 100644 index 000000000..4fa0e8b10 --- /dev/null +++ b/src/assistant/helpers.js @@ -0,0 +1,19 @@ +export const getInstantMessage = assistantState => + Object.keys(assistantState.message) + .sort((a, b) => a - b) + .map(key => assistantState.message[key]) + .join('') + +export const makeConversationId = () => + `${Date.now()}-${Math.floor(Math.random() * 90000) + 10000}` + +export const pushMessagesIdInState = (res, setState) => { + const messagesId = res.messages.map(message => message.id) + setState(v => ({ + ...v, + messagesId + })) +} + +export const isMessageForThisConversation = (res, messagesId) => + messagesId.includes(res._id)