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-21 09:55:07 -06:00
|
|
|
export default function LinkIcon({
|
|
|
|
link,
|
|
|
|
width,
|
2024-04-01 01:56:54 -05:00
|
|
|
className,
|
2023-12-21 09:55:07 -06:00
|
|
|
}: {
|
2023-12-15 21:25:39 -06:00
|
|
|
link: LinkIncludingShortenedCollectionAndTags;
|
2023-12-17 00:25:46 -06:00
|
|
|
width?: string;
|
2024-04-01 01:56:54 -05:00
|
|
|
className?: 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-23 18:00:53 -06:00
|
|
|
"bg-white shadow rounded-md border-[2px] flex item-center justify-center border-white select-none z-10" +
|
2023-12-17 00:25:46 -06:00
|
|
|
" " +
|
2024-04-01 01:56:54 -05:00
|
|
|
(width || "w-12") +
|
|
|
|
" " +
|
|
|
|
(className || "");
|
2023-12-15 21:25:39 -06:00
|
|
|
|
2023-12-23 11:11:47 -06:00
|
|
|
const [showFavicon, setShowFavicon] = React.useState<boolean>(true);
|
|
|
|
|
2023-12-24 05:46:08 -06:00
|
|
|
return (
|
|
|
|
<>
|
2024-04-01 01:56:54 -05:00
|
|
|
{link.type === "url" && url ? (
|
|
|
|
showFavicon ? (
|
|
|
|
<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={() => {
|
|
|
|
setShowFavicon(false);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<LinkPlaceholderIcon iconClasses={iconClasses} icon="bi-link-45deg" />
|
|
|
|
)
|
2023-12-24 05:46:08 -06:00
|
|
|
) : link.type === "pdf" ? (
|
2024-04-01 01:56:54 -05:00
|
|
|
<LinkPlaceholderIcon
|
|
|
|
iconClasses={iconClasses}
|
|
|
|
icon="bi-file-earmark-pdf"
|
|
|
|
/>
|
2023-12-24 05:46:08 -06:00
|
|
|
) : link.type === "image" ? (
|
2024-04-01 01:56:54 -05:00
|
|
|
<LinkPlaceholderIcon
|
|
|
|
iconClasses={iconClasses}
|
|
|
|
icon="bi-file-earmark-image"
|
|
|
|
/>
|
2023-12-24 05:46:08 -06:00
|
|
|
) : undefined}
|
|
|
|
</>
|
|
|
|
);
|
2023-12-15 21:25:39 -06:00
|
|
|
}
|
2024-04-01 01:56:54 -05:00
|
|
|
|
|
|
|
const LinkPlaceholderIcon = ({
|
|
|
|
iconClasses,
|
|
|
|
icon,
|
|
|
|
}: {
|
|
|
|
iconClasses: string;
|
|
|
|
icon: string;
|
|
|
|
}) => {
|
|
|
|
return (
|
|
|
|
<div className={`text-4xl text-black aspect-square ${iconClasses}`}>
|
|
|
|
<i className={`${icon} m-auto`}></i>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|