unified library options (#168)

* Unified the way options are being handled. now everything uses the LibraryOptionsContext, removed the no-longer used useLibraryOptions hook

* removed query from library options. since it doesn't make sense for this to be cached

* fixed a bug in the useLocalStorage implementation
This commit is contained in:
amr
2022-04-07 12:53:14 +02:00
committed by GitHub
parent ee9c3d8c69
commit 13e6456190
7 changed files with 92 additions and 90 deletions

View File

@@ -1,51 +0,0 @@
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { BooleanParam, useQueryParam, StringParam } from 'use-query-params';
interface IUseLibraryOptions {
downloaded: NullAndUndefined<boolean>
setDownloaded: (downloaded: NullAndUndefined<boolean>) => void
unread: NullAndUndefined<boolean>
setUnread: (unread: NullAndUndefined<boolean>) => void
query: NullAndUndefined<string>
setQuery: (query: NullAndUndefined<string>) => void
active: boolean
activeSort: boolean
sorts: NullAndUndefined<string>
setSorts: (sorts: NullAndUndefined<string>) => void
sortDesc: NullAndUndefined<boolean>
setSortDesc: (sortDesc: NullAndUndefined<boolean>) => void
}
export default function useLibraryOptions(): IUseLibraryOptions {
const [downloaded, setDownloaded] = useQueryParam('downloaded', BooleanParam);
const [unread, setUnread] = useQueryParam('unread', BooleanParam);
const [query, setQuery] = useQueryParam('query', StringParam);
const [sorts, setSorts] = useQueryParam('sorts', StringParam);
const [sortDesc, setSortDesc] = useQueryParam('sortDesc', BooleanParam);
// eslint-disable-next-line eqeqeq
const active = !(unread == undefined) || !(downloaded == undefined);
// eslint-disable-next-line eqeqeq
const activeSort = (sortDesc != undefined) || (sorts != undefined);
return {
downloaded,
setDownloaded,
unread,
setUnread,
query,
setQuery,
active,
activeSort,
sorts,
setSorts,
sortDesc,
setSortDesc,
};
}

View File

@@ -11,6 +11,7 @@ import React, {
SetStateAction,
useReducer,
Reducer,
useCallback,
} from 'react';
import storage from './localStorage';
@@ -24,12 +25,16 @@ export default function useLocalStorage<T>(
storage.getItem(key, initialState),
);
const setValue = ((value: T | ((prevState: T) => T)) => {
// Allow value to be a function so we have same API as useState
const valueToStore = value instanceof Function ? value(storedValue) : value;
setStoredValue(valueToStore);
storage.setItem(key, valueToStore);
}) as React.Dispatch<React.SetStateAction<T>>;
const setValue = useCallback<React.Dispatch<React.SetStateAction<T>>>(
((value) => {
setStoredValue((prevValue) => {
// Allow value to be a function so we have same API as useState
const valueToStore = value instanceof Function ? value(prevValue) : value;
storage.setItem(key, valueToStore);
return valueToStore;
});
}), [key],
);
return [storedValue, setValue];
}