index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import React, { useCallback, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import produce from 'immer'
  4. import { RiEditLine } from '@remixicon/react'
  5. import { LoveMessage } from '@/app/components/base/icons/src/vender/features'
  6. import FeatureCard from '@/app/components/base/features/new-feature-panel/feature-card'
  7. import Button from '@/app/components/base/button'
  8. import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks'
  9. import type { OnFeaturesChange } from '@/app/components/base/features/types'
  10. import { FeatureEnum } from '@/app/components/base/features/types'
  11. import { useModalContext } from '@/context/modal-context'
  12. import type { PromptVariable } from '@/models/debug'
  13. import type { InputVar } from '@/app/components/workflow/types'
  14. type Props = {
  15. disabled?: boolean
  16. onChange?: OnFeaturesChange
  17. promptVariables?: PromptVariable[]
  18. workflowVariables?: InputVar[]
  19. onAutoAddPromptVariable?: (variable: PromptVariable[]) => void
  20. }
  21. const ConversationOpener = ({
  22. disabled,
  23. onChange,
  24. promptVariables,
  25. workflowVariables,
  26. onAutoAddPromptVariable,
  27. }: Props) => {
  28. const { t } = useTranslation()
  29. const { setShowOpeningModal } = useModalContext()
  30. const opening = useFeatures(s => s.features.opening)
  31. const featuresStore = useFeaturesStore()
  32. const [isHovering, setIsHovering] = useState(false)
  33. const handleOpenOpeningModal = useCallback(() => {
  34. if (disabled)
  35. return
  36. const {
  37. features,
  38. setFeatures,
  39. } = featuresStore!.getState()
  40. setShowOpeningModal({
  41. payload: {
  42. ...opening,
  43. promptVariables,
  44. workflowVariables,
  45. onAutoAddPromptVariable,
  46. },
  47. onSaveCallback: (newOpening) => {
  48. const newFeatures = produce(features, (draft) => {
  49. draft.opening = newOpening
  50. })
  51. setFeatures(newFeatures)
  52. if (onChange)
  53. onChange()
  54. },
  55. onCancelCallback: () => {
  56. if (onChange)
  57. onChange()
  58. },
  59. })
  60. }, [disabled, featuresStore, onAutoAddPromptVariable, onChange, opening, promptVariables, setShowOpeningModal])
  61. const handleChange = useCallback((type: FeatureEnum, enabled: boolean) => {
  62. const {
  63. features,
  64. setFeatures,
  65. } = featuresStore!.getState()
  66. const newFeatures = produce(features, (draft) => {
  67. draft[type] = {
  68. ...draft[type],
  69. enabled,
  70. }
  71. })
  72. setFeatures(newFeatures)
  73. if (onChange)
  74. onChange()
  75. }, [featuresStore, onChange])
  76. return (
  77. <FeatureCard
  78. icon={
  79. <div className='shrink-0 p-1 rounded-lg border-[0.5px] border-divider-subtle shadow-xs bg-util-colors-blue-light-blue-light-500'>
  80. <LoveMessage className='w-4 h-4 text-text-primary-on-surface' />
  81. </div>
  82. }
  83. title={t('appDebug.feature.conversationOpener.title')}
  84. value={!!opening?.enabled}
  85. onChange={state => handleChange(FeatureEnum.opening, state)}
  86. onMouseEnter={() => setIsHovering(true)}
  87. onMouseLeave={() => setIsHovering(false)}
  88. disabled={disabled}
  89. >
  90. <>
  91. {!opening?.enabled && (
  92. <div className='min-h-8 text-text-tertiary system-xs-regular line-clamp-2'>{t('appDebug.feature.conversationOpener.description')}</div>
  93. )}
  94. {!!opening?.enabled && (
  95. <>
  96. {!isHovering && (
  97. <div className='min-h-8 text-text-tertiary system-xs-regular line-clamp-2'>
  98. {opening.opening_statement || t('appDebug.openingStatement.placeholder')}
  99. </div>
  100. )}
  101. {isHovering && (
  102. <Button className='w-full' onClick={handleOpenOpeningModal} disabled={disabled}>
  103. <RiEditLine className='mr-1 w-4 h-4' />
  104. {t('appDebug.openingStatement.writeOpener')}
  105. </Button>
  106. )}
  107. </>
  108. )}
  109. </>
  110. </FeatureCard>
  111. )
  112. }
  113. export default ConversationOpener