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";
|
2023-06-09 17:31:14 -05:00
|
|
|
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;
|
2023-06-09 17:31:14 -05:00
|
|
|
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) {
|
2023-06-09 17:31:14 -05:00
|
|
|
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(() => {
|
2023-06-09 17:31:14 -05:00
|
|
|
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-22 09:35:02 -05:00
|
|
|
className={`bg-sky-500 text-white h-10 w-10 aspect-square shadow rounded-full border 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>
|
|
|
|
) : (
|
2023-06-09 17:31:14 -05:00
|
|
|
<Image
|
2023-06-11 08:55:36 -05:00
|
|
|
alt=""
|
2023-06-07 14:34:50 -05:00
|
|
|
src={src}
|
2023-06-09 17:31:14 -05:00
|
|
|
height={112}
|
|
|
|
width={112}
|
2023-06-22 09:35:02 -05:00
|
|
|
className={`h-10 w-10 shadow rounded-full aspect-square border border-slate-200 ${className}`}
|
2023-06-07 14:34:50 -05:00
|
|
|
/>
|
2023-05-27 14:05:07 -05:00
|
|
|
);
|
|
|
|
}
|