Use the new embed player via TIDALs OEmbed API.

This commit is contained in:
Jeremy Karlsson 2023-02-02 14:12:28 +01:00
parent 69a4dfffb7
commit b1c98a2438
No known key found for this signature in database
GPG Key ID: 58E2A72093092D1C

View File

@ -1,27 +1,32 @@
import { useEffect, useMemo } from "react";
import { useEffect, useState } from "react";
import { TidalRegex } from "Const";
const TidalEmbed = ({ link }: { link: string }) => {
const data = useMemo(() => {
const match = link.match(TidalRegex);
if (match?.length != 3) {
return null;
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;
}
let type = match[1][0];
let id = match[2];
return { type, id };
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 [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;