context.tsx 1.3 KB

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