mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-07-03 19:04:39 -05:00
migrate to axios, front-end part of configurable ServerAddress
This commit is contained in:
@@ -3,39 +3,17 @@
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
import { useState, Dispatch, SetStateAction } from 'react';
|
||||
import storage from './localStorage';
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export default function useLocalStorage<T>(key: string, initialValue: T) : [T, Dispatch<SetStateAction<T>>] {
|
||||
// State to store our value
|
||||
// Pass initial state function to useState so logic is only executed once
|
||||
const [storedValue, setStoredValue] = useState<T>(() => {
|
||||
try {
|
||||
// Get from local storage by key
|
||||
const item = window.localStorage.getItem(key);
|
||||
export default function useLocalStorage<T>(key: string, defaultValue: T) : [T, Dispatch<SetStateAction<T>>] {
|
||||
const [storedValue, setStoredValue] = useState<T>(storage.getItem(key, defaultValue));
|
||||
|
||||
// Parse stored json or if null return set and return initialValue
|
||||
if (item !== null) { return JSON.parse(item); }
|
||||
|
||||
window.localStorage.setItem(key, JSON.stringify(initialValue));
|
||||
} finally {
|
||||
// eslint-disable-next-line no-unsafe-finally
|
||||
return initialValue;
|
||||
}
|
||||
});
|
||||
|
||||
// Return a wrapped version of useState's setter function that ...
|
||||
// ... persists the new value to localStorage.
|
||||
const setValue = (value: T | ((prevState: T) => T)) => {
|
||||
try {
|
||||
// Allow value to be a function so we have same API as useState
|
||||
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
||||
// Save state
|
||||
setStoredValue(valueToStore);
|
||||
// Save to local storage
|
||||
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
||||
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch (error) { }
|
||||
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
||||
setStoredValue(valueToStore);
|
||||
storage.setItem(key, valueToStore);
|
||||
};
|
||||
|
||||
return [storedValue, setValue];
|
||||
|
||||
Reference in New Issue
Block a user