import type React from "react"; import { useEffect, useState } from "react"; import { useLinkMetadata } from "@/hooks/useMemoQueries"; import { cn } from "@/lib/utils"; interface LinkMetadataCardProps { url: string; fallback: React.ReactNode; } function getHostname(url: string): string { try { return new URL(url).hostname.replace(/^www\./, ""); } catch { return ""; } } const LinkMetadataCard = ({ url, fallback }: LinkMetadataCardProps) => { const [imageFailed, setImageFailed] = useState(false); const { data: metadata, isSuccess } = useLinkMetadata(url); const title = metadata?.title.trim() ?? ""; const description = metadata?.description.trim() ?? ""; const image = metadata?.image.trim() ?? ""; const hostname = getHostname(metadata?.url || url); const hasUsefulMetadata = title !== "" || description !== ""; useEffect(() => { setImageFailed(false); }, [url, image]); if (!isSuccess || !hasUsefulMetadata) { return fallback; } return ( {hostname && {hostname}} {title && {title}} {description && {description}} {image && !imageFailed && ( setImageFailed(true)} /> )} ); }; export default LinkMetadataCard;