From 0e5dd85db2cc0b34ae2176e3f469323669e83cc0 Mon Sep 17 00:00:00 2001 From: Seif Soliman Date: Wed, 17 Dec 2025 21:41:55 +0200 Subject: [PATCH] fix(lazy-mode): respect manual toggle after unsupported language (@byseif21) (#7260) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Description * lazy mode status “stuck” after switching to a language that does not support it then goes back to the one that supports it. * to reproduce After seeing the “This language does not support lazy mode” warning, switch back to the one that was working with and manually toggling lazy mode. It would not update to the selected option. --- frontend/src/ts/states/arabic-lazy-mode.ts | 16 ----- frontend/src/ts/states/remember-lazy-mode.ts | 30 +++++++++ frontend/src/ts/test/test-logic.ts | 66 ++++++++++---------- 3 files changed, 63 insertions(+), 49 deletions(-) delete mode 100644 frontend/src/ts/states/arabic-lazy-mode.ts create mode 100644 frontend/src/ts/states/remember-lazy-mode.ts diff --git a/frontend/src/ts/states/arabic-lazy-mode.ts b/frontend/src/ts/states/arabic-lazy-mode.ts deleted file mode 100644 index 590fd50e5..000000000 --- a/frontend/src/ts/states/arabic-lazy-mode.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { z } from "zod"; -import { LocalStorageWithSchema } from "../utils/local-storage-with-schema"; - -const ls = new LocalStorageWithSchema({ - key: "prefersArabicLazyMode", - schema: z.boolean(), - fallback: true, -}); - -export function get(): boolean { - return ls.get(); -} - -export function set(value: boolean): void { - ls.set(value); -} diff --git a/frontend/src/ts/states/remember-lazy-mode.ts b/frontend/src/ts/states/remember-lazy-mode.ts new file mode 100644 index 000000000..08d04a662 --- /dev/null +++ b/frontend/src/ts/states/remember-lazy-mode.ts @@ -0,0 +1,30 @@ +import { z } from "zod"; +import { LocalStorageWithSchema } from "../utils/local-storage-with-schema"; + +const rememberLazyModeLS = new LocalStorageWithSchema({ + key: "rememberLazyMode", + schema: z.boolean(), + fallback: false, +}); + +const arabicLazyModeLS = new LocalStorageWithSchema({ + key: "prefersArabicLazyMode", + schema: z.boolean(), + fallback: true, +}); + +export function getRemember(): boolean { + return rememberLazyModeLS.get(); +} + +export function setRemember(value: boolean): void { + rememberLazyModeLS.set(value); +} + +export function getArabicPref(): boolean { + return arabicLazyModeLS.get(); +} + +export function setArabicPref(value: boolean): void { + arabicLazyModeLS.set(value); +} diff --git a/frontend/src/ts/test/test-logic.ts b/frontend/src/ts/test/test-logic.ts index 76d3a3470..479fa9df3 100644 --- a/frontend/src/ts/test/test-logic.ts +++ b/frontend/src/ts/test/test-logic.ts @@ -39,7 +39,7 @@ import * as AnalyticsController from "../controllers/analytics-controller"; import { getAuthenticatedUser, isAuthenticated } from "../firebase"; import * as ConnectionState from "../states/connection"; import * as KeymapEvent from "../observables/keymap-event"; -import * as ArabicLazyMode from "../states/arabic-lazy-mode"; +import * as LazyModeState from "../states/remember-lazy-mode"; import Format from "../utils/format"; import { QuoteLength, QuoteLengthConfig } from "@monkeytype/schemas/configs"; import { Mode } from "@monkeytype/schemas/shared"; @@ -342,7 +342,6 @@ export function restart(options = {} as RestartOptions): void { } let lastInitError: Error | null = null; -let rememberLazyMode: boolean; let showedLazyModeNotification: boolean = false; let testReinitCount = 0; @@ -421,39 +420,43 @@ async function init(): Promise { .some((lang) => !lang.noLazyMode); if (Config.lazyMode && !anySupportsLazyMode) { - rememberLazyMode = true; - Notifications.add( - "None of the selected polyglot languages support lazy mode.", - 0, - { - important: true, - }, - ); + LazyModeState.setRemember(true); + if (!showedLazyModeNotification) { + Notifications.add( + "None of the selected polyglot languages support lazy mode.", + 0, + { + important: true, + }, + ); + showedLazyModeNotification = true; + } setConfig("lazyMode", false); - } else if (rememberLazyMode && anySupportsLazyMode) { - setConfig("lazyMode", true, { - nosave: true, - }); + } else if (LazyModeState.getRemember() && anySupportsLazyMode) { + setConfig("lazyMode", true); + LazyModeState.setRemember(false); + showedLazyModeNotification = false; } } else { // normal mode if (Config.lazyMode && !allowLazyMode) { - rememberLazyMode = true; - showedLazyModeNotification = true; - Notifications.add("This language does not support lazy mode.", 0, { - important: true, - }); - + LazyModeState.setRemember(true); + if (!showedLazyModeNotification) { + Notifications.add("This language does not support lazy mode.", 0, { + important: true, + }); + showedLazyModeNotification = true; + } setConfig("lazyMode", false); - } else if (rememberLazyMode && !language.noLazyMode) { - setConfig("lazyMode", true, { - nosave: true, - }); + } else if (LazyModeState.getRemember() && allowLazyMode) { + setConfig("lazyMode", true); + LazyModeState.setRemember(false); + showedLazyModeNotification = false; } } if (!Config.lazyMode && !language.noLazyMode) { - rememberLazyMode = false; + LazyModeState.setRemember(false); } if (Config.mode === "custom") { @@ -1549,7 +1552,10 @@ ConfigEvent.subscribe(({ key, newValue, nosave }) => { if (ActivePage.get() === "test") { if (key === "language") { //automatically enable lazy mode for arabic - if ((newValue as string)?.startsWith("arabic") && ArabicLazyMode.get()) { + if ( + (newValue as string)?.startsWith("arabic") && + LazyModeState.getArabicPref() + ) { setConfig("lazyMode", true, { nosave: true, }); @@ -1582,13 +1588,7 @@ ConfigEvent.subscribe(({ key, newValue, nosave }) => { } if (key === "lazyMode" && !nosave) { if (Config.language.startsWith("arabic")) { - ArabicLazyMode.set(newValue); - } - if (newValue) { - if (!showedLazyModeNotification) { - rememberLazyMode = false; - } - showedLazyModeNotification = false; + LazyModeState.setArabicPref(newValue); } } });