diff --git a/server/src/main/kotlin/ir/armor/tachidesk/Config.kt b/server/src/main/kotlin/ir/armor/tachidesk/Config.kt index 82cd347..ce0d1be 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/Config.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/Config.kt @@ -11,4 +11,5 @@ object Config { val extensionsRoot = "$dataRoot/extensions" val thumbnailsRoot = "$dataRoot/thumbnails" val mangaRoot = "$dataRoot/manga" + val serverPort = 4567 } diff --git a/webUI/react/src/App.tsx b/webUI/react/src/App.tsx index b0ac65c..ad00ba5 100644 --- a/webUI/react/src/App.tsx +++ b/webUI/react/src/App.tsx @@ -22,10 +22,11 @@ import DarkTheme from './context/DarkTheme'; import Library from './screens/Library'; import Settings from './screens/Settings'; import Categories from './screens/settings/Categories'; +import useLocalStorage from './util/useLocalStorage'; export default function App() { const [title, setTitle] = useState('Tachidesk'); - const [darkTheme, setDarkTheme] = useState(true); + const [darkTheme, setDarkTheme] = useLocalStorage('darkTheme', true); const navTitleContext = { title, setTitle }; const darkThemeContext = { darkTheme, setDarkTheme }; diff --git a/webUI/react/src/index.tsx b/webUI/react/src/index.tsx index 9323515..c10d98b 100644 --- a/webUI/react/src/index.tsx +++ b/webUI/react/src/index.tsx @@ -5,7 +5,6 @@ import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; -import reportWebVitals from './reportWebVitals'; import './index.css'; // roboto font import 'fontsource-roboto'; @@ -16,8 +15,3 @@ ReactDOM.render( , document.getElementById('root'), ); - -// If you want to start measuring performance in your app, pass a function -// to log results (for example: reportWebVitals(console.log)) -// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals -reportWebVitals(); diff --git a/webUI/react/src/reportWebVitals.ts b/webUI/react/src/reportWebVitals.ts deleted file mode 100644 index a7c0b68..0000000 --- a/webUI/react/src/reportWebVitals.ts +++ /dev/null @@ -1,21 +0,0 @@ -/* 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 { ReportHandler } from 'web-vitals'; - -const reportWebVitals = (onPerfEntry?: ReportHandler) => { - if (onPerfEntry) { - import('web-vitals').then(({ - getCLS, getFID, getFCP, getLCP, getTTFB, - }) => { - getCLS(onPerfEntry); - getFID(onPerfEntry); - getFCP(onPerfEntry); - getLCP(onPerfEntry); - getTTFB(onPerfEntry); - }); - } -}; - -export default reportWebVitals; diff --git a/webUI/react/src/util/useLocalStorage.tsx b/webUI/react/src/util/useLocalStorage.tsx new file mode 100644 index 0000000..6e3902f --- /dev/null +++ b/webUI/react/src/util/useLocalStorage.tsx @@ -0,0 +1,42 @@ +/* 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 { useState, Dispatch, SetStateAction } from 'react'; + +// eslint-disable-next-line max-len +export default function useLocalStorage(key: string, initialValue: T) : [T, Dispatch>] { + // State to store our value + // Pass initial state function to useState so logic is only executed once + const [storedValue, setStoredValue] = useState(() => { + try { + // Get from local storage by key + const item = window.localStorage.getItem(key); + + // 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) { } + }; + + return [storedValue, setValue]; +}