-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
297 lines (279 loc) · 8.49 KB
/
index.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import parse from "html-react-parser";
import LatestTweetStyles from "@styles/LatestTweet.module.css";
import Image from "next/image";
import Twitter from "@components/LatestTweet/svg/Twitter";
import LinkIcon from "@components/LatestTweet/svg/LinkIcon";
import Heart from "@components/LatestTweet/svg/Heart";
import Retweet from "@components/LatestTweet/svg/Retweet";
import Reply from "@components/LatestTweet/svg/Reply";
import Verified from "@components/LatestTweet/svg/Verified";
function formatUrlForShortDisplay(url) {
return new URL(url).hostname;
}
/**
* DONE:
* - mentions
* - show photo media
* - do not show reply tweets
* - show rich url previews with images
* - use mentions entities rather than regex for users
* - likes and retweets and stuff
* - Link component to actual tweet
* - accessibility on metrics
* - check mark (isaud_)
* - show time
* - hashtags
*
*
* IN PROGRESS:
*
*
* - video media - not possible with api v2?
* - render multiple entities - do we really want to do this? - NO - expand only the last entity
* - check gifs - not possible with api v2?
*
*
* TODO:
* - i.e. youtube link
* - other entities?
*
*
* Problems:
*
* 1. Some urlEntities I want to show, i.e. those that are in line with the tweet text
* some I do not want to show, because they should be replaced by the media embedded below
*
* Is there a way to cross reference url entities with media urls and only show the url entities
* that are not present in a media object
*
* 2. The tweet is getting a bit thicc
* do we create a priority list of what we show - so we don't have a million bits of media etc
* -- only expand the last media object
*/
export function processTweet(data) {
let _text = data.text;
const { entities } = data;
if (entities.hashtags) {
entities.hashtags.forEach((hashtag) => {
_text = _text.replace(
`#${hashtag.tag}`,
`<a href="https://twitter.com/hashtag/${hashtag.tag}" className="${LatestTweetStyles.hashtag}" target="_blank">#${hashtag.tag}</a>`,
);
});
}
// remove the url string which is replaced by a rich
// preview in processUrls
if (entities.urls) {
entities.urls.forEach((urlEntity) => {
_text = _text.replace(
urlEntity.url,
`<a href="https://twitter.com/${urlEntity.url}" target="_blank">${urlEntity.display_url}</a>`,
);
});
}
// replace @mentions with anchor links
// warning - probably use the start and end of the mention
// to properly replace the text with the link
if (entities.mentions) {
entities.mentions.forEach((mention) => {
_text = _text.replace(
`@${mention.username}`,
`<a href="https://twitter.com/${mention.username}" target="_blank">@${mention.username}</a>`,
);
});
}
return parse(_text);
}
function processMedia(includes) {
const altText =
"Image media from the latest tweet - sorry there's no real alternative text - the Twitter API doesn't seem to provide it!";
const media = includes.media
.map((media) => {
switch (media.type) {
case "animated_gif":
return (
<Image
alt={altText}
src={media.preview_image_url}
height={media.height}
width={media.width}
layout="responsive"
className={LatestTweetStyles.mediaImg}
/>
);
case "video":
return (
<Image
alt={altText}
src={media.preview_image_url}
height={media.height}
width={media.width}
layout="responsive"
className={LatestTweetStyles.mediaImg}
/>
);
case "photo":
return (
<Image
alt={altText}
src={media.url}
height={media.height}
width={media.width}
layout="responsive"
className={LatestTweetStyles.mediaImg}
/>
);
default:
return null;
}
})
.pop();
return media;
}
function processUrls(urls) {
const processed = urls.map((url) => {
// at the moment, we check for url[images] as currently
// a single image is also a url...
if (url.images) {
return (
<a
key={url.title}
href={url.expanded_url}
target="_blank"
className={LatestTweetStyles.urlPreview}
>
<div className={LatestTweetStyles.urlPreview__imgContainer}>
<Image alt={url.title} src={url.images[0].url} layout="fill" />
</div>
<div className={LatestTweetStyles.urlPreview__inner}>
<p className={LatestTweetStyles.urlPreview__title}>{url.title}</p>
<p className={LatestTweetStyles.urlPreview__description}>
{url.description}
</p>
<div className={LatestTweetStyles.urlPreview__shortUrlDisplay}>
<span className={LatestTweetStyles.urlPreview__linkIconContainer}>
<LinkIcon />
</span>
<p className={LatestTweetStyles.urlPreview__shortUrlDisplay__url}>
{formatUrlForShortDisplay(url.expanded_url)}
</p>
</div>
</div>
</a>
);
} else {
return null;
}
});
return processed;
}
function processTweetMetrics(metrics) {
return (
<ul className={LatestTweetStyles.metrics}>
<li className={LatestTweetStyles.metrics__block}>
<span className={LatestTweetStyles.metrics__iconContainer}>
<Reply />
</span>
<p
className={LatestTweetStyles.metrics__metric}
aria-label="Tweet reply count"
>
{metrics.reply_count}
</p>
</li>
<li className={LatestTweetStyles.metrics__block}>
<span className={LatestTweetStyles.metrics__iconContainer}>
<Retweet />
</span>
<p
className={LatestTweetStyles.metrics__metric}
aria-label="Tweet retweet and quote count"
>
{metrics.retweet_count + metrics.quote_count}
</p>
</li>
<li className={LatestTweetStyles.metrics__block}>
<span className={LatestTweetStyles.metrics__iconContainer}>
<Heart />
</span>
<p
className={LatestTweetStyles.metrics__metric}
aria-label="Tweet like count"
>
{metrics.like_count}
</p>
</li>
</ul>
);
}
function processDate(created_at) {
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const date = new Date(created_at);
return `${date.getHours()}:${date.getMinutes()} ${date.getDate()} ${
months[date.getMonth()]
} ${date.getFullYear()}`;
}
export default function LatestTweet({ latestTweet }) {
const { tweet, metrics, profileImgUrl } = latestTweet;
// Do not show latest tweet if it is a reply tweet
if (tweet.data.referenced_tweets) {
return null;
}
function goToTweet(id) {
window.open(`https://twitter.com/whitep4nth3r/status/${id}`, "_blank");
}
return (
<div
onClick={() => goToTweet(tweet.data.id)}
className={LatestTweetStyles.container}
>
<div className={LatestTweetStyles.top}>
<div className={LatestTweetStyles.imgContainer}>
<Image
alt="whitep4nth3r's Twitter profile image"
height={48}
width={48}
src={profileImgUrl}
layout="responsive"
className={LatestTweetStyles.img}
/>
</div>
<div className={LatestTweetStyles.nameContainer}>
<h3 className={LatestTweetStyles.twitterName}>
{metrics.name}{" "}
<span className={LatestTweetStyles.verifiedIconContainer}>
<Verified />
</span>
</h3>
<h4 className={LatestTweetStyles.twitterUsername}>
@{metrics.username}
<time
className={LatestTweetStyles.createdAt}
dateTime={tweet.data.created_at}
>
{processDate(tweet.data.created_at)}
</time>
</h4>
</div>
<Twitter className={LatestTweetStyles.logo} />
</div>
<p className={LatestTweetStyles.tweetText}>{processTweet(tweet.data)}</p>
{tweet.includes && processMedia(tweet.includes)}
{tweet.data.entities.urls && processUrls(tweet.data.entities.urls)}
{processTweetMetrics(tweet.data.public_metrics)}
</div>
);
}