|
| 1 | +/** |
| 2 | + * 社区分享链接卡片。 |
| 3 | + * - 整卡可点击,跳转到原文(target="_blank") |
| 4 | + * - OG 封面:有则显示,没有则渲染 host 首字母占位块 |
| 5 | + * - 举报按钮由 ReportButton 组件负责,阻止冒泡不触发整卡跳转 |
| 6 | + * - 服务端渲染(纯展示,无 client state),ReportButton 是 client 组件 |
| 7 | + */ |
| 8 | + |
| 9 | +import { useTranslations } from "next-intl"; |
| 10 | +import type { SharedLinkView } from "@/app/feed/types"; |
| 11 | +import { ReportButton } from "@/app/feed/components/ReportButton"; |
| 12 | +import { Badge } from "@/components/ui/badge"; |
| 13 | + |
| 14 | +interface LinkCardProps { |
| 15 | + link: SharedLinkView; |
| 16 | + /** 分类显示名(由父组件从 i18n 翻译后传入,避免在纯 server 组件里调 useTranslations) */ |
| 17 | + categoryLabel: string; |
| 18 | + /** 当前用户是否已登录(影响举报按钮行为) */ |
| 19 | + isLoggedIn: boolean; |
| 20 | +} |
| 21 | + |
| 22 | +/** 从 host 字符串提取首字母大写,作为封面占位符 */ |
| 23 | +function getHostInitial(host: string): string { |
| 24 | + const cleaned = host.replace(/^www\./, ""); |
| 25 | + return (cleaned[0] ?? "?").toUpperCase(); |
| 26 | +} |
| 27 | + |
| 28 | +export function LinkCard({ link, categoryLabel, isLoggedIn }: LinkCardProps) { |
| 29 | + const t = useTranslations("feed.card"); |
| 30 | + |
| 31 | + return ( |
| 32 | + <li className="group border border-[var(--foreground)] hover:border-[#CC0000] transition-colors duration-150 flex flex-col"> |
| 33 | + {/* 整卡可点击区域,跳到原文 */} |
| 34 | + <a |
| 35 | + href={link.url} |
| 36 | + target="_blank" |
| 37 | + rel="noopener noreferrer" |
| 38 | + className="flex flex-col flex-1" |
| 39 | + aria-label={link.ogTitle ?? link.url} |
| 40 | + > |
| 41 | + {/* OG 封面 / 占位块 */} |
| 42 | + {link.ogCover && !link.ogFetchFailed ? ( |
| 43 | + // next/image 全站 unoptimized:true,用 img 即可(与 events 页一致) |
| 44 | + // eslint-disable-next-line @next/next/no-img-element |
| 45 | + <img |
| 46 | + src={link.ogCover} |
| 47 | + alt={link.ogTitle ?? link.host} |
| 48 | + className="w-full aspect-[16/9] object-cover border-b border-[var(--foreground)]" |
| 49 | + /> |
| 50 | + ) : ( |
| 51 | + // 无封面:显示 host 首字母占位 |
| 52 | + <div className="w-full aspect-[16/9] bg-neutral-100 dark:bg-neutral-900 border-b border-[var(--foreground)] flex flex-col items-center justify-center gap-1"> |
| 53 | + <span className="font-serif text-4xl font-black text-neutral-400 select-none"> |
| 54 | + {getHostInitial(link.host)} |
| 55 | + </span> |
| 56 | + <span className="font-mono text-[9px] uppercase tracking-widest text-neutral-400"> |
| 57 | + {link.host} |
| 58 | + </span> |
| 59 | + {link.ogFetchFailed && ( |
| 60 | + // OG 抓取失败时给用户一个弱提示 |
| 61 | + <span className="font-mono text-[9px] text-neutral-400 mt-1"> |
| 62 | + {t("ogFallback")} |
| 63 | + </span> |
| 64 | + )} |
| 65 | + </div> |
| 66 | + )} |
| 67 | + |
| 68 | + {/* 卡片内容区 */} |
| 69 | + <div className="p-4 flex flex-col gap-2 flex-1"> |
| 70 | + {/* 标题 */} |
| 71 | + <h3 className="font-serif text-base font-black leading-snug group-hover:text-[#CC0000] transition-colors line-clamp-2 text-[var(--foreground)]"> |
| 72 | + {link.ogTitle ?? link.url} |
| 73 | + </h3> |
| 74 | + |
| 75 | + {/* OG 描述 / 用户推荐语 */} |
| 76 | + {(link.recommendation || link.ogDescription) && ( |
| 77 | + <p className="text-sm text-neutral-600 dark:text-neutral-400 line-clamp-3 leading-relaxed"> |
| 78 | + {/* 用户推荐语优先展示,没有则展示 OG description */} |
| 79 | + {link.recommendation ?? link.ogDescription} |
| 80 | + </p> |
| 81 | + )} |
| 82 | + |
| 83 | + {/* 分类 badge + 失效标记 */} |
| 84 | + <div className="flex items-center gap-2 flex-wrap mt-auto pt-2"> |
| 85 | + {link.category && ( |
| 86 | + <Badge |
| 87 | + variant="outline" |
| 88 | + className="font-mono text-[9px] uppercase tracking-widest rounded-none border-[var(--foreground)]/40 text-neutral-500" |
| 89 | + > |
| 90 | + {categoryLabel} |
| 91 | + </Badge> |
| 92 | + )} |
| 93 | + {link.status === "ARCHIVED" && ( |
| 94 | + <Badge |
| 95 | + variant="outline" |
| 96 | + className="font-mono text-[9px] uppercase tracking-widest rounded-none border-[#CC0000]/60 text-[#CC0000]" |
| 97 | + > |
| 98 | + {t("archivedBadge")} |
| 99 | + </Badge> |
| 100 | + )} |
| 101 | + </div> |
| 102 | + |
| 103 | + {/* 提交人 + host 来源 */} |
| 104 | + <div className="flex items-center justify-between text-[10px] font-mono text-neutral-400 pt-1"> |
| 105 | + <span className="truncate max-w-[60%]">{link.host}</span> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </a> |
| 109 | + |
| 110 | + {/* 举报区:与整卡点击分离(ReportButton 内部阻止冒泡) */} |
| 111 | + <div className="px-4 pb-3 border-t border-[var(--foreground)]/10 pt-2 flex justify-end"> |
| 112 | + <ReportButton linkId={link.id} isLoggedIn={isLoggedIn} /> |
| 113 | + </div> |
| 114 | + </li> |
| 115 | + ); |
| 116 | +} |
0 commit comments