Skip to content

Commit 0bd49db

Browse files
committed
impr(funbox): notify user if polyglot languages are not valid
1 parent 2835a02 commit 0bd49db

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

frontend/src/ts/commandline/lists.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ import {
108108
} from "@monkeytype/contracts/schemas/configs";
109109
import { Command, CommandsSubgroup } from "./types";
110110
import { parseWithSchema as parseJsonWithSchema } from "@monkeytype/util/json";
111+
import * as TestLogic from "../test/test-logic";
112+
import * as ActivePage from "../states/active-page";
111113

112114
const languagesPromise = JSONData.getLanguageList();
113115
languagesPromise
@@ -237,6 +239,9 @@ export const commands: CommandsSubgroup = {
237239
exec: ({ input }): void => {
238240
if (input === undefined) return;
239241
void UpdateConfig.setCustomPolyglot(input.split(" "));
242+
if (ActivePage.get() === "test") {
243+
TestLogic.restart();
244+
}
240245
},
241246
},
242247

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { getSection } from "../wikipedia";
2222
import * as WeakSpot from "../weak-spot";
2323
import * as IPAddresses from "../../utils/ip-addresses";
2424
import * as TestState from "../test-state";
25+
import { WordGenError } from "../words-generator";
2526

2627
export type FunboxFunctions = {
2728
getWord?: (wordset?: Wordset, wordIndex?: number) => string;
@@ -647,9 +648,45 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
647648
},
648649
polyglot: {
649650
async withWords(_words) {
650-
const promises = Config.customPolyglot.map(JSONData.getLanguage);
651+
const promises = Config.customPolyglot.map(async (language) =>
652+
JSONData.getLanguage(language).catch(() => {
653+
Notifications.add(
654+
`Failed to load language: ${language}. It will be ignored.`,
655+
0
656+
);
657+
return null; // Return null for failed languages
658+
})
659+
);
660+
661+
const languages = (await Promise.all(promises)).filter(
662+
(lang) => lang !== null
663+
);
664+
665+
if (languages.length === 0) {
666+
UpdateConfig.toggleFunbox("polyglot");
667+
throw new Error(
668+
`No valid languages found. Please check your polyglot languages config (${Config.customPolyglot.join(
669+
", "
670+
)}).`
671+
);
672+
}
673+
674+
if (languages.length === 1) {
675+
const lang = languages[0] as JSONData.LanguageObject;
676+
UpdateConfig.setLanguage(lang.name, true);
677+
UpdateConfig.toggleFunbox("polyglot", true);
678+
Notifications.add(
679+
`Disabled polyglot funbox because only one valid language was found. Check your polyglot languages config (${Config.customPolyglot.join(
680+
", "
681+
)}).`,
682+
0,
683+
{
684+
duration: 7,
685+
}
686+
);
687+
throw new WordGenError("");
688+
}
651689

652-
const languages = await Promise.all(promises);
653690
const wordSet = languages.flatMap((it) => it.words);
654691
Arrays.shuffle(wordSet);
655692
return new Wordset(wordSet);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,11 @@ export async function init(): Promise<void> {
470470
} catch (e) {
471471
console.error(e);
472472
if (e instanceof WordsGenerator.WordGenError) {
473-
Notifications.add(e.message, 0, {
474-
important: true,
475-
});
473+
if (e.message.length > 0) {
474+
Notifications.add(e.message, 0, {
475+
important: true,
476+
});
477+
}
476478
} else {
477479
Notifications.add(
478480
Misc.createErrorMessage(e, "Failed to generate words"),

0 commit comments

Comments
 (0)