|
| 1 | +'use client'; |
| 2 | +import Image from 'next/image'; |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import Skeleton from '@/app/entities/common/Skeleton/Skeleton'; |
| 5 | + |
| 6 | +interface OGData { |
| 7 | + url: string; |
| 8 | + title: string | null; |
| 9 | + description: string | null; |
| 10 | + image: string | null; |
| 11 | + siteName: string | null; |
| 12 | + favicon: string | null; |
| 13 | + hostname: string; |
| 14 | +} |
| 15 | + |
| 16 | +const fetchOGData = async (href: string): Promise<OGData | null> => { |
| 17 | + const baseUrl = |
| 18 | + process.env.NEXT_PUBLIC_DEPLOYMENT_URL || process.env.NEXT_PUBLIC_URL || ''; |
| 19 | + const absoluteUrl = href.startsWith('/') ? `${baseUrl}${href}` : href; |
| 20 | + try { |
| 21 | + const res = await fetch( |
| 22 | + `/api/opengraph?url=${encodeURIComponent(absoluteUrl)}` |
| 23 | + ); |
| 24 | + if (!res.ok) return null; |
| 25 | + return await res.json(); |
| 26 | + } catch { |
| 27 | + return null; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +const OgLinkCardSkeleton = () => ( |
| 32 | + <div className="border-border bg-card mb-4 flex h-[112px] overflow-hidden rounded-xl border animate-pulse"> |
| 33 | + <div className="flex min-w-0 flex-1 flex-col justify-center gap-2 px-4"> |
| 34 | + <div className="flex items-center gap-1.5"> |
| 35 | + <Skeleton className="h-3.5 w-3.5" /> |
| 36 | + <Skeleton className="h-3 w-24" /> |
| 37 | + </div> |
| 38 | + <Skeleton className="h-4 w-3/4" /> |
| 39 | + <Skeleton className="h-3 w-full" /> |
| 40 | + <Skeleton className="h-3 w-2/3" /> |
| 41 | + </div> |
| 42 | + <div |
| 43 | + className="relative hidden shrink-0 rounded-r-xl bg-gray-200/80 dark:bg-neutral-700/80 sm:block" |
| 44 | + style={{ width: '160px' }} |
| 45 | + /> |
| 46 | + </div> |
| 47 | +); |
| 48 | + |
| 49 | +interface OgLinkCardProps { |
| 50 | + href: string; |
| 51 | +} |
| 52 | + |
| 53 | +const OgLinkCard = ({ href }: OgLinkCardProps) => { |
| 54 | + const [data, setData] = useState<OGData | null | undefined>(undefined); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + fetchOGData(href).then(setData); |
| 58 | + }, [href]); |
| 59 | + |
| 60 | + if (data === undefined) return <OgLinkCardSkeleton />; |
| 61 | + |
| 62 | + if (!data || (!data.title && !data.description)) { |
| 63 | + return ( |
| 64 | + <a |
| 65 | + href={href} |
| 66 | + target="_blank" |
| 67 | + rel="noopener noreferrer" |
| 68 | + className="text-accent decoration-accent/30 hover:text-accent/80 my-4 block break-all underline underline-offset-2 transition-colors" |
| 69 | + > |
| 70 | + {href} |
| 71 | + </a> |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + const { title, description, image, favicon, siteName, hostname } = data; |
| 76 | + const siteLabel = siteName || hostname || ''; |
| 77 | + |
| 78 | + return ( |
| 79 | + <a |
| 80 | + href={href} |
| 81 | + target="_blank" |
| 82 | + rel="noopener noreferrer" |
| 83 | + className="border-border dark:border-neutral-800/50 bg-white dark:bg-neutral-800 hover:border-white/90 hover:bg-neutral-800/10 mb-4 flex h-[112px] overflow-hidden rounded-xl border transition-colors" |
| 84 | + > |
| 85 | + <div className="flex min-w-0 flex-1 flex-col justify-center gap-1 px-4 py-3"> |
| 86 | + <div className="flex h-4 items-center gap-1.5"> |
| 87 | + {favicon && ( |
| 88 | + // eslint-disable-next-line @next/next/no-img-element |
| 89 | + <img |
| 90 | + src={favicon} |
| 91 | + alt="" |
| 92 | + width={14} |
| 93 | + height={14} |
| 94 | + className="shrink-0 rounded-sm" |
| 95 | + /> |
| 96 | + )} |
| 97 | + <span className="!text-muted-foreground truncate text-xs"> |
| 98 | + {siteLabel} |
| 99 | + </span> |
| 100 | + </div> |
| 101 | + {title && ( |
| 102 | + <p className="!text-foreground !mb-0 line-clamp-1 text-sm font-semibold leading-snug"> |
| 103 | + {title} |
| 104 | + </p> |
| 105 | + )} |
| 106 | + {description && ( |
| 107 | + <p className="!text-muted-foreground !mb-0 line-clamp-2 text-xs leading-relaxed"> |
| 108 | + {description} |
| 109 | + </p> |
| 110 | + )} |
| 111 | + </div> |
| 112 | + {image && ( |
| 113 | + <div |
| 114 | + className="relative hidden h-full shrink-0 sm:block !m-0" |
| 115 | + style={{ width: '160px' }} |
| 116 | + > |
| 117 | + <Image |
| 118 | + width={160} |
| 119 | + height={112} |
| 120 | + src={image} |
| 121 | + alt={title ?? ''} |
| 122 | + className="!h-full !w-full !max-w-none !m-0 !rounded-none object-cover" |
| 123 | + /> |
| 124 | + </div> |
| 125 | + )} |
| 126 | + </a> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +export default OgLinkCard; |
0 commit comments