Skip to content

Commit c253d2f

Browse files
authored
refactor: make customLayoutfluid an array (@fehmer) (#6494)
merge after #6487
1 parent 212b8d3 commit c253d2f

File tree

9 files changed

+56
-39
lines changed

9 files changed

+56
-39
lines changed

frontend/__tests__/root/config.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,13 @@ describe("Config", () => {
517517

518518
expect(Config.setQuoteLength([-4 as any, 5 as any])).toBe(false);
519519
});
520+
it("setCustomLayoutfluid", () => {
521+
expect(Config.setCustomLayoutfluid(["qwerty"])).toBe(true);
522+
523+
expect(Config.setCustomLayoutfluid([])).toBe(false);
524+
expect(Config.setCustomLayoutfluid("qwerty#qwertz" as any)).toBe(false);
525+
expect(Config.setCustomLayoutfluid("invalid" as any)).toBe(false);
526+
});
520527
});
521528

522529
function customThemeColors(n: number): CustomThemeColors {

frontend/src/ts/commandline/lists.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import * as FPSCounter from "../elements/fps-counter";
104104
import { migrateConfig } from "../utils/config";
105105
import {
106106
CustomBackgroundSchema,
107+
CustomLayoutFluid,
107108
PartialConfigSchema,
108109
} from "@monkeytype/contracts/schemas/configs";
109110
import { Command, CommandsSubgroup } from "./types";
@@ -219,13 +220,15 @@ export const commands: CommandsSubgroup = {
219220
id: "changeCustomLayoutfluid",
220221
display: "Custom layoutfluid...",
221222
defaultValue: (): string => {
222-
return Config.customLayoutfluid.replace(/#/g, " ");
223+
return Config.customLayoutfluid.join(" ");
223224
},
224225
input: true,
225226
icon: "fa-tint",
226227
exec: ({ input }): void => {
227228
if (input === undefined) return;
228-
UpdateConfig.setCustomLayoutfluid(input.replace(/ /g, "#"));
229+
UpdateConfig.setCustomLayoutfluid(
230+
input.split(" ") as CustomLayoutFluid
231+
);
229232
},
230233
},
231234
{

frontend/src/ts/config.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as Notifications from "./elements/notifications";
44
import {
55
isConfigValueValidBoolean,
66
isConfigValueValid,
7-
invalid as notifyInvalid,
87
} from "./config-validation";
98
import * as ConfigEvent from "./observables/config-event";
109
import { isAuthenticated } from "./firebase";
@@ -30,8 +29,6 @@ import { LocalStorageWithSchema } from "./utils/local-storage-with-schema";
3029
import { migrateConfig } from "./utils/config";
3130
import { roundTo1 } from "@monkeytype/util/numbers";
3231
import { getDefaultConfig } from "./constants/default-config";
33-
import { LayoutsList } from "./constants/layouts";
34-
import { LayoutName } from "@monkeytype/contracts/schemas/layouts";
3532

3633
const configLS = new LocalStorageWithSchema({
3734
key: "config",
@@ -1871,26 +1868,17 @@ export function setCustomLayoutfluid(
18711868
value: ConfigSchemas.CustomLayoutFluid,
18721869
nosave?: boolean
18731870
): boolean {
1874-
const trimmed = value.trim();
1875-
1876-
const invalidLayouts = trimmed
1877-
.split(/[# ]+/) //can be space or hash
1878-
.filter((it) => !LayoutsList.includes(it as LayoutName));
1879-
1880-
if (invalidLayouts.length !== 0) {
1881-
notifyInvalid(
1871+
if (
1872+
!isConfigValueValid(
18821873
"layoutfluid",
1883-
trimmed,
1884-
`The following inputted layouts do not exist: ${invalidLayouts.join(
1885-
", "
1886-
)}`
1887-
);
1888-
1874+
value,
1875+
ConfigSchemas.CustomLayoutFluidSchema
1876+
)
1877+
) {
18891878
return false;
18901879
}
18911880

1892-
const customLayoutfluid = trimmed.replace(/ /g, "#");
1893-
config.customLayoutfluid = customLayoutfluid;
1881+
config.customLayoutfluid = value;
18941882
saveToLocalStorage("customLayoutfluid", nosave);
18951883
ConfigEvent.dispatch("customLayoutfluid", config.customLayoutfluid);
18961884

frontend/src/ts/constants/default-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const obj = {
9292
customBackground: "",
9393
customBackgroundSize: "cover",
9494
customBackgroundFilter: [0, 1, 1, 1],
95-
customLayoutfluid: "qwerty#dvorak#colemak",
95+
customLayoutfluid: ["qwerty", "dvorak", "colemak"],
9696
customPolyglot: ["english", "spanish", "french", "german"],
9797
monkeyPowerLevel: "off",
9898
minBurst: "off",

frontend/src/ts/pages/settings.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ import * as CustomBackgroundFilter from "../elements/custom-background-filter";
2323
import {
2424
ConfigValue,
2525
CustomBackgroundSchema,
26+
CustomLayoutFluid,
2627
FunboxName,
2728
} from "@monkeytype/contracts/schemas/configs";
2829
import { getAllFunboxes, checkCompatibility } from "@monkeytype/funbox";
2930
import { getActiveFunboxNames } from "../test/funbox/list";
3031
import { SnapshotPreset } from "../constants/default-snapshot";
3132
import { LayoutsList } from "../constants/layouts";
3233
import { DataArrayPartial, Optgroup } from "slim-select/store";
33-
import { areUnsortedArraysEqual } from "../utils/arrays";
3434
import { tryCatch } from "@monkeytype/util/trycatch";
35+
import { areSortedArraysEqual, areUnsortedArraysEqual } from "../utils/arrays";
3536

3637
type SettingsGroups<T extends ConfigValue> = Record<string, SettingsGroup<T>>;
3738

@@ -690,8 +691,13 @@ async function fillSettingsPage(): Promise<void> {
690691
settings: { keepOrder: true },
691692
events: {
692693
afterChange: (newVal): void => {
693-
const customLayoutfluid = newVal.map((it) => it.value).join("#");
694-
if (customLayoutfluid !== Config.customLayoutfluid) {
694+
const customLayoutfluid = newVal.map(
695+
(it) => it.value
696+
) as CustomLayoutFluid;
697+
//checking equal with order, because customLayoutfluid is ordered
698+
if (
699+
!areSortedArraysEqual(customLayoutfluid, Config.customLayoutfluid)
700+
) {
695701
void UpdateConfig.setCustomLayoutfluid(customLayoutfluid);
696702
}
697703
},
@@ -706,6 +712,7 @@ async function fillSettingsPage(): Promise<void> {
706712
events: {
707713
afterChange: (newVal): void => {
708714
const customPolyglot = newVal.map((it) => it.value);
715+
//checking equal without order, because customPolyglot is not ordered
709716
if (!areUnsortedArraysEqual(customPolyglot, Config.customPolyglot)) {
710717
void UpdateConfig.setCustomPolyglot(customPolyglot);
711718
}
@@ -914,14 +921,23 @@ export async function update(groupUpdate = true): Promise<void> {
914921

915922
if (
916923
customLayoutFluidSelect !== undefined &&
917-
customLayoutFluidSelect.getSelected().join("#") !== Config.customLayoutfluid
924+
//checking equal with order, because customLayoutFluid is ordered
925+
!areSortedArraysEqual(
926+
customLayoutFluidSelect.getSelected(),
927+
Config.customLayoutfluid
928+
)
918929
) {
930+
//replace the data because the data is ordered. do not use setSelected
919931
customLayoutFluidSelect.setData(getLayoutfluidDropdownData());
920932
}
921933

922934
if (
923935
customPolyglotSelect !== undefined &&
924-
customPolyglotSelect.getSelected() !== Config.customPolyglot
936+
//checking equal without order, because customPolyglot is not ordered
937+
!areUnsortedArraysEqual(
938+
customPolyglotSelect.getSelected(),
939+
Config.customPolyglot
940+
)
925941
) {
926942
customPolyglotSelect.setSelected(Config.customPolyglot);
927943
}
@@ -1391,7 +1407,7 @@ function getLanguageDropdownData(
13911407
}
13921408

13931409
function getLayoutfluidDropdownData(): DataArrayPartial {
1394-
const customLayoutfluidActive = Config.customLayoutfluid.split("#");
1410+
const customLayoutfluidActive = Config.customLayoutfluid;
13951411
return [
13961412
...customLayoutfluidActive,
13971413
...LayoutsList.filter((it) => !customLayoutfluidActive.includes(it)),

frontend/src/ts/test/funbox/funbox-functions.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
347347
},
348348
layoutfluid: {
349349
applyConfig(): void {
350-
const layout = Config.customLayoutfluid.split("#")[0] ?? "qwerty";
350+
const layout = Config.customLayoutfluid[0] ?? "qwerty";
351351

352352
UpdateConfig.setLayout(layout as Layout, true);
353353
UpdateConfig.setKeymapLayout(layout as KeymapLayout, true);
@@ -359,11 +359,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
359359
},
360360
handleSpace(): void {
361361
if (Config.mode !== "time") {
362-
// here I need to check if Config.customLayoutFluid exists because of my
363-
// scuffed solution of returning whenever value is undefined in the setCustomLayoutfluid function
364-
const layouts: Layout[] = Config.customLayoutfluid
365-
? (Config.customLayoutfluid.split("#") as Layout[])
366-
: ["qwerty", "dvorak", "colemak"];
362+
const layouts = Config.customLayoutfluid;
367363
const outOf: number = TestWords.words.length;
368364
const wordsPerLayout = Math.floor(outOf / layouts.length);
369365
const index = Math.floor(
@@ -403,7 +399,7 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
403399
}
404400
},
405401
getResultContent(): string {
406-
return Config.customLayoutfluid.replace(/#/g, " ");
402+
return Config.customLayoutfluid.join(" ");
407403
},
408404
restart(): void {
409405
if (this.applyConfig) this.applyConfig();

frontend/src/ts/test/test-timer.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ function calculateAcc(): number {
9292
function layoutfluid(): void {
9393
if (timerDebug) console.time("layoutfluid");
9494
if (Config.funbox.includes("layoutfluid") && Config.mode === "time") {
95-
const layouts = Config.customLayoutfluid
96-
? Config.customLayoutfluid.split("#")
97-
: ["qwerty", "dvorak", "colemak"];
95+
const layouts = Config.customLayoutfluid;
9896
const switchTime = Config.time / layouts.length;
9997
const time = Time.get();
10098
const index = Math.floor(time / switchTime);

frontend/src/ts/utils/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,11 @@ export function replaceLegacyValues(
123123
}
124124
}
125125

126+
if (typeof configObj.customLayoutfluid === "string") {
127+
configObj.customLayoutfluid = (configObj.customLayoutfluid as string).split(
128+
"#"
129+
) as ConfigSchemas.CustomLayoutFluid;
130+
}
131+
126132
return configObj;
127133
}

packages/contracts/src/schemas/configs.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ export type CustomBackgroundFilter = z.infer<
196196
typeof CustomBackgroundFilterSchema
197197
>;
198198

199-
export const CustomLayoutFluidSchema = z.string().regex(/^[0-9a-zA-Z_#]+$/); //TODO better regex
199+
export const CustomLayoutFluidSchema = z
200+
.array(Layouts.LayoutNameSchema)
201+
.min(1)
202+
.max(15);
200203
export type CustomLayoutFluid = z.infer<typeof CustomLayoutFluidSchema>;
201204

202205
export const CustomPolyglotSchema = z.array(LanguageSchema).min(1);

0 commit comments

Comments
 (0)