xonecas.io
8058
Ir

irvc / files

irvc / web / src / components / LinkPreview.tsx

3.6 KB
1import { ChevronDown, ChevronRight, X } from "lucide-react";
2import { cn } from "cn";
3import { IconButton } from "@/ui";
4import { userColor } from "../lib/scheme";
5import type { Preview } from "../lib/types";
6
7const host = (url: string) => {
8  try {
9    return new URL(url).hostname.replace(/^www\./, "");
10  } catch {
11    return url;
12  }
13};
14
15// LinkPreview is the Open Graph card for a message's first link. The server
16// fetched it and cached the thumbnail, so nothing here contacts the linked site
17// until the user clicks through. The header toggles it, like the image header.
18export default function LinkPreview({
19  preview,
20  collapsed,
21  onToggle,
22  onRemove,
23  pinned,
24}: {
25  preview: Preview;
26  collapsed: boolean;
27  onToggle: () => void;
28  // Given only to the message's author: removes the card for everyone.
29  onRemove?: () => void;
30  // The message is selected: keep the remove button showing without a hover.
31  pinned?: boolean;
32}) {
33  const site = preview.site || host(preview.url);
34  return (
35    <div className="pl-5 md:pl-8">
36      <button
37        type="button"
38        onClick={onToggle}
39        title={collapsed ? "show preview" : "hide preview"}
40        className="flex max-w-full items-center gap-1 text-xs leading-6 text-faint hover:text-muted"
41      >
42        {collapsed ? (
43          <ChevronRight className="size-3 shrink-0" />
44        ) : (
45          <ChevronDown className="size-3 shrink-0" />
46        )}
47        <span className="truncate">preview{collapsed && ` · ${site}`}</span>
48      </button>
49      {!collapsed && (
50        <div className="mt-0.5 mb-1 flex items-start gap-1">
51          <a
52            href={preview.url}
53            target="_blank"
54            rel="noopener noreferrer nofollow"
55            className="block min-w-0 max-w-md flex-1 overflow-hidden rounded-lg border border-line bg-surface transition-colors hover:border-accent/60"
56          >
57            {preview.image && (
58              <img
59                src={preview.image}
60                alt=""
61                loading="lazy"
62                className="max-h-48 w-full object-cover"
63              />
64            )}
65            <div className="space-y-0.5 px-3 py-2">
66              <div className="truncate text-[0.7rem] text-faint">{site}</div>
67              {preview.title && (
68                // Skittles color when enabled (plain text color otherwise). Keyed on
69                // the URL, so it varies from card to card but is stable for a given link.
70                <div
71                  className="line-clamp-2 text-sm font-medium text-fg"
72                  style={{ color: userColor(preview.url, "var(--fg)") }}
73                >
74                  {preview.title}
75                </div>
76              )}
77              {preview.description && (
78                <div className="line-clamp-3 text-xs text-muted">{preview.description}</div>
79              )}
80            </div>
81          </a>
82          {onRemove && (
83            <IconButton
84              label="remove preview"
85              size="sm"
86              className={cn(
87                "text-danger hover:bg-danger/10 hover:text-danger",
88                // Shown while the row is hovered, selected or has keyboard focus;
89                // otherwise every card would carry an X.
90                pinned
91                  ? "opacity-100"
92                  : "pointer-events-none opacity-0 group-hover:pointer-events-auto group-hover:opacity-100 focus-visible:pointer-events-auto focus-visible:opacity-100",
93              )}
94              onClick={onRemove}
95            >
96              <X />
97            </IconButton>
98          )}
99        </div>
100      )}
101    </div>
102  );
103}