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";
|
2024-08-24 14:50:29 -05:00
|
|
|
import Icon from "@/components/Icon";
|
|
|
|
import { IconWeight } from "@phosphor-icons/react";
|
|
|
|
import clsx from "clsx";
|
2023-12-15 21:25:39 -06:00
|
|
|
|
2023-12-21 09:55:07 -06:00
|
|
|
export default function LinkIcon({
|
|
|
|
link,
|
2024-04-01 01:56:54 -05:00
|
|
|
className,
|
2024-08-24 14:50:29 -05:00
|
|
|
hideBackground,
|
2023-12-21 09:55:07 -06:00
|
|
|
}: {
|
2023-12-15 21:25:39 -06:00
|
|
|
link: LinkIncludingShortenedCollectionAndTags;
|
2024-04-01 01:56:54 -05:00
|
|
|
className?: string;
|
2024-08-24 14:50:29 -05:00
|
|
|
hideBackground?: boolean;
|
2023-12-15 21:25:39 -06:00
|
|
|
}) {
|
2024-08-24 14:50:29 -05:00
|
|
|
let iconClasses: string = clsx(
|
2024-08-28 19:30:57 -05:00
|
|
|
"rounded flex item-center justify-center shadow select-none z-10 w-12 h-12",
|
2024-08-26 15:11:02 -05:00
|
|
|
!hideBackground && "rounded-md bg-white backdrop-blur-lg bg-opacity-50 p-1",
|
2024-08-24 14:50:29 -05:00
|
|
|
className
|
|
|
|
);
|
2024-04-27 11:23:33 -05:00
|
|
|
|
2023-12-15 21:25:39 -06:00
|
|
|
const url =
|
|
|
|
isValidUrl(link.url || "") && link.url ? new URL(link.url) : undefined;
|
|
|
|
|
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-08-24 14:50:29 -05:00
|
|
|
{link.icon ? (
|
2024-08-28 19:30:57 -05:00
|
|
|
<div className={iconClasses}>
|
|
|
|
<Icon
|
|
|
|
icon={link.icon}
|
|
|
|
size={30}
|
|
|
|
weight={(link.iconWeight || "regular") as IconWeight}
|
|
|
|
color={link.color || "#0ea5e9"}
|
|
|
|
className="m-auto"
|
|
|
|
/>
|
|
|
|
</div>
|
2024-08-24 14:50:29 -05:00
|
|
|
) : link.type === "url" && url ? (
|
2024-04-01 01:56:54 -05:00
|
|
|
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=""
|
2024-08-24 14:50:29 -05:00
|
|
|
className={iconClasses}
|
2024-04-01 01:56:54 -05:00
|
|
|
draggable="false"
|
|
|
|
onError={() => {
|
|
|
|
setShowFavicon(false);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
) : (
|
2024-08-26 15:11:02 -05:00
|
|
|
<LinkPlaceholderIcon iconClasses={iconClasses} icon="bi-link-45deg" />
|
2024-04-01 01:56:54 -05:00
|
|
|
)
|
2023-12-24 05:46:08 -06:00
|
|
|
) : link.type === "pdf" ? (
|
2024-04-01 01:56:54 -05:00
|
|
|
<LinkPlaceholderIcon
|
2024-08-24 14:50:29 -05:00
|
|
|
iconClasses={iconClasses}
|
2024-04-01 01:56:54 -05:00
|
|
|
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
|
2024-08-24 14:50:29 -05:00
|
|
|
iconClasses={iconClasses}
|
2024-04-01 01:56:54 -05:00
|
|
|
icon="bi-file-earmark-image"
|
|
|
|
/>
|
2024-06-27 20:58:07 -05:00
|
|
|
) : // : link.type === "monolith" ? (
|
|
|
|
// <LinkPlaceholderIcon
|
|
|
|
// iconClasses={iconClasses + dimension}
|
|
|
|
// size={size}
|
|
|
|
// icon="bi-filetype-html"
|
|
|
|
// />
|
|
|
|
// )
|
|
|
|
undefined}
|
2023-12-24 05:46:08 -06:00
|
|
|
</>
|
|
|
|
);
|
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 (
|
2024-08-29 17:26:15 -05:00
|
|
|
<div className={clsx(iconClasses, "aspect-square text-4xl text-[#000000]")}>
|
2024-04-01 01:56:54 -05:00
|
|
|
<i className={`${icon} m-auto`}></i>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2024-08-24 14:50:29 -05:00
|
|
|
|
|
|
|
// `text-black aspect-square text-4xl ${iconClasses}`
|