Use the new embed player via TIDALs OEmbed API.
This commit is contained in:
parent
69a4dfffb7
commit
b1c98a2438
@ -1,27 +1,32 @@
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
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 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 };
|
||||
const [embed, setEmbed] = useState();
|
||||
|
||||
useEffect(() => {
|
||||
oembedLookup(link).then(setEmbed);
|
||||
}, [link]);
|
||||
|
||||
const ScriptSrc = "https://embed.tidal.com/tidal-embed.js";
|
||||
useEffect(() => {
|
||||
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>;
|
||||
if (!embed) return <a href={link} target="_blank" rel="noreferrer" onClick={(e) => e.stopPropagation()} className="ext">{link}</a>;
|
||||
return <div dangerouslySetInnerHTML={{__html: embed}}></div>;
|
||||
}
|
||||
|
||||
export default TidalEmbed;
|
Loading…
x
Reference in New Issue
Block a user