mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-22 15:25:46 +00:00
refactor(search): co-locate search feature into features/search
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it, beforeEach } from 'vitest';
|
||||
import { useLiveSearchScopeStore } from '@/store/liveSearchScopeStore';
|
||||
import { syncLiveSearchRouteScope } from '@/features/search/hooks/useLiveSearchRouteScope';
|
||||
|
||||
describe('syncLiveSearchRouteScope', () => {
|
||||
beforeEach(() => {
|
||||
useLiveSearchScopeStore.setState({ query: '', scope: null, undoStack: [] });
|
||||
});
|
||||
|
||||
it('activates scope on supported browse routes', () => {
|
||||
syncLiveSearchRouteScope('/albums');
|
||||
expect(useLiveSearchScopeStore.getState().scope).toBe('albums');
|
||||
|
||||
syncLiveSearchRouteScope('/tracks');
|
||||
expect(useLiveSearchScopeStore.getState().scope).toBe('tracks');
|
||||
|
||||
syncLiveSearchRouteScope('/composers');
|
||||
expect(useLiveSearchScopeStore.getState().scope).toBe('composers');
|
||||
});
|
||||
|
||||
it('clears scope and query when leaving browse routes', () => {
|
||||
useLiveSearchScopeStore.setState({ query: 'beatles', scope: 'albums' });
|
||||
|
||||
syncLiveSearchRouteScope('/album/abc123');
|
||||
|
||||
expect(useLiveSearchScopeStore.getState().scope).toBeNull();
|
||||
expect(useLiveSearchScopeStore.getState().query).toBe('');
|
||||
});
|
||||
|
||||
it('clears query when leaving browse with scope already cleared (ghost mode)', () => {
|
||||
useLiveSearchScopeStore.setState({ query: 'beatles', scope: null });
|
||||
|
||||
syncLiveSearchRouteScope('/album/abc123');
|
||||
|
||||
expect(useLiveSearchScopeStore.getState().scope).toBeNull();
|
||||
expect(useLiveSearchScopeStore.getState().query).toBe('');
|
||||
});
|
||||
|
||||
it('preserves query when switching between browse routes', () => {
|
||||
useLiveSearchScopeStore.setState({ query: 'jazz', scope: 'albums' });
|
||||
|
||||
syncLiveSearchRouteScope('/artists');
|
||||
|
||||
expect(useLiveSearchScopeStore.getState().scope).toBe('artists');
|
||||
expect(useLiveSearchScopeStore.getState().query).toBe('jazz');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { isAlbumsBrowsePath, isNewReleasesBrowsePath } from '@/store/albumBrowseSessionStore';
|
||||
import { isArtistsBrowsePath } from '@/store/artistBrowseSessionStore';
|
||||
import { isTracksBrowsePath } from '@/store/advancedSearchSessionStore';
|
||||
import { isComposersBrowsePath } from '@/store/composerBrowseSessionStore';
|
||||
import { useLiveSearchScopeStore } from '@/store/liveSearchScopeStore';
|
||||
|
||||
/** Keep scope badge in sync with browse routes; clear field text when leaving browse. */
|
||||
export function syncLiveSearchRouteScope(pathname: string): void {
|
||||
const store = useLiveSearchScopeStore.getState();
|
||||
|
||||
if (isArtistsBrowsePath(pathname)) {
|
||||
store.setScope('artists');
|
||||
} else if (isAlbumsBrowsePath(pathname)) {
|
||||
store.setScope('albums');
|
||||
} else if (isNewReleasesBrowsePath(pathname)) {
|
||||
store.setScope('newReleases');
|
||||
} else if (isTracksBrowsePath(pathname)) {
|
||||
store.setScope('tracks');
|
||||
} else if (isComposersBrowsePath(pathname)) {
|
||||
store.setScope('composers');
|
||||
} else {
|
||||
if (store.scope != null) store.clearScope();
|
||||
if (store.query !== '') store.setQuery('');
|
||||
}
|
||||
}
|
||||
|
||||
/** Activate the browse scope badge when a supported route is open; clear on leave. */
|
||||
export function useLiveSearchRouteScope() {
|
||||
const location = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
syncLiveSearchRouteScope(location.pathname);
|
||||
}, [location.pathname]);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import type { SubsonicSong } from '@/api/subsonicTypes';
|
||||
import {
|
||||
resolveShareSearchPayload,
|
||||
type ShareSearchResolveResult,
|
||||
} from '@/utils/share/enqueueShareSearchPayload';
|
||||
import type { QueueableShareSearchPayload } from '@/utils/share/shareSearch';
|
||||
|
||||
export type ShareQueuePreviewState =
|
||||
| { status: 'idle' }
|
||||
| { status: 'loading' }
|
||||
| { status: 'ok'; songs: SubsonicSong[]; total: number; skipped: number }
|
||||
| { status: 'error'; result: Exclude<ShareSearchResolveResult, { type: 'ok' }> };
|
||||
|
||||
const IDLE: ShareQueuePreviewState = { status: 'idle' };
|
||||
|
||||
export function useShareQueuePreview(
|
||||
payload: Extract<QueueableShareSearchPayload, { k: 'queue' }> | null,
|
||||
open: boolean,
|
||||
): ShareQueuePreviewState {
|
||||
const [state, setState] = useState<ShareQueuePreviewState>(IDLE);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || !payload) {
|
||||
// React Compiler set-state-in-effect rule: state set from an async result resolved in this effect.
|
||||
// eslint-disable-next-line react-hooks/set-state-in-effect
|
||||
setState(IDLE);
|
||||
return;
|
||||
}
|
||||
|
||||
let cancelled = false;
|
||||
setState({ status: 'loading' });
|
||||
|
||||
void resolveShareSearchPayload(payload)
|
||||
.then(result => {
|
||||
if (cancelled) return;
|
||||
if (result.type === 'ok') {
|
||||
setState({
|
||||
status: 'ok',
|
||||
songs: result.songs,
|
||||
total: result.total,
|
||||
skipped: result.skipped,
|
||||
});
|
||||
return;
|
||||
}
|
||||
setState({ status: 'error', result });
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setState({ status: 'error', result: { type: 'error' } });
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [open, payload]);
|
||||
|
||||
return state;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useNavigateToAlbum } from '@/hooks/useNavigateToAlbum';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useAuthStore } from '@/store/authStore';
|
||||
import {
|
||||
activateShareSearchServer,
|
||||
enqueueShareSearchPayload,
|
||||
} from '@/utils/share/enqueueShareSearchPayload';
|
||||
import type { ServerProfile } from '@/store/authStoreTypes';
|
||||
import { findServerIdForShareUrl } from '@/utils/share/shareLink';
|
||||
import { shareServerOriginLabel } from '@/utils/share/shareServerOriginLabel';
|
||||
import { parseShareSearchText } from '@/utils/share/shareSearch';
|
||||
import { serverIndexKeyFromUrl } from '@/utils/server/serverIndexKey';
|
||||
import { useShareSearchPreview } from '@/features/search/hooks/useShareSearchPreview';
|
||||
|
||||
export function useShareSearch(query: string, onSuccess?: () => void) {
|
||||
const { t } = useTranslation();
|
||||
const navigate = useNavigate();
|
||||
const navigateToAlbum = useNavigateToAlbum();
|
||||
const servers = useAuthStore(s => s.servers);
|
||||
const activeServerId = useAuthStore(s => s.activeServerId);
|
||||
const shareMatch = useMemo(() => parseShareSearchText(query), [query]);
|
||||
const shareServerLabel = useMemo(
|
||||
() => shareServerOriginLabel(shareMatch, servers, activeServerId),
|
||||
[shareMatch, servers, activeServerId],
|
||||
);
|
||||
const shareCoverServer = useMemo((): ServerProfile | null => {
|
||||
if (!shareMatch || shareMatch.type === 'unsupported') return null;
|
||||
const serverId = findServerIdForShareUrl(servers, shareMatch.payload.srv);
|
||||
if (!serverId || serverId === activeServerId) return null;
|
||||
return servers.find(s => s.id === serverId)
|
||||
?? servers.find(s => serverIndexKeyFromUrl(s.url) === serverId)
|
||||
?? null;
|
||||
}, [shareMatch, servers, activeServerId]);
|
||||
const preview = useShareSearchPreview(shareMatch);
|
||||
const [shareQueueBusy, setShareQueueBusy] = useState(false);
|
||||
|
||||
const canQueueShareMatch =
|
||||
shareMatch?.type === 'queueable' &&
|
||||
(shareMatch.payload.k === 'queue' ||
|
||||
(!preview.shareTrackResolving && !!preview.shareTrackSong));
|
||||
|
||||
const canOpenShareAlbum =
|
||||
shareMatch?.type === 'album' && !!preview.shareAlbum && !preview.shareAlbumResolving;
|
||||
const canOpenShareArtist =
|
||||
shareMatch?.type === 'artist' && !!preview.shareArtist && !preview.shareArtistResolving;
|
||||
const canOpenShareComposer =
|
||||
shareMatch?.type === 'composer' && !!preview.shareComposer && !preview.shareComposerResolving;
|
||||
|
||||
const hasShareKeyboardTarget =
|
||||
canQueueShareMatch || canOpenShareAlbum || canOpenShareArtist || canOpenShareComposer;
|
||||
|
||||
const openShareAlbum = useCallback(() => {
|
||||
if (shareMatch?.type !== 'album' || !preview.shareAlbum) return;
|
||||
if (!activateShareSearchServer(shareMatch.payload.srv, t)) return;
|
||||
navigateToAlbum(preview.shareAlbum.id);
|
||||
onSuccess?.();
|
||||
}, [shareMatch, preview.shareAlbum, navigateToAlbum, t, onSuccess]);
|
||||
|
||||
const openShareArtist = useCallback(() => {
|
||||
if (shareMatch?.type !== 'artist' || !preview.shareArtist) return;
|
||||
if (!activateShareSearchServer(shareMatch.payload.srv, t)) return;
|
||||
navigate(`/artist/${preview.shareArtist.id}`);
|
||||
onSuccess?.();
|
||||
}, [shareMatch, preview.shareArtist, navigate, t, onSuccess]);
|
||||
|
||||
const openShareComposer = useCallback(() => {
|
||||
if (shareMatch?.type !== 'composer' || !preview.shareComposer) return;
|
||||
if (!activateShareSearchServer(shareMatch.payload.srv, t)) return;
|
||||
navigate(`/composer/${preview.shareComposer.id}`);
|
||||
onSuccess?.();
|
||||
}, [shareMatch, preview.shareComposer, navigate, t, onSuccess]);
|
||||
|
||||
const enqueueShareMatch = useCallback(async () => {
|
||||
if (shareMatch?.type !== 'queueable' || shareQueueBusy) return false;
|
||||
if (shareMatch.payload.k === 'track' && (!preview.shareTrackSong || preview.shareTrackResolving)) {
|
||||
return false;
|
||||
}
|
||||
setShareQueueBusy(true);
|
||||
const ok = await enqueueShareSearchPayload(shareMatch.payload, t);
|
||||
setShareQueueBusy(false);
|
||||
if (ok) onSuccess?.();
|
||||
return ok;
|
||||
}, [shareMatch, shareQueueBusy, preview.shareTrackSong, preview.shareTrackResolving, t, onSuccess]);
|
||||
|
||||
return {
|
||||
shareMatch,
|
||||
shareServerLabel,
|
||||
shareCoverServer,
|
||||
shareQueueBusy,
|
||||
canQueueShareMatch,
|
||||
canOpenShareAlbum,
|
||||
canOpenShareArtist,
|
||||
canOpenShareComposer,
|
||||
hasShareKeyboardTarget,
|
||||
openShareAlbum,
|
||||
openShareArtist,
|
||||
openShareComposer,
|
||||
enqueueShareMatch,
|
||||
...preview,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import type { SubsonicAlbum, SubsonicArtist, SubsonicSong } from '@/api/subsonicTypes';
|
||||
import {
|
||||
resolveShareSearchAlbum,
|
||||
resolveShareSearchArtist,
|
||||
resolveShareSearchPayload,
|
||||
} from '@/utils/share/enqueueShareSearchPayload';
|
||||
import type { ShareSearchMatch } from '@/utils/share/shareSearch';
|
||||
|
||||
export interface ShareSearchPreviewState {
|
||||
shareTrackSong: SubsonicSong | null;
|
||||
shareTrackResolving: boolean;
|
||||
shareTrackUnavailable: boolean;
|
||||
shareAlbum: SubsonicAlbum | null;
|
||||
shareAlbumResolving: boolean;
|
||||
shareAlbumUnavailable: boolean;
|
||||
shareArtist: SubsonicArtist | null;
|
||||
shareArtistResolving: boolean;
|
||||
shareArtistUnavailable: boolean;
|
||||
shareComposer: SubsonicArtist | null;
|
||||
shareComposerResolving: boolean;
|
||||
shareComposerUnavailable: boolean;
|
||||
}
|
||||
|
||||
const EMPTY_PREVIEW: ShareSearchPreviewState = {
|
||||
shareTrackSong: null,
|
||||
shareTrackResolving: false,
|
||||
shareTrackUnavailable: false,
|
||||
shareAlbum: null,
|
||||
shareAlbumResolving: false,
|
||||
shareAlbumUnavailable: false,
|
||||
shareArtist: null,
|
||||
shareArtistResolving: false,
|
||||
shareArtistUnavailable: false,
|
||||
shareComposer: null,
|
||||
shareComposerResolving: false,
|
||||
shareComposerUnavailable: false,
|
||||
};
|
||||
|
||||
export function useShareSearchPreview(shareMatch: ShareSearchMatch | null): ShareSearchPreviewState {
|
||||
const [preview, setPreview] = useState<ShareSearchPreviewState>(EMPTY_PREVIEW);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
// React Compiler set-state-in-effect rule: state set from an async result resolved in this effect.
|
||||
// eslint-disable-next-line react-hooks/set-state-in-effect
|
||||
setPreview(EMPTY_PREVIEW);
|
||||
|
||||
if (shareMatch?.type === 'queueable' && shareMatch.payload.k === 'track') {
|
||||
setPreview({ ...EMPTY_PREVIEW, shareTrackResolving: true });
|
||||
void resolveShareSearchPayload(shareMatch.payload)
|
||||
.then(result => {
|
||||
if (cancelled) return;
|
||||
setPreview({
|
||||
...EMPTY_PREVIEW,
|
||||
shareTrackSong: result.type === 'ok' ? (result.songs[0] ?? null) : null,
|
||||
shareTrackUnavailable: result.type !== 'ok' || result.songs.length === 0,
|
||||
});
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) {
|
||||
setPreview(current => ({ ...current, shareTrackResolving: false }));
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}
|
||||
|
||||
if (shareMatch?.type === 'artist') {
|
||||
setPreview({ ...EMPTY_PREVIEW, shareArtistResolving: true });
|
||||
void resolveShareSearchArtist(shareMatch.payload)
|
||||
.then(result => {
|
||||
if (cancelled) return;
|
||||
setPreview({
|
||||
...EMPTY_PREVIEW,
|
||||
shareArtist: result.type === 'ok' ? result.artist : null,
|
||||
shareArtistUnavailable: result.type !== 'ok',
|
||||
});
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) {
|
||||
setPreview(current => ({ ...current, shareArtistResolving: false }));
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}
|
||||
|
||||
if (shareMatch?.type === 'composer') {
|
||||
setPreview({ ...EMPTY_PREVIEW, shareComposerResolving: true });
|
||||
void resolveShareSearchArtist(shareMatch.payload)
|
||||
.then(result => {
|
||||
if (cancelled) return;
|
||||
setPreview({
|
||||
...EMPTY_PREVIEW,
|
||||
shareComposer: result.type === 'ok' ? result.artist : null,
|
||||
shareComposerUnavailable: result.type !== 'ok',
|
||||
});
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) {
|
||||
setPreview(current => ({ ...current, shareComposerResolving: false }));
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}
|
||||
|
||||
if (shareMatch?.type === 'album') {
|
||||
setPreview({ ...EMPTY_PREVIEW, shareAlbumResolving: true });
|
||||
void resolveShareSearchAlbum(shareMatch.payload)
|
||||
.then(result => {
|
||||
if (cancelled) return;
|
||||
setPreview({
|
||||
...EMPTY_PREVIEW,
|
||||
shareAlbum: result.type === 'ok' ? result.album : null,
|
||||
shareAlbumUnavailable: result.type !== 'ok',
|
||||
});
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) {
|
||||
setPreview(current => ({ ...current, shareAlbumResolving: false }));
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [shareMatch]);
|
||||
|
||||
return preview;
|
||||
}
|
||||
Reference in New Issue
Block a user