el.xwx.moe/components/ProfilePhoto.tsx

53 lines
1.4 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;
priority?: boolean;
2023-05-27 14:05:07 -05:00
};
2023-06-11 08:55:36 -05:00
export default function ProfilePhoto({
src,
className,
emptyImage,
status,
priority,
2023-06-11 08:55:36 -05:00
}: 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-08-14 22:25:25 -05:00
className={`bg-sky-600 dark:bg-sky-600 text-white h-10 w-10 aspect-square shadow rounded-full border border-slate-200 dark:border-neutral-700 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}
priority={priority}
2023-10-09 07:35:33 -05:00
className={`h-10 w-10 bg-sky-600 dark:bg-sky-600 shadow rounded-full aspect-square border border-slate-200 dark:border-neutral-700 ${className}`}
2023-06-07 14:34:50 -05:00
/>
2023-05-27 14:05:07 -05:00
);
}