You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context.tsx 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use client'
  2. import type { RefObject } from 'react'
  3. import { createContext, useContext } from 'use-context-selector'
  4. import type {
  5. ChatConfig,
  6. ChatItem,
  7. Feedback,
  8. } from '../types'
  9. import type { ThemeBuilder } from './theme/theme-context'
  10. import type {
  11. AppConversationData,
  12. AppData,
  13. AppMeta,
  14. ConversationItem,
  15. } from '@/models/share'
  16. import { noop } from 'lodash-es'
  17. import { AccessMode } from '@/models/access-control'
  18. export type EmbeddedChatbotContextValue = {
  19. accessMode?: AccessMode
  20. userCanAccess?: boolean
  21. appInfoError?: any
  22. appInfoLoading?: boolean
  23. appMeta?: AppMeta
  24. appData?: AppData
  25. appParams?: ChatConfig
  26. appChatListDataLoading?: boolean
  27. currentConversationId: string
  28. currentConversationItem?: ConversationItem
  29. appPrevChatList: ChatItem[]
  30. pinnedConversationList: AppConversationData['data']
  31. conversationList: AppConversationData['data']
  32. newConversationInputs: Record<string, any>
  33. newConversationInputsRef: RefObject<Record<string, any>>
  34. handleNewConversationInputsChange: (v: Record<string, any>) => void
  35. inputsForms: any[]
  36. handleNewConversation: () => void
  37. handleStartChat: (callback?: any) => void
  38. handleChangeConversation: (conversationId: string) => void
  39. handleNewConversationCompleted: (newConversationId: string) => void
  40. chatShouldReloadKey: string
  41. isMobile: boolean
  42. isInstalledApp: boolean
  43. allowResetChat: boolean
  44. appId?: string
  45. handleFeedback: (messageId: string, feedback: Feedback) => void
  46. currentChatInstanceRef: RefObject<{ handleStop: () => void }>
  47. themeBuilder?: ThemeBuilder
  48. clearChatList?: boolean
  49. setClearChatList: (state: boolean) => void
  50. isResponding?: boolean
  51. setIsResponding: (state: boolean) => void,
  52. currentConversationInputs: Record<string, any> | null,
  53. setCurrentConversationInputs: (v: Record<string, any>) => void,
  54. allInputsHidden: boolean
  55. }
  56. export const EmbeddedChatbotContext = createContext<EmbeddedChatbotContextValue>({
  57. userCanAccess: false,
  58. accessMode: AccessMode.SPECIFIC_GROUPS_MEMBERS,
  59. currentConversationId: '',
  60. appPrevChatList: [],
  61. pinnedConversationList: [],
  62. conversationList: [],
  63. newConversationInputs: {},
  64. newConversationInputsRef: { current: {} },
  65. handleNewConversationInputsChange: noop,
  66. inputsForms: [],
  67. handleNewConversation: noop,
  68. handleStartChat: noop,
  69. handleChangeConversation: noop,
  70. handleNewConversationCompleted: noop,
  71. chatShouldReloadKey: '',
  72. isMobile: false,
  73. isInstalledApp: false,
  74. allowResetChat: true,
  75. handleFeedback: noop,
  76. currentChatInstanceRef: { current: { handleStop: noop } },
  77. clearChatList: false,
  78. setClearChatList: noop,
  79. isResponding: false,
  80. setIsResponding: noop,
  81. currentConversationInputs: {},
  82. setCurrentConversationInputs: noop,
  83. allInputsHidden: false,
  84. })
  85. export const useEmbeddedChatbotContext = () => useContext(EmbeddedChatbotContext)