import { themeVars } from "@/theme/theme.css"; import { fBytes } from "@/utils/format-number"; import { Typography, Upload } from "antd"; import type { UploadChangeParam, UploadFile, UploadProps } from "antd/es/upload"; import { useState } from "react"; import { Iconify } from "../icon"; import { StyledUploadAvatar } from "./styles"; import { beforeAvatarUpload, getBlobUrl } from "./utils"; interface Props extends UploadProps { defaultAvatar?: string; helperText?: React.ReactElement | string; } export function UploadAvatar({ helperText, defaultAvatar = "", ...other }: Props) { const [imageUrl, setImageUrl] = useState(defaultAvatar); const [isHover, setIsHover] = useState(false); const handelHover = (hover: boolean) => { setIsHover(hover); }; const handleChange: UploadProps["onChange"] = (info: UploadChangeParam) => { if (info.file.status === "uploading") { return; } if (info.file.status === "done" || info.file.status === "error") { // TODO: Get this url from response in real world. if (info.file.originFileObj) { setImageUrl(getBlobUrl(info.file.originFileObj)); } } }; const renderPreview = ; const renderPlaceholder = (
Upload Photo
); const renderContent = (
handelHover(true)} onMouseLeave={() => handelHover(false)} > {imageUrl ? renderPreview : null} {!imageUrl || isHover ? renderPlaceholder : null}
); const defaultHelperText = ( Allowed *.jpeg, *.jpg, *.png, *.gif
max size of {fBytes(3145728)}
); const renderHelpText =
{helperText || defaultHelperText}
; return ( {renderContent} {renderHelpText} ); }