|
| 1 | +'use client' |
| 2 | + |
| 3 | +import type { Area } from 'react-easy-crop' |
| 4 | +import React, { useCallback, useState } from 'react' |
| 5 | +import { useTranslation } from 'react-i18next' |
| 6 | +import { useContext } from 'use-context-selector' |
| 7 | +import { RiPencilLine } from '@remixicon/react' |
| 8 | +import { updateUserProfile } from '@/service/common' |
| 9 | +import { ToastContext } from '@/app/components/base/toast' |
| 10 | +import ImageInput, { type OnImageInput } from '@/app/components/base/app-icon-picker/ImageInput' |
| 11 | +import Modal from '@/app/components/base/modal' |
| 12 | +import Divider from '@/app/components/base/divider' |
| 13 | +import Button from '@/app/components/base/button' |
| 14 | +import Avatar, { type AvatarProps } from '@/app/components/base/avatar' |
| 15 | +import { useLocalFileUploader } from '@/app/components/base/image-uploader/hooks' |
| 16 | +import type { ImageFile } from '@/types/app' |
| 17 | +import getCroppedImg from '@/app/components/base/app-icon-picker/utils' |
| 18 | +import { DISABLE_UPLOAD_IMAGE_AS_ICON } from '@/config' |
| 19 | + |
| 20 | +type InputImageInfo = { file: File } | { tempUrl: string; croppedAreaPixels: Area; fileName: string } |
| 21 | +type AvatarWithEditProps = AvatarProps & { onSave?: () => void } |
| 22 | + |
| 23 | +const AvatarWithEdit = ({ onSave, ...props }: AvatarWithEditProps) => { |
| 24 | + const { t } = useTranslation() |
| 25 | + const { notify } = useContext(ToastContext) |
| 26 | + |
| 27 | + const [inputImageInfo, setInputImageInfo] = useState<InputImageInfo>() |
| 28 | + const [isShowAvatarPicker, setIsShowAvatarPicker] = useState(false) |
| 29 | + const [uploading, setUploading] = useState(false) |
| 30 | + |
| 31 | + const handleImageInput: OnImageInput = useCallback(async (isCropped: boolean, fileOrTempUrl: string | File, croppedAreaPixels?: Area, fileName?: string) => { |
| 32 | + setInputImageInfo( |
| 33 | + isCropped |
| 34 | + ? { tempUrl: fileOrTempUrl as string, croppedAreaPixels: croppedAreaPixels!, fileName: fileName! } |
| 35 | + : { file: fileOrTempUrl as File }, |
| 36 | + ) |
| 37 | + }, [setInputImageInfo]) |
| 38 | + |
| 39 | + const handleSaveAvatar = useCallback(async (uploadedFileId: string) => { |
| 40 | + try { |
| 41 | + await updateUserProfile({ url: 'account/avatar', body: { avatar: uploadedFileId } }) |
| 42 | + notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') }) |
| 43 | + setIsShowAvatarPicker(false) |
| 44 | + onSave?.() |
| 45 | + } |
| 46 | + catch (e) { |
| 47 | + notify({ type: 'error', message: (e as Error).message }) |
| 48 | + } |
| 49 | + }, [notify, onSave, t]) |
| 50 | + |
| 51 | + const { handleLocalFileUpload } = useLocalFileUploader({ |
| 52 | + limit: 3, |
| 53 | + disabled: false, |
| 54 | + onUpload: (imageFile: ImageFile) => { |
| 55 | + if (imageFile.progress === 100) { |
| 56 | + setUploading(false) |
| 57 | + setInputImageInfo(undefined) |
| 58 | + handleSaveAvatar(imageFile.fileId) |
| 59 | + } |
| 60 | + |
| 61 | + // Error |
| 62 | + if (imageFile.progress === -1) |
| 63 | + setUploading(false) |
| 64 | + }, |
| 65 | + }) |
| 66 | + |
| 67 | + const handleSelect = useCallback(async () => { |
| 68 | + if (!inputImageInfo) |
| 69 | + return |
| 70 | + setUploading(true) |
| 71 | + if ('file' in inputImageInfo) { |
| 72 | + handleLocalFileUpload(inputImageInfo.file) |
| 73 | + return |
| 74 | + } |
| 75 | + const blob = await getCroppedImg(inputImageInfo.tempUrl, inputImageInfo.croppedAreaPixels, inputImageInfo.fileName) |
| 76 | + const file = new File([blob], inputImageInfo.fileName, { type: blob.type }) |
| 77 | + handleLocalFileUpload(file) |
| 78 | + }, [handleLocalFileUpload, inputImageInfo]) |
| 79 | + |
| 80 | + if (DISABLE_UPLOAD_IMAGE_AS_ICON) |
| 81 | + return <Avatar {...props} /> |
| 82 | + |
| 83 | + return ( |
| 84 | + <> |
| 85 | + <div> |
| 86 | + <div className="relative group"> |
| 87 | + <Avatar {...props} /> |
| 88 | + <div |
| 89 | + onClick={() => { setIsShowAvatarPicker(true) }} |
| 90 | + className="absolute inset-0 bg-black bg-opacity-50 rounded-full opacity-0 group-hover:opacity-100 transition-opacity cursor-pointer flex items-center justify-center" |
| 91 | + > |
| 92 | + <span className="text-white text-xs"> |
| 93 | + <RiPencilLine /> |
| 94 | + </span> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + |
| 99 | + <Modal |
| 100 | + closable |
| 101 | + className="!w-[362px] !p-0" |
| 102 | + isShow={isShowAvatarPicker} |
| 103 | + onClose={() => setIsShowAvatarPicker(false)} |
| 104 | + > |
| 105 | + <ImageInput onImageInput={handleImageInput} cropShape='round' /> |
| 106 | + <Divider className='m-0' /> |
| 107 | + |
| 108 | + <div className='w-full flex items-center justify-center p-3 gap-2'> |
| 109 | + <Button className='w-full' onClick={() => setIsShowAvatarPicker(false)}> |
| 110 | + {t('app.iconPicker.cancel')} |
| 111 | + </Button> |
| 112 | + |
| 113 | + <Button variant="primary" className='w-full' disabled={uploading || !inputImageInfo} loading={uploading} onClick={handleSelect}> |
| 114 | + {t('app.iconPicker.ok')} |
| 115 | + </Button> |
| 116 | + </div> |
| 117 | + </Modal> |
| 118 | + </> |
| 119 | + ) |
| 120 | +} |
| 121 | + |
| 122 | +export default AvatarWithEdit |
0 commit comments