feat(search): preserve results and support mouse navigation
This commit is contained in:
1 parent
fbb0b10be3
commit
b6a3f2127b
3 files changed
+78
-21
No files matched your search
@@ -3,6 +3,7 @@
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { Search, Disc3, Music, Mic2, User } from 'lucide-svelte';
|
||||
import AlbumCard from '../AlbumCard.svelte';
|
||||
import { getSearchState, setSearchState, type SearchResults, type SearchTab } from '$lib/stores/searchState';
|
||||
|
||||
onMount(() => {
|
||||
console.log('SearchView mounted!');
|
||||
@@ -60,21 +61,16 @@
|
||||
albums_count?: number;
|
||||
}
|
||||
|
||||
interface SearchResults<T> {
|
||||
items: T[];
|
||||
total: number;
|
||||
offset: number;
|
||||
limit: number;
|
||||
}
|
||||
const cachedState = getSearchState<Album, Track, Artist>();
|
||||
|
||||
let query = $state('');
|
||||
let activeTab = $state<'albums' | 'tracks' | 'artists'>('albums');
|
||||
let query = $state(cachedState.query ?? '');
|
||||
let activeTab = $state<SearchTab>(cachedState.activeTab ?? 'albums');
|
||||
let isSearching = $state(false);
|
||||
let searchError = $state<string | null>(null);
|
||||
|
||||
let albumResults = $state<SearchResults<Album> | null>(null);
|
||||
let trackResults = $state<SearchResults<Track> | null>(null);
|
||||
let artistResults = $state<SearchResults<Artist> | null>(null);
|
||||
let albumResults = $state<SearchResults<Album> | null>(cachedState.albumResults ?? null);
|
||||
let trackResults = $state<SearchResults<Track> | null>(cachedState.trackResults ?? null);
|
||||
let artistResults = $state<SearchResults<Artist> | null>(cachedState.artistResults ?? null);
|
||||
|
||||
let searchTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
let isLoadingMore = $state(false);
|
||||
@@ -96,6 +92,16 @@
|
||||
searchTimeout = setTimeout(() => performSearch(), 300);
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
setSearchState<Album, Track, Artist>({
|
||||
query,
|
||||
activeTab,
|
||||
albumResults,
|
||||
trackResults,
|
||||
artistResults
|
||||
});
|
||||
});
|
||||
|
||||
async function performSearch() {
|
||||
if (!query.trim()) return;
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
export type SearchTab = 'albums' | 'tracks' | 'artists';
|
||||
|
||||
export interface SearchResults<T> {
|
||||
items: T[];
|
||||
total: number;
|
||||
offset: number;
|
||||
limit: number;
|
||||
}
|
||||
|
||||
export interface SearchState<Album, Track, Artist> {
|
||||
query: string;
|
||||
activeTab: SearchTab;
|
||||
albumResults: SearchResults<Album> | null;
|
||||
trackResults: SearchResults<Track> | null;
|
||||
artistResults: SearchResults<Artist> | null;
|
||||
}
|
||||
|
||||
let searchState: SearchState<unknown, unknown, unknown> = {
|
||||
query: '',
|
||||
activeTab: 'albums',
|
||||
albumResults: null,
|
||||
trackResults: null,
|
||||
artistResults: null,
|
||||
};
|
||||
|
||||
export function getSearchState<Album, Track, Artist>(): SearchState<Album, Track, Artist> {
|
||||
return searchState as SearchState<Album, Track, Artist>;
|
||||
}
|
||||
|
||||
export function setSearchState<Album, Track, Artist>(next: SearchState<Album, Track, Artist>): void {
|
||||
searchState = next as SearchState<unknown, unknown, unknown>;
|
||||
}
|
||||
+29
-10
@@ -167,6 +167,7 @@
|
||||
type ViewType = 'home' | 'search' | 'library' | 'settings' | 'album' | 'artist' | 'playlist' | 'favorites';
|
||||
let activeView = $state<ViewType>('home');
|
||||
let viewHistory = $state<ViewType[]>(['home']);
|
||||
let forwardHistory = $state<ViewType[]>([]);
|
||||
let selectedAlbum = $state<AlbumDetail | null>(null);
|
||||
let selectedArtist = $state<ArtistDetail | null>(null);
|
||||
let selectedPlaylistId = $state<number | null>(null);
|
||||
@@ -211,6 +212,7 @@
|
||||
const typedView = view as ViewType;
|
||||
if (typedView !== activeView) {
|
||||
viewHistory = [...viewHistory, typedView];
|
||||
forwardHistory = [];
|
||||
activeView = typedView;
|
||||
console.log('View changed to:', activeView);
|
||||
} else {
|
||||
@@ -220,17 +222,19 @@
|
||||
|
||||
function goBack() {
|
||||
if (viewHistory.length > 1) {
|
||||
const lastView = viewHistory[viewHistory.length - 1];
|
||||
viewHistory = viewHistory.slice(0, -1);
|
||||
forwardHistory = [...forwardHistory, lastView];
|
||||
activeView = viewHistory[viewHistory.length - 1];
|
||||
if (activeView !== 'album') {
|
||||
selectedAlbum = null;
|
||||
}
|
||||
if (activeView !== 'artist') {
|
||||
selectedArtist = null;
|
||||
}
|
||||
if (activeView !== 'playlist') {
|
||||
selectedPlaylistId = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function goForward() {
|
||||
if (forwardHistory.length > 0) {
|
||||
const nextView = forwardHistory[forwardHistory.length - 1];
|
||||
forwardHistory = forwardHistory.slice(0, -1);
|
||||
viewHistory = [...viewHistory, nextView];
|
||||
activeView = nextView;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1149,8 +1153,23 @@
|
||||
document.documentElement.setAttribute('data-theme', savedTheme);
|
||||
}
|
||||
|
||||
const handleMouseNavigation = (event: MouseEvent) => {
|
||||
if (event.button === 3) {
|
||||
event.preventDefault();
|
||||
goBack();
|
||||
} else if (event.button === 4) {
|
||||
event.preventDefault();
|
||||
goForward();
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('keydown', handleKeydown);
|
||||
return () => document.removeEventListener('keydown', handleKeydown);
|
||||
window.addEventListener('mouseup', handleMouseNavigation);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('keydown', handleKeydown);
|
||||
window.removeEventListener('mouseup', handleMouseNavigation);
|
||||
};
|
||||
});
|
||||
|
||||
// Sync queue state when opening queue panel
|
||||
|
||||
Reference in new issue
Block a user