Skip to content

Commit e113dff

Browse files
MiodecCopilot
andauthored
refactor: convert the rest of test state to solid (@Miodec) (monkeytypegame#8261)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 4a64492 commit e113dff

25 files changed

Lines changed: 154 additions & 161 deletions

frontend/__tests__/controllers/url-handler.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { compressToURI } from "lz-ts";
44
import * as UpdateConfig from "../../src/ts/config/setters";
55
import * as Notifications from "../../src/ts/states/notifications";
66
import * as TestLogic from "../../src/ts/test/test-logic";
7-
import * as TestState from "../../src/ts/test/test-state";
7+
import * as TestState from "../../src/ts/states/test";
88
import * as Misc from "../../src/ts/utils/misc";
99
import { FunboxName } from "@monkeytype/schemas/configs";
1010
import { CustomTextSettings } from "@monkeytype/schemas/results";

frontend/__tests__/test/events/stats.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ vi.mock("../../../src/ts/test/test-stats", () => ({
44
start: 1000,
55
}));
66

7-
vi.mock("../../../src/ts/test/test-state", () => ({
8-
koreanStatus: false,
9-
}));
10-
117
const mockState = vi.hoisted(() => ({ activeWordIndex: 0 }));
128

139
vi.mock("../../../src/ts/config/store", () => ({
@@ -53,6 +49,7 @@ vi.mock("../../../src/ts/states/test", () => ({
5349
getActiveWordIndex: () => mockState.activeWordIndex,
5450
isResultCalculating: () => false,
5551
getBailedOut: () => false,
52+
getKoreanStatus: () => false,
5653
}));
5754

5855
import {

frontend/src/ts/commandline/lists.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
showFpsCounter,
3636
} from "../components/layout/overlays/FpsCounter";
3737
import { applyConfigFromJson } from "../config/lifecycle";
38-
import { lastEventLog } from "../test/test-state";
38+
import { getLastEventLog } from "../states/test";
3939

4040
const adsCommands = buildCommands("ads");
4141

@@ -277,11 +277,11 @@ export const commands: CommandsSubgroup = {
277277
icon: "fa-cog",
278278
visible: false,
279279
available: (): boolean => {
280-
return lastEventLog !== null;
280+
return getLastEventLog() !== null;
281281
},
282282
exec: async (): Promise<void> => {
283283
navigator.clipboard
284-
.writeText(JSON.stringify(lastEventLog))
284+
.writeText(JSON.stringify(getLastEventLog()))
285285
.then(() => {
286286
showSuccessNotification("Copied to clipboard");
287287
})

frontend/src/ts/commandline/lists/result-screen.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import {
55
showErrorNotification,
66
showSuccessNotification,
77
} from "../../states/notifications";
8-
import * as TestState from "../../test/test-state";
98
import * as TestWords from "../../test/test-words";
109
import { Config } from "../../config/store";
1110
import * as PractiseWords from "../../test/practise-words";
1211
import { Command, CommandsSubgroup } from "../types";
1312
import * as TestScreenshot from "../../test/test-screenshot";
1413
import { getInputHistory } from "../../test/events/stats";
15-
import { getResultVisible } from "../../states/test";
14+
import { getLastEventLog, getResultVisible } from "../../states/test";
1615

1716
const practiceSubgroup: CommandsSubgroup = {
1817
title: "Practice words...",
@@ -140,12 +139,13 @@ const commands: Command[] = [
140139
display: "Copy words to clipboard",
141140
icon: "fa-copy",
142141
exec: (): void => {
143-
if (TestState.lastEventLog === null) {
142+
const eventLog = getLastEventLog();
143+
if (eventLog === null) {
144144
showErrorNotification("No event log found!");
145145
return;
146146
}
147147

148-
const inputHistory = getInputHistory(TestState.lastEventLog);
148+
const inputHistory = getInputHistory(eventLog);
149149
const words =
150150
Config.mode === "zen"
151151
? inputHistory.join("")

frontend/src/ts/components/modals/QuoteSearchModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import {
2828
} from "../../states/notifications";
2929
import { showQuoteReportModal } from "../../states/quote-report";
3030
import { showSimpleModal } from "../../states/simple-modal";
31+
import { setSelectedQuoteId } from "../../states/test";
3132
import * as TestLogic from "../../test/test-logic";
32-
import * as TestState from "../../test/test-state";
3333
import { cn } from "../../utils/cn";
3434
import { getLanguage } from "../../utils/json-data";
3535
import * as Misc from "../../utils/misc";
@@ -398,7 +398,7 @@ export function QuoteSearchModal(): JSXElement {
398398
showNoticeNotification("Quote ID must be at least 1");
399399
return;
400400
}
401-
TestState.setSelectedQuoteId(quoteId);
401+
setSelectedQuoteId(quoteId);
402402
setConfig("quoteLength", [-2]);
403403
void TestLogic.restart();
404404
hideModalAndClearChain("QuoteSearch");

frontend/src/ts/controllers/challenge-controller.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ import * as Funbox from "../test/funbox/funbox";
99
import { setConfig } from "../config/setters";
1010
import { Config } from "../config/store";
1111
import { configEvent } from "../events/config";
12-
import * as TestState from "../test/test-state";
1312

1413
import { ChallengeSettings, getChallenge } from "@monkeytype/challenges";
1514
import { ChallengeName } from "@monkeytype/schemas/challenges";
1615
import { CompletedEvent } from "@monkeytype/schemas/results";
1716
import { typedKeys } from "@monkeytype/util/objects";
1817
import { hideLoaderBar, showLoaderBar } from "../states/loader-bar";
19-
import { getLoadedChallenge, setLoadedChallenge } from "../states/test";
18+
import {
19+
isTestRestarting,
20+
getLoadedChallenge,
21+
setLoadedChallenge,
22+
} from "../states/test";
2023
import { areUnsortedArraysEqual } from "../utils/arrays";
2124
import { qs } from "../utils/dom";
2225

@@ -26,7 +29,7 @@ export function clearActive(): void {
2629
if (
2730
getLoadedChallenge() !== null &&
2831
!challengeLoading &&
29-
!TestState.testRestarting
32+
!isTestRestarting()
3033
) {
3134
showNoticeNotification("Challenge cleared");
3235
setLoadedChallenge(null);

frontend/src/ts/controllers/chart-controller.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import { getTheme } from "../states/theme";
6565
import { Theme } from "../constants/themes";
6666
import { createDebouncedEffectOn } from "../hooks/effects";
6767
import { getWordIndexesForSecond } from "../test/events/stats";
68-
import { lastEventLog } from "../test/test-state";
68+
import { getLastEventLog } from "../states/test";
6969
import { typedKeys } from "@monkeytype/util/objects";
7070

7171
export class ChartWithUpdateColors<
@@ -273,13 +273,14 @@ export const result = new ChartWithUpdateColors<
273273
callbacks: {
274274
afterLabel: function (ti): string {
275275
if (prevTi === ti) return "";
276-
if (lastEventLog === null) return "";
276+
const eventLog = getLastEventLog();
277+
if (eventLog === null) return "";
277278

278279
prevTi = ti;
279280
try {
280281
const keypressIndex = Math.round(parseFloat(ti.label)) - 1;
281282
const wordsToHighlight = getWordIndexesForSecond(
282-
lastEventLog,
283+
eventLog,
283284
keypressIndex,
284285
);
285286

frontend/src/ts/controllers/route-controller.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import * as PageTransition from "../legacy-states/page-transition";
33
import { isAuthAvailable } from "../firebase";
44
import { isAuthenticated } from "../states/core";
55
import { isFunboxActive } from "../test/funbox/list";
6-
import * as TestState from "../test/test-state";
76
import { showNoticeNotification } from "../states/notifications";
87
import { navigationEvent, type NavigateOptions } from "../events/navigation";
98
import { authEvent } from "../events/auth";
10-
import { isResultCalculating, isTestActive } from "../states/test";
9+
import {
10+
isTestRestarting,
11+
isResultCalculating,
12+
isTestActive,
13+
} from "../states/test";
1114

1215
//source: https://www.youtube.com/watch?v=OstALBk-jTc
1316
// https://www.youtube.com/watch?v=OstALBk-jTc
@@ -162,12 +165,10 @@ export async function navigate(
162165
): Promise<void> {
163166
if (
164167
!options.force &&
165-
(TestState.testRestarting || isResultCalculating() || PageTransition.get())
168+
(isTestRestarting() || isResultCalculating() || PageTransition.get())
166169
) {
167170
console.debug(
168-
`navigate: ${url} ignored, page is busy (testRestarting: ${
169-
TestState.testRestarting
170-
}, resultCalculating: ${isResultCalculating()}, pageTransition: ${PageTransition.get()})`,
171+
`navigate: ${url} ignored, page is busy (testRestarting: ${isTestRestarting()}, resultCalculating: ${isResultCalculating()}, pageTransition: ${PageTransition.get()})`,
171172
);
172173
return;
173174
}

frontend/src/ts/controllers/url-handler.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ import {
3232
showNoticeNotification,
3333
showSuccessNotification,
3434
} from "../states/notifications";
35+
import { setSelectedQuoteId } from "../states/test";
3536
import * as CustomText from "../test/custom-text";
3637
import { restart as restartTest } from "../test/test-logic";
37-
import * as TestState from "../test/test-state";
3838
import * as Misc from "../utils/misc";
3939
import * as ChallengeController from "./challenge-controller";
4040

@@ -205,7 +205,7 @@ export function loadTestSettingsFromUrl(getOverride?: string): void {
205205
});
206206
} else if (mode === "quote") {
207207
setConfig("quoteLength", [-2]);
208-
TestState.setSelectedQuoteId(parseInt(de[1], 10));
208+
setSelectedQuoteId(parseInt(de[1], 10));
209209
}
210210
applied["mode2"] = de[1];
211211
}

frontend/src/ts/elements/result-word-highlight.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Constants for padding around the highlights
55

66
import * as Misc from "../utils/misc";
7-
import * as TestState from "../test/test-state";
7+
import { isLanguageRightToLeft } from "../states/test";
88
import { qsr } from "../utils/dom";
99

1010
const PADDING_X = 16;
@@ -103,7 +103,7 @@ export async function highlightWordsInRange(
103103
const newHighlightElementPositions = getHighlightElementPositions(
104104
firstWordIndex,
105105
lastWordIndex,
106-
TestState.isLanguageRightToLeft,
106+
isLanguageRightToLeft(),
107107
);
108108

109109
// For each line...
@@ -304,7 +304,7 @@ async function init(): Promise<boolean> {
304304

305305
// For RTL languages, account for difference between highlightContainer left and RWH_el left
306306
let RTL_offset;
307-
if (TestState.isLanguageRightToLeft) {
307+
if (isLanguageRightToLeft()) {
308308
RTL_offset = line.rect.left - RWH_rect.left + PADDING_X;
309309
} else {
310310
RTL_offset = 0;

0 commit comments

Comments
 (0)