import React, { useState, useEffect, useCallback } from 'react'; import { MediaList } from '../components/MediaList'; import { MediaItem } from '../api/jellyfin'; import { Loader } from '../components/Loader'; import { usePageTitle } from '../context/PageTitleContext/PageTitleContext'; import { useNavigate } from 'react-router-dom'; import { PullToRefreshWrapper } from '../components/PullToRefreshWrapper'; import './Discover.css'; interface DiscoverySection { title: string; description?: string; tracks: MediaItem[]; } interface AlbumDiscoverySection { title: string; description?: string; albums: MediaItem[]; } interface GenreSection { genre: string; title: string; tracks: MediaItem[]; } interface DiscoveryData { for_you?: DiscoverySection; genres?: GenreSection[]; new_releases?: DiscoverySection; trending?: DiscoverySection; trending_albums?: AlbumDiscoverySection; new_albums?: AlbumDiscoverySection; } export const Discover: React.FC = () => { const { setPageTitle } = usePageTitle(); const [discoveryData, setDiscoveryData] = useState({}); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const navigate = useNavigate(); const fetchDiscoveryData = useCallback(async () => { setLoading(true); setError(null); try { // Determine the correct backend URL based on environment let fetchUrl; // Check if we're on the live site if (window.location.hostname === 'music.samcloud.ca') { // Use relative URL for live site (will use the same domain with reverse proxy) fetchUrl = '/api/discover'; } else { // Use development backend URL for localhost const backendUrl = import.meta.env.VITE_BACKEND_URL || 'http://localhost:5140'; fetchUrl = `${backendUrl}/api/discover`; } console.log('Environment:', window.location.hostname); console.log('Fetching comprehensive discovery data from:', fetchUrl); const response = await fetch(fetchUrl); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error || `HTTP error! status: ${response.status}`); } const data = await response.json(); console.log('Discovery data received:', data); setDiscoveryData(data); } catch (e: any) { console.error("Failed to fetch discovery data:", e); setError(e.message || 'Failed to fetch discovery content.'); } finally { setLoading(false); } }, []); useEffect(() => { setPageTitle('Discover'); fetchDiscoveryData(); }, [setPageTitle, fetchDiscoveryData]); if (loading) { return (

Discover

Finding new music just for you...

); } if (error) { return (

Discover

Explore new music tailored to your taste

{error}

We're having trouble loading new music recommendations. Please try again later.

); } const hasContent = Object.keys(discoveryData).length > 0; if (!hasContent) { return (

Discover

Explore new music tailored to your taste

No recommendations available

Start listening to music to get personalized recommendations!

); } return (

Discover

Explore new music tailored to your taste

{/* For You Section */} {discoveryData.for_you && discoveryData.for_you.tracks.length > 0 && (

{discoveryData.for_you.title}

{discoveryData.for_you.description}

)} {/* New Releases Section */} {discoveryData.new_releases && discoveryData.new_releases.tracks.length > 0 && (

{discoveryData.new_releases.title}

{discoveryData.new_releases.description}

)} {/* Trending Section */} {discoveryData.trending && discoveryData.trending.tracks.length > 0 && (

{discoveryData.trending.title}

{discoveryData.trending.description}

)} {/* Popular Albums Section */} {discoveryData.trending_albums && discoveryData.trending_albums.albums.length > 0 && (

{discoveryData.trending_albums.title}

{discoveryData.trending_albums.description}

)} {/* New Albums Release Section */} {discoveryData.new_albums && discoveryData.new_albums.albums.length > 0 && (

New Albums Release

Fresh albums just for you

)} {/* Genre Sections */} {discoveryData.genres && discoveryData.genres.length > 0 && ( <>

Explore by Genre

Discover new music across different genres

{discoveryData.genres.map((genreSection: GenreSection, index: number) => (

{genreSection.title}

))} )}
); };