Skip to content

Commit

Permalink
Working spotify palette share feature
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcrair committed Feb 19, 2024
1 parent 1497540 commit 2bfb217
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 2 deletions.
121 changes: 120 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
"dependencies": {
"axios": "^1.6.7",
"bootstrap": "^5.3.2",
"canvg": "^4.0.1",
"cors": "^2.8.5",
"dotenv": "^16.4.4",
"express": "^4.18.2",
"firebase": "^10.8.0",
"html-to-image": "^1.11.11",
"next": "^14.1.0",
"react": "^18",
"react-bootstrap": "^2.10.1",
"react-dom": "^18",
"react-router-dom": "^6.22.0"
"react-native-share": "^10.0.2",
"react-router-dom": "^6.22.0",
"react-share": "^5.1.0",
"react-svg-to-image": "^3.0.0"
},
"devDependencies": {
"autoprefixer": "^10.0.1",
Expand Down
Binary file added src/Spotify_logo_without_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions src/components/SongPlayer.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import React, { useRef, useState, useEffect } from "react";

import toImg from "react-svg-to-image";
import * as htmlToImage from "html-to-image";
import { Canvg } from "canvg";
import GetSpotifyCode from "./SpotifyCodeGenerator";

function SongPlayer({ song, token }) {
const audioRef = useRef(null);
const [isPlaying, setIsPlaying] = useState(false);

const addSongToQueue = () => {
// Use fetch for making HTTP requests
fetch(`http://localhost:5000/addSongToQueue?token=${token}&uri=${song.id}`)
.then((response) => {
if (!response.ok) {
throw new Error("Failed to add song to queue");
}
// Handle success if needed
})
.catch((error) => {
console.error("Error adding song to queue:", error);
});
};

const togglePlay = () => {
const audioElement = audioRef.current;

Expand All @@ -16,6 +35,55 @@ function SongPlayer({ song, token }) {
setIsPlaying(!isPlaying);
};

// Function to handle sharing
const shareSong = async () => {
console.log("sharing song");

// Use Web Share API to share the default image
const svgString = await GetSpotifyCode(song.external_urls.spotify);
console.log(svgString);

const img = new Image();

// Set the source of the image
img.src = `data:image/svg+xml;utf8,${encodeURIComponent(svgString)}`;

// Wait for the image to load
img.onload = () => {
// Create a canvas element
const canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;

// Draw the image on the canvas
canvas.getContext("2d")?.drawImage(img, 0, 0);

// Convert canvas to blob
canvas.toBlob((blob) => {
console.log(blob);

if (navigator.share) {
console.log("Web share API supported");
navigator
.share({
title: "Check out this song!",
text: `Listen to ${song.name}`,
url: song.external_urls.spotify,
files: [
new File([blob], "file.png", {
type: blob.type,
}),
],
})
.then(() => console.log("Shared successfully"))
.catch((error) => console.error("Error sharing:", error));
} else {
console.log("Web Share API not supported");
}
}, "image/png");
};
};

return (
<div
key={song.id}
Expand All @@ -32,6 +100,9 @@ function SongPlayer({ song, token }) {
<audio ref={audioRef} src={song.preview_url}>
<track kind="captions" />
</audio>
<button type="button" onClick={shareSong}>
Share
</button>
</div>
);
}
Expand Down
Loading

0 comments on commit 2bfb217

Please sign in to comment.