| 1 | "use client"; |
| 2 | |
| 3 | import { fileToAvatarDataUrl } from |
| 4 | import { useAuth, useSession |
| 5 | import { Trash2, Upload |
| 6 | import { type ChangeEvent, |
| 7 | import { toast } |
| 8 | import { UserAvatar } |
| 9 | import { Button, buttonVariants |
| 10 | import { |
| 11 | DropdownMenu, |
| 12 | DropdownMenuContent, |
| 13 | DropdownMenuItem, |
| 14 | DropdownMenuTrigger, |
| 15 | } from "@/components/ui/dropdown-menu"; |
| 16 | import { Field, FieldLabel |
| 17 | import { Spinner } |
| 18 | import { cn } |
| 19 | |
| 20 | export type ChangeAvatarProps |
| 21 | className?: string |
| 22 | }; |
| 23 | |
| 24 | export function ChangeAvatar({ |
| 25 | const { authClient, |
| 26 | const { data: |
| 27 | |
| 28 | const { mutate: |
| 29 | useUpdateUser(authClient); |
| 30 | |
| 31 | const fileInputRef = |
| 32 | const [isUploading, |
| 33 | const [isDeleting, |
| 34 | |
| 35 | const isPending = |
| 36 | |
| 37 | async function handleFileChange |
| 38 | const file = |
| 39 | if (!file) |
| 40 | |
| 41 | e.target.value |
| 42 | |
| 43 | setIsUploading(true); |
| 44 | |
| 45 | try { |
| 46 | const resized = |
| 47 | (await avatar.resize |
| 48 | |
| 49 | const image = |
| 50 | (await avatar.upload |
| 51 | (await fileToAvatarDataUrl(resized |
| 52 | |
| 53 | updateUser( |
| 54 | { image }, |
| 55 | { |
| 56 | onSuccess: () => |
| 57 | toast.success(localization |
| 58 | }, |
| 59 | ); |
| 60 | } catch (error) |
| 61 | if (error instanceof |
| 62 | toast.error(error |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | setIsUploading(false); |
| 67 | } |
| 68 | |
| 69 | async function handleDelete |
| 70 | const currentImage = |
| 71 | |
| 72 | updateUser( |
| 73 | { image: null }, |
| 74 | { |
| 75 | onSuccess: async () |
| 76 | if (currentImage) { |
| 77 | setIsDeleting(true); |
| 78 | try { |
| 79 | await avatar.delete |
| 80 | } finally { |
| 81 | setIsDeleting(false); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | toast.success(localization |
| 86 | }, |
| 87 | }, |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | return ( |
| 92 | <Field className={ |
| 93 | <FieldLabel>{localization. |
| 94 | |
| 95 | <input |
| 96 | ref={fileInputRef} |
| 97 | type="file" |
| 98 | accept="image/*" |
| 99 | className="hidden" |
| 100 | onChange={handleFileChange} |
| 101 | /> |
| 102 | |
| 103 | <div className="flex items-center gap-4 |
| 104 | <Button |
| 105 | type="button" |
| 106 | variant="ghost" |
| 107 | className="p-0 h-auto w-auto rounded-full" |
| 108 | disabled={isPending} |
| 109 | onClick={() => |
| 110 | > |
| 111 | <UserAvatar className=" |
| 112 | </Button> |
| 113 | |
| 114 | <DropdownMenu> |
| 115 | <DropdownMenuTrigger |
| 116 | className={cn(buttonVariants |
| 117 | disabled={!session |
| 118 | > |
| 119 | {isPending && <Spinner |
| 120 | |
| 121 | {localization.settings.changeAvatar |
| 122 | </DropdownMenuTrigger> |
| 123 | |
| 124 | <DropdownMenuContent className=" |
| 125 | <DropdownMenuItem onClick={ |
| 126 | <Upload className=" |
| 127 | |
| 128 | {localization.settings.uploadAvatar |
| 129 | </DropdownMenuItem> |
| 130 | |
| 131 | <DropdownMenuItem |
| 132 | variant="destructive" |
| 133 | disabled={!session?. |
| 134 | onClick={handleDelete} |
| 135 | > |
| 136 | <Trash2 /> |
| 137 | |
| 138 | {localization.settings.deleteAvatar |
| 139 | </DropdownMenuItem> |
| 140 | </DropdownMenuContent> |
| 141 | </DropdownMenu> |
| 142 | </div> |
| 143 | </Field> |
| 144 | ); |
| 145 | } |