snort/packages/app/src/Element/HyperText.tsx

106 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-02-03 16:43:21 +00:00
import { TwitterTweetEmbed } from "react-twitter-embed";
import {
FileExtensionRegex,
YoutubeUrlRegex,
TweetUrlRegex,
TidalRegex,
SoundCloudRegex,
MixCloudRegex,
SpotifyRegex,
2023-02-13 11:19:50 +00:00
TwitchRegex,
2023-02-14 03:12:15 +00:00
AppleMusicRegex,
NostrNestsRegex,
WavlakeRegex,
2023-02-03 16:43:21 +00:00
} from "Const";
2023-04-08 21:29:38 +00:00
import { magnetURIDecode } from "Util";
import SoundCloudEmbed from "Element/SoundCloudEmded";
import MixCloudEmbed from "Element/MixCloudEmbed";
2023-02-03 16:43:21 +00:00
import SpotifyEmbed from "Element/SpotifyEmbed";
import TidalEmbed from "Element/TidalEmbed";
2023-02-28 15:52:05 +00:00
import TwitchEmbed from "Element/TwitchEmbed";
import AppleMusicEmbed from "Element/AppleMusicEmbed";
import WavlakeEmbed from "Element/WavlakeEmbed";
2023-03-28 09:52:44 +00:00
import LinkPreview from "Element/LinkPreview";
2023-03-25 22:55:34 +00:00
import NostrLink from "Element/NostrLink";
2023-04-08 21:29:38 +00:00
import RevealMedia from "Element/RevealMedia";
import MagnetLink from "Element/MagnetLink";
2023-02-03 16:43:21 +00:00
2023-04-18 21:20:13 +00:00
export default function HyperText({ link, creator, depth }: { link: string; creator: string; depth?: number }) {
2023-04-08 21:29:38 +00:00
const a = link;
try {
const url = new URL(a);
const youtubeId = YoutubeUrlRegex.test(a) && RegExp.$1;
const tweetId = TweetUrlRegex.test(a) && RegExp.$2;
const tidalId = TidalRegex.test(a) && RegExp.$1;
const soundcloundId = SoundCloudRegex.test(a) && RegExp.$1;
const mixcloudId = MixCloudRegex.test(a) && RegExp.$1;
const isSpotifyLink = SpotifyRegex.test(a);
const isTwitchLink = TwitchRegex.test(a);
const isAppleMusicLink = AppleMusicRegex.test(a);
const isNostrNestsLink = NostrNestsRegex.test(a);
const isWavlakeLink = WavlakeRegex.test(a);
const extension = FileExtensionRegex.test(url.pathname.toLowerCase()) && RegExp.$1;
if (extension && !isAppleMusicLink) {
return <RevealMedia link={a} creator={creator} />;
} else if (tweetId) {
return (
<div className="tweet" key={tweetId}>
<TwitterTweetEmbed tweetId={tweetId} />
</div>
);
} else if (youtubeId) {
return (
<iframe
className="w-max"
src={`https://www.youtube.com/embed/${youtubeId}`}
title="YouTube video player"
key={youtubeId}
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowFullScreen={true}
/>
);
} else if (tidalId) {
return <TidalEmbed link={a} />;
} else if (soundcloundId) {
return <SoundCloudEmbed link={a} />;
} else if (mixcloudId) {
return <MixCloudEmbed link={a} />;
} else if (isSpotifyLink) {
return <SpotifyEmbed link={a} />;
} else if (isTwitchLink) {
return <TwitchEmbed link={a} />;
} else if (isAppleMusicLink) {
return <AppleMusicEmbed link={a} />;
} else if (isNostrNestsLink) {
return (
<>
<a href={a} onClick={e => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">
{a}
2023-04-08 21:29:38 +00:00
</a>
{/*<NostrNestsEmbed link={a} />,*/}
</>
);
} else if (isWavlakeLink) {
return <WavlakeEmbed link={a} />;
} else if (url.protocol === "nostr:" || url.protocol === "web+nostr:") {
2023-04-18 21:20:13 +00:00
return <NostrLink link={a} depth={depth} />;
2023-04-08 21:29:38 +00:00
} else if (url.protocol === "magnet:") {
const parsed = magnetURIDecode(a);
if (parsed) {
return <MagnetLink magnet={parsed} />;
}
} else {
2023-04-18 11:47:01 +00:00
return <LinkPreview url={a} />;
2023-02-07 19:47:57 +00:00
}
2023-04-08 21:29:38 +00:00
} catch {
// Ignore the error.
}
return (
<a href={a} onClick={e => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">
{a}
</a>
);
2023-02-03 21:38:14 +00:00
}