Skip to content

Commit a7f4dcf

Browse files
committed
fix: unhelpful error messages when custom background image was not a valid url
1 parent 248de12 commit a7f4dcf

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

frontend/src/ts/commandline/lists.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ import * as TestStats from "../test/test-stats";
106106
import * as QuoteSearchModal from "../modals/quote-search";
107107
import * as FPSCounter from "../elements/fps-counter";
108108
import { migrateConfig } from "../utils/config";
109-
import { PartialConfigSchema } from "@monkeytype/contracts/schemas/configs";
109+
import {
110+
CustomBackgroundSchema,
111+
PartialConfigSchema,
112+
} from "@monkeytype/contracts/schemas/configs";
110113
import { Command, CommandsSubgroup } from "./types";
111114
import { parseWithSchema as parseJsonWithSchema } from "@monkeytype/util/json";
112115

@@ -307,6 +310,14 @@ export const commands: CommandsSubgroup = {
307310
},
308311
input: true,
309312
exec: ({ input }): void => {
313+
const parsed = CustomBackgroundSchema.safeParse(input);
314+
if (!parsed.success) {
315+
Notifications.add(
316+
`Invalid custom background URL (${parsed.error.issues[0]?.message})`,
317+
0
318+
);
319+
return;
320+
}
310321
UpdateConfig.setCustomBackground(input ?? "");
311322
},
312323
},

frontend/src/ts/pages/settings.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as Skeleton from "../utils/skeleton";
2222
import * as CustomBackgroundFilter from "../elements/custom-background-filter";
2323
import {
2424
ConfigValue,
25+
CustomBackgroundSchema,
2526
CustomLayoutFluid,
2627
} from "@monkeytype/contracts/schemas/configs";
2728
import {
@@ -1108,11 +1109,21 @@ $(".pageSettings .sectionGroupTitle").on("click", (e) => {
11081109
$(
11091110
".pageSettings .section[data-config-name='customBackgroundSize'] .inputAndButton button.save"
11101111
).on("click", () => {
1111-
UpdateConfig.setCustomBackground(
1112-
$(
1113-
".pageSettings .section[data-config-name='customBackgroundSize'] .inputAndButton input"
1114-
).val() as string
1115-
);
1112+
const newVal = $(
1113+
".pageSettings .section[data-config-name='customBackgroundSize'] .inputAndButton input"
1114+
).val() as string;
1115+
1116+
const parsed = CustomBackgroundSchema.safeParse(newVal);
1117+
1118+
if (!parsed.success) {
1119+
Notifications.add(
1120+
`Invalid custom background URL (${parsed.error.issues[0]?.message})`,
1121+
0
1122+
);
1123+
return;
1124+
}
1125+
1126+
UpdateConfig.setCustomBackground(newVal);
11161127
});
11171128

11181129
$(
@@ -1125,11 +1136,21 @@ $(
11251136
".pageSettings .section[data-config-name='customBackgroundSize'] .inputAndButton input"
11261137
).on("keypress", (e) => {
11271138
if (e.key === "Enter") {
1128-
UpdateConfig.setCustomBackground(
1129-
$(
1130-
".pageSettings .section[data-config-name='customBackgroundSize'] .inputAndButton input"
1131-
).val() as string
1132-
);
1139+
const newVal = $(
1140+
".pageSettings .section[data-config-name='customBackgroundSize'] .inputAndButton input"
1141+
).val() as string;
1142+
1143+
const parsed = CustomBackgroundSchema.safeParse(newVal);
1144+
1145+
if (!parsed.success) {
1146+
Notifications.add(
1147+
`Invalid custom background URL (${parsed.error.issues[0]?.message})`,
1148+
0
1149+
);
1150+
return;
1151+
}
1152+
1153+
UpdateConfig.setCustomBackground(newVal);
11331154
}
11341155
});
11351156

0 commit comments

Comments
 (0)