Skip to content

Commit

Permalink
check upload avatar image size in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
wwayne committed Mar 29, 2024
1 parent cc3e568 commit 0cbde28
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions ee/tabby-ui/app/(dashboard)/profile/components/avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const uploadUserAvatarMutation = graphql(/* GraphQL */ `
}
`)

const MAX_UPLOAD_SIZE_KB = 500

export const Avatar = () => {
const [isSubmitting, setIsSubmitting] = useState(false)
const [uploadedImgString, setUploadedImgString] = useState('')
Expand All @@ -31,7 +33,13 @@ export const Avatar = () => {

const onPreviewAvatar = (e: ChangeEvent<HTMLInputElement>) => {
const file = e.target.files ? e.target.files[0] : null

if (file) {
const fileSizeInKB = parseFloat((file.size / 1024).toFixed(2))
if (fileSizeInKB > MAX_UPLOAD_SIZE_KB) {
return toast.error(`The image you are attempting to upload is too large. Please ensure the file size is under ${MAX_UPLOAD_SIZE_KB}KB and try again.`)
}

const reader = new FileReader()

reader.onloadend = () => {
Expand Down Expand Up @@ -59,7 +67,7 @@ export const Avatar = () => {
await delay(200)
setUploadedImgString('')
}

setIsSubmitting(false)
}

Expand Down Expand Up @@ -106,7 +114,7 @@ export const Avatar = () => {

<div className="mt-1.5 flex flex-1 justify-end">
<p className=" text-xs text-muted-foreground lg:text-sm">
Square image recommended. Accepted file types: .png, .jpg. Max file size: 500KB.
{`Square image recommended. Accepted file types: .png, .jpg. Max file size: ${MAX_UPLOAD_SIZE_KB}KB.`}
</p>
</div>
</div>
Expand Down

0 comments on commit 0cbde28

Please sign in to comment.