Skip to content

Commit

Permalink
fix(Assistant): Don't clear provider when navigating
Browse files Browse the repository at this point in the history
we should not use clear providers method in combination with navigation, because the behavior will be different when using back browser button
  • Loading branch information
JF-Cozy committed Oct 28, 2024
1 parent 49036d1 commit fb001fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
30 changes: 13 additions & 17 deletions src/assistant/AssistantProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const AssistantProvider = ({ children }) => {
useRealtime(client, {
[CHAT_EVENTS_DOCTYPE]: {
created: res => {
// to exclude realtime messages if not relevant to the actual conversation
if (!isMessageForThisConversation(res, assistantState.messagesId)) {
return
}
Expand Down Expand Up @@ -79,17 +80,23 @@ const AssistantProvider = ({ children }) => {
}
})

const clearAssistant = useCallback(
() =>
setAssistantState({
message: '',
status: 'idle',
messagesId: []
}),
[]
)

const onAssistantExecute = useCallback(
async ({ value, conversationId }, callback) => {
if (!value) return

callback?.()

setAssistantState(v => ({
...v,
message: '',
status: 'idle'
}))
clearAssistant()

await client.stackClient.fetchJSON(
'POST',
Expand All @@ -101,21 +108,10 @@ const AssistantProvider = ({ children }) => {

setAssistantState(v => ({
...v,
message: '',
status: 'pending'
}))
},
[client]
)

const clearAssistant = useCallback(
() =>
setAssistantState({
message: '',
status: 'idle',
messagesId: []
}),
[]
[client, clearAssistant]
)

const value = useMemo(
Expand Down
3 changes: 1 addition & 2 deletions src/assistant/Views/AssistantDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import ConversationBar from '../Conversations/ConversationBar'
import { useAssistant } from '../AssistantProvider'

const AssistantDialog = () => {
const { assistantState, clearAssistant } = useAssistant()
const { assistantState } = useAssistant()
const { isMobile } = useBreakpoints()
const navigate = useNavigate()
const { conversationId } = useParams()

const onClose = () => {
navigate('..')
clearAssistant()
}

return (
Expand Down

0 comments on commit fb001fc

Please sign in to comment.