'use client' import { useState, useEffect, useCallback } from 'react' import { usePartyStore } from '@/store/partyStore' import { fetchTopCharts } from '@/lib/api' import type { SearchResult } from '@/types' const SEARCH_HISTORY_KEY = 'pm_search_history' const MAX_HISTORY = 8 function getSearchHistory(): string[] { if (typeof window === 'undefined') return [] try { return JSON.parse(localStorage.getItem(SEARCH_HISTORY_KEY) ?? '[]') } catch { return [] } } function addToSearchHistory(q: string) { const prev = getSearchHistory().filter((s) => s !== q) const next = [q, ...prev].slice(0, MAX_HISTORY) try { localStorage.setItem(SEARCH_HISTORY_KEY, JSON.stringify(next)) } catch {} } function clearSearchHistory() { try { localStorage.removeItem(SEARCH_HISTORY_KEY) } catch {} } function TopChartsCard({ tracks }: { tracks: string[] }) { const { loadPlaylist } = usePartyStore() const [launched, setLaunched] = useState(false) const handlePlay = () => { loadPlaylist(tracks) setLaunched(true) window.scrollTo({ top: 0, behavior: 'smooth' }) setTimeout(() => setLaunched(false), 2500) } return (
Прямо сейчас

Топ чарт

{tracks.length} треков

) } export default function SoloTab() { const [search, setSearch] = useState('') const [topTracks, setTopTracks] = useState(null) const [searchHistory, setSearchHistory] = useState([]) const { loadPlaylist } = usePartyStore() useEffect(() => { setSearchHistory(getSearchHistory()) fetchTopCharts().then((results: SearchResult[]) => { if (!results.length) { setTopTracks([]); return } setTopTracks(results.map((r) => (r.artist ? `${r.artist} — ${r.title}` : r.title))) }) }, []) const doSearch = useCallback((q: string) => { if (!q.trim()) return addToSearchHistory(q.trim()) setSearchHistory(getSearchHistory()) loadPlaylist([q.trim()]) setSearch('') window.scrollTo({ top: 0, behavior: 'smooth' }) }, [loadPlaylist]) const handleSearch = (e: React.FormEvent) => { e.preventDefault() doSearch(search) } const handleClearHistory = () => { clearSearchHistory() setSearchHistory([]) } return (
{topTracks === null && (
Загружаем чарт...
)} {topTracks !== null && topTracks.length > 0 && ( )}

Быстрый поиск

setSearch(e.target.value)} className="flex-1 font-sans text-[13px] bg-surface2 border border-white/[0.07] rounded-[9px] px-3 py-2.5 text-app-text outline-none focus:border-accent/35 placeholder:text-muted transition-colors" />
{searchHistory.length > 0 && (
Недавние
{searchHistory.map((q) => ( ))}
)}
) }