el.xwx.moe/components/LinkViews/LinkComponents/LinkIcon.tsx

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-12-15 21:25:39 -06:00
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import Image from "next/image";
import isValidUrl from "@/lib/shared/isValidUrl";
2023-12-17 02:30:09 -06:00
import React from "react";
2023-12-15 21:25:39 -06:00
2023-12-17 02:30:09 -06:00
export default function LinkIcon({ link, width }: {
2023-12-15 21:25:39 -06:00
link: LinkIncludingShortenedCollectionAndTags;
width?: string;
2023-12-15 21:25:39 -06:00
}) {
const url =
isValidUrl(link.url || "") && link.url ? new URL(link.url) : undefined;
2023-12-15 22:16:56 -06:00
const iconClasses: string =
2023-12-16 23:11:15 -06:00
"bg-white text-primary shadow rounded-md border-[2px] border-white select-none z-10" +
" " +
(width || "w-12");
2023-12-15 21:25:39 -06:00
return (
<div>
2023-12-15 22:16:56 -06:00
{link.url && url ? (
<Image
src={`https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=${link.url}&size=32`}
width={64}
height={64}
alt=""
className={iconClasses}
draggable="false"
onError={(e) => {
const target = e.target as HTMLElement;
target.style.display = "none";
}}
/>
) : link.type === "pdf" ? (
2023-12-17 02:30:09 -06:00
<i className={`bi-file-earmark-pdf ${iconClasses}`}></i>
2023-12-15 22:16:56 -06:00
) : link.type === "image" ? (
2023-12-17 02:30:09 -06:00
<i className={`bi-file-earmark-image ${iconClasses}`}></i>
2023-12-15 22:16:56 -06:00
) : undefined}
2023-12-15 21:25:39 -06:00
</div>
);
}