mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-09-08 07:36:55 +08:00
impr(funbox): notify user if polyglot languages are not valid
This commit is contained in:
parent
2835a02bbb
commit
0bd49db8e5
3 changed files with 49 additions and 5 deletions
|
@ -108,6 +108,8 @@ import {
|
|||
} from "@monkeytype/contracts/schemas/configs";
|
||||
import { Command, CommandsSubgroup } from "./types";
|
||||
import { parseWithSchema as parseJsonWithSchema } from "@monkeytype/util/json";
|
||||
import * as TestLogic from "../test/test-logic";
|
||||
import * as ActivePage from "../states/active-page";
|
||||
|
||||
const languagesPromise = JSONData.getLanguageList();
|
||||
languagesPromise
|
||||
|
@ -237,6 +239,9 @@ export const commands: CommandsSubgroup = {
|
|||
exec: ({ input }): void => {
|
||||
if (input === undefined) return;
|
||||
void UpdateConfig.setCustomPolyglot(input.split(" "));
|
||||
if (ActivePage.get() === "test") {
|
||||
TestLogic.restart();
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ import { getSection } from "../wikipedia";
|
|||
import * as WeakSpot from "../weak-spot";
|
||||
import * as IPAddresses from "../../utils/ip-addresses";
|
||||
import * as TestState from "../test-state";
|
||||
import { WordGenError } from "../words-generator";
|
||||
|
||||
export type FunboxFunctions = {
|
||||
getWord?: (wordset?: Wordset, wordIndex?: number) => string;
|
||||
|
@ -647,9 +648,45 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
|
|||
},
|
||||
polyglot: {
|
||||
async withWords(_words) {
|
||||
const promises = Config.customPolyglot.map(JSONData.getLanguage);
|
||||
const promises = Config.customPolyglot.map(async (language) =>
|
||||
JSONData.getLanguage(language).catch(() => {
|
||||
Notifications.add(
|
||||
`Failed to load language: ${language}. It will be ignored.`,
|
||||
0
|
||||
);
|
||||
return null; // Return null for failed languages
|
||||
})
|
||||
);
|
||||
|
||||
const languages = (await Promise.all(promises)).filter(
|
||||
(lang) => lang !== null
|
||||
);
|
||||
|
||||
if (languages.length === 0) {
|
||||
UpdateConfig.toggleFunbox("polyglot");
|
||||
throw new Error(
|
||||
`No valid languages found. Please check your polyglot languages config (${Config.customPolyglot.join(
|
||||
", "
|
||||
)}).`
|
||||
);
|
||||
}
|
||||
|
||||
if (languages.length === 1) {
|
||||
const lang = languages[0] as JSONData.LanguageObject;
|
||||
UpdateConfig.setLanguage(lang.name, true);
|
||||
UpdateConfig.toggleFunbox("polyglot", true);
|
||||
Notifications.add(
|
||||
`Disabled polyglot funbox because only one valid language was found. Check your polyglot languages config (${Config.customPolyglot.join(
|
||||
", "
|
||||
)}).`,
|
||||
0,
|
||||
{
|
||||
duration: 7,
|
||||
}
|
||||
);
|
||||
throw new WordGenError("");
|
||||
}
|
||||
|
||||
const languages = await Promise.all(promises);
|
||||
const wordSet = languages.flatMap((it) => it.words);
|
||||
Arrays.shuffle(wordSet);
|
||||
return new Wordset(wordSet);
|
||||
|
|
|
@ -470,9 +470,11 @@ export async function init(): Promise<void> {
|
|||
} catch (e) {
|
||||
console.error(e);
|
||||
if (e instanceof WordsGenerator.WordGenError) {
|
||||
Notifications.add(e.message, 0, {
|
||||
important: true,
|
||||
});
|
||||
if (e.message.length > 0) {
|
||||
Notifications.add(e.message, 0, {
|
||||
important: true,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
Notifications.add(
|
||||
Misc.createErrorMessage(e, "Failed to generate words"),
|
||||
|
|
Loading…
Add table
Reference in a new issue