feat: spotify search
This commit is contained in:
parent
46c08d8540
commit
5b57f1b9ac
1 changed files with 135 additions and 66 deletions
|
@ -8,11 +8,17 @@ import {
|
||||||
IonSearchbar,
|
IonSearchbar,
|
||||||
IonTitle,
|
IonTitle,
|
||||||
IonToolbar,
|
IonToolbar,
|
||||||
|
IonList,
|
||||||
|
IonItem,
|
||||||
|
IonLabel,
|
||||||
} from "@ionic/react";
|
} from "@ionic/react";
|
||||||
|
|
||||||
import React, { useState } from "react";
|
import React, { useState, useEffect, useRef } from "react";
|
||||||
|
|
||||||
import { ArrowBigUp, Plus } from "lucide-react";
|
import { ArrowBigUp, Plus } from "lucide-react";
|
||||||
import { motion, AnimatePresence } from "framer-motion";
|
import { useCoda } from "../hooks/useCoda";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
interface NowPlayingDiscProps {
|
interface NowPlayingDiscProps {
|
||||||
id: string;
|
id: string;
|
||||||
|
@ -23,19 +29,80 @@ interface NowPlayingDiscProps {
|
||||||
upvotes?: number;
|
upvotes?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface QueueProps {
|
const Queue: React.FC = () => {
|
||||||
songs: NowPlayingDiscProps[];
|
const { records, addRecord, updateVoti } = useCoda();
|
||||||
onUpvote: (id: string) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Queue: React.FC<QueueProps> = ({ songs, onUpvote }) => {
|
|
||||||
const [showModal, setShowModal] = useState(false);
|
const [showModal, setShowModal] = useState(false);
|
||||||
const [searchText, setSearchText] = useState("");
|
const [searchText, setSearchText] = useState("");
|
||||||
|
const [searchResults, setSearchResults] = useState<NowPlayingDiscProps[]>([]);
|
||||||
|
const debounceTimeout = useRef<NodeJS.Timeout>();
|
||||||
|
|
||||||
// Ordina i brani in ordine decrescente per upvotes
|
// Ordina i brani in ordine decrescente per voti (dal DB)
|
||||||
const sortedSongs = [...songs].sort(
|
const sortedSongs = [...records].sort((a, b) => b.voti - a.voti);
|
||||||
(a, b) => (b.upvotes ?? 0) - (a.upvotes ?? 0)
|
|
||||||
);
|
// Effetto debounce per chiamare API dopo 2 secondi
|
||||||
|
useEffect(() => {
|
||||||
|
if (!searchText) {
|
||||||
|
setSearchResults([]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (debounceTimeout.current) {
|
||||||
|
clearTimeout(debounceTimeout.current);
|
||||||
|
}
|
||||||
|
|
||||||
|
debounceTimeout.current = setTimeout(() => {
|
||||||
|
fetch(
|
||||||
|
`https://fbbb261497e3.ngrok-free.app/music/search?query=${encodeURIComponent(searchText)}`
|
||||||
|
)
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
const results: NowPlayingDiscProps[] = data.tracks?.items.map((item: any) => ({
|
||||||
|
id: item.id,
|
||||||
|
coverUrl: item.album?.images?.[0]?.url || "",
|
||||||
|
title: item.name,
|
||||||
|
artist: item.artists?.map((a: any) => a.name).join(", ") || "",
|
||||||
|
upvotes: 0,
|
||||||
|
upvoted: false,
|
||||||
|
})) || [];
|
||||||
|
|
||||||
|
setSearchResults(results);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error("Error fetching search results:", err);
|
||||||
|
setSearchResults([]);
|
||||||
|
});
|
||||||
|
}, 2000);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (debounceTimeout.current) {
|
||||||
|
clearTimeout(debounceTimeout.current);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [searchText]);
|
||||||
|
|
||||||
|
// Funzione per aggiungere o incrementare voto in DB
|
||||||
|
const handleAddSong = (song: NowPlayingDiscProps) => {
|
||||||
|
// Cerco se è già presente nel DB (records)
|
||||||
|
const existing = records.find((r) => r.id === song.id);
|
||||||
|
|
||||||
|
if (existing) {
|
||||||
|
// Incrementa il voto
|
||||||
|
updateVoti(existing.id, true);
|
||||||
|
} else {
|
||||||
|
// Aggiunge la nuova canzone al DB
|
||||||
|
addRecord({
|
||||||
|
titolo: song.title,
|
||||||
|
coverUrl: song.coverUrl,
|
||||||
|
artista: song.artist,
|
||||||
|
color: "#ff6600", // puoi decidere un colore o gestirlo dinamicamente
|
||||||
|
voti: 1,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
setShowModal(false);
|
||||||
|
setSearchText("");
|
||||||
|
setSearchResults([]);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="queue-container">
|
<div className="queue-container">
|
||||||
|
@ -46,32 +113,20 @@ const Queue: React.FC<QueueProps> = ({ songs, onUpvote }) => {
|
||||||
color="white"
|
color="white"
|
||||||
strokeWidth={2}
|
strokeWidth={2}
|
||||||
onClick={() => setShowModal(true)}
|
onClick={() => setShowModal(true)}
|
||||||
|
style={{ cursor: "pointer" }}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<AnimatePresence>
|
{sortedSongs.map((song) => (
|
||||||
{sortedSongs.map((song) => {
|
<div className="song-item" key={song.id}>
|
||||||
const isUpvoted = song.upvoted ?? false;
|
|
||||||
const votes = song.upvotes ?? 0;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<motion.div
|
|
||||||
layout
|
|
||||||
key={song.id}
|
|
||||||
initial={{ opacity: 0, y: 20 }}
|
|
||||||
animate={{ opacity: 1, y: 0 }}
|
|
||||||
exit={{ opacity: 0, y: -20 }}
|
|
||||||
transition={{ duration: 0.3 }}
|
|
||||||
className="song-item"
|
|
||||||
>
|
|
||||||
<img
|
<img
|
||||||
className="cover"
|
className="cover"
|
||||||
src={song.coverUrl}
|
src={song.coverUrl}
|
||||||
alt={`${song.title} cover`}
|
alt={`${song.titolo} cover`}
|
||||||
/>
|
/>
|
||||||
<div className="text-info">
|
<div className="text-info">
|
||||||
<div className="title">{song.title}</div>
|
<div className="title">{song.titolo}</div>
|
||||||
<div className="artist">{song.artist}</div>
|
<div className="artist">{song.artista}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
@ -83,34 +138,32 @@ const Queue: React.FC<QueueProps> = ({ songs, onUpvote }) => {
|
||||||
marginLeft: "auto",
|
marginLeft: "auto",
|
||||||
cursor: "pointer",
|
cursor: "pointer",
|
||||||
}}
|
}}
|
||||||
onClick={() => onUpvote(song.id)}
|
onClick={() => updateVoti(song.id, true)}
|
||||||
>
|
>
|
||||||
<span
|
<span
|
||||||
style={{
|
style={{ fontWeight: "600", color: "#444", userSelect: "none" }}
|
||||||
fontWeight: "600",
|
|
||||||
color: "#444",
|
|
||||||
userSelect: "none",
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{votes}
|
{song.voti}
|
||||||
</span>
|
</span>
|
||||||
<ArrowBigUp
|
<ArrowBigUp
|
||||||
size={24}
|
size={24}
|
||||||
strokeWidth={2.5}
|
strokeWidth={2.5}
|
||||||
color={isUpvoted ? "#ff6600" : "#999"}
|
color="#ff6600"
|
||||||
fill={isUpvoted ? "#ff6600" : "none"}
|
fill="#ff6600"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</motion.div>
|
</div>
|
||||||
);
|
))}
|
||||||
})}
|
|
||||||
</AnimatePresence>
|
|
||||||
|
|
||||||
<IonModal isOpen={showModal} onDidDismiss={() => setShowModal(false)}>
|
<IonModal isOpen={showModal} onDidDismiss={() => setShowModal(false)}>
|
||||||
<IonHeader>
|
<IonHeader>
|
||||||
<IonToolbar>
|
<IonToolbar>
|
||||||
<IonTitle>Add Songs</IonTitle>
|
<IonTitle>Add Songs</IonTitle>
|
||||||
<IonButton slot="end" fill="clear" onClick={() => setShowModal(false)}>
|
<IonButton
|
||||||
|
slot="end"
|
||||||
|
fill="clear"
|
||||||
|
onClick={() => setShowModal(false)}
|
||||||
|
>
|
||||||
Close
|
Close
|
||||||
</IonButton>
|
</IonButton>
|
||||||
</IonToolbar>
|
</IonToolbar>
|
||||||
|
@ -121,6 +174,22 @@ const Queue: React.FC<QueueProps> = ({ songs, onUpvote }) => {
|
||||||
onIonInput={(e) => setSearchText(e.detail.value!)}
|
onIonInput={(e) => setSearchText(e.detail.value!)}
|
||||||
placeholder="Search..."
|
placeholder="Search..."
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<IonList>
|
||||||
|
{searchResults.map((song) => (
|
||||||
|
<IonItem button key={song.id} onClick={() => handleAddSong(song)}>
|
||||||
|
<img
|
||||||
|
src={song.coverUrl}
|
||||||
|
alt={song.title}
|
||||||
|
style={{ width: 50, height: 50, marginRight: 10 }}
|
||||||
|
/>
|
||||||
|
<IonLabel>
|
||||||
|
<h2>{song.title}</h2>
|
||||||
|
<p>{song.artist}</p>
|
||||||
|
</IonLabel>
|
||||||
|
</IonItem>
|
||||||
|
))}
|
||||||
|
</IonList>
|
||||||
</IonContent>
|
</IonContent>
|
||||||
</IonModal>
|
</IonModal>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue