diff --git a/frontend/src/pages/Settings/UI/index.tsx b/frontend/src/pages/Settings/UI/index.tsx index 2f03e8c18..2b85eeaca 100644 --- a/frontend/src/pages/Settings/UI/index.tsx +++ b/frontend/src/pages/Settings/UI/index.tsx @@ -1,10 +1,9 @@ -import { uiPageSizeKey, usePageSize } from "@/utilities/storage"; +import { uiPageSizeKey } from "@/utilities/storage"; import { FunctionComponent } from "react"; import { Layout, Section, Selector } from "../components"; import { pageSizeOptions } from "./options"; const SettingsUIView: FunctionComponent = () => { - const [pageSize] = usePageSize(); return (
@@ -13,7 +12,7 @@ const SettingsUIView: FunctionComponent = () => { options={pageSizeOptions} location="storages" settingKey={uiPageSizeKey} - settingOptions={{ onLoaded: () => pageSize }} + defaultValue={50} >
diff --git a/frontend/src/pages/Settings/components/Layout.tsx b/frontend/src/pages/Settings/components/Layout.tsx index 623893fff..af2556f96 100644 --- a/frontend/src/pages/Settings/components/Layout.tsx +++ b/frontend/src/pages/Settings/components/Layout.tsx @@ -39,7 +39,7 @@ const Layout: FunctionComponent = (props) => { useOnValueChange(isRefetching, (value) => { if (!value) { - form.reset(); + form.setValues((values) => ({ ...values, settings: {} })); } }); @@ -60,9 +60,11 @@ const Layout: FunctionComponent = (props) => { const storagesToSubmit = { ...storages }; LOG("info", "submitting storages", storagesToSubmit); updateStorage(storagesToSubmit); + + form.setValues((values) => ({ ...values, storages: {} })); } }, - [mutate, submitHooks, updateStorage] + [form, mutate, submitHooks, updateStorage] ); const totalStagedCount = useMemo(() => { diff --git a/frontend/src/pages/Settings/utilities/hooks.ts b/frontend/src/pages/Settings/utilities/hooks.ts index 2c6e392da..a5a8aebf9 100644 --- a/frontend/src/pages/Settings/utilities/hooks.ts +++ b/frontend/src/pages/Settings/utilities/hooks.ts @@ -1,4 +1,4 @@ -import { LOG } from "@/utilities/console"; +import { ASSERT, LOG } from "@/utilities/console"; import { get, isNull, isUndefined, uniqBy } from "lodash"; import { useCallback, useMemo, useRef } from "react"; import { @@ -19,7 +19,7 @@ export interface BaseInput { export type SettingValueOptions = { original?: boolean; defaultValue?: T; - onLoaded?: (settings: Settings) => T; + onLoaded?: (settings: Settings, storage: Storage) => T; onSaved?: (value: T) => unknown; onSubmit?: (value: T) => unknown; }; @@ -49,6 +49,11 @@ export function useSettingValue( options?: SettingValueOptions ): Readonly> { const settings = useSettings(); + const storage = window.localStorage; + + const isSettingsKey = useMemo(() => key.startsWith("settings"), [key]); + + ASSERT(isSettingsKey === false && key.startsWith("storage")); const optionsRef = useRef(options); optionsRef.current = options; @@ -61,12 +66,20 @@ export function useSettingValue( if (onLoaded) { LOG("info", `${key} is using custom loader`); - return onLoaded(settings); + return onLoaded(settings, storage); } - const path = key.replaceAll("-", "."); + let value: Nullable = null; + if (isSettingsKey) { + const path = key.replaceAll("-", "."); - const value = get({ settings }, path, null) as Nullable; + value = get({ settings }, path, null) as Nullable; + } else { + const storageValue = storage.getItem(key); + if (storageValue !== null) { + value = JSON.parse(storageValue); + } + } if (defaultValue && (isNull(value) || isUndefined(value))) { LOG("info", `${key} is falling back to`, defaultValue); @@ -75,7 +88,7 @@ export function useSettingValue( } return value; - }, [key, settings]); + }, [isSettingsKey, key, settings, storage]); const stagedValue = useStagedValues(); diff --git a/frontend/src/utilities/console.ts b/frontend/src/utilities/console.ts index e04a390b8..ac474fec1 100644 --- a/frontend/src/utilities/console.ts +++ b/frontend/src/utilities/console.ts @@ -34,4 +34,5 @@ export function GROUP( } } -export const ASSERT = console.assert; +// eslint-disable-next-line @typescript-eslint/no-empty-function +export const ASSERT = isProdEnv ? () => {} : console.assert; diff --git a/frontend/src/utilities/storage.ts b/frontend/src/utilities/storage.ts index 737b76ca3..d7e004087 100644 --- a/frontend/src/utilities/storage.ts +++ b/frontend/src/utilities/storage.ts @@ -12,6 +12,18 @@ export function useUpdateLocalStorage() { }, []); } +export function getPageSize(storage: Storage): number { + const defaultValue = 50; + + const value = storage.getItem(uiPageSizeKey); + + if (value === null) { + return defaultValue; + } + + return JSON.parse(value); +} + export function usePageSize() { return useLocalStorage({ key: uiPageSizeKey, defaultValue: 50 }); }