2021-10-29 20:11:23 +02:00
|
|
|
/*
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-31 12:45:34 +01:00
|
|
|
import { BooleanParam, useQueryParams, StringParam } from 'use-query-params';
|
2021-10-29 20:11:23 +02:00
|
|
|
|
|
|
|
|
export type NullAndUndefined<T> = T | null | undefined;
|
|
|
|
|
|
|
|
|
|
interface IUseLibraryOptions {
|
|
|
|
|
unread: NullAndUndefined<boolean>
|
|
|
|
|
setUnread: (unread: NullAndUndefined<boolean>) => void
|
2021-10-31 12:45:34 +01:00
|
|
|
query: NullAndUndefined<string>
|
|
|
|
|
setQuery: (query: NullAndUndefined<string>) => void
|
2021-10-29 20:11:23 +02:00
|
|
|
active: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function useLibraryOptions(): IUseLibraryOptions {
|
2021-10-31 12:45:34 +01:00
|
|
|
const [searchQuery, setSearchQuery] = useQueryParams({
|
2021-10-29 20:11:23 +02:00
|
|
|
unread: BooleanParam,
|
2021-10-31 12:45:34 +01:00
|
|
|
query: StringParam,
|
2021-10-29 20:11:23 +02:00
|
|
|
});
|
2021-10-31 12:45:34 +01:00
|
|
|
const { unread, query } = searchQuery;
|
2021-10-29 20:11:23 +02:00
|
|
|
const setUnread = (newUnread: NullAndUndefined<boolean>) => {
|
2021-10-31 12:45:34 +01:00
|
|
|
setSearchQuery(Object.assign(searchQuery, { unread: newUnread }), 'replace');
|
|
|
|
|
};
|
|
|
|
|
const setQuery = (newQuery: NullAndUndefined<string>) => {
|
|
|
|
|
setSearchQuery(Object.assign(searchQuery, { query: newQuery }), 'replace');
|
2021-10-29 20:11:23 +02:00
|
|
|
};
|
|
|
|
|
// eslint-disable-next-line eqeqeq
|
|
|
|
|
const active = !(unread == undefined);
|
|
|
|
|
return {
|
2021-10-31 12:45:34 +01:00
|
|
|
unread, setUnread, active, query, setQuery,
|
2021-10-29 20:11:23 +02:00
|
|
|
};
|
|
|
|
|
}
|