chat-context.tsx
1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use client'
import { useCallback, useMemo } from 'react'
import { createContext, useContext } from 'use-context-selector'
import { noop } from 'lodash-es'
import type { ChatItem, Feedback, InputMessageType, OnRegenerate } from '@/app/components/base/chat/types'
import { FileEntity } from '@/app/components/base/file-uploader/types'
export type OnSendMessage = (
message: string,
type?: InputMessageType,
files?: FileEntity[],
parentMessageId?: string,
) => void
export type ChatContextValue = {
onSend: OnSendMessage
onRegenerate?: OnRegenerate
onRestart: () => void
onFeedback?: (messageId: string, feedback: Feedback) => void
onStopResponding?: () => void
}
const ChatContext = createContext<ChatContextValue>({
onRestart: noop,
onSend: noop,
onFeedback: noop,
onStopResponding: noop,
})
type ChatContextProviderProps = {
children: React.ReactNode
} & ChatContextValue
export const ChatContextProvider = ({
children,
onSend,
onRegenerate,
onRestart,
onFeedback,
onStopResponding,
}: ChatContextProviderProps) => {
const value = useMemo(
() => ({
onSend,
onRegenerate,
onRestart,
onFeedback,
onStopResponding,
}),
[onSend, onRegenerate, onRestart, onFeedback, onStopResponding],
)
return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>
}
export const useChatContext = () => useContext(ChatContext)
export default ChatContext