Merge pull request #191 from enjikaka/tidal-embed-fix
Use the current embed player via TIDALs OEmbed API.
This commit is contained in:
commit
e494d2f098
@ -1,27 +1,50 @@
|
|||||||
import { useEffect, useMemo } from "react";
|
import { CSSProperties, useEffect, useState } from "react";
|
||||||
import { TidalRegex } from "Const";
|
import { TidalRegex } from "Const";
|
||||||
|
|
||||||
const TidalEmbed = ({ link }: { link: string }) => {
|
// Re-use dom parser across instances of TidalEmbed
|
||||||
const data = useMemo(() => {
|
const domParser = new DOMParser();
|
||||||
const match = link.match(TidalRegex);
|
|
||||||
if (match?.length != 3) {
|
async function oembedLookup (link: string) {
|
||||||
return null;
|
// 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 Promise.reject('Not a TIDAL link.');
|
||||||
}
|
}
|
||||||
let type = match[1][0];
|
|
||||||
let id = match[2];
|
const [, productType, productId] = regexResult;
|
||||||
return { type, id };
|
const oembedApi = `https://oembed.tidal.com/?url=https://tidal.com/browse/${productType}/${productId}`;
|
||||||
|
|
||||||
|
const apiResponse = await fetch(oembedApi);
|
||||||
|
const json = await apiResponse.json();
|
||||||
|
|
||||||
|
const doc = domParser.parseFromString(json.html, 'text/html');
|
||||||
|
const iframe = doc.querySelector('iframe');
|
||||||
|
|
||||||
|
if (!iframe) {
|
||||||
|
return Promise.reject('No iframe delivered.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
source: iframe.getAttribute('src'),
|
||||||
|
height: json.height
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const TidalEmbed = ({ link }: { link: string }) => {
|
||||||
|
const [source, setSource] = useState<string>();
|
||||||
|
const [height, setHeight] = useState<number>();
|
||||||
|
const extraStyles = link.includes('video') ? { aspectRatio: "16 / 9" } : { height };
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
oembedLookup(link).then(data => {
|
||||||
|
setSource(data.source || undefined);
|
||||||
|
setHeight(data.height);
|
||||||
|
}).catch(console.error);
|
||||||
}, [link]);
|
}, [link]);
|
||||||
|
|
||||||
const ScriptSrc = "https://embed.tidal.com/tidal-embed.js";
|
if (!source) return <a href={link} target="_blank" rel="noreferrer" onClick={(e) => e.stopPropagation()} className="ext">{link}</a>;
|
||||||
useEffect(() => {
|
return <iframe src={source} style={extraStyles} width="100%" title="TIDAL Embed" frameBorder={0} />;
|
||||||
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;
|
Loading…
Reference in New Issue
Block a user