el.xwx.moe/components/ProfilePhoto.tsx

65 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-06-07 14:34:50 -05:00
import React, { useEffect, useState } from "react";
import Image from "next/image";
2023-05-27 14:05:07 -05:00
type Props = {
src?: string;
2023-05-27 14:05:07 -05:00
className?: string;
priority?: boolean;
2023-11-27 15:38:38 -06:00
name?: string;
2023-12-17 22:32:33 -06:00
large?: boolean;
2023-05-27 14:05:07 -05:00
};
2023-11-27 15:38:38 -06:00
export default function ProfilePhoto({
src,
className,
priority,
name,
2023-12-17 22:32:33 -06:00
large,
2023-11-27 15:38:38 -06:00
}: Props) {
const [image, setImage] = useState("");
2023-06-07 14:34:50 -05:00
useEffect(() => {
if (src && !src?.includes("base64") && !src.startsWith("http"))
setImage(`/api/v1/${src.replace("uploads/", "").replace(".jpg", "")}`);
else if (!src) setImage("");
else {
setImage(src);
}
}, [src]);
2023-06-11 08:55:36 -05:00
return !image ? (
2023-12-01 13:00:52 -06:00
<div
2023-12-05 03:39:01 -06:00
className={`avatar drop-shadow-md placeholder ${className || ""} ${
2023-12-17 22:32:33 -06:00
large ? "w-28 h-28" : "w-8 h-8"
2023-12-01 13:00:52 -06:00
}`}
>
<div className="bg-base-100 text-neutral rounded-full w-full h-full ring-2 ring-neutral-content select-none">
2023-11-27 15:38:38 -06:00
{name ? (
<span className="text-2xl capitalize">{name.slice(0, 1)}</span>
) : (
2023-12-17 22:32:33 -06:00
<i className={`bi-person ${large ? "text-5xl" : "text-xl"}`}></i>
2023-11-27 15:38:38 -06:00
)}
</div>
2023-06-07 14:34:50 -05:00
</div>
) : (
2023-12-01 13:00:52 -06:00
<div
2023-12-12 09:43:40 -06:00
className={`avatar skeleton rounded-full drop-shadow-md ${
className || ""
} ${large ? "w-28 h-28" : "w-8 h-8"}`}
2023-12-01 13:00:52 -06:00
>
<div className="rounded-full w-full h-full ring-2 ring-neutral-content">
2023-11-27 15:38:38 -06:00
<Image
alt=""
src={image}
height={112}
width={112}
priority={priority}
draggable={false}
onError={() => setImage("")}
className="aspect-square rounded-full"
/>
</div>
</div>
2023-05-27 14:05:07 -05:00
);
}