Newer
Older
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
import classNames from 'classnames';
import EmbedMedia from './EmbedMedia';
import styles from "./Embed.module.scss";
import { useContext } from 'preact/hooks';
import { Embed as EmbedRJS } from "revolt.js/dist/api/objects";
import { useIntermediate } from '../../../../context/intermediate/Intermediate';
import { MessageAreaWidthContext } from '../../../../pages/channels/messaging/MessageArea';
interface Props {
embed: EmbedRJS;
}
const MAX_EMBED_WIDTH = 480;
const MAX_EMBED_HEIGHT = 640;
const CONTAINER_PADDING = 24;
const MAX_PREVIEW_SIZE = 150;
export default function Embed({ embed }: Props) {
// ! FIXME: temp code
// ! add proxy function to client
function proxyImage(url: string) {
return 'https://jan.revolt.chat/proxy?url=' + encodeURIComponent(url);
}
const { openScreen } = useIntermediate();
const maxWidth = Math.min(useContext(MessageAreaWidthContext) - CONTAINER_PADDING, MAX_EMBED_WIDTH);
function calculateSize(w: number, h: number): { width: number, height: number } {
let limitingWidth = Math.min(
maxWidth,
w
);
let limitingHeight = Math.min(
MAX_EMBED_HEIGHT,
h
);
// Calculate smallest possible WxH.
let width = Math.min(
limitingWidth,
limitingHeight * (w / h)
);
let height = Math.min(
limitingHeight,
limitingWidth * (h / w)
);
return { width, height };
}
switch (embed.type) {
case 'Website': {
// ! FIXME: move this to january
/*if (embed.url && YOUTUBE_RE.test(embed.url)) {
embed.color = '#FF424F';
}
if (embed.url && TWITCH_RE.test(embed.url)) {
embed.color = '#7B68EE';
}
if (embed.url && SPOTIFY_RE.test(embed.url)) {
embed.color = '#1ABC9C';
}
if (embed.url && SOUNDCLOUD_RE.test(embed.url)) {
embed.color = '#FF7F50';
}*/
// Determine special embed size.
let mw, mh;
let largeMedia = (embed.special && embed.special.type !== 'None') || embed.image?.size === 'Large';
switch (embed.special?.type) {
case 'YouTube':
case 'Bandcamp': {
mw = embed.video?.width ?? 1280;
mh = embed.video?.height ?? 720;
break;
}
case 'Twitch': {
mw = 1280;
mh = 720;
break;
}
default: {
if (embed.image?.size === 'Preview') {
mw = MAX_EMBED_WIDTH;
mh = Math.min(embed.image.height ?? 0, MAX_PREVIEW_SIZE);
} else {
mw = embed.image?.width ?? MAX_EMBED_WIDTH;
mh = embed.image?.height ?? 0;
}
}
}
let { width, height } = calculateSize(mw, mh);
return (
<div
className={classNames(styles.embed, styles.website)}
style={{
borderInlineStartColor: embed.color ?? 'var(--tertiary-background)',
width: width + CONTAINER_PADDING
}}>
<div>
{ embed.site_name && <div className={styles.siteinfo}>
{ embed.icon_url && <img className={styles.favicon} src={proxyImage(embed.icon_url)} draggable={false} onError={e => e.currentTarget.style.display = 'none'} /> }
<div className={styles.site}>{ embed.site_name } </div>
</div> }
{/*<span><a href={embed.url} target={"_blank"} className={styles.author}>Author</a></span>*/}
{ embed.title && <span><a href={embed.url} target={"_blank"} className={styles.title}>{ embed.title }</a></span> }
{ embed.description && <div className={styles.description}>{ embed.description }</div> }
{ largeMedia && <EmbedMedia embed={embed} height={height} /> }
</div>
{
!largeMedia && <div>
<EmbedMedia embed={embed} width={height * ((embed.image?.width ?? 0) / (embed.image?.height ?? 0))} height={height} />
</div>
}
</div>
)
}
case 'Image': {
return (
<img className={classNames(styles.embed, styles.image)}
style={calculateSize(embed.width, embed.height)}
src={proxyImage(embed.url)}
type="text/html"
frameBorder="0"
onClick={() =>
openScreen({ id: "image_viewer", embed })
}
onMouseDown={ev =>
ev.button === 1 &&
window.open(embed.url, "_blank")
}
/>
)
}
default: return null;
}
}