context.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use client'
  2. import type { ReactNode } from 'react'
  3. import { createContext, useContext } from 'use-context-selector'
  4. import type { ChatProps } from './index'
  5. export type ChatContextValue = Pick<ChatProps, 'config'
  6. | 'isResponsing'
  7. | 'chatList'
  8. | 'showPromptLog'
  9. | 'questionIcon'
  10. | 'answerIcon'
  11. | 'allToolIcons'
  12. | 'onSend'
  13. | 'onAnnotationEdited'
  14. | 'onAnnotationAdded'
  15. | 'onAnnotationRemoved'
  16. >
  17. const ChatContext = createContext<ChatContextValue>({
  18. chatList: [],
  19. })
  20. type ChatContextProviderProps = {
  21. children: ReactNode
  22. } & ChatContextValue
  23. export const ChatContextProvider = ({
  24. children,
  25. config,
  26. isResponsing,
  27. chatList,
  28. showPromptLog,
  29. questionIcon,
  30. answerIcon,
  31. allToolIcons,
  32. onSend,
  33. onAnnotationEdited,
  34. onAnnotationAdded,
  35. onAnnotationRemoved,
  36. }: ChatContextProviderProps) => {
  37. return (
  38. <ChatContext.Provider value={{
  39. config,
  40. isResponsing,
  41. chatList: chatList || [],
  42. showPromptLog,
  43. questionIcon,
  44. answerIcon,
  45. allToolIcons,
  46. onSend,
  47. onAnnotationEdited,
  48. onAnnotationAdded,
  49. onAnnotationRemoved,
  50. }}>
  51. {children}
  52. </ChatContext.Provider>
  53. )
  54. }
  55. export const useChatContext = () => useContext(ChatContext)
  56. export default ChatContext