more-like-this.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import React, { useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import produce from 'immer'
  4. import { RiSparklingFill } from '@remixicon/react'
  5. import FeatureCard from '@/app/components/base/features/new-feature-panel/feature-card'
  6. import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks'
  7. import type { OnFeaturesChange } from '@/app/components/base/features/types'
  8. import { FeatureEnum } from '@/app/components/base/features/types'
  9. type Props = {
  10. disabled?: boolean
  11. onChange?: OnFeaturesChange
  12. }
  13. const MoreLikeThis = ({
  14. disabled,
  15. onChange,
  16. }: Props) => {
  17. const { t } = useTranslation()
  18. const features = useFeatures(s => s.features)
  19. const featuresStore = useFeaturesStore()
  20. const handleChange = useCallback((type: FeatureEnum, enabled: boolean) => {
  21. const {
  22. features,
  23. setFeatures,
  24. } = featuresStore!.getState()
  25. const newFeatures = produce(features, (draft) => {
  26. draft[type] = {
  27. ...draft[type],
  28. enabled,
  29. }
  30. })
  31. setFeatures(newFeatures)
  32. if (onChange)
  33. onChange()
  34. }, [featuresStore, onChange])
  35. return (
  36. <FeatureCard
  37. icon={
  38. <div className='shrink-0 p-1 rounded-lg border-[0.5px] border-divider-subtle shadow-xs bg-util-colors-blue-light-blue-light-500'>
  39. <RiSparklingFill className='w-4 h-4 text-text-primary-on-surface' />
  40. </div>
  41. }
  42. title={t('appDebug.feature.moreLikeThis.title')}
  43. tooltip={t('appDebug.feature.moreLikeThis.tip')}
  44. value={!!features.moreLikeThis?.enabled}
  45. description={t('appDebug.feature.moreLikeThis.description')!}
  46. onChange={state => handleChange(FeatureEnum.moreLikeThis, state)}
  47. disabled={disabled}
  48. />
  49. )
  50. }
  51. export default MoreLikeThis