From 7269d443370132199ebc2ecbe489658bd458959c Mon Sep 17 00:00:00 2001 From: Robert Knight <95928279+microbit-robert@users.noreply.github.com> Date: Tue, 19 Sep 2023 14:02:21 +0100 Subject: [PATCH] Allow local storage settings to be overridden (#1129) Required for classroom language switching via query param. --- src/common/use-storage.ts | 4 +++- src/settings/settings.tsx | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/common/use-storage.ts b/src/common/use-storage.ts index 3cf8fd4dc..fe7ed6973 100644 --- a/src/common/use-storage.ts +++ b/src/common/use-storage.ts @@ -14,7 +14,8 @@ export function useStorage( storageType: storageType, key: string, defaultValue: T, - validate?: (x: unknown) => x is T + validate?: (x: unknown) => x is T, + overrides?: Partial ): [T, (value: T) => void] { const [state, setState] = useState(() => { const storage = @@ -29,6 +30,7 @@ export function useStorage( parsed = { ...defaultValue, ...parsed, + ...overrides, }; // Remove any top-level keys that aren't present in defaults. const unknownKeys = new Set(Object.keys(parsed)); diff --git a/src/settings/settings.tsx b/src/settings/settings.tsx index 9cb554a6e..c90bcfcd3 100644 --- a/src/settings/settings.tsx +++ b/src/settings/settings.tsx @@ -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; @@ -168,14 +169,15 @@ export const useSettings = (): SettingsContextValue => { }; const SettingsProvider = ({ children }: { children: ReactNode }) => { - const settings = useStorage( + const [settings, setSettings] = useStorage( "local", "settings", defaultSettings, - isValidSettingsObject + isValidSettingsObject, + flags.noLang ? { languageId: getLanguageFromQuery() } : {} ); return ( - + {children} );