"use client"; import { useState, useRef } from "react"; import { Card } from "@/components/ui/Card"; import { api } from "@/lib/api"; import { toast } from "sonner"; import { Trash2, Loader2 } from "lucide-react"; import type { DiscoverConfig } from "../types"; interface DiscoverSettingsProps { config: DiscoverConfig | null; onUpdateConfig: (updatedConfig: DiscoverConfig | null) => void; onPlaylistCleared?: () => void; } export function DiscoverSettings({ config, onUpdateConfig, onPlaylistCleared, }: DiscoverSettingsProps) { const [isClearing, setIsClearing] = useState(false); const debounceRef = useRef(null); // Generic handler for config changes with debounce function handleConfigChange(key: K, value: DiscoverConfig[K]) { // Update local state immediately for responsive UI if (config) { onUpdateConfig({ ...config, [key]: value }); } // Debounce the API call if (debounceRef.current) { clearTimeout(debounceRef.current); } debounceRef.current = setTimeout(async () => { try { await api.updateDiscoverConfig({ [key]: value }); } catch (error) { toast.error("Failed to save setting"); } }, 500); } async function handleClearPlaylist() { if (isClearing) return; const confirmed = window.confirm( "Clear Discovery Playlist?\n\n" + "• Liked albums will be moved to your library\n" + "• Non-liked albums will be deleted\n\n" + "This action cannot be undone." ); if (!confirmed) return; setIsClearing(true); try { const result = await api.clearDiscoverPlaylist(); if (result.likedMoved > 0 && result.activeDeleted > 0) { toast.success( `Moved ${result.likedMoved} liked album${result.likedMoved !== 1 ? "s" : ""} to library, deleted ${result.activeDeleted} album${result.activeDeleted !== 1 ? "s" : ""}` ); } else if (result.likedMoved > 0) { toast.success( `Moved ${result.likedMoved} liked album${result.likedMoved !== 1 ? "s" : ""} to library` ); } else if (result.activeDeleted > 0) { toast.success( `Deleted ${result.activeDeleted} album${result.activeDeleted !== 1 ? "s" : ""}` ); } else { toast.info("No albums to clear"); } onPlaylistCleared?.(); } catch (error) { toast.error("Failed to clear playlist"); } finally { setIsClearing(false); } } return (

Settings

handleConfigChange("playlistSize", parseInt(e.target.value)) } className="w-full h-2 bg-white/10 rounded-lg appearance-none cursor-pointer [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-4 [&::-webkit-slider-thumb]:h-4 [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-purple-500" />

One song per album. Larger = more discovery.

handleConfigChange("downloadRatio", parseFloat(e.target.value)) } className="w-full h-2 bg-white/10 rounded-lg appearance-none cursor-pointer [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-4 [&::-webkit-slider-thumb]:h-4 [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-purple-500" />

Extra albums to download in case some fail. Higher = more reliable, but uses more bandwidth.

handleConfigChange("exclusionMonths", parseInt(e.target.value)) } className="w-full h-2 bg-white/10 rounded-lg appearance-none cursor-pointer [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-4 [&::-webkit-slider-thumb]:h-4 [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-purple-500" />

How long to wait before recommending the same album again. Set to 0 to disable.

{/* Clear Playlist */}

Remove the current playlist. Liked albums will be moved to your library, non-liked albums will be deleted.

); }