index.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import React, { useCallback, useMemo, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import produce from 'immer'
  4. import { RiEqualizer2Line, RiImage2Fill } from '@remixicon/react'
  5. import FeatureCard from '@/app/components/base/features/new-feature-panel/feature-card'
  6. import SettingModal from '@/app/components/base/features/new-feature-panel/file-upload/setting-modal'
  7. import Badge from '@/app/components/base/badge'
  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-indigo-indigo-600'>
  48. <RiImage2Fill className='w-4 h-4 text-text-primary-on-surface' />
  49. </div>
  50. }
  51. title={
  52. <div className='flex items-center'>
  53. {t('appDebug.feature.imageUpload.title')}
  54. <Badge
  55. text='LEGACY'
  56. className='shrink-0 mx-1 border-text-accent-secondary text-text-accent-secondary'
  57. />
  58. </div>
  59. }
  60. value={file?.enabled}
  61. onChange={state => handleChange(FeatureEnum.file, state)}
  62. onMouseEnter={() => setIsHovering(true)}
  63. onMouseLeave={() => setIsHovering(false)}
  64. disabled={disabled}
  65. >
  66. <>
  67. {!file?.enabled && (
  68. <div className='min-h-8 text-text-tertiary system-xs-regular line-clamp-2'>{t('appDebug.feature.imageUpload.description')}</div>
  69. )}
  70. {file?.enabled && (
  71. <>
  72. {!isHovering && !modalOpen && (
  73. <div className='pt-0.5 flex items-center gap-4'>
  74. <div className=''>
  75. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('appDebug.feature.imageUpload.supportedTypes')}</div>
  76. <div className='text-text-secondary system-xs-regular'>{supportedTypes}</div>
  77. </div>
  78. <div className='w-px h-[27px] bg-divider-subtle rotate-12'></div>
  79. <div className=''>
  80. <div className='mb-0.5 text-text-tertiary system-2xs-medium-uppercase'>{t('appDebug.feature.imageUpload.numberLimit')}</div>
  81. <div className='text-text-secondary system-xs-regular'>{file?.number_limits}</div>
  82. </div>
  83. </div>
  84. )}
  85. {(isHovering || modalOpen) && (
  86. <SettingModal
  87. imageUpload
  88. open={modalOpen && !disabled}
  89. onOpen={(v) => {
  90. setModalOpen(v)
  91. setIsHovering(v)
  92. }}
  93. onChange={onChange}
  94. >
  95. <Button className='w-full' disabled={disabled}>
  96. <RiEqualizer2Line className='mr-1 w-4 h-4' />
  97. {t('common.operation.settings')}
  98. </Button>
  99. </SettingModal>
  100. )}
  101. </>
  102. )}
  103. </>
  104. </FeatureCard>
  105. )
  106. }
  107. export default FileUpload