Use the new embed player via TIDALs OEmbed API.
This commit is contained in:
@ -1,27 +1,32 @@
|
|||||||
import { useEffect, useMemo } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { TidalRegex } from "Const";
|
import { TidalRegex } from "Const";
|
||||||
|
|
||||||
|
async function oembedLookup (link: string) {
|
||||||
|
// Regex + re-construct to handle both tidal.com/type/id and tidal.com/browse/type/id links.
|
||||||
|
const regexResult = TidalRegex.exec(link);
|
||||||
|
|
||||||
|
if (!regexResult) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [, productType, productId] = regexResult;
|
||||||
|
const oembedApi = `https://oembed.stage.tidal.com/?url=https://tidal.com/browse/${productType}/${productId}`;
|
||||||
|
|
||||||
|
const apiResponse = await fetch(oembedApi);
|
||||||
|
const json = await apiResponse.json();
|
||||||
|
|
||||||
|
return json.html;
|
||||||
|
}
|
||||||
|
|
||||||
const TidalEmbed = ({ link }: { link: string }) => {
|
const TidalEmbed = ({ link }: { link: string }) => {
|
||||||
const data = useMemo(() => {
|
const [embed, setEmbed] = useState();
|
||||||
const match = link.match(TidalRegex);
|
|
||||||
if (match?.length != 3) {
|
useEffect(() => {
|
||||||
return null;
|
oembedLookup(link).then(setEmbed);
|
||||||
}
|
|
||||||
let type = match[1][0];
|
|
||||||
let id = match[2];
|
|
||||||
return { type, id };
|
|
||||||
}, [link]);
|
}, [link]);
|
||||||
|
|
||||||
const ScriptSrc = "https://embed.tidal.com/tidal-embed.js";
|
if (!embed) return <a href={link} target="_blank" rel="noreferrer" onClick={(e) => e.stopPropagation()} className="ext">{link}</a>;
|
||||||
useEffect(() => {
|
return <div dangerouslySetInnerHTML={{__html: embed}}></div>;
|
||||||
let sTag = document.createElement("script");
|
|
||||||
sTag.src = ScriptSrc;
|
|
||||||
sTag.async = true;
|
|
||||||
document.head.appendChild(sTag);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (!data) return <a href={link} target="_blank" rel="noreferrer" onClick={(e) => e.stopPropagation()} className="ext">{link}</a>;
|
|
||||||
return <div className="tidal-embed" data-type={data.type} data-id={data.id}></div>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default TidalEmbed;
|
export default TidalEmbed;
|
Reference in New Issue
Block a user