Files
portabase/src/features/profile/avatar-with-upload.tsx
T

101 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-05-16 15:54:17 +02:00
"use client";
import {Avatar, AvatarFallback, AvatarImage} from "@/components/ui/avatar";
import {UploadIcon} from "lucide-react";
import {toast} from "sonner";
2026-05-24 17:09:12 +02:00
import {uploadUserImageAction} from "@/features/upload/upload.action";
import {useMutation} from "@tanstack/react-query";
2026-05-24 17:09:12 +02:00
import {updateImageUserAction} from "@/features/profile/avatar.action";
import {useRouter} from "next/navigation";
import {User} from "@/db/schema/02_user";
2025-12-21 16:55:12 +01:00
import React, {ChangeEvent} from "react";
2026-06-22 16:56:17 +02:00
import type {AvatarMode} from "@/features/onboarding/types";
2024-11-11 19:31:32 +01:00
export type AvatarWithUploadProps = {
2025-05-16 15:54:17 +02:00
user: User;
2026-06-22 16:56:17 +02:00
avatarMode?: AvatarMode;
avatarUrl?: string;
2025-05-16 15:54:17 +02:00
};
2024-11-11 19:31:32 +01:00
export const AvatarWithUpload = (props: AvatarWithUploadProps) => {
2025-05-16 15:54:17 +02:00
const user = props.user;
2026-06-22 16:56:17 +02:00
const canUpload = !props.avatarMode || props.avatarMode === "internal";
const src = props.avatarUrl ?? user.image ?? undefined;
const router = useRouter();
2024-11-11 19:31:32 +01:00
const submitImage = useMutation({
mutationFn: async (file: File) => {
const formData = new FormData();
formData.set("file", file);
const result = await uploadUserImageAction(formData);
2024-11-11 19:31:32 +01:00
const inner = result?.data;
if (inner?.success) {
const updateUser = await updateImageUserAction(inner.value ?? "");
const dataUser = updateUser?.data;
if (updateUser?.serverError || !dataUser) {
toast.error(updateUser?.serverError);
return;
}
toast.success(inner.actionSuccess?.message);
router.refresh();
} else {
toast.error(inner?.actionError?.message);
2024-11-11 19:31:32 +01:00
}
2025-05-16 15:54:17 +02:00
},
});
2024-11-11 19:31:32 +01:00
2025-08-02 20:13:29 +02:00
const handleImageUpload = async (event: ChangeEvent<HTMLInputElement>) => {
2024-11-11 19:31:32 +01:00
const file = event.target.files?.[0];
if (!file) return;
2026-01-24 13:14:38 +01:00
const allowedFormats = ["image/jpeg", "image/png", "image/webp"];
if (!allowedFormats.includes(file.type)) {
toast.error("Only JPG, PNG or WebP images are allowed.");
2024-11-11 19:31:32 +01:00
return;
}
2026-01-24 13:14:38 +01:00
const maxSizeInMB = 5;
if (file.size > maxSizeInMB * 1024 * 1024) {
toast.error(`Image must be smaller than ${maxSizeInMB}MB.`);
return;
}
2025-05-16 15:54:17 +02:00
submitImage.mutate(file);
2024-11-11 19:31:32 +01:00
};
2025-07-29 09:53:38 +02:00
2026-01-24 13:14:38 +01:00
2024-11-11 19:31:32 +01:00
return (
2025-05-16 15:54:17 +02:00
<div className="relative ">
2025-12-21 16:55:12 +01:00
<Avatar className="w-24 h-24 lg:w-32 lg:h-32 border-4 border-muted/20">
2026-06-22 16:56:17 +02:00
<AvatarImage className="object-cover" src={src}/>
<AvatarFallback className="text-3xl">{(user.name?.charAt(0) ?? user.email?.charAt(0) ?? "?").toUpperCase()}</AvatarFallback>
2025-05-16 15:54:17 +02:00
</Avatar>
2025-12-21 16:55:12 +01:00
2026-06-22 16:56:17 +02:00
{canUpload && (
<div
onClick={() => {
const fileInput = document.createElement("input");
fileInput.type = "file";
fileInput.accept = ".jpg,.jpeg,.png,.webp";
fileInput.onchange = (e: Event) =>
handleImageUpload(e as unknown as React.ChangeEvent<HTMLInputElement>);
fileInput.click();
}}
className="cursor-pointer absolute inset-0 flex justify-center items-center opacity-0 transition-opacity hover:opacity-30 hover:bg-gray-500 hover:bg-opacity-50 rounded-full w-24 h-24 lg:w-32 lg:h-32"
>
<UploadIcon className="w-12 h-12 lg:w-16 lg:h-16 text-primary"/>
</div>
)}
2024-11-11 19:31:32 +01:00
</div>
2025-05-16 15:54:17 +02:00
);
};