el.xwx.moe/components/ProfilePhoto.tsx

50 lines
1.2 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-06-11 08:55:36 -05:00
status?: Function;
2023-05-27 14:05:07 -05:00
};
2023-06-11 08:55:36 -05:00
export default function ProfilePhoto({
src,
className,
emptyImage,
status,
}: 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-07 14:34:50 -05:00
2023-06-11 08:55:36 -05:00
status && status(error || !src);
}, [src, error]);
return error || !src ? (
2023-06-07 14:34:50 -05:00
<div
2023-06-13 14:19:37 -05:00
className={`bg-sky-500 text-white h-10 w-10 aspect-square shadow rounded-full border-[3px] border-slate-200 flex items-center justify-center ${className}`}
2023-05-27 14:05:07 -05:00
>
2023-06-13 14:19:37 -05:00
<FontAwesomeIcon icon={faUser} className="w-1/2 h-1/2 aspect-square" />
2023-06-07 14:34:50 -05:00
</div>
) : (
<Image
2023-06-11 08:55:36 -05:00
alt=""
2023-06-07 14:34:50 -05:00
src={src}
height={112}
width={112}
2023-06-13 14:19:37 -05:00
className={`h-10 w-10 shadow rounded-full aspect-square border-[3px] border-slate-200 ${className}`}
2023-06-07 14:34:50 -05:00
/>
2023-05-27 14:05:07 -05:00
);
}