Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 1090x 313x 1565x 313x 313x | import * as React from "react";
import { palette } from "@palette";
import isString from "lodash/isString";
import "react-dropzone-uploader/dist/styles.css";
import Dropzone, { IDropzoneProps } from "react-dropzone-uploader";
// mui
import Box, { BoxProps } from "@mui/material/Box";
// components
import { Text } from "@common/Text";
import Layout from "./Layout";
// assets
import placeholder from "@assets/images/avatar-placeholder.png";
import ImagePlacholder from "@assets/images/Placeholder.png";
import { getFilesFromEvent as getFilesFromEventUtil } from "@shared/FileUpload/utils";
export type TImageSize = "small" | "medium" | "large" | "thumb" | "original";
export type UploadAvatarProps = BoxProps & {
file?: any;
avatarType?: "avatar" | "thumbnail";
title?: string;
titleComponent?: React.ReactNode;
imageSize?: TImageSize;
width?: string;
height?: string;
disabled?: boolean;
onChangeStatus?: IDropzoneProps["onChangeStatus"];
getUploadParams?: IDropzoneProps["getUploadParams"];
getFilesFromEvent?: IDropzoneProps["getFilesFromEvent"];
};
export default function UploadAvatar({
file,
avatarType = "avatar",
disabled,
width = "80px",
height = "80px",
title = "Avatar",
imageSize = "small",
titleComponent,
onChangeStatus,
getUploadParams,
getFilesFromEvent,
...boxProps
}: UploadAvatarProps) {
const imageUrl = file
? isString(file)
? addSizeToImage(file, imageSize)
: file.preview
: avatarType === "avatar"
? placeholder
: ImagePlacholder;
return (
<Box {...boxProps}>
{titleComponent ? (
<>{titleComponent}</>
) : (
<Text fontWeight="medium" color={palette.neutral[800]}>
{title}
</Text>
)}
<Box
sx={{
marginTop: 1,
width: width,
height: height,
borderRadius: avatarType === "avatar" ? "50%" : "8px",
position: "relative",
backgroundSize: "cover",
backgroundPosition: "center",
backgroundRepeat: "no-repeat",
backgroundImage: `url(${imageUrl})`,
overflow: "hidden",
...(!disabled && {
"&:hover": {
border: `2px solid ${palette.neutral[70]}`,
},
}),
}}
>
<Dropzone
maxFiles={1}
accept="image/*"
multiple={false}
LayoutComponent={Layout}
onChangeStatus={onChangeStatus}
getUploadParams={getUploadParams}
getFilesFromEvent={getFilesFromEvent || getFilesFromEventUtil}
InputComponent={({
accept,
onFiles,
getFilesFromEvent: getFilesFromEventProp,
}) => (
<Box
component="label"
sx={{
opacity: 0,
width: "80px",
height: "80px",
...(!disabled && {
cursor: "pointer",
}),
}}
>
<input
type="file"
id="upload"
accept={accept}
disabled={disabled}
style={{ display: "none" }}
onChange={async (e) => {
const chosenFiles = await (
getFilesFromEventProp || getFilesFromEventUtil
)(e);
onFiles(chosenFiles);
e.target.value = "";
}}
/>
</Box>
)}
/>
</Box>
</Box>
);
}
export function addSizeToImage(url: string, size: TImageSize) {
if (!url) return "";
const urlIncludesSize = [
"/small",
"/large",
"/medium",
"/original",
"/thumb",
].some((substring) => url.includes(substring));
Iif (urlIncludesSize) return url;
return url + "/" + size;
}
|