Skip to content

Commit c8fd68a

Browse files
committed
refactor: config group definitions
- moved group definitionsg to the shared pacakge - made sure typescript will throw errors if a config is left without a group - removed the 'missing group' check because its not possible anymore
1 parent 0f61225 commit c8fd68a

File tree

3 files changed

+158
-189
lines changed

3 files changed

+158
-189
lines changed

frontend/src/ts/modals/edit-preset.ts

Lines changed: 36 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,23 @@ import * as Notifications from "../elements/notifications";
77
import * as ConnectionState from "../states/connection";
88
import AnimatedModal from "../utils/animated-modal";
99
import {
10-
ActiveSettingGroups,
11-
ActiveSettingGroupsSchema,
12-
PresetSettingGroup,
13-
PresetSettingGroupSchema,
1410
PresetType,
1511
PresetTypeSchema,
1612
} from "@monkeytype/contracts/schemas/presets";
1713
import { getPreset } from "../controllers/preset-controller";
1814
import defaultConfig from "../constants/default-config";
19-
import { Config as ConfigType } from "@monkeytype/contracts/schemas/configs";
15+
import {
16+
ConfigGroupName,
17+
ConfigGroupNameSchema,
18+
ConfigGroupsLiteral,
19+
ConfigKey,
20+
Config as ConfigType,
21+
} from "@monkeytype/contracts/schemas/configs";
2022

2123
const state = {
2224
presetType: "full" as PresetType,
2325
checkboxes: new Map(
24-
PresetSettingGroupSchema.options.map((key: PresetSettingGroup) => [
25-
key,
26-
true,
27-
])
26+
ConfigGroupNameSchema.options.map((key: ConfigGroupName) => [key, true])
2827
),
2928
setPresetToCurrent: false,
3029
};
@@ -122,19 +121,17 @@ async function initializeEditState(id: string): Promise<void> {
122121
}
123122

124123
function addCheckboxListeners(): void {
125-
PresetSettingGroupSchema.options.forEach(
126-
(settingGroup: PresetSettingGroup) => {
127-
const checkboxInput = $(
128-
`#editPresetModal .modal .checkboxList .checkboxTitlePair[data-id="${settingGroup}"] input`
124+
ConfigGroupNameSchema.options.forEach((settingGroup: ConfigGroupName) => {
125+
const checkboxInput = $(
126+
`#editPresetModal .modal .checkboxList .checkboxTitlePair[data-id="${settingGroup}"] input`
127+
);
128+
checkboxInput.on("change", (e) => {
129+
state.checkboxes.set(
130+
settingGroup,
131+
checkboxInput.prop("checked") as boolean
129132
);
130-
checkboxInput.on("change", (e) => {
131-
state.checkboxes.set(
132-
settingGroup,
133-
checkboxInput.prop("checked") as boolean
134-
);
135-
});
136-
}
137-
);
133+
});
134+
});
138135

139136
const presetToCurrentCheckbox = $(
140137
`#editPresetModal .modal .changePresetToCurrentCheckbox input`
@@ -154,7 +151,7 @@ function addCheckBoxes(): void {
154151
const settingGroupListEl = $(
155152
"#editPresetModal .modal .inputs .checkboxList"
156153
).empty();
157-
PresetSettingGroupSchema.options.forEach((currSettingGroup) => {
154+
ConfigGroupNameSchema.options.forEach((currSettingGroup) => {
158155
const currSettingGroupTitle = camelCaseToSpaced(currSettingGroup);
159156
const settingGroupCheckbox: string = `<label class="checkboxTitlePair" data-id="${currSettingGroup}">
160157
<input type="checkbox" />
@@ -169,13 +166,11 @@ function addCheckBoxes(): void {
169166
}
170167

171168
function updateUI(): void {
172-
PresetSettingGroupSchema.options.forEach(
173-
(settingGroup: PresetSettingGroup) => {
174-
$(
175-
`#editPresetModal .modal .checkboxList .checkboxTitlePair[data-id="${settingGroup}"] input`
176-
).prop("checked", state.checkboxes.get(settingGroup));
177-
}
178-
);
169+
ConfigGroupNameSchema.options.forEach((settingGroup: ConfigGroupName) => {
170+
$(
171+
`#editPresetModal .modal .checkboxList .checkboxTitlePair[data-id="${settingGroup}"] input`
172+
).prop("checked", state.checkboxes.get(settingGroup));
173+
});
179174
$(`#editPresetModal .modal .presetType button`).removeClass("active");
180175
$(
181176
`#editPresetModal .modal .presetType button[value="${state.presetType}"]`
@@ -291,7 +286,7 @@ async function apply(): Promise<void> {
291286
return;
292287
}
293288
const configChanges = getConfigChanges();
294-
const activeSettingGroups: ActiveSettingGroups | null =
289+
const activeSettingGroups: ConfigGroupName[] | null =
295290
state.presetType === "partial" ? getActiveSettingGroupsFromState() : null;
296291
const response = await Ape.presets.save({
297292
body: {
@@ -342,147 +337,23 @@ async function apply(): Promise<void> {
342337
Loader.hide();
343338
}
344339

345-
function getSettingGroup(configFieldName: string): PresetSettingGroup | null {
346-
const themeSettings = [
347-
"theme",
348-
"themeLight",
349-
"themeDark",
350-
"autoSwitchTheme",
351-
"customTheme",
352-
"customThemeColors",
353-
"favThemes",
354-
"flipTestColors",
355-
"colorfulMode",
356-
"randomTheme",
357-
"customBackground",
358-
"customBackgroundSize",
359-
"customBackgroundFilter",
360-
];
361-
const hideElementsSettings = [
362-
"showKeyTips",
363-
"capsLockWarning",
364-
"showOutOfFocusWarning",
365-
"showAverage",
366-
];
367-
const caretSettings = [
368-
"smoothCaret",
369-
"caretStyle",
370-
"paceCaretStyle",
371-
"paceCaret",
372-
"paceCaretCustomSpeed",
373-
"repeatedPace",
374-
];
375-
const behaviorSettings = [
376-
"quickRestart",
377-
"difficulty",
378-
"blindMode",
379-
"funbox",
380-
"alwaysShowWordsHistory",
381-
"singleListCommandLine",
382-
"minWpm",
383-
"minWpmCustomSpeed",
384-
"minAcc",
385-
"minAccCustom",
386-
"repeatQuotes",
387-
"customLayoutfluid",
388-
"minBurst",
389-
"minBurstCustomSpeed",
390-
"britishEnglish",
391-
"tags",
392-
];
393-
const testSettings = [
394-
"punctuation",
395-
"words",
396-
"time",
397-
"numbers",
398-
"mode",
399-
"quoteLength",
400-
"language",
401-
"burstHeatmap",
402-
];
403-
const appearanceSettings = [
404-
"fontSize",
405-
"timerStyle",
406-
"liveSpeedStyle",
407-
"liveAccStyle",
408-
"liveBurstStyle",
409-
"timerColor",
410-
"timerOpacity",
411-
"showAllLines",
412-
"keymapMode",
413-
"keymapStyle",
414-
"keymapLegendStyle",
415-
"keymapLayout",
416-
"keymapShowTopRow",
417-
"keymapSize",
418-
"fontFamily",
419-
"smoothLineScroll",
420-
"alwaysShowDecimalPlaces",
421-
"startGraphsAtZero",
422-
"highlightMode",
423-
"tapeMode",
424-
"tapeMargin",
425-
"typingSpeedUnit",
426-
"maxLineWidth",
427-
];
428-
const inputSettings = [
429-
"freedomMode",
430-
"quickEnd",
431-
"layout",
432-
"confidenceMode",
433-
"indicateTypos",
434-
"stopOnError",
435-
"hideExtraLetters",
436-
"strictSpace",
437-
"oppositeShiftMode",
438-
"lazyMode",
439-
"codeUnindentOnBackspace",
440-
];
441-
const soundSettings = ["playSoundOnError", "playSoundOnClick", "soundVolume"];
442-
const hiddenSettings = ["accountChart", "monkey", "monkeyPowerLevel"];
443-
const adsSettings = ["ads"];
444-
445-
if (themeSettings.includes(configFieldName)) {
446-
return "theme";
447-
} else if (hideElementsSettings.includes(configFieldName)) {
448-
return "hideElements";
449-
} else if (caretSettings.includes(configFieldName)) {
450-
return "caret";
451-
} else if (behaviorSettings.includes(configFieldName)) {
452-
return "behavior";
453-
} else if (testSettings.includes(configFieldName)) {
454-
return "test";
455-
} else if (appearanceSettings.includes(configFieldName)) {
456-
return "appearance";
457-
} else if (inputSettings.includes(configFieldName)) {
458-
return "input";
459-
} else if (soundSettings.includes(configFieldName)) {
460-
return "sound";
461-
} else if (hiddenSettings.includes(configFieldName)) {
462-
return "hidden";
463-
} else if (adsSettings.includes(configFieldName)) {
464-
return "ads";
465-
}
466-
467-
Notifications.add(
468-
`Setting group not found for setting ${configFieldName} - it will not be saved. Please report this.`,
469-
-1
470-
);
471-
return null;
340+
function getSettingGroup(configFieldName: ConfigKey): ConfigGroupName {
341+
return ConfigGroupsLiteral[configFieldName];
472342
}
473343

474344
function getPartialConfigChanges(
475345
configChanges: Partial<ConfigType>
476346
): Partial<ConfigType> {
477347
const activeConfigChanges: Partial<ConfigType> = {};
478-
Object.keys(defaultConfig)
348+
349+
(Object.keys(defaultConfig) as ConfigKey[])
479350
.filter((settingName) => {
480351
const group = getSettingGroup(settingName);
481352
if (group === null) return false;
482353
return state.checkboxes.get(group) === true;
483354
})
484355
.forEach((settingName) => {
485-
const safeSettingName = settingName as keyof Partial<ConfigType>;
356+
const safeSettingName = settingName;
486357
const newValue =
487358
configChanges[safeSettingName] !== undefined
488359
? configChanges[safeSettingName]
@@ -492,13 +363,13 @@ function getPartialConfigChanges(
492363
});
493364
return activeConfigChanges;
494365
}
495-
function getActiveSettingGroupsFromState(): ActiveSettingGroups {
496-
return ActiveSettingGroupsSchema.parse(
497-
Array.from(state.checkboxes.entries())
498-
.filter(([, value]) => value)
499-
.map(([key]) => key)
500-
);
366+
367+
function getActiveSettingGroupsFromState(): ConfigGroupName[] {
368+
return Array.from(state.checkboxes.entries())
369+
.filter(([, value]) => value)
370+
.map(([key]) => key);
501371
}
372+
502373
function getConfigChanges(): Partial<ConfigType> {
503374
const activeConfigChanges =
504375
state.presetType === "partial"

0 commit comments

Comments
 (0)