index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback } from 'react'
  4. import produce from 'immer'
  5. import s from './style.module.css'
  6. import cn from '@/utils/classnames'
  7. import Switch from '@/app/components/base/switch'
  8. import { FeatureEnum } from '@/app/components/base/features/types'
  9. import { useFeaturesStore } from '@/app/components/base/features/hooks'
  10. import { useModalContext } from '@/context/modal-context'
  11. export type IFeatureItemProps = {
  12. icon: React.ReactNode
  13. previewImgClassName?: string
  14. title: string
  15. description: string
  16. value: boolean
  17. onChange: (type: FeatureEnum, value: boolean) => void
  18. type: FeatureEnum
  19. }
  20. const FeatureItem: FC<IFeatureItemProps> = ({
  21. icon,
  22. previewImgClassName,
  23. title,
  24. description,
  25. value,
  26. onChange,
  27. type,
  28. }) => {
  29. const featuresStore = useFeaturesStore()
  30. const { setShowModerationSettingModal } = useModalContext()
  31. const handleChange = useCallback((newValue: boolean) => {
  32. const {
  33. features,
  34. setFeatures,
  35. } = featuresStore!.getState()
  36. if (newValue && !features.moderation?.type && type === FeatureEnum.moderation) {
  37. setShowModerationSettingModal({
  38. payload: {
  39. enabled: true,
  40. type: 'keywords',
  41. config: {
  42. keywords: '',
  43. inputs_config: {
  44. enabled: true,
  45. preset_response: '',
  46. },
  47. },
  48. },
  49. onSaveCallback: (newModeration) => {
  50. setFeatures(produce(features, (draft) => {
  51. draft.moderation = newModeration
  52. }))
  53. },
  54. onCancelCallback: () => {
  55. setFeatures(produce(features, (draft) => {
  56. draft.moderation = { enabled: false }
  57. }))
  58. },
  59. })
  60. return
  61. }
  62. onChange(type, newValue)
  63. }, [type, onChange, featuresStore, setShowModerationSettingModal])
  64. return (
  65. <div className={cn(s.wrap, 'relative flex justify-between p-3 rounded-xl border border-transparent bg-gray-50 hover:border-gray-200 cursor-pointer')}>
  66. <div className='flex space-x-3 mr-2'>
  67. {/* icon */}
  68. <div
  69. className='shrink-0 flex items-center justify-center w-8 h-8 rounded-lg border border-gray-200 bg-white'
  70. style={{
  71. boxShadow: '0px 1px 2px rgba(16, 24, 40, 0.05)',
  72. }}
  73. >
  74. {icon}
  75. </div>
  76. <div>
  77. <div className='text-sm font-semibold text-gray-800'>{title}</div>
  78. <div className='text-xs font-normal text-gray-500'>{description}</div>
  79. </div>
  80. </div>
  81. <Switch onChange={handleChange} defaultValue={value} />
  82. {
  83. previewImgClassName && (
  84. <div className={cn(s.preview, s[previewImgClassName])}>
  85. </div>)
  86. }
  87. </div>
  88. )
  89. }
  90. export default React.memo(FeatureItem)