index.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import React, { useCallback, useMemo, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import useSWR from 'swr'
  4. import produce from 'immer'
  5. import { useContext } from 'use-context-selector'
  6. import { RiEqualizer2Line } from '@remixicon/react'
  7. import { ContentModeration } from '@/app/components/base/icons/src/vender/features'
  8. import FeatureCard from '@/app/components/base/features/new-feature-panel/feature-card'
  9. import Button from '@/app/components/base/button'
  10. import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks'
  11. import type { OnFeaturesChange } from '@/app/components/base/features/types'
  12. import { FeatureEnum } from '@/app/components/base/features/types'
  13. import { fetchCodeBasedExtensionList } from '@/service/common'
  14. import { useModalContext } from '@/context/modal-context'
  15. import I18n from '@/context/i18n'
  16. type Props = {
  17. disabled?: boolean
  18. onChange?: OnFeaturesChange
  19. }
  20. const Moderation = ({
  21. disabled,
  22. onChange,
  23. }: Props) => {
  24. const { t } = useTranslation()
  25. const { setShowModerationSettingModal } = useModalContext()
  26. const { locale } = useContext(I18n)
  27. const featuresStore = useFeaturesStore()
  28. const moderation = useFeatures(s => s.features.moderation)
  29. const { data: codeBasedExtensionList } = useSWR(
  30. '/code-based-extension?module=moderation',
  31. fetchCodeBasedExtensionList,
  32. )
  33. const [isHovering, setIsHovering] = useState(false)
  34. const handleOpenModerationSettingModal = () => {
  35. if (disabled)
  36. return
  37. const {
  38. features,
  39. setFeatures,
  40. } = featuresStore!.getState()
  41. setShowModerationSettingModal({
  42. payload: moderation as any,
  43. onSaveCallback: (newModeration) => {
  44. const newFeatures = produce(features, (draft) => {
  45. draft.moderation = newModeration
  46. })
  47. setFeatures(newFeatures)
  48. if (onChange)
  49. onChange(newFeatures)
  50. },
  51. onCancelCallback: () => {
  52. if (onChange)
  53. onChange()
  54. },
  55. })
  56. }
  57. const handleChange = useCallback((type: FeatureEnum, enabled: boolean) => {
  58. const {
  59. features,
  60. setFeatures,
  61. } = featuresStore!.getState()
  62. if (enabled && !features.moderation?.type && type === FeatureEnum.moderation) {
  63. setShowModerationSettingModal({
  64. payload: {
  65. enabled: true,
  66. type: 'keywords',
  67. config: {
  68. keywords: '',
  69. inputs_config: {
  70. enabled: true,
  71. preset_response: '',
  72. },
  73. },
  74. },
  75. onSaveCallback: (newModeration) => {
  76. const newFeatures = produce(features, (draft) => {
  77. draft.moderation = newModeration
  78. })
  79. setFeatures(newFeatures)
  80. if (onChange)
  81. onChange(newFeatures)
  82. },
  83. onCancelCallback: () => {
  84. const newFeatures = produce(features, (draft) => {
  85. draft.moderation = { enabled: false }
  86. })
  87. setFeatures(newFeatures)
  88. if (onChange)
  89. onChange()
  90. },
  91. })
  92. }
  93. if (!enabled) {
  94. const newFeatures = produce(features, (draft) => {
  95. draft.moderation = { enabled: false }
  96. })
  97. setFeatures(newFeatures)
  98. if (onChange)
  99. onChange(newFeatures)
  100. }
  101. }, [featuresStore, onChange, setShowModerationSettingModal])
  102. const providerContent = useMemo(() => {
  103. if (moderation?.type === 'openai_moderation')
  104. return t('appDebug.feature.moderation.modal.provider.openai')
  105. else if (moderation?.type === 'keywords')
  106. return t('appDebug.feature.moderation.modal.provider.keywords')
  107. else if (moderation?.type === 'api')
  108. return t('common.apiBasedExtension.selector.title')
  109. else
  110. return codeBasedExtensionList?.data.find(item => item.name === moderation?.type)?.label[locale] || '-'
  111. }, [codeBasedExtensionList?.data, locale, moderation?.type, t])
  112. const enableContent = useMemo(() => {
  113. if (moderation?.config?.inputs_config?.enabled && moderation.config?.outputs_config?.enabled)
  114. return t('appDebug.feature.moderation.allEnabled')
  115. else if (moderation?.config?.inputs_config?.enabled)
  116. return t('appDebug.feature.moderation.inputEnabled')
  117. else if (moderation?.config?.outputs_config?.enabled)
  118. return t('appDebug.feature.moderation.outputEnabled')
  119. }, [moderation?.config?.inputs_config?.enabled, moderation?.config?.outputs_config?.enabled, t])
  120. return (
  121. <FeatureCard
  122. icon={
  123. <div className='shrink-0 p-1 rounded-lg border-[0.5px] border-divider-subtle shadow-xs bg-text-success'>
  124. <ContentModeration className='w-4 h-4 text-text-primary-on-surface' />
  125. </div>
  126. }
  127. title={t('appDebug.feature.moderation.title')}
  128. value={!!moderation?.enabled}
  129. onChange={state => handleChange(FeatureEnum.moderation, state)}
  130. onMouseEnter={() => setIsHovering(true)}
  131. onMouseLeave={() => setIsHovering(false)}
  132. disabled={disabled}
  133. >
  134. <>
  135. {!moderation?.enabled && (
  136. <div className='min-h-8 text-text-tertiary system-xs-regular line-clamp-2'>{t('appDebug.feature.moderation.description')}</div>
  137. )}
  138. {!!moderation?.enabled && (
  139. <>
  140. {!isHovering && (
  141. <div className='pt-0.5 flex items-center gap-4'>
  142. <div className=''>
  143. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('appDebug.feature.moderation.modal.provider.title')}</div>
  144. <div className='text-text-secondary system-xs-regular'>{providerContent}</div>
  145. </div>
  146. <div className='w-px h-[27px] bg-divider-subtle rotate-12'></div>
  147. <div className=''>
  148. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('appDebug.feature.moderation.contentEnableLabel')}</div>
  149. <div className='text-text-secondary system-xs-regular'>{enableContent}</div>
  150. </div>
  151. </div>
  152. )}
  153. {isHovering && (
  154. <Button className='w-full' onClick={handleOpenModerationSettingModal} disabled={disabled}>
  155. <RiEqualizer2Line className='mr-1 w-4 h-4' />
  156. {t('common.operation.settings')}
  157. </Button>
  158. )}
  159. </>
  160. )}
  161. </>
  162. </FeatureCard>
  163. )
  164. }
  165. export default Moderation