index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import React, { useCallback, useMemo, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import produce from 'immer'
  4. import { RiEqualizer2Line } from '@remixicon/react'
  5. import { FolderUpload } from '@/app/components/base/icons/src/vender/features'
  6. import FeatureCard from '@/app/components/base/features/new-feature-panel/feature-card'
  7. import SettingModal from '@/app/components/base/features/new-feature-panel/file-upload/setting-modal'
  8. import Button from '@/app/components/base/button'
  9. import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks'
  10. import type { OnFeaturesChange } from '@/app/components/base/features/types'
  11. import { FeatureEnum } from '@/app/components/base/features/types'
  12. type Props = {
  13. disabled: boolean
  14. onChange?: OnFeaturesChange
  15. }
  16. const FileUpload = ({
  17. disabled,
  18. onChange,
  19. }: Props) => {
  20. const { t } = useTranslation()
  21. const file = useFeatures(s => s.features.file)
  22. const featuresStore = useFeaturesStore()
  23. const [modalOpen, setModalOpen] = useState(false)
  24. const [isHovering, setIsHovering] = useState(false)
  25. const supportedTypes = useMemo(() => {
  26. return file?.allowed_file_types?.join(',') || '-'
  27. }, [file?.allowed_file_types])
  28. const handleChange = useCallback((type: FeatureEnum, enabled: boolean) => {
  29. const {
  30. features,
  31. setFeatures,
  32. } = featuresStore!.getState()
  33. const newFeatures = produce(features, (draft) => {
  34. draft[type] = {
  35. ...draft[type],
  36. enabled,
  37. image: { enabled },
  38. }
  39. })
  40. setFeatures(newFeatures)
  41. if (onChange)
  42. onChange()
  43. }, [featuresStore, onChange])
  44. return (
  45. <FeatureCard
  46. icon={
  47. <div className='shrink-0 p-1 rounded-lg border-[0.5px] border-divider-subtle shadow-xs bg-util-colors-blue-blue-600'>
  48. <FolderUpload className='w-4 h-4 text-text-primary-on-surface' />
  49. </div>
  50. }
  51. title={t('appDebug.feature.fileUpload.title')}
  52. value={file?.enabled}
  53. onChange={state => handleChange(FeatureEnum.file, state)}
  54. onMouseEnter={() => setIsHovering(true)}
  55. onMouseLeave={() => setIsHovering(false)}
  56. disabled={disabled}
  57. >
  58. <>
  59. {!file?.enabled && (
  60. <div className='min-h-8 text-text-tertiary system-xs-regular line-clamp-2'>{t('appDebug.feature.fileUpload.description')}</div>
  61. )}
  62. {file?.enabled && (
  63. <>
  64. {!isHovering && !modalOpen && (
  65. <div className='pt-0.5 flex items-center gap-4'>
  66. <div className=''>
  67. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('appDebug.feature.fileUpload.supportedTypes')}</div>
  68. <div className='text-text-secondary system-xs-regular'>{supportedTypes}</div>
  69. </div>
  70. <div className='w-px h-[27px] bg-divider-subtle rotate-12'></div>
  71. <div className=''>
  72. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('appDebug.feature.fileUpload.numberLimit')}</div>
  73. <div className='text-text-secondary system-xs-regular'>{file?.number_limits}</div>
  74. </div>
  75. </div>
  76. )}
  77. {(isHovering || modalOpen) && (
  78. <SettingModal
  79. open={modalOpen && !disabled}
  80. onOpen={(v) => {
  81. setModalOpen(v)
  82. setIsHovering(v)
  83. }}
  84. onChange={onChange}
  85. >
  86. <Button className='w-full' disabled={disabled}>
  87. <RiEqualizer2Line className='mr-1 w-4 h-4' />
  88. {t('common.operation.settings')}
  89. </Button>
  90. </SettingModal>
  91. )}
  92. </>
  93. )}
  94. </>
  95. </FeatureCard>
  96. )
  97. }
  98. export default FileUpload