From 65f24c2c03dbcce708bdde9539d3752e6bb05159 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Tue, 2 Dec 2025 00:43:36 -0800 Subject: [PATCH] more fixes to list filters --- .../albums/hooks/use-album-list-filters.ts | 33 ++++++++------- .../hooks/use-playlist-list-filters.ts | 41 +++++++++---------- .../hooks/use-playlist-song-list-filters.ts | 39 +++++++++--------- 3 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/renderer/features/albums/hooks/use-album-list-filters.ts b/src/renderer/features/albums/hooks/use-album-list-filters.ts index a37184e48..6c4a6333b 100644 --- a/src/renderer/features/albums/hooks/use-album-list-filters.ts +++ b/src/renderer/features/albums/hooks/use-album-list-filters.ts @@ -155,23 +155,28 @@ export const useAlbumListFilters = () => { const setCustom = useCallback( (value: null | Record) => { - setSearchParams((prev) => { - const previousValue = prev.get(FILTER_KEYS.ALBUM._CUSTOM); + setSearchParams( + (prev) => { + const previousValue = prev.get(FILTER_KEYS.ALBUM._CUSTOM); - const newCustom = { - ...(previousValue ? JSON.parse(previousValue) : {}), - ...value, - }; + const newCustom = { + ...(previousValue ? JSON.parse(previousValue) : {}), + ...value, + }; - const filteredNewCustom = Object.fromEntries( - Object.entries(newCustom).filter( - ([, value]) => value !== null && value !== undefined, - ), - ); + const filteredNewCustom = Object.fromEntries( + Object.entries(newCustom).filter( + ([, value]) => value !== null && value !== undefined, + ), + ); - prev.set(FILTER_KEYS.ALBUM._CUSTOM, JSON.stringify(filteredNewCustom)); - return prev; - }); + prev.set(FILTER_KEYS.ALBUM._CUSTOM, JSON.stringify(filteredNewCustom)); + return prev; + }, + { + replace: true, + }, + ); }, [setSearchParams], ); diff --git a/src/renderer/features/playlists/hooks/use-playlist-list-filters.ts b/src/renderer/features/playlists/hooks/use-playlist-list-filters.ts index 57674ed7d..f07302dbe 100644 --- a/src/renderer/features/playlists/hooks/use-playlist-list-filters.ts +++ b/src/renderer/features/playlists/hooks/use-playlist-list-filters.ts @@ -5,7 +5,7 @@ import { useSearchTermFilter } from '/@/renderer/features/shared/hooks/use-searc import { useSortByFilter } from '/@/renderer/features/shared/hooks/use-sort-by-filter'; import { useSortOrderFilter } from '/@/renderer/features/shared/hooks/use-sort-order-filter'; import { FILTER_KEYS } from '/@/renderer/features/shared/utils'; -import { parseCustomFiltersParam, setJsonSearchParam } from '/@/renderer/utils/query-params'; +import { parseCustomFiltersParam } from '/@/renderer/utils/query-params'; import { PlaylistListSort } from '/@/shared/types/domain-types'; import { ItemListKey } from '/@/shared/types/types'; @@ -23,31 +23,28 @@ export const usePlaylistListFilters = () => { ); const setCustom = useCallback( - ( - value: - | ((prev: null | Record) => null | Record) - | null - | Record, - ) => { + (value: null | Record) => { setSearchParams( (prev) => { - const currentCustom = parseCustomFiltersParam( - prev, - FILTER_KEYS.PLAYLIST.CUSTOM, + const previousValue = prev.get(FILTER_KEYS.ALBUM._CUSTOM); + + const newCustom = { + ...(previousValue ? JSON.parse(previousValue) : {}), + ...value, + }; + + const filteredNewCustom = Object.fromEntries( + Object.entries(newCustom).filter( + ([, value]) => value !== null && value !== undefined, + ), ); - let newValue = - typeof value === 'function' ? value(currentCustom ?? null) : value; - // Convert empty objects to null to clear them from URL - if ( - newValue && - typeof newValue === 'object' && - Object.keys(newValue).length === 0 - ) { - newValue = null; - } - return setJsonSearchParam(prev, FILTER_KEYS.PLAYLIST.CUSTOM, newValue); + + prev.set(FILTER_KEYS.ALBUM._CUSTOM, JSON.stringify(filteredNewCustom)); + return prev; + }, + { + replace: true, }, - { replace: true }, ); }, [setSearchParams], diff --git a/src/renderer/features/playlists/hooks/use-playlist-song-list-filters.ts b/src/renderer/features/playlists/hooks/use-playlist-song-list-filters.ts index 4d4b21323..767d245d1 100644 --- a/src/renderer/features/playlists/hooks/use-playlist-song-list-filters.ts +++ b/src/renderer/features/playlists/hooks/use-playlist-song-list-filters.ts @@ -10,7 +10,6 @@ import { parseBooleanParam, parseCustomFiltersParam, parseIntParam, - setJsonSearchParam, setSearchParam, } from '/@/renderer/utils/query-params'; import { SongListSort, SortOrder } from '/@/shared/types/domain-types'; @@ -115,28 +114,28 @@ export const usePlaylistSongListFilters = () => { ); const setCustom = useCallback( - ( - value: - | ((prev: null | Record) => null | Record) - | null - | Record, - ) => { + (value: null | Record) => { setSearchParams( (prev) => { - const currentCustom = parseCustomFiltersParam(prev, FILTER_KEYS.SONG._CUSTOM); - let newValue = - typeof value === 'function' ? value(currentCustom ?? null) : value; - // Convert empty objects to null to clear them from URL - if ( - newValue && - typeof newValue === 'object' && - Object.keys(newValue).length === 0 - ) { - newValue = null; - } - return setJsonSearchParam(prev, FILTER_KEYS.SONG._CUSTOM, newValue); + const previousValue = prev.get(FILTER_KEYS.ALBUM._CUSTOM); + + const newCustom = { + ...(previousValue ? JSON.parse(previousValue) : {}), + ...value, + }; + + const filteredNewCustom = Object.fromEntries( + Object.entries(newCustom).filter( + ([, value]) => value !== null && value !== undefined, + ), + ); + + prev.set(FILTER_KEYS.ALBUM._CUSTOM, JSON.stringify(filteredNewCustom)); + return prev; + }, + { + replace: true, }, - { replace: true }, ); }, [setSearchParams],