Skip to content

Commit 0e5dd85

Browse files
authored
fix(lazy-mode): respect manual toggle after unsupported language (@byseif21) (monkeytypegame#7260)
### 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.
1 parent bd99519 commit 0e5dd85

File tree

3 files changed

+63
-49
lines changed

3 files changed

+63
-49
lines changed

frontend/src/ts/states/arabic-lazy-mode.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { z } from "zod";
2+
import { LocalStorageWithSchema } from "../utils/local-storage-with-schema";
3+
4+
const rememberLazyModeLS = new LocalStorageWithSchema({
5+
key: "rememberLazyMode",
6+
schema: z.boolean(),
7+
fallback: false,
8+
});
9+
10+
const arabicLazyModeLS = new LocalStorageWithSchema({
11+
key: "prefersArabicLazyMode",
12+
schema: z.boolean(),
13+
fallback: true,
14+
});
15+
16+
export function getRemember(): boolean {
17+
return rememberLazyModeLS.get();
18+
}
19+
20+
export function setRemember(value: boolean): void {
21+
rememberLazyModeLS.set(value);
22+
}
23+
24+
export function getArabicPref(): boolean {
25+
return arabicLazyModeLS.get();
26+
}
27+
28+
export function setArabicPref(value: boolean): void {
29+
arabicLazyModeLS.set(value);
30+
}

frontend/src/ts/test/test-logic.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import * as AnalyticsController from "../controllers/analytics-controller";
3939
import { getAuthenticatedUser, isAuthenticated } from "../firebase";
4040
import * as ConnectionState from "../states/connection";
4141
import * as KeymapEvent from "../observables/keymap-event";
42-
import * as ArabicLazyMode from "../states/arabic-lazy-mode";
42+
import * as LazyModeState from "../states/remember-lazy-mode";
4343
import Format from "../utils/format";
4444
import { QuoteLength, QuoteLengthConfig } from "@monkeytype/schemas/configs";
4545
import { Mode } from "@monkeytype/schemas/shared";
@@ -342,7 +342,6 @@ export function restart(options = {} as RestartOptions): void {
342342
}
343343

344344
let lastInitError: Error | null = null;
345-
let rememberLazyMode: boolean;
346345
let showedLazyModeNotification: boolean = false;
347346
let testReinitCount = 0;
348347

@@ -421,39 +420,43 @@ async function init(): Promise<boolean> {
421420
.some((lang) => !lang.noLazyMode);
422421

423422
if (Config.lazyMode && !anySupportsLazyMode) {
424-
rememberLazyMode = true;
425-
Notifications.add(
426-
"None of the selected polyglot languages support lazy mode.",
427-
0,
428-
{
429-
important: true,
430-
},
431-
);
423+
LazyModeState.setRemember(true);
424+
if (!showedLazyModeNotification) {
425+
Notifications.add(
426+
"None of the selected polyglot languages support lazy mode.",
427+
0,
428+
{
429+
important: true,
430+
},
431+
);
432+
showedLazyModeNotification = true;
433+
}
432434
setConfig("lazyMode", false);
433-
} else if (rememberLazyMode && anySupportsLazyMode) {
434-
setConfig("lazyMode", true, {
435-
nosave: true,
436-
});
435+
} else if (LazyModeState.getRemember() && anySupportsLazyMode) {
436+
setConfig("lazyMode", true);
437+
LazyModeState.setRemember(false);
438+
showedLazyModeNotification = false;
437439
}
438440
} else {
439441
// normal mode
440442
if (Config.lazyMode && !allowLazyMode) {
441-
rememberLazyMode = true;
442-
showedLazyModeNotification = true;
443-
Notifications.add("This language does not support lazy mode.", 0, {
444-
important: true,
445-
});
446-
443+
LazyModeState.setRemember(true);
444+
if (!showedLazyModeNotification) {
445+
Notifications.add("This language does not support lazy mode.", 0, {
446+
important: true,
447+
});
448+
showedLazyModeNotification = true;
449+
}
447450
setConfig("lazyMode", false);
448-
} else if (rememberLazyMode && !language.noLazyMode) {
449-
setConfig("lazyMode", true, {
450-
nosave: true,
451-
});
451+
} else if (LazyModeState.getRemember() && allowLazyMode) {
452+
setConfig("lazyMode", true);
453+
LazyModeState.setRemember(false);
454+
showedLazyModeNotification = false;
452455
}
453456
}
454457

455458
if (!Config.lazyMode && !language.noLazyMode) {
456-
rememberLazyMode = false;
459+
LazyModeState.setRemember(false);
457460
}
458461

459462
if (Config.mode === "custom") {
@@ -1549,7 +1552,10 @@ ConfigEvent.subscribe(({ key, newValue, nosave }) => {
15491552
if (ActivePage.get() === "test") {
15501553
if (key === "language") {
15511554
//automatically enable lazy mode for arabic
1552-
if ((newValue as string)?.startsWith("arabic") && ArabicLazyMode.get()) {
1555+
if (
1556+
(newValue as string)?.startsWith("arabic") &&
1557+
LazyModeState.getArabicPref()
1558+
) {
15531559
setConfig("lazyMode", true, {
15541560
nosave: true,
15551561
});
@@ -1582,13 +1588,7 @@ ConfigEvent.subscribe(({ key, newValue, nosave }) => {
15821588
}
15831589
if (key === "lazyMode" && !nosave) {
15841590
if (Config.language.startsWith("arabic")) {
1585-
ArabicLazyMode.set(newValue);
1586-
}
1587-
if (newValue) {
1588-
if (!showedLazyModeNotification) {
1589-
rememberLazyMode = false;
1590-
}
1591-
showedLazyModeNotification = false;
1591+
LazyModeState.setArabicPref(newValue);
15921592
}
15931593
}
15941594
});

0 commit comments

Comments
 (0)