feat: tidal embeds
This commit is contained in:
@ -4,7 +4,7 @@ import { Link } from "react-router-dom";
|
||||
import ReactMarkdown from "react-markdown";
|
||||
import { TwitterTweetEmbed } from "react-twitter-embed";
|
||||
|
||||
import { UrlRegex, FileExtensionRegex, MentionRegex, InvoiceRegex, YoutubeUrlRegex, TweetUrlRegex, HashtagRegex } from "Const";
|
||||
import { UrlRegex, FileExtensionRegex, MentionRegex, InvoiceRegex, YoutubeUrlRegex, TweetUrlRegex, HashtagRegex, TidalRegex } from "Const";
|
||||
import { eventLink, hexToBech32 } from "Util";
|
||||
import Invoice from "Element/Invoice";
|
||||
import Hashtag from "Element/Hashtag";
|
||||
@ -12,12 +12,14 @@ import Hashtag from "Element/Hashtag";
|
||||
import Tag from "Nostr/Tag";
|
||||
import { MetadataCache } from "Db/User";
|
||||
import Mention from "Element/Mention";
|
||||
import TidalEmbed from "Element/TidalEmbed";
|
||||
|
||||
function transformHttpLink(a: string) {
|
||||
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 extension = FileExtensionRegex.test(url.pathname.toLowerCase()) && RegExp.$1;
|
||||
if (extension) {
|
||||
switch (extension) {
|
||||
@ -61,6 +63,8 @@ function transformHttpLink(a: string) {
|
||||
<br />
|
||||
</>
|
||||
)
|
||||
} else if (tidalId) {
|
||||
return <TidalEmbed link={a} />
|
||||
} else {
|
||||
return <a href={a} onClick={(e) => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">{a}</a>
|
||||
}
|
||||
|
39
src/Element/TidalEmbed.tsx
Normal file
39
src/Element/TidalEmbed.tsx
Normal file
@ -0,0 +1,39 @@
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { TidalRegex } from "../Const";
|
||||
|
||||
const TidalEmbed = ({ link }: { link: string }) => {
|
||||
// https://tidal.com/browse/mix/0029457ec7eed3b340ee2b907fc4d8
|
||||
// https://tidal.com/browse/track/168295350
|
||||
// https://tidal.com/browse/album/168295347
|
||||
// https://tidal.com/browse/playlist/4261748a-4287-4758-aaab-6d5be3e99e52
|
||||
|
||||
const data = useMemo(() => {
|
||||
const match = link.match(TidalRegex);
|
||||
if (match?.length != 3) {
|
||||
return null;
|
||||
}
|
||||
let type = match[1][0];
|
||||
let id = match[2];
|
||||
return { type, id };
|
||||
}, [link]);
|
||||
|
||||
const ScriptSrc = "https://embed.tidal.com/tidal-embed.js";
|
||||
useEffect(() => {
|
||||
let head = document.head.querySelector(`script[src="${ScriptSrc}"]`);
|
||||
console.debug(head);
|
||||
if (!head) {
|
||||
let sTag = document.createElement("script");
|
||||
sTag.src = ScriptSrc;
|
||||
sTag.async = true;
|
||||
document.head.appendChild(sTag);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="tidal-embed" data-type={data?.type} data-id={data?.id}></div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default TidalEmbed;
|
Reference in New Issue
Block a user