Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow local storage settings to be overridden #1129

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/common/use-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export function useStorage<T>(
storageType: storageType,
key: string,
defaultValue: T,
validate?: (x: unknown) => x is T
validate?: (x: unknown) => x is T,
overrides?: Partial<T>
): [T, (value: T) => void] {
const [state, setState] = useState<T>(() => {
const storage =
Expand All @@ -29,6 +30,7 @@ export function useStorage<T>(
parsed = {
...defaultValue,
...parsed,
...overrides,
};
// Remove any top-level keys that aren't present in defaults.
const unknownKeys = new Set(Object.keys(parsed));
Expand Down
8 changes: 5 additions & 3 deletions src/settings/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createContext, ReactNode, useContext } from "react";
import { useStorage } from "../common/use-storage";
import { defaultCodeFontSizePt } from "../deployment/misc";
import { stage } from "../environment";
import { flags } from "../flags";

export interface Language {
id: string;
Expand Down Expand Up @@ -168,14 +169,15 @@ export const useSettings = (): SettingsContextValue => {
};

const SettingsProvider = ({ children }: { children: ReactNode }) => {
const settings = useStorage<Settings>(
const [settings, setSettings] = useStorage<Settings>(
"local",
"settings",
defaultSettings,
isValidSettingsObject
isValidSettingsObject,
flags.noLang ? { languageId: getLanguageFromQuery() } : {}
);
return (
<SettingsContext.Provider value={settings}>
<SettingsContext.Provider value={[settings, setSettings]}>
{children}
</SettingsContext.Provider>
);
Expand Down
Loading