Skip to content

Commit

Permalink
refactor(Assistant): Extract some helpers from provider
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Oct 31, 2024
1 parent e8cee3e commit 913059a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
21 changes: 1 addition & 20 deletions src/assistant/AssistantProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion src/assistant/Conversations/ChatConversation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/assistant/Search/SearchBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
3 changes: 2 additions & 1 deletion src/assistant/Views/SearchDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
19 changes: 19 additions & 0 deletions src/assistant/helpers.js
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 913059a

Please sign in to comment.