mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-22 06:25:41 +00:00
93b724fc65
Two coupled UX fixes around the header live search and the global context menu, born from the same testing pass. LiveSearch / MobileSearchOverlay - Single click on a song no longer calls playTrack(track) without a queue argument. The store fell back to the existing queue, didn't find the new track, and ended up playing something the user couldn't navigate or see in the queue list — the player header showed metadata but the queue itself didn't contain it. - New behaviour: enqueue([track]) + "Added to queue" toast. Whatever was playing keeps playing; the new track lands at the bottom and is visible/navigable. Mobile gets the same behaviour (tap-only). - Desktop also gets a right-click context menu on song rows for users who want immediate playback or different routing (Play Now, Play Next, Add to Playlist, etc.). The dropdown stays open while the CM is up, and the row picks up the .context-active highlight so the user can see which song the menu refers to. ContextMenu - Removed the transparent fullscreen backdrop (z-index 998) that previously caught outside clicks. It also blocked right-clicks from reaching elements *underneath* it — so right-clicking a different song row to reposition the menu hit the backdrop instead, closed the menu, and never opened a new one for the row the user actually pointed at. - Replaced with a document-level mousedown listener (gated on `contextMenu.isOpen`) that closes the menu when the click lands outside `menuRef`. Standard outside-click pattern, doesn't occlude the underlying UI, and right-clicking another row now naturally pivots the CM to that row. i18n: new search.addedToQueueToast key in 8 locales. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
263 lines
10 KiB
TypeScript
263 lines
10 KiB
TypeScript
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
|
import { createPortal } from 'react-dom';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { X, Search, Disc3, Users, Music, Music2, Clock, ChevronRight } from 'lucide-react';
|
|
import { search, SearchResults, buildCoverArtUrl, coverArtCacheKey } from '../api/subsonic';
|
|
import { usePlayerStore, songToTrack } from '../store/playerStore';
|
|
import { useAuthStore } from '../store/authStore';
|
|
import { useTranslation } from 'react-i18next';
|
|
import CachedImage from './CachedImage';
|
|
import { showToast } from '../utils/toast';
|
|
|
|
const STORAGE_KEY = 'psysonic_recent_searches';
|
|
const MAX_RECENT = 6;
|
|
|
|
function loadRecent(): string[] {
|
|
try { return JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]'); } catch { return []; }
|
|
}
|
|
|
|
function saveRecent(q: string, prev: string[]): string[] {
|
|
const updated = [q.trim(), ...prev.filter(s => s !== q.trim())].slice(0, MAX_RECENT);
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(updated));
|
|
return updated;
|
|
}
|
|
|
|
function debounce(fn: (q: string) => void, ms: number): (q: string) => void {
|
|
let timer: ReturnType<typeof setTimeout>;
|
|
return (q: string) => { clearTimeout(timer); timer = setTimeout(() => fn(q), ms); };
|
|
}
|
|
|
|
export default function MobileSearchOverlay({ onClose }: { onClose: () => void }) {
|
|
const { t } = useTranslation();
|
|
const navigate = useNavigate();
|
|
const enqueue = usePlayerStore(s => s.enqueue);
|
|
|
|
const [query, setQuery] = useState('');
|
|
const [results, setResults] = useState<SearchResults | null>(null);
|
|
const [loading, setLoading] = useState(false);
|
|
const [recentSearches, setRecentSearches] = useState<string[]>(loadRecent);
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
const musicLibraryFilterVersion = useAuthStore(s => s.musicLibraryFilterVersion);
|
|
|
|
useEffect(() => { inputRef.current?.focus(); }, []);
|
|
|
|
useEffect(() => {
|
|
const prev = document.body.style.overflow;
|
|
document.body.style.overflow = 'hidden';
|
|
return () => { document.body.style.overflow = prev; };
|
|
}, []);
|
|
|
|
const doSearch = useCallback(
|
|
debounce(async (q: string) => {
|
|
if (!q.trim()) { setResults(null); setLoading(false); return; }
|
|
setLoading(true);
|
|
try { setResults(await search(q)); }
|
|
finally { setLoading(false); }
|
|
}, 300),
|
|
[musicLibraryFilterVersion]
|
|
);
|
|
|
|
useEffect(() => { doSearch(query); }, [query, doSearch]);
|
|
|
|
const commit = (q: string) => {
|
|
if (q.trim()) setRecentSearches(prev => saveRecent(q, prev));
|
|
};
|
|
|
|
const goTo = (path: string) => { commit(query); navigate(path); onClose(); };
|
|
const goCategory = (path: string) => { navigate(path); onClose(); };
|
|
const enqueueSong = (song: SearchResults['songs'][number]) => {
|
|
commit(query);
|
|
const track = songToTrack(song);
|
|
enqueue([track]);
|
|
showToast(t('search.addedToQueueToast', { title: track.title }), 2200, 'info');
|
|
onClose();
|
|
};
|
|
const useRecent = (term: string) => {
|
|
setQuery(term);
|
|
inputRef.current?.focus();
|
|
};
|
|
const removeRecent = (term: string, e: React.MouseEvent) => {
|
|
e.stopPropagation();
|
|
setRecentSearches(prev => {
|
|
const updated = prev.filter(s => s !== term);
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(updated));
|
|
return updated;
|
|
});
|
|
};
|
|
|
|
const hasResults = results && (results.artists.length || results.albums.length || results.songs.length);
|
|
const showEmpty = !query;
|
|
|
|
return createPortal(
|
|
<div className="mobile-search-overlay">
|
|
{/* ── Search bar ── */}
|
|
<div className="mobile-search-bar">
|
|
<div className="mobile-search-field">
|
|
{loading ? (
|
|
<div className="mobile-search-spinner" />
|
|
) : (
|
|
<Search size={16} className="mobile-search-icon" />
|
|
)}
|
|
<input
|
|
ref={inputRef}
|
|
className="mobile-search-input"
|
|
type="search"
|
|
placeholder={t('search.placeholder')}
|
|
value={query}
|
|
onChange={e => setQuery(e.target.value)}
|
|
autoComplete="off"
|
|
autoCorrect="off"
|
|
autoCapitalize="off"
|
|
/>
|
|
{query && (
|
|
<button
|
|
className="mobile-search-clear"
|
|
onClick={() => { setQuery(''); setResults(null); inputRef.current?.focus(); }}
|
|
aria-label={t('search.clearLabel')}
|
|
>
|
|
<X size={15} />
|
|
</button>
|
|
)}
|
|
</div>
|
|
<button className="mobile-search-cancel" onClick={onClose}>
|
|
{t('common.cancel')}
|
|
</button>
|
|
</div>
|
|
|
|
<div className="mobile-search-results">
|
|
{/* ── Empty state ── */}
|
|
{showEmpty && (
|
|
<div className="mobile-search-empty-state">
|
|
{recentSearches.length > 0 && (
|
|
<div className="mobile-search-section">
|
|
<div className="mobile-search-section-label">{t('search.recentSearches')}</div>
|
|
{recentSearches.map(term => (
|
|
<button key={term} className="mobile-search-item" onClick={() => useRecent(term)}>
|
|
<div className="mobile-search-avatar">
|
|
<Clock size={18} />
|
|
</div>
|
|
<div className="mobile-search-item-info" style={{ flex: 1 }}>
|
|
<span className="mobile-search-item-title">{term}</span>
|
|
</div>
|
|
<button
|
|
className="mobile-search-recent-remove"
|
|
onClick={e => removeRecent(term, e)}
|
|
aria-label={t('search.clearLabel')}
|
|
>
|
|
<X size={14} />
|
|
</button>
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
<div className="mobile-search-section">
|
|
<div className="mobile-search-section-label">{t('search.browse')}</div>
|
|
<div className="mobile-search-chips">
|
|
<button className="mobile-search-chip" onClick={() => goCategory('/albums')}>
|
|
<Music2 size={15} /> {t('search.albums')}
|
|
</button>
|
|
<button className="mobile-search-chip" onClick={() => goCategory('/artists')}>
|
|
<Users size={15} /> {t('search.artists')}
|
|
</button>
|
|
<button className="mobile-search-chip" onClick={() => goCategory('/genres')}>
|
|
<Music size={15} /> {t('search.genres')}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="mobile-search-hint">
|
|
<Search size={52} className="mobile-search-hint-icon" />
|
|
<span className="mobile-search-hint-text">{t('search.emptyHint')}</span>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* ── No results ── */}
|
|
{!loading && query && !hasResults && (
|
|
<div className="mobile-search-noresults">
|
|
{t('search.noResults', { query })}
|
|
</div>
|
|
)}
|
|
|
|
{/* ── Results ── */}
|
|
{hasResults && (
|
|
<>
|
|
{results!.artists.length > 0 && (
|
|
<div className="mobile-search-section">
|
|
<div className="mobile-search-section-label">{t('search.artists')}</div>
|
|
{results!.artists.map(a => (
|
|
<button key={a.id} className="mobile-search-item" onClick={() => goTo(`/artist/${a.id}`)}>
|
|
<div className="mobile-search-avatar mobile-search-avatar--circle">
|
|
<Users size={20} />
|
|
</div>
|
|
<div className="mobile-search-item-info">
|
|
<span className="mobile-search-item-title">{a.name}</span>
|
|
<span className="mobile-search-item-sub">{t('search.artists')}</span>
|
|
</div>
|
|
<ChevronRight size={16} className="mobile-search-item-chevron" />
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
{results!.albums.length > 0 && (
|
|
<div className="mobile-search-section">
|
|
<div className="mobile-search-section-label">{t('search.albums')}</div>
|
|
{results!.albums.map(a => (
|
|
<button key={a.id} className="mobile-search-item" onClick={() => goTo(`/album/${a.id}`)}>
|
|
{a.coverArt ? (
|
|
<CachedImage
|
|
className="mobile-search-thumb"
|
|
src={buildCoverArtUrl(a.coverArt, 80)}
|
|
cacheKey={coverArtCacheKey(a.coverArt, 80)}
|
|
alt=""
|
|
/>
|
|
) : (
|
|
<div className="mobile-search-avatar">
|
|
<Disc3 size={20} />
|
|
</div>
|
|
)}
|
|
<div className="mobile-search-item-info">
|
|
<span className="mobile-search-item-title">{a.name}</span>
|
|
<span className="mobile-search-item-sub">{a.artist}</span>
|
|
</div>
|
|
<ChevronRight size={16} className="mobile-search-item-chevron" />
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
{results!.songs.length > 0 && (
|
|
<div className="mobile-search-section">
|
|
<div className="mobile-search-section-label">{t('search.songs')}</div>
|
|
{results!.songs.map(s => (
|
|
<button key={s.id} className="mobile-search-item" onClick={() => enqueueSong(s)}>
|
|
{s.coverArt ? (
|
|
<CachedImage
|
|
className="mobile-search-thumb"
|
|
src={buildCoverArtUrl(s.coverArt, 80)}
|
|
cacheKey={coverArtCacheKey(s.coverArt, 80)}
|
|
alt=""
|
|
/>
|
|
) : (
|
|
<div className="mobile-search-avatar">
|
|
<Music size={20} />
|
|
</div>
|
|
)}
|
|
<div className="mobile-search-item-info">
|
|
<span className="mobile-search-item-title">{s.title}</span>
|
|
<span className="mobile-search-item-sub">{s.artist}{s.album ? ` · ${s.album}` : ''}</span>
|
|
</div>
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>,
|
|
document.body
|
|
);
|
|
}
|