Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Assistant): Now exclude realtime message if not relevant #2240

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/assistant/AssistantProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useMemo, useContext, useState, useCallback } from 'react'
import { useParams } from 'react-router-dom'
import set from 'lodash/set'

import { useClient } from 'cozy-client'
Expand All @@ -19,10 +20,11 @@ export const useAssistant = () => {
}

const AssistantProvider = ({ children }) => {
const { conversationId } = useParams()
const client = useClient()
const [assistantState, setAssistantState] = useState({
message: {},
status: 'idle',
status: 'pending',
messagesId: []
})

Expand All @@ -31,10 +33,10 @@ const AssistantProvider = ({ children }) => {
{
[CHAT_CONVERSATIONS_DOCTYPE]: {
created: res => {
pushMessagesIdInState(res, setAssistantState)
pushMessagesIdInState(conversationId, res, setAssistantState)
},
updated: res => {
pushMessagesIdInState(res, setAssistantState)
pushMessagesIdInState(conversationId, res, setAssistantState)
}
}
},
Expand Down Expand Up @@ -82,7 +84,7 @@ const AssistantProvider = ({ children }) => {
() =>
setAssistantState({
message: {},
status: 'idle',
status: 'pending',
messagesId: []
}),
[]
Expand Down
9 changes: 6 additions & 3 deletions src/assistant/AssistantWrapperDesktop.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import React from 'react'

import SearchBar from './Search/SearchBar'
import SearchProvider from './Search/SearchProvider'
import AssistantProvider from './AssistantProvider'

const AssistantWrapperDesktop = () => {
return (
<div className="app-list-wrapper u-mb-3 u-mh-auto u-w-100">
<SearchProvider>
<SearchBar />
</SearchProvider>
<AssistantProvider>
<SearchProvider>
<SearchBar />
</SearchProvider>
</AssistantProvider>
</div>
)
}
Expand Down
12 changes: 10 additions & 2 deletions src/assistant/Views/AssistantDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useBreakpoints } from 'cozy-ui/transpiled/react/providers/Breakpoints'

import Conversation from '../Conversations/Conversation'
import ConversationBar from '../Conversations/ConversationBar'
import { useAssistant } from '../AssistantProvider'
import AssistantProvider, { useAssistant } from '../AssistantProvider'

const AssistantDialog = () => {
const { assistantState } = useAssistant()
Expand Down Expand Up @@ -36,4 +36,12 @@ const AssistantDialog = () => {
)
}

export default AssistantDialog
const AssistantDialogWithProviders = () => {
return (
<AssistantProvider>
<AssistantDialog />
</AssistantProvider>
)
}

export default AssistantDialogWithProviders
10 changes: 6 additions & 4 deletions src/assistant/Views/SearchDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import flag from 'cozy-flags'
import { FixedDialog } from 'cozy-ui/transpiled/react/CozyDialogs'

import SearchProvider from '../Search/SearchProvider'
import { useAssistant } from '../AssistantProvider'
import AssistantProvider, { useAssistant } from '../AssistantProvider'
import { makeConversationId } from '../helpers'
import ResultMenuContent from '../ResultMenu/ResultMenuContent'
import { useSearch } from '../Search/SearchProvider'
Expand Down Expand Up @@ -54,9 +54,11 @@ const SearchDialog = () => {

const SearchDialogWithProviders = () => {
return (
<SearchProvider>
<SearchDialog />
</SearchProvider>
<AssistantProvider>
<SearchProvider>
<SearchDialog />
</SearchProvider>
</AssistantProvider>
)
}

Expand Down
4 changes: 3 additions & 1 deletion src/assistant/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export const getInstantMessage = assistantState =>
export const makeConversationId = () =>
`${Date.now()}-${Math.floor(Math.random() * 90000) + 10000}`

export const pushMessagesIdInState = (res, setState) => {
export const pushMessagesIdInState = (id, res, setState) => {
if (id !== res._id) return

const messagesId = res.messages.map(message => message.id)
setState(v => ({
...v,
Expand Down
29 changes: 13 additions & 16 deletions src/components/AppWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { useWallpaperContext } from 'hooks/useWallpaperContext'

import schema from '../schema'
import { ConditionalWrapper } from './ConditionalWrapper'
import AssistantProvider from 'assistant/AssistantProvider'
import { WallPaperProvider } from 'hooks/useWallpaperContext'
import { SectionsProvider } from './Sections/SectionsContext'
const dictRequire = lang => require(`locales/${lang}.json`)
Expand Down Expand Up @@ -116,22 +115,20 @@ const AppWrapper = ({ children }) => {
<CozyTheme>
<ThemeProvider>
<AlertProvider>
<AssistantProvider>
<ReduxProvider store={store}>
<ConditionalWrapper
condition={persistor}
wrapper={children => (
<PersistGate loading={null} persistor={persistor}>
{children}
</PersistGate>
)}
>
<Inner lang={lang} context={context}>
<ReduxProvider store={store}>
<ConditionalWrapper
condition={persistor}
wrapper={children => (
<PersistGate loading={null} persistor={persistor}>
{children}
</Inner>
</ConditionalWrapper>
</ReduxProvider>
</AssistantProvider>
</PersistGate>
)}
>
<Inner lang={lang} context={context}>
{children}
</Inner>
</ConditionalWrapper>
</ReduxProvider>
</AlertProvider>
</ThemeProvider>
</CozyTheme>
Expand Down
Loading