-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
serve widgets from backend, cache in frontend
- Loading branch information
Showing
8 changed files
with
118 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,65 @@ | ||
import { useState } from 'react'; | ||
import { Suspense } from 'react'; | ||
import Spinner from 'react-bootstrap/Spinner'; | ||
import './Track.css'; | ||
|
||
function SpotifyTrackWidget({ track_id, highlight, loaded, onLoad = f => f }) { | ||
function createResource(pending) { | ||
let error, response; | ||
pending.then(r => response = r).catch(e => error = e); | ||
return { | ||
read() { | ||
if (error) throw error; | ||
if (response) return response; | ||
throw pending; | ||
} | ||
}; | ||
} | ||
|
||
function SpotifyTrackWidget({ track_id, highlight, resource }) { | ||
const iframe = (resource) ? resource.read() : null; | ||
|
||
return ( | ||
<iframe | ||
className={(loaded && highlight) ? "highlight" : ""} | ||
className={highlight ? "highlight" : ""} | ||
title={track_id} | ||
src={"https://open.spotify.com/embed/track/" + track_id} | ||
width={loaded ? "100%" : "0%"} | ||
//src={`https://open.spotify.com/embed/track/${track_id}`} | ||
srcdoc={Buffer.from(iframe.data, 'base64')} | ||
width="100%" | ||
height="80" | ||
frameBorder="0" | ||
allowtransparency="true" | ||
allow="encrypted-media" | ||
onLoad={() => onLoad()} | ||
/> | ||
); | ||
} | ||
|
||
export default function Track({ track_id, highlight = false, onLoad = f => f }) { | ||
const [loaded, setLoaded] = useState(false); | ||
export default function Track({ track_id, highlight = false }) { | ||
if (typeof Track.cache == 'undefined') { | ||
Track.cache = {}; | ||
} | ||
|
||
const resource = (track_id in Track.cache) ? Track.cache[track_id] : | ||
createResource(new Promise(resolves => { | ||
fetch(`${process.env.REACT_APP_API_URL}/widget?track_id=${track_id}`) | ||
.then(response => (response.status === 200) ? response.text() : '') | ||
.then(data => resolves({ data: data })) | ||
.catch(error => console.error('Error:', error)); | ||
})); | ||
Track.cache[track_id] = resource; | ||
|
||
return ( | ||
<> | ||
{!loaded ? | ||
<> | ||
<Spinner animation="border" /> | ||
<SpotifyTrackWidget | ||
track_id={track_id} | ||
highlight={highlight} | ||
loaded={loaded} | ||
onLoad={() => { | ||
setLoaded(true); | ||
onLoad(); | ||
}} | ||
/> | ||
</> : | ||
<SpotifyTrackWidget | ||
track_id={track_id} | ||
highlight={highlight} | ||
loaded={loaded} | ||
onLoad={() => { | ||
setLoaded(true); | ||
onLoad(); | ||
}} | ||
/> | ||
} | ||
</> | ||
<Suspense fallback={ | ||
<div | ||
className="d-flex justify-content-center align-items-center align-middle" | ||
style={{ height: 80, width: "100%" }} | ||
> | ||
<Spinner animation="border" /> | ||
</div> | ||
} > | ||
<SpotifyTrackWidget | ||
track_id={track_id} | ||
highlight={highlight} | ||
resource={resource} | ||
/> | ||
</Suspense> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters