more fixes to list filters

This commit is contained in:
jeffvli
2025-12-02 00:43:36 -08:00
parent 4889f2b51a
commit 65f24c2c03
3 changed files with 57 additions and 56 deletions
@@ -155,23 +155,28 @@ export const useAlbumListFilters = () => {
const setCustom = useCallback( const setCustom = useCallback(
(value: null | Record<string, any>) => { (value: null | Record<string, any>) => {
setSearchParams((prev) => { setSearchParams(
const previousValue = prev.get(FILTER_KEYS.ALBUM._CUSTOM); (prev) => {
const previousValue = prev.get(FILTER_KEYS.ALBUM._CUSTOM);
const newCustom = { const newCustom = {
...(previousValue ? JSON.parse(previousValue) : {}), ...(previousValue ? JSON.parse(previousValue) : {}),
...value, ...value,
}; };
const filteredNewCustom = Object.fromEntries( const filteredNewCustom = Object.fromEntries(
Object.entries(newCustom).filter( Object.entries(newCustom).filter(
([, value]) => value !== null && value !== undefined, ([, value]) => value !== null && value !== undefined,
), ),
); );
prev.set(FILTER_KEYS.ALBUM._CUSTOM, JSON.stringify(filteredNewCustom)); prev.set(FILTER_KEYS.ALBUM._CUSTOM, JSON.stringify(filteredNewCustom));
return prev; return prev;
}); },
{
replace: true,
},
);
}, },
[setSearchParams], [setSearchParams],
); );
@@ -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 { useSortByFilter } from '/@/renderer/features/shared/hooks/use-sort-by-filter';
import { useSortOrderFilter } from '/@/renderer/features/shared/hooks/use-sort-order-filter'; import { useSortOrderFilter } from '/@/renderer/features/shared/hooks/use-sort-order-filter';
import { FILTER_KEYS } from '/@/renderer/features/shared/utils'; 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 { PlaylistListSort } from '/@/shared/types/domain-types';
import { ItemListKey } from '/@/shared/types/types'; import { ItemListKey } from '/@/shared/types/types';
@@ -23,31 +23,28 @@ export const usePlaylistListFilters = () => {
); );
const setCustom = useCallback( const setCustom = useCallback(
( (value: null | Record<string, any>) => {
value:
| ((prev: null | Record<string, any>) => null | Record<string, any>)
| null
| Record<string, any>,
) => {
setSearchParams( setSearchParams(
(prev) => { (prev) => {
const currentCustom = parseCustomFiltersParam( const previousValue = prev.get(FILTER_KEYS.ALBUM._CUSTOM);
prev,
FILTER_KEYS.PLAYLIST.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; prev.set(FILTER_KEYS.ALBUM._CUSTOM, JSON.stringify(filteredNewCustom));
// Convert empty objects to null to clear them from URL return prev;
if ( },
newValue && {
typeof newValue === 'object' && replace: true,
Object.keys(newValue).length === 0
) {
newValue = null;
}
return setJsonSearchParam(prev, FILTER_KEYS.PLAYLIST.CUSTOM, newValue);
}, },
{ replace: true },
); );
}, },
[setSearchParams], [setSearchParams],
@@ -10,7 +10,6 @@ import {
parseBooleanParam, parseBooleanParam,
parseCustomFiltersParam, parseCustomFiltersParam,
parseIntParam, parseIntParam,
setJsonSearchParam,
setSearchParam, setSearchParam,
} from '/@/renderer/utils/query-params'; } from '/@/renderer/utils/query-params';
import { SongListSort, SortOrder } from '/@/shared/types/domain-types'; import { SongListSort, SortOrder } from '/@/shared/types/domain-types';
@@ -115,28 +114,28 @@ export const usePlaylistSongListFilters = () => {
); );
const setCustom = useCallback( const setCustom = useCallback(
( (value: null | Record<string, any>) => {
value:
| ((prev: null | Record<string, any>) => null | Record<string, any>)
| null
| Record<string, any>,
) => {
setSearchParams( setSearchParams(
(prev) => { (prev) => {
const currentCustom = parseCustomFiltersParam(prev, FILTER_KEYS.SONG._CUSTOM); const previousValue = prev.get(FILTER_KEYS.ALBUM._CUSTOM);
let newValue =
typeof value === 'function' ? value(currentCustom ?? null) : value; const newCustom = {
// Convert empty objects to null to clear them from URL ...(previousValue ? JSON.parse(previousValue) : {}),
if ( ...value,
newValue && };
typeof newValue === 'object' &&
Object.keys(newValue).length === 0 const filteredNewCustom = Object.fromEntries(
) { Object.entries(newCustom).filter(
newValue = null; ([, value]) => value !== null && value !== undefined,
} ),
return setJsonSearchParam(prev, FILTER_KEYS.SONG._CUSTOM, newValue); );
prev.set(FILTER_KEYS.ALBUM._CUSTOM, JSON.stringify(filteredNewCustom));
return prev;
},
{
replace: true,
}, },
{ replace: true },
); );
}, },
[setSearchParams], [setSearchParams],