-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathLinkPreview.tsx
More file actions
165 lines (151 loc) · 5.59 KB
/
LinkPreview.tsx
File metadata and controls
165 lines (151 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import React, { type MouseEventHandler, type JSX, useCallback } from "react";
import { Tooltip, Text, Avatar, Button } from "@vector-im/compound-web";
import PlaySolidIcon from "@vector-im/compound-design-tokens/assets/web/icons/play-solid";
import classNames from "classnames";
import { useI18n } from "../../../../../core/i18n/i18nContext";
import type { UrlPreview } from "../types";
import { LinkedText } from "../../../../../core/utils/LinkedText";
import styles from "./LinkPreview.module.css";
export interface LinkPreviewActions {
onImageClick: () => void;
}
export type LinkPreviewProps = UrlPreview & LinkPreviewActions;
function LinkTitle({
title,
showTooltipOnLink,
link,
}: Pick<LinkPreviewProps, "title" | "showTooltipOnLink" | "link">): JSX.Element {
const caption = new URL(link).toString();
const anchor = (
<Text
as="a"
type="body"
weight="semibold"
size="md"
className={styles.title}
href={link}
target="_blank"
rel="noreferrer noopener"
>
{title}
</Text>
);
return showTooltipOnLink ? <Tooltip label={caption}>{anchor}</Tooltip> : anchor;
}
function LinkSiteName({ siteIcon, siteName }: { siteIcon?: string; siteName: string }): JSX.Element {
return (
<div className={styles.siteName}>
{siteIcon && <Avatar size="16px" name={siteName} id={siteName} src={siteIcon} />}
<Text as="span" size="sm" weight="regular">
{siteName}
</Text>
</div>
);
}
/**
* A condensed link preview that only contains the site icon, the title of the link and the site name.
*/
function LinkPreviewInline({
title,
showTooltipOnLink,
siteIcon,
siteName,
link,
}: Omit<LinkPreviewProps, "image" | "description" | "author" | "onImageClick">): JSX.Element {
return (
<div className={classNames(styles.container, styles.inline)}>
{siteIcon && (
<div className={styles.siteAvatar}>
<Avatar type="square" size="48px" name={title} id={title} src={siteIcon} />
</div>
)}
<div className={classNames(styles.textContent, styles.inline)}>
<LinkTitle title={title} showTooltipOnLink={showTooltipOnLink} link={link} />
{siteName && <LinkSiteName siteName={siteName} />}
</div>
</div>
);
}
/**
* LinkPreview renders a single preview component for a single link on an event. It is usually rendered as part of
* a `UrlPreviewGroupView`.
*/
export function LinkPreview({ onImageClick, ...preview }: LinkPreviewProps): JSX.Element {
const { translate: _t } = useI18n();
const onImageClickHandler = useCallback<MouseEventHandler>(
(ev) => {
if (ev.button != 0 || ev.metaKey) return;
ev.preventDefault();
if (!preview.image?.imageFull) {
return;
}
onImageClick();
},
[preview.image?.imageFull, onImageClick],
);
if (!preview.image && !preview.author && !preview.description) {
return <LinkPreviewInline {...preview} />;
}
let img: JSX.Element | undefined;
if (preview.image) {
if (preview.image.playable) {
// Playable media do not have clickable images so we don't
// overlay buttons atop buttons, instead we render a
// button for them to open the media.
img = (
<div
style={{
backgroundImage: `url('${preview.image.imageThumb}')`,
}}
className={styles.preview}
>
<Button
as="a"
href={preview.link}
aria-label={_t("timeline|url_preview|open_link")}
className={styles.playButton}
target="_blank"
rel="noreferrer noopener"
kind="primary"
>
<PlaySolidIcon width="24px" height="24px" />
</Button>
</div>
);
} else {
// Otherwise, the preview can be clicked on.
img = (
<button
className={styles.preview}
onClick={onImageClickHandler}
aria-label={_t("timeline|url_preview|view_image")}
>
<img src={preview.image.imageThumb} alt={preview.image.alt} title={preview.image.alt} />
</button>
);
}
}
return (
<div className={styles.container}>
{img}
<div className={styles.textContent}>
{preview.author && (
<Text as="span" size="md" weight="semibold">
{preview.author}
</Text>
)}
<LinkTitle title={preview.title} showTooltipOnLink={preview.showTooltipOnLink} link={preview.link} />
<LinkedText type="body" size="md" className={styles.description}>
{preview.description}
</LinkedText>
{preview.siteName && <LinkSiteName siteName={preview.siteName} siteIcon={preview.siteIcon} />}
</div>
</div>
);
}