index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import type { FC } from 'react'
  2. import { useCallback, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import type { Area } from 'react-easy-crop'
  5. import Modal from '../modal'
  6. import Divider from '../divider'
  7. import Button from '../button'
  8. import { ImagePlus } from '../icons/src/vender/line/images'
  9. import { useLocalFileUploader } from '../image-uploader/hooks'
  10. import EmojiPickerInner from '../emoji-picker/Inner'
  11. import type { OnImageInput } from './ImageInput'
  12. import ImageInput from './ImageInput'
  13. import s from './style.module.css'
  14. import getCroppedImg from './utils'
  15. import type { AppIconType, ImageFile } from '@/types/app'
  16. import cn from '@/utils/classnames'
  17. import { DISABLE_UPLOAD_IMAGE_AS_ICON } from '@/config'
  18. export type AppIconEmojiSelection = {
  19. type: 'emoji'
  20. icon: string
  21. background: string
  22. }
  23. export type AppIconImageSelection = {
  24. type: 'image'
  25. fileId: string
  26. url: string
  27. }
  28. export type AppIconSelection = AppIconEmojiSelection | AppIconImageSelection
  29. type AppIconPickerProps = {
  30. onSelect?: (payload: AppIconSelection) => void
  31. onClose?: () => void
  32. className?: string
  33. }
  34. const AppIconPicker: FC<AppIconPickerProps> = ({
  35. onSelect,
  36. onClose,
  37. className,
  38. }) => {
  39. const { t } = useTranslation()
  40. const tabs = [
  41. { key: 'emoji', label: t('app.iconPicker.emoji'), icon: <span className="text-lg">🤖</span> },
  42. { key: 'image', label: t('app.iconPicker.image'), icon: <ImagePlus /> },
  43. ]
  44. const [activeTab, setActiveTab] = useState<AppIconType>('emoji')
  45. const [emoji, setEmoji] = useState<{ emoji: string; background: string }>()
  46. const handleSelectEmoji = useCallback((emoji: string, background: string) => {
  47. setEmoji({ emoji, background })
  48. }, [setEmoji])
  49. const [uploading, setUploading] = useState<boolean>()
  50. const { handleLocalFileUpload } = useLocalFileUploader({
  51. limit: 3,
  52. disabled: false,
  53. onUpload: (imageFile: ImageFile) => {
  54. if (imageFile.fileId) {
  55. setUploading(false)
  56. onSelect?.({
  57. type: 'image',
  58. fileId: imageFile.fileId,
  59. url: imageFile.url,
  60. })
  61. }
  62. },
  63. })
  64. type InputImageInfo = { file: File } | { tempUrl: string; croppedAreaPixels: Area; fileName: string }
  65. const [inputImageInfo, setInputImageInfo] = useState<InputImageInfo>()
  66. const handleImageInput: OnImageInput = async (isCropped: boolean, fileOrTempUrl: string | File, croppedAreaPixels?: Area, fileName?: string) => {
  67. setInputImageInfo(
  68. isCropped
  69. ? { tempUrl: fileOrTempUrl as string, croppedAreaPixels: croppedAreaPixels!, fileName: fileName! }
  70. : { file: fileOrTempUrl as File },
  71. )
  72. }
  73. const handleSelect = async () => {
  74. if (activeTab === 'emoji') {
  75. if (emoji) {
  76. onSelect?.({
  77. type: 'emoji',
  78. icon: emoji.emoji,
  79. background: emoji.background,
  80. })
  81. }
  82. }
  83. else {
  84. if (!inputImageInfo)
  85. return
  86. setUploading(true)
  87. if ('file' in inputImageInfo) {
  88. handleLocalFileUpload(inputImageInfo.file)
  89. return
  90. }
  91. const blob = await getCroppedImg(inputImageInfo.tempUrl, inputImageInfo.croppedAreaPixels, inputImageInfo.fileName)
  92. const file = new File([blob], inputImageInfo.fileName, { type: blob.type })
  93. handleLocalFileUpload(file)
  94. }
  95. }
  96. return <Modal
  97. onClose={() => { }}
  98. isShow
  99. closable={false}
  100. wrapperClassName={className}
  101. className={cn(s.container, '!w-[362px] !p-0')}
  102. >
  103. {!DISABLE_UPLOAD_IMAGE_AS_ICON && <div className="p-2 pb-0 w-full">
  104. <div className='p-1 flex items-center justify-center gap-2 bg-background-body rounded-xl'>
  105. {tabs.map(tab => (
  106. <button
  107. key={tab.key}
  108. className={`
  109. p-2 flex-1 flex justify-center items-center h-8 rounded-xl text-sm shrink-0 font-medium
  110. ${activeTab === tab.key && 'bg-components-main-nav-nav-button-bg-active shadow-md'}
  111. `}
  112. onClick={() => setActiveTab(tab.key as AppIconType)}
  113. >
  114. {tab.icon} &nbsp; {tab.label}
  115. </button>
  116. ))}
  117. </div>
  118. </div>}
  119. <EmojiPickerInner className={cn(activeTab === 'emoji' ? 'block' : 'hidden', 'pt-2')} onSelect={handleSelectEmoji} />
  120. <ImageInput className={activeTab === 'image' ? 'block' : 'hidden'} onImageInput={handleImageInput} />
  121. <Divider className='m-0' />
  122. <div className='w-full flex items-center justify-center p-3 gap-2'>
  123. <Button className='w-full' onClick={() => onClose?.()}>
  124. {t('app.iconPicker.cancel')}
  125. </Button>
  126. <Button variant="primary" className='w-full' disabled={uploading} loading={uploading} onClick={handleSelect}>
  127. {t('app.iconPicker.ok')}
  128. </Button>
  129. </div>
  130. </Modal>
  131. }
  132. export default AppIconPicker