refactor: move pb crown aria-label text update to a function

This commit is contained in:
Miodec 2024-08-05 12:41:27 +02:00
parent f0441e2b66
commit cbd6725ad6

View file

@ -370,6 +370,14 @@ export function showCrown(type: PbCrown.CrownType): void {
PbCrown.update(type);
}
export function updateCrownText(text: string, wide = false): void {
$("#result .stats .wpm .crown").attr("aria-label", text);
$("#result .stats .wpm .crown").attr(
"data-balloon-length",
wide ? "medium" : ""
);
}
export async function updateCrown(dontSave: boolean): Promise<void> {
if (Config.mode === "quote" || dontSave) {
hideCrown();
@ -396,8 +404,7 @@ export async function updateCrown(dontSave: boolean): Promise<void> {
} else {
//show half crown as the pb is not confirmed by the server
showCrown("pending");
$("#result .stats .wpm .crown").attr(
"aria-label",
updateCrownText(
"+" + Format.typingSpeed(pbDiff, { showDecimalPlaces: true })
);
}
@ -416,17 +423,17 @@ export async function updateCrown(dontSave: boolean): Promise<void> {
if (pbDiff <= 0) {
// hideCrown();
showCrown("warning");
$("#result .stats .wpm .crown").attr(
"aria-label",
`This result is not eligible for a new PB (${canGetPb.reason})`
updateCrownText(
`This result is not eligible for a new PB (${canGetPb.reason})`,
true
);
} else {
showCrown("ineligible");
$("#result .stats .wpm .crown").attr(
"aria-label",
updateCrownText(
`You could've gotten a new PB (+${Format.typingSpeed(pbDiff, {
showDecimalPlaces: true,
})}), but your config does not allow it (${canGetPb.reason})`
})}), but your config does not allow it (${canGetPb.reason})`,
true
);
}
}
@ -434,16 +441,16 @@ export async function updateCrown(dontSave: boolean): Promise<void> {
export function hideCrown(): void {
PbCrown.hide();
$("#result .stats .wpm .crown").attr("aria-label", "");
updateCrownText("");
}
export function showErrorCrownIfNeeded(): void {
if (PbCrown.getCurrentType() !== "pending") return;
PbCrown.show();
PbCrown.update("error");
$("#result .stats .wpm .crown").attr(
"aria-label",
`Local PB data is out of sync with the server - please refresh (pb mismatch)`
updateCrownText(
`Local PB data is out of sync with the server - please refresh (pb mismatch)`,
true
);
}