param-config-content.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use client'
  2. import produce from 'immer'
  3. import React, { useCallback, useMemo } 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 RadioGroup from './radio-group'
  11. import { TransferMethod } from '@/types/app'
  12. import ParamItem from '@/app/components/base/param-item'
  13. const MIN = 1
  14. const MAX = 6
  15. type ParamConfigContentProps = {
  16. onChange?: OnFeaturesChange
  17. }
  18. const ParamConfigContent = ({
  19. onChange,
  20. }: ParamConfigContentProps) => {
  21. const { t } = useTranslation()
  22. const featuresStore = useFeaturesStore()
  23. const file = useFeatures(s => s.features.file)
  24. const transferMethod = useMemo(() => {
  25. if (!file?.image?.transfer_methods || file?.image.transfer_methods.length === 2)
  26. return TransferMethod.all
  27. return file.image.transfer_methods[0]
  28. }, [file?.image?.transfer_methods])
  29. const handleTransferMethodsChange = useCallback((value: TransferMethod) => {
  30. const {
  31. features,
  32. setFeatures,
  33. } = featuresStore!.getState()
  34. const newFeatures = produce(features, (draft) => {
  35. if (draft.file?.image) {
  36. if (value === TransferMethod.all)
  37. draft.file.image.transfer_methods = [TransferMethod.remote_url, TransferMethod.local_file]
  38. else
  39. draft.file.image.transfer_methods = [value]
  40. }
  41. })
  42. setFeatures(newFeatures)
  43. if (onChange)
  44. onChange(newFeatures)
  45. }, [featuresStore, onChange])
  46. const handleLimitsChange = useCallback((_key: string, value: number) => {
  47. if (!value)
  48. return
  49. const {
  50. features,
  51. setFeatures,
  52. } = featuresStore!.getState()
  53. const newFeatures = produce(features, (draft) => {
  54. if (draft.file?.image)
  55. draft.file.image.number_limits = value
  56. })
  57. setFeatures(newFeatures)
  58. if (onChange)
  59. onChange(newFeatures)
  60. }, [featuresStore, onChange])
  61. return (
  62. <div>
  63. <div>
  64. <div className='leading-6 text-base font-semibold text-gray-800'>{t('common.operation.settings')}</div>
  65. <div className='pt-3 space-y-6'>
  66. <div>
  67. <div className='mb-2 leading-[18px] text-[13px] font-semibold text-gray-800'>{t('appDebug.vision.visionSettings.uploadMethod')}</div>
  68. <RadioGroup
  69. className='space-x-3'
  70. options={[
  71. {
  72. label: t('appDebug.vision.visionSettings.both'),
  73. value: TransferMethod.all,
  74. },
  75. {
  76. label: t('appDebug.vision.visionSettings.localUpload'),
  77. value: TransferMethod.local_file,
  78. },
  79. {
  80. label: t('appDebug.vision.visionSettings.url'),
  81. value: TransferMethod.remote_url,
  82. },
  83. ]}
  84. value={transferMethod}
  85. onChange={handleTransferMethodsChange}
  86. />
  87. </div>
  88. <div>
  89. <ParamItem
  90. id='upload_limit'
  91. className=''
  92. name={t('appDebug.vision.visionSettings.uploadLimit')}
  93. noTooltip
  94. {...{
  95. default: 2,
  96. step: 1,
  97. min: MIN,
  98. max: MAX,
  99. }}
  100. value={file?.image?.number_limits || 3}
  101. enable={true}
  102. onChange={handleLimitsChange}
  103. />
  104. </div>
  105. </div>
  106. </div>
  107. </div>
  108. )
  109. }
  110. export default React.memo(ParamConfigContent)