el.xwx.moe/components/ProfilePhoto.tsx

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-06-07 14:34:50 -05:00
import React, { useEffect, useState } from "react";
2023-05-27 14:05:07 -05:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faUser } from "@fortawesome/free-solid-svg-icons";
import Image from "next/image";
import avatarExists from "@/lib/client/avatarExists";
2023-05-27 14:05:07 -05:00
type Props = {
src: string;
className?: string;
emptyImage?: boolean;
2023-05-27 14:05:07 -05:00
};
export default function ProfilePhoto({ src, className, emptyImage }: Props) {
const [error, setError] = useState<boolean>(emptyImage || true);
const checkAvatarExistence = async () => {
const canPass = await avatarExists(src);
setError(!canPass);
};
2023-06-07 14:34:50 -05:00
useEffect(() => {
if (src) checkAvatarExistence();
2023-06-08 08:39:22 -05:00
}, [src]);
2023-06-07 14:34:50 -05:00
return error ? (
2023-06-07 14:34:50 -05:00
<div
className={`bg-sky-500 text-white h-10 w-10 shadow rounded-full border-[3px] border-slate-200 flex items-center justify-center ${className}`}
2023-05-27 14:05:07 -05:00
>
2023-06-08 08:39:22 -05:00
<FontAwesomeIcon icon={faUser} className="w-1/2 h-1/2" />
2023-06-07 14:34:50 -05:00
</div>
) : (
<Image
alt="Avatar"
2023-06-07 14:34:50 -05:00
src={src}
height={112}
width={112}
2023-06-07 14:34:50 -05:00
className={`h-10 w-10 shadow rounded-full border-[3px] border-slate-200 ${className}`}
/>
2023-05-27 14:05:07 -05:00
);
}