|
1 | | -import NiceAvatar, { genConfig } from 'react-nice-avatar' |
| 1 | +'use client' |
2 | 2 |
|
| 3 | +import { ChangeEvent, useState } from 'react' |
| 4 | +import { toast } from 'sonner' |
| 5 | + |
| 6 | +import { graphql } from '@/lib/gql/generates' |
3 | 7 | import { useMe } from '@/lib/hooks/use-me' |
| 8 | +import { useMutation } from '@/lib/tabby/gql' |
| 9 | +import { delay } from '@/lib/utils' |
| 10 | +import { Button } from '@/components/ui/button' |
| 11 | +import { IconCloudUpload, IconSpinner } from '@/components/ui/icons' |
| 12 | +import { Separator } from '@/components/ui/separator' |
| 13 | +import { mutateAvatar, UserAvatar } from '@/components/user-avatar' |
| 14 | + |
| 15 | +const uploadUserAvatarMutation = graphql(/* GraphQL */ ` |
| 16 | + mutation uploadUserAvatarBase64($id: ID!, $avatarBase64: String!) { |
| 17 | + uploadUserAvatarBase64(id: $id, avatarBase64: $avatarBase64) |
| 18 | + } |
| 19 | +`) |
| 20 | + |
| 21 | +const MAX_UPLOAD_SIZE_KB = 500 |
4 | 22 |
|
5 | 23 | export const Avatar = () => { |
| 24 | + const [isSubmitting, setIsSubmitting] = useState(false) |
| 25 | + const [uploadedImgString, setUploadedImgString] = useState('') |
6 | 26 | const [{ data }] = useMe() |
7 | | - |
| 27 | + const uploadUserAvatar = useMutation(uploadUserAvatarMutation, { |
| 28 | + onError(err) { |
| 29 | + toast.error(err.message) |
| 30 | + } |
| 31 | + }) |
8 | 32 | if (!data?.me?.email) return null |
9 | 33 |
|
10 | | - const config = genConfig(data?.me?.email) |
| 34 | + const onPreviewAvatar = (e: ChangeEvent<HTMLInputElement>) => { |
| 35 | + const file = e.target.files ? e.target.files[0] : null |
| 36 | + |
| 37 | + if (file) { |
| 38 | + const fileSizeInKB = parseFloat((file.size / 1024).toFixed(2)) |
| 39 | + if (fileSizeInKB > MAX_UPLOAD_SIZE_KB) { |
| 40 | + return toast.error( |
| 41 | + `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.` |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + const reader = new FileReader() |
| 46 | + |
| 47 | + reader.onloadend = () => { |
| 48 | + const imageString = reader.result as string |
| 49 | + setUploadedImgString(imageString) |
| 50 | + } |
| 51 | + |
| 52 | + reader.readAsDataURL(file) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + const onUploadAvatar = async () => { |
| 57 | + setIsSubmitting(true) |
| 58 | + |
| 59 | + const response = await uploadUserAvatar({ |
| 60 | + avatarBase64: uploadedImgString.split(',')[1], |
| 61 | + id: data.me.id |
| 62 | + }) |
| 63 | + |
| 64 | + if (response?.data?.uploadUserAvatarBase64 === true) { |
| 65 | + await delay(1000) |
| 66 | + mutateAvatar(data.me.id) |
| 67 | + toast.success('Successfully updated your profile picture!') |
| 68 | + |
| 69 | + await delay(200) |
| 70 | + setUploadedImgString('') |
| 71 | + } |
| 72 | + |
| 73 | + setIsSubmitting(false) |
| 74 | + } |
11 | 75 |
|
12 | 76 | return ( |
13 | | - <div className="flex h-16 w-16 rounded-full border"> |
14 | | - <NiceAvatar className="w-full" {...config} /> |
| 77 | + <div className="grid gap-6"> |
| 78 | + <div className="relative"> |
| 79 | + <label |
| 80 | + htmlFor="avatar-file" |
| 81 | + className="absolute left-0 top-0 z-20 flex h-16 w-16 cursor-pointer items-center justify-center rounded-full bg-background/90 opacity-0 transition-all hover:opacity-100" |
| 82 | + > |
| 83 | + <IconCloudUpload /> |
| 84 | + </label> |
| 85 | + <input |
| 86 | + id="avatar-file" |
| 87 | + type="file" |
| 88 | + accept="image/*" |
| 89 | + className="hidden" |
| 90 | + onChange={onPreviewAvatar} |
| 91 | + /> |
| 92 | + {uploadedImgString && ( |
| 93 | + <img |
| 94 | + src={uploadedImgString} |
| 95 | + className="absolute left-0 top-0 z-10 h-16 w-16 rounded-full border object-cover" |
| 96 | + alt="avatar to be uploaded" |
| 97 | + /> |
| 98 | + )} |
| 99 | + <UserAvatar className="relative h-16 w-16 border" /> |
| 100 | + </div> |
| 101 | + |
| 102 | + <Separator /> |
| 103 | + |
| 104 | + <div className="flex items-center justify-between"> |
| 105 | + <Button |
| 106 | + type="submit" |
| 107 | + disabled={!uploadedImgString || isSubmitting} |
| 108 | + onClick={onUploadAvatar} |
| 109 | + className="mr-5 w-40" |
| 110 | + > |
| 111 | + {isSubmitting && ( |
| 112 | + <IconSpinner className="mr-2 h-4 w-4 animate-spin" /> |
| 113 | + )} |
| 114 | + Save Changes |
| 115 | + </Button> |
| 116 | + |
| 117 | + <div className="mt-1.5 flex flex-1 justify-end"> |
| 118 | + <p className=" text-xs text-muted-foreground lg:text-sm"> |
| 119 | + {`Square image recommended. Accepted file types: .png, .jpg. Max file size: ${MAX_UPLOAD_SIZE_KB}KB.`} |
| 120 | + </p> |
| 121 | + </div> |
| 122 | + </div> |
15 | 123 | </div> |
16 | 124 | ) |
17 | 125 | } |
0 commit comments