chat-responding-context.tsx
1.05 KB
// app/components/base/chat/chat/chat-runtime-context.tsx
'use client'
import { createContext, useContext } from 'use-context-selector'
import { useEffect, useMemo } from 'react'
import { usePostMessage } from '../hooks'
type RespondingContext = {
isResponding?: boolean
taskId?: string
}
const ChatRespondingContext = createContext<RespondingContext>({
isResponding: false,
taskId: '',
})
export const useChatRespondingContext = () => useContext(ChatRespondingContext)
type Props = {
children: React.ReactNode
} & RespondingContext
const postMessage = usePostMessage()
export const ChatRespondingProvider = ({ children, isResponding, taskId }: Props) => {
const __isResponding = useMemo(() => isResponding, [isResponding])
const __taskId = useMemo(() => taskId, [taskId])
const value = useMemo(() => ({ isResponding: __isResponding, taskId: __taskId }), [__isResponding, __taskId])
useEffect(() => postMessage(value), [__isResponding, __taskId])
return <ChatRespondingContext.Provider value={value}>{children}</ChatRespondingContext.Provider>
}