import type { FC } from 'react' import { useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' import type { Area } from 'react-easy-crop' import Modal from '../modal' import Divider from '../divider' import Button from '../button' import { ImagePlus } from '../icons/src/vender/line/images' import { useLocalFileUploader } from '../image-uploader/hooks' import EmojiPickerInner from '../emoji-picker/Inner' import Uploader from './Uploader' import s from './style.module.css' import getCroppedImg from './utils' import type { AppIconType, ImageFile } from '@/types/app' import cn from '@/utils/classnames' import { DISABLE_UPLOAD_IMAGE_AS_ICON } from '@/config' export type AppIconEmojiSelection = { type: 'emoji' icon: string background: string } export type AppIconImageSelection = { type: 'image' fileId: string url: string } export type AppIconSelection = AppIconEmojiSelection | AppIconImageSelection type AppIconPickerProps = { onSelect?: (payload: AppIconSelection) => void onClose?: () => void className?: string } const AppIconPicker: FC = ({ onSelect, onClose, className, }) => { const { t } = useTranslation() const tabs = [ { key: 'emoji', label: t('app.iconPicker.emoji'), icon: 🤖 }, { key: 'image', label: t('app.iconPicker.image'), icon: }, ] const [activeTab, setActiveTab] = useState('emoji') const [emoji, setEmoji] = useState<{ emoji: string; background: string }>() const handleSelectEmoji = useCallback((emoji: string, background: string) => { setEmoji({ emoji, background }) }, [setEmoji]) const [uploading, setUploading] = useState() const { handleLocalFileUpload } = useLocalFileUploader({ limit: 3, disabled: false, onUpload: (imageFile: ImageFile) => { if (imageFile.fileId) { setUploading(false) onSelect?.({ type: 'image', fileId: imageFile.fileId, url: imageFile.url, }) } }, }) const [imageCropInfo, setImageCropInfo] = useState<{ tempUrl: string; croppedAreaPixels: Area; fileName: string }>() const handleImageCropped = async (tempUrl: string, croppedAreaPixels: Area, fileName: string) => { setImageCropInfo({ tempUrl, croppedAreaPixels, fileName }) } const [uploadImageInfo, setUploadImageInfo] = useState<{ file?: File }>() const handleUpload = async (file?: File) => { setUploadImageInfo({ file }) } const handleSelect = async () => { if (activeTab === 'emoji') { if (emoji) { onSelect?.({ type: 'emoji', icon: emoji.emoji, background: emoji.background, }) } } else { if (!imageCropInfo && !uploadImageInfo) return setUploading(true) if (imageCropInfo.file) { handleLocalFileUpload(imageCropInfo.file) return } const blob = await getCroppedImg(imageCropInfo.tempUrl, imageCropInfo.croppedAreaPixels, imageCropInfo.fileName) const file = new File([blob], imageCropInfo.fileName, { type: blob.type }) handleLocalFileUpload(file) } } return { }} isShow closable={false} wrapperClassName={className} className={cn(s.container, '!w-[362px] !p-0')} > {!DISABLE_UPLOAD_IMAGE_AS_ICON &&
{tabs.map(tab => ( ))}
}
} export default AppIconPicker