context.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. | 'onSend'
  12. | 'onRegenerate'
  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. onSend,
  33. onRegenerate,
  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. onSend,
  48. onRegenerate,
  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