From cbd6725ad695c1979e96ce19a9be4cc477e4ecca Mon Sep 17 00:00:00 2001 From: Miodec Date: Mon, 5 Aug 2024 12:41:27 +0200 Subject: [PATCH] refactor: move pb crown aria-label text update to a function --- frontend/src/ts/test/result.ts | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/frontend/src/ts/test/result.ts b/frontend/src/ts/test/result.ts index f55417af5..34450e07f 100644 --- a/frontend/src/ts/test/result.ts +++ b/frontend/src/ts/test/result.ts @@ -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 { if (Config.mode === "quote" || dontSave) { hideCrown(); @@ -396,8 +404,7 @@ export async function updateCrown(dontSave: boolean): Promise { } 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 { 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 { 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 ); }