better logic when showing link icons

This commit is contained in:
daniel31x13 2024-11-07 00:19:12 -05:00
parent ac3888f9b3
commit 7931e2d7b6

View File

@ -1,7 +1,7 @@
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global"; import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import Image from "next/image"; import Image from "next/image";
import isValidUrl from "@/lib/shared/isValidUrl"; import isValidUrl from "@/lib/shared/isValidUrl";
import React from "react"; import React, { useState } from "react";
import Icon from "@/components/Icon"; import Icon from "@/components/Icon";
import { IconWeight } from "@phosphor-icons/react"; import { IconWeight } from "@phosphor-icons/react";
import clsx from "clsx"; import clsx from "clsx";
@ -26,7 +26,7 @@ export default function LinkIcon({
const url = const url =
isValidUrl(link.url || "") && link.url ? new URL(link.url) : undefined; isValidUrl(link.url || "") && link.url ? new URL(link.url) : undefined;
const [showFavicon, setShowFavicon] = React.useState<boolean>(true); const [faviconLoaded, setFaviconLoaded] = useState(false);
return ( return (
<div onClick={() => onClick && onClick()}> <div onClick={() => onClick && onClick()}>
@ -41,21 +41,27 @@ export default function LinkIcon({
/> />
</div> </div>
) : link.type === "url" && url ? ( ) : link.type === "url" && url ? (
showFavicon ? ( <>
<Image <Image
src={`https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=${link.url}&size=32`} src={`https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=${link.url}&size=32`}
width={64} width={64}
height={64} height={64}
alt="" alt=""
className={iconClasses} className={clsx(
iconClasses,
faviconLoaded ? "" : "absolute opacity-0"
)}
draggable="false" draggable="false"
onError={() => { onLoadingComplete={() => setFaviconLoaded(true)}
setShowFavicon(false); onError={() => setFaviconLoaded(false)}
}}
/> />
) : ( {!faviconLoaded && (
<LinkPlaceholderIcon iconClasses={iconClasses} icon="bi-link-45deg" /> <LinkPlaceholderIcon
) iconClasses={iconClasses}
icon="bi-link-45deg"
/>
)}
</>
) : link.type === "pdf" ? ( ) : link.type === "pdf" ? (
<LinkPlaceholderIcon <LinkPlaceholderIcon
iconClasses={iconClasses} iconClasses={iconClasses}