index.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use client'
  2. import produce from 'immer'
  3. import React, { useCallback } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import type { OnFeaturesChange } from '../../types'
  6. import {
  7. useFeatures,
  8. useFeaturesStore,
  9. } from '../../hooks'
  10. import ParamConfig from './param-config'
  11. import Switch from '@/app/components/base/switch'
  12. import { File05 } from '@/app/components/base/icons/src/vender/solid/files'
  13. type FileUploadProps = {
  14. onChange?: OnFeaturesChange
  15. disabled?: boolean
  16. }
  17. const FileUpload = ({
  18. onChange,
  19. disabled,
  20. }: FileUploadProps) => {
  21. const { t } = useTranslation()
  22. const featuresStore = useFeaturesStore()
  23. const file = useFeatures(s => s.features.file)
  24. const handleSwitch = useCallback((value: boolean) => {
  25. const {
  26. features,
  27. setFeatures,
  28. } = featuresStore!.getState()
  29. const newFeatures = produce(features, (draft) => {
  30. if (draft.file?.image)
  31. draft.file.image.enabled = value
  32. })
  33. setFeatures(newFeatures)
  34. if (onChange)
  35. onChange(newFeatures)
  36. }, [featuresStore, onChange])
  37. return (
  38. <div className='flex items-center px-3 h-12 bg-gray-50 rounded-xl overflow-hidden'>
  39. <div className='shrink-0 flex items-center justify-center mr-1 w-6 h-6'>
  40. <File05 className='shrink-0 w-4 h-4 text-[#6938EF]' />
  41. </div>
  42. <div className='shrink-0 mr-2 whitespace-nowrap text-sm text-gray-800 font-semibold'>
  43. {t('common.imageUploader.imageUpload')}
  44. </div>
  45. <div className='grow' />
  46. <div className='flex items-center'>
  47. <ParamConfig onChange={onChange} disabled={disabled} />
  48. <div className='ml-4 mr-3 w-[1px] h-3.5 bg-gray-200'></div>
  49. <Switch
  50. defaultValue={file?.image?.enabled}
  51. onChange={handleSwitch}
  52. disabled={disabled}
  53. size='md'
  54. />
  55. </div>
  56. </div>
  57. )
  58. }
  59. export default React.memo(FileUpload)