context.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. | 'isResponding'
  7. | 'chatList'
  8. | 'showPromptLog'
  9. | 'questionIcon'
  10. | 'answerIcon'
  11. | 'allToolIcons'
  12. | 'onSend'
  13. | 'onAnnotationEdited'
  14. | 'onAnnotationAdded'
  15. | 'onAnnotationRemoved'
  16. | 'onFeedback'
  17. >
  18. const ChatContext = createContext<ChatContextValue>({
  19. chatList: [],
  20. })
  21. type ChatContextProviderProps = {
  22. children: ReactNode
  23. } & ChatContextValue
  24. export const ChatContextProvider = ({
  25. children,
  26. config,
  27. isResponding,
  28. chatList,
  29. showPromptLog,
  30. questionIcon,
  31. answerIcon,
  32. allToolIcons,
  33. onSend,
  34. onAnnotationEdited,
  35. onAnnotationAdded,
  36. onAnnotationRemoved,
  37. onFeedback,
  38. }: ChatContextProviderProps) => {
  39. return (
  40. <ChatContext.Provider value={{
  41. config,
  42. isResponding,
  43. chatList: chatList || [],
  44. showPromptLog,
  45. questionIcon,
  46. answerIcon,
  47. allToolIcons,
  48. onSend,
  49. onAnnotationEdited,
  50. onAnnotationAdded,
  51. onAnnotationRemoved,
  52. onFeedback,
  53. }}>
  54. {children}
  55. </ChatContext.Provider>
  56. )
  57. }
  58. export const useChatContext = () => useContext(ChatContext)
  59. export default ChatContext