Skip to content

Commit 47de0c8

Browse files
authored
refactor: cleanup schema and types for CustomText (@fehmer) (monkeytypegame#6605)
1 parent 1cada77 commit 47de0c8

File tree

11 files changed

+59
-67
lines changed

11 files changed

+59
-67
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe("url-handler", () => {
251251

252252
//THEN
253253
expect(addNotificationMock).toHaveBeenCalledWith(
254-
`Failed to load test settings from URL: JSON does not match schema: \"0\" invalid enum value. expected 'time' | 'words' | 'quote' | 'custom' | 'zen', received 'invalidmode', \"1\" needs to be a number or a number represented as a string e.g. \"10\"., \"2.text\" expected array, received string, \"2.mode\" invalid enum value. expected 'repeat' | 'random' | 'shuffle', received 'invalid', \"2.limit\" expected object, received string, \"2.pipeDelimiter\" expected boolean, received string, \"3\" expected boolean, received string, \"4\" expected boolean, received string, \"6\" invalid enum value. expected 'normal' | 'expert' | 'master', received 'invalid', \"7\" invalid input`,
254+
`Failed to load test settings from URL: JSON does not match schema: \"0\" invalid enum value. expected 'time' | 'words' | 'quote' | 'custom' | 'zen', received 'invalidmode', \"1\" needs to be a number or a number represented as a string e.g. \"10\"., \"2.mode\" invalid enum value. expected 'repeat' | 'random' | 'shuffle', received 'invalid', \"2.pipeDelimiter\" expected boolean, received string, \"2.limit\" expected object, received string, \"2.text\" expected array, received string, \"3\" expected boolean, received string, \"4\" expected boolean, received string, \"6\" invalid enum value. expected 'normal' | 'expert' | 'master', received 'invalid', \"7\" invalid input`,
255255
0
256256
);
257257
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
getActiveFunboxNames,
4343
} from "../test/funbox/list";
4444
import { tryCatchSync } from "@monkeytype/util/trycatch";
45+
import { canQuickRestart } from "../utils/quick-restart";
4546

4647
let dontInsertSpace = false;
4748
let correctShiftUsed = true;
@@ -1073,7 +1074,7 @@ $(document).on("keydown", async (event) => {
10731074
if (Config.mode === "zen") {
10741075
void TestLogic.finish();
10751076
} else if (
1076-
!Misc.canQuickRestart(
1077+
!canQuickRestart(
10771078
Config.mode,
10781079
Config.words,
10791080
Config.time,

frontend/src/ts/modals/share-test-settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function getCheckboxValue(checkbox: string): boolean {
1616
type SharedTestSettings = [
1717
Mode | null,
1818
Mode2<Mode> | null,
19-
CustomText.CustomTextData | null,
19+
CustomText.CustomTextSettings | null,
2020
boolean | null,
2121
boolean | null,
2222
string | null,

frontend/src/ts/test/custom-text.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import {
22
CustomTextLimitMode,
3-
CustomTextLimitModeSchema,
43
CustomTextMode,
5-
CustomTextModeSchema,
64
} from "@monkeytype/contracts/schemas/util";
75
import { LocalStorageWithSchema } from "../utils/local-storage-with-schema";
86
import { z } from "zod";
9-
import { CustomTextDataWithTextLen } from "@monkeytype/contracts/schemas/results";
7+
import { CompletedEventCustomTextSchema } from "@monkeytype/contracts/schemas/results";
108
import { deepClone } from "../utils/misc";
119

1210
const CustomTextObjectSchema = z.record(z.string(), z.string());
@@ -30,11 +28,10 @@ const customTextLongLS = new LocalStorageWithSchema({
3028
fallback: {},
3129
});
3230

33-
export const CustomTextSettingsSchema = z.object({
31+
export const CustomTextSettingsSchema = CompletedEventCustomTextSchema.omit({
32+
textLen: true,
33+
}).extend({
3434
text: z.array(z.string()).min(1),
35-
mode: CustomTextModeSchema,
36-
limit: z.object({ value: z.number(), mode: CustomTextLimitModeSchema }),
37-
pipeDelimiter: z.boolean(),
3835
});
3936

4037
export type CustomTextSettings = z.infer<typeof CustomTextSettingsSchema>;
@@ -139,17 +136,8 @@ export function setPipeDelimiter(val: boolean): void {
139136
});
140137
}
141138

142-
export type CustomTextData = Omit<CustomTextDataWithTextLen, "textLen"> & {
143-
text: string[];
144-
};
145-
146-
export function getData(): CustomTextData {
147-
return {
148-
text: getText(),
149-
mode: getMode(),
150-
limit: getLimit(),
151-
pipeDelimiter: getPipeDelimiter(),
152-
};
139+
export function getData(): CustomTextSettings {
140+
return customTextSettings.get();
153141
}
154142

155143
export function getCustomText(name: string, long = false): string[] {

frontend/src/ts/test/practise-words.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type Before = {
1111
mode: Mode | null;
1212
punctuation: boolean | null;
1313
numbers: boolean | null;
14-
customText: CustomText.CustomTextData | null;
14+
customText: CustomText.CustomTextSettings | null;
1515
};
1616

1717
export const before: Before = {

frontend/src/ts/test/result.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { getActiveFunboxes, isFunboxActiveWithProperty } from "./funbox/list";
4141
import { getFunbox } from "@monkeytype/funbox";
4242
import { SnapshotUserTag } from "../constants/default-snapshot";
4343
import { Language } from "@monkeytype/contracts/schemas/languages";
44+
import { canQuickRestart as canQuickRestartFn } from "../utils/quick-restart";
4445

4546
let result: CompletedEvent;
4647
let maxChartVal: number;
@@ -966,7 +967,7 @@ export async function update(
966967
Misc.applyReducedMotion(125)
967968
);
968969

969-
const canQuickRestart = Misc.canQuickRestart(
970+
const canQuickRestart = canQuickRestartFn(
970971
Config.mode,
971972
Config.words,
972973
Config.time,

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import { QuoteLength } from "@monkeytype/contracts/schemas/configs";
6262
import { Mode } from "@monkeytype/contracts/schemas/shared";
6363
import {
6464
CompletedEvent,
65-
CustomTextDataWithTextLen,
65+
CompletedEventCustomText,
6666
} from "@monkeytype/contracts/schemas/results";
6767
import * as XPBar from "../elements/xp-bar";
6868
import {
@@ -78,6 +78,7 @@ import { tryCatch } from "@monkeytype/util/trycatch";
7878
import { captureException } from "../sentry";
7979
import * as Loader from "../elements/loader";
8080
import * as TestInitFailed from "../elements/test-init-failed";
81+
import { canQuickRestart } from "../utils/quick-restart";
8182

8283
let failReason = "";
8384
const koInputVisual = document.getElementById("koInputVisual") as HTMLElement;
@@ -170,7 +171,7 @@ export function restart(options = {} as RestartOptions): void {
170171
if (!ManualRestart.get()) {
171172
if (Config.mode !== "zen") event?.preventDefault();
172173
if (
173-
!Misc.canQuickRestart(
174+
!canQuickRestart(
174175
Config.mode,
175176
Config.words,
176177
Config.time,
@@ -801,7 +802,7 @@ function buildCompletedEvent(
801802
const wpmCons = Numbers.roundTo2(Numbers.kogasa(stddev3 / avg3));
802803
const wpmConsistency = isNaN(wpmCons) ? 0 : wpmCons;
803804

804-
let customText: CustomTextDataWithTextLen | undefined = undefined;
805+
let customText: CompletedEventCustomText | undefined = undefined;
805806
if (Config.mode === "custom") {
806807
const temp = CustomText.getData();
807808
customText = {

frontend/src/ts/ui.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import * as ConfigEvent from "./observables/config-event";
66
import { debounce, throttle } from "throttle-debounce";
77
import * as TestUI from "./test/test-ui";
88
import { get as getActivePage } from "./states/active-page";
9-
import { canQuickRestart, isDevEnvironment } from "./utils/misc";
9+
import { isDevEnvironment } from "./utils/misc";
1010
import { isCustomTextLong } from "./states/custom-text-name";
11+
import { canQuickRestart } from "./utils/quick-restart";
1112

1213
let isPreviewingFont = false;
1314
export function previewFontFamily(font: string): void {

frontend/src/ts/utils/misc.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import {
77
Mode2,
88
PersonalBests,
99
} from "@monkeytype/contracts/schemas/shared";
10-
import {
11-
CustomTextDataWithTextLen,
12-
Result,
13-
} from "@monkeytype/contracts/schemas/results";
10+
import { Result } from "@monkeytype/contracts/schemas/results";
1411
import { z } from "zod";
1512

1613
export function whorf(speed: number, wordlen: number): number {
@@ -195,39 +192,6 @@ export function isUsernameValid(name: string): boolean {
195192
return /^[0-9a-zA-Z_.-]+$/.test(name);
196193
}
197194

198-
export function canQuickRestart(
199-
mode: string,
200-
words: number,
201-
time: number,
202-
CustomText: Omit<CustomTextDataWithTextLen, "textLen">,
203-
customTextIsLong: boolean
204-
): boolean {
205-
const wordsLong = mode === "words" && (words >= 1000 || words === 0);
206-
const timeLong = mode === "time" && (time >= 900 || time === 0);
207-
const customTextLong = mode === "custom" && customTextIsLong;
208-
209-
const customTextRandomWordsLong =
210-
mode === "custom" &&
211-
(CustomText.limit.mode === "word" || CustomText.limit.mode === "section") &&
212-
(CustomText.limit.value >= 1000 || CustomText.limit.value === 0);
213-
const customTextRandomTimeLong =
214-
mode === "custom" &&
215-
CustomText.limit.mode === "time" &&
216-
(CustomText.limit.value >= 900 || CustomText.limit.value === 0);
217-
218-
if (
219-
wordsLong ||
220-
timeLong ||
221-
customTextLong ||
222-
customTextRandomWordsLong ||
223-
customTextRandomTimeLong
224-
) {
225-
return false;
226-
} else {
227-
return true;
228-
}
229-
}
230-
231195
export function clearTimeouts(timeouts: (number | NodeJS.Timeout)[]): void {
232196
timeouts.forEach((to) => {
233197
if (typeof to === "number") clearTimeout(to);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { CustomTextSettings } from "../test/custom-text";
2+
3+
export function canQuickRestart(
4+
mode: string,
5+
words: number,
6+
time: number,
7+
CustomText: CustomTextSettings,
8+
customTextIsLong: boolean
9+
): boolean {
10+
const wordsLong = mode === "words" && (words >= 1000 || words === 0);
11+
const timeLong = mode === "time" && (time >= 900 || time === 0);
12+
const customTextLong = mode === "custom" && customTextIsLong;
13+
14+
const customTextRandomWordsLong =
15+
mode === "custom" &&
16+
(CustomText.limit.mode === "word" || CustomText.limit.mode === "section") &&
17+
(CustomText.limit.value >= 1000 || CustomText.limit.value === 0);
18+
const customTextRandomTimeLong =
19+
mode === "custom" &&
20+
CustomText.limit.mode === "time" &&
21+
(CustomText.limit.value >= 900 || CustomText.limit.value === 0);
22+
23+
if (
24+
wordsLong ||
25+
timeLong ||
26+
customTextLong ||
27+
customTextRandomWordsLong ||
28+
customTextRandomTimeLong
29+
) {
30+
return false;
31+
} else {
32+
return true;
33+
}
34+
}

0 commit comments

Comments
 (0)