Skip to content

Commit dc6d451

Browse files
committed
chore: block typing when test is not initialised correctly, show message
1 parent 499754a commit dc6d451

File tree

7 files changed

+102
-17
lines changed

7 files changed

+102
-17
lines changed

frontend/src/html/pages/test.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@
9999
Test settings
100100
</button>
101101
</div>
102+
<div id="testInitFailed" class="content-grid hidden">
103+
<div class="message">
104+
Test initialization failed. Please try different settings or refreshing
105+
the page. If the problem persists, please contact support.
106+
<div class="error"></div>
107+
<button class="active restart">
108+
<i class="fas fa-fw fa-redo-alt"></i>
109+
Restart
110+
</button>
111+
</div>
112+
</div>
102113
<div id="typingTest" class="content-grid full-width-padding">
103114
<div id="capsWarning" class="hidden">
104115
<i class="fas fa-lock"></i>

frontend/src/styles/test.scss

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@
7474
.pageTest {
7575
position: relative;
7676

77+
#testInitFailed {
78+
margin-top: 2rem;
79+
font-size: 1rem;
80+
text-align: center;
81+
.message {
82+
max-width: 800px;
83+
grid-area: content;
84+
justify-self: center;
85+
}
86+
.error {
87+
margin-top: 2rem;
88+
}
89+
button {
90+
padding: 1rem 2rem;
91+
margin-top: 2rem;
92+
}
93+
}
94+
7795
#typingTest {
7896
position: relative;
7997
width: 100%;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,8 @@ $("#wordsInput").on("keydown", (event) => {
873873
!popupVisible &&
874874
!TestUI.resultVisible &&
875875
event.key !== "Enter" &&
876-
!awaitingNextWord;
876+
!awaitingNextWord &&
877+
TestState.testInitSuccess;
877878

878879
if (!allowTyping) {
879880
event.preventDefault();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class QuotesController {
6363
const { data, error } = await tryCatch(
6464
cachedFetchJson<QuoteData>(`quotes/${normalizedLanguage}.json`)
6565
);
66-
6766
if (error) {
6867
if (
6968
error instanceof Error &&
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const elem = document.querySelector<HTMLElement>(".pageTest #testInitFailed");
2+
const testElem = document.querySelector<HTMLElement>(".pageTest #typingTest");
3+
const errorElem = document.querySelector<HTMLElement>(
4+
".pageTest #testInitFailed .error"
5+
);
6+
7+
export function show(): void {
8+
if (elem && testElem) {
9+
elem.classList.remove("hidden");
10+
testElem.classList.add("hidden");
11+
}
12+
}
13+
14+
function hideError(): void {
15+
if (errorElem) {
16+
errorElem.classList.add("hidden");
17+
}
18+
}
19+
20+
export function showError(text: string): void {
21+
if (errorElem) {
22+
errorElem.classList.remove("hidden");
23+
errorElem.innerText = text;
24+
}
25+
}
26+
27+
export function hide(): void {
28+
if (elem && testElem) {
29+
hideError();
30+
elem.classList.add("hidden");
31+
testElem.classList.remove("hidden");
32+
}
33+
}

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

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import { WordGenError } from "../utils/word-gen-error";
7777
import { tryCatch } from "@monkeytype/util/trycatch";
7878
import { captureException } from "../sentry";
7979
import * as Loader from "../elements/loader";
80+
import * as TestInitFailed from "../elements/test-init-failed";
8081

8182
let failReason = "";
8283
const koInputVisual = document.getElementById("koInputVisual") as HTMLElement;
@@ -338,7 +339,15 @@ export function restart(options = {} as RestartOptions): void {
338339

339340
TestState.setRepeated(options.withSameWordset ?? false);
340341
TestState.setPaceRepeat(repeatWithPace);
341-
await init();
342+
TestInitFailed.hide();
343+
TestState.setTestInitSuccess(true);
344+
const initResult = await init();
345+
346+
if (initResult === null) {
347+
TestUI.setTestRestarting(false);
348+
return;
349+
}
350+
342351
await PaceCaret.init();
343352

344353
for (const fb of getActiveFunboxesWithFunction("restart")) {
@@ -385,22 +394,28 @@ export function restart(options = {} as RestartOptions): void {
385394
let lastInitError: Error | null = null;
386395
let rememberLazyMode: boolean;
387396
let testReinitCount = 0;
388-
export async function init(): Promise<void> {
397+
export async function init(): Promise<void | null> {
389398
console.debug("Initializing test");
390399
testReinitCount++;
391-
if (testReinitCount >= 4) {
400+
if (testReinitCount > 3) {
392401
if (lastInitError) {
393402
captureException(lastInitError);
403+
TestInitFailed.showError(
404+
`${lastInitError.name}: ${lastInitError.message}`
405+
);
394406
}
407+
TestInitFailed.show();
395408
TestUI.setTestRestarting(false);
396-
Notifications.add(
397-
"Too many test reinitialization attempts. Something is going very wrong. Please contact support.",
398-
-1,
399-
{
400-
important: true,
401-
}
402-
);
403-
return;
409+
TestState.setTestInitSuccess(false);
410+
Focus.set(false);
411+
// Notifications.add(
412+
// "Too many test reinitialization attempts. Something is going very wrong. Please contact support.",
413+
// -1,
414+
// {
415+
// important: true,
416+
// }
417+
// );
418+
return null;
404419
}
405420

406421
MonkeyPower.reset();
@@ -426,8 +441,7 @@ export async function init(): Promise<void> {
426441

427442
if (!language || language.name !== Config.language) {
428443
UpdateConfig.setLanguage("english");
429-
await init();
430-
return;
444+
return await init();
431445
}
432446

433447
if (ActivePage.get() === "test") {
@@ -492,6 +506,7 @@ export async function init(): Promise<void> {
492506
wordsHaveTab = gen.hasTab;
493507
wordsHaveNewline = gen.hasNewline;
494508
} catch (e) {
509+
Loader.hide();
495510
if (e instanceof WordGenError || e instanceof Error) {
496511
lastInitError = e;
497512
}
@@ -512,8 +527,7 @@ export async function init(): Promise<void> {
512527
);
513528
}
514529

515-
await init();
516-
return;
530+
return await init();
517531
}
518532

519533
const beforeHasNumbers = TestWords.hasNumbers;
@@ -1348,6 +1362,10 @@ $(".pageTest").on("click", "#testModesNotice .textButton.restart", () => {
13481362
restart();
13491363
});
13501364

1365+
$(".pageTest").on("click", "#testInitFailed button.restart", () => {
1366+
restart();
1367+
});
1368+
13511369
$(".pageTest").on("click", "#restartTestButton", () => {
13521370
ManualRestart.set();
13531371
if (TestUI.resultCalculating) return;

frontend/src/ts/test/test-state.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export let savingEnabled = true;
88
export let bailedOut = false;
99
export let selectedQuoteId = 1;
1010
export let activeWordIndex = 0;
11+
export let testInitSuccess = true;
1112

1213
export function setRepeated(tf: boolean): void {
1314
isRepeated = tf;
@@ -48,3 +49,7 @@ export function increaseActiveWordIndex(): void {
4849
export function decreaseActiveWordIndex(): void {
4950
activeWordIndex--;
5051
}
52+
53+
export function setTestInitSuccess(tf: boolean): void {
54+
testInitSuccess = tf;
55+
}

0 commit comments

Comments
 (0)