impr(pace caret): add tags pb mode (@NadAlaba) (#5715)

* impr(pace caret): add tags pb mode (@NadAlaba)

* update modes notice when clear tags from commandline

* remove all mode because it grabs only 1k results

* short circuit tag.active

* update setting name

---------

Co-authored-by: Miodec <jack@monkeytype.com>
This commit is contained in:
Nad Alaba 2024-08-02 22:50:56 +03:00 committed by GitHub
parent 8c7ece3721
commit 738574ed33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 76 additions and 8 deletions

View file

@ -674,8 +674,9 @@
</div>
<div class="text">
Displays a second caret that moves at constant speed. The 'average'
option averages the speed of last 10 results. The 'daily' option takes
the highest speed of the last 24 hours.
option averages the speed of last 10 results. The 'tag pb' option takes
the highest PB of any active tag. The 'daily' option takes the highest
speed of the last 24 hours.
</div>
<div class="inputs">
<div class="inputAndButton">
@ -696,6 +697,7 @@
<button data-config-value="off">off</button>
<button data-config-value="average">avg</button>
<button data-config-value="pb">pb</button>
<button data-config-value="tagPb">tag pb</button>
<button data-config-value="last">last</button>
<button data-config-value="daily">daily</button>
<button data-config-value="custom">custom</button>

View file

@ -24,6 +24,15 @@ const subgroup: MonkeyTypes.CommandsSubgroup = {
TestLogic.restart();
},
},
{
id: "setPaceCaretTagPb",
display: "tag pb",
configValue: "tagPb",
exec: (): void => {
UpdateConfig.setPaceCaret("tagPb");
TestLogic.restart();
},
},
{
id: "setPaceCaretLast",
display: "last",

View file

@ -56,7 +56,7 @@ function update(): void {
display: `Clear tags`,
icon: "fa-times",
sticky: true,
exec: (): void => {
exec: async (): Promise<void> => {
const snapshot = DB.getSnapshot();
if (!snapshot) return;
@ -67,6 +67,13 @@ function update(): void {
});
DB.setSnapshot(snapshot);
if (
Config.paceCaret === "average" ||
Config.paceCaret === "tagPb" ||
Config.paceCaret === "daily"
) {
await PaceCaret.init();
}
void ModesNotice.update();
TagController.saveActiveToLocalStorage();
},
@ -85,12 +92,15 @@ function update(): void {
},
exec: async (): Promise<void> => {
TagController.toggle(tag._id);
void ModesNotice.update();
if (Config.paceCaret === "average") {
if (
Config.paceCaret === "average" ||
Config.paceCaret === "tagPb" ||
Config.paceCaret === "daily"
) {
await PaceCaret.init();
void ModesNotice.update();
}
void ModesNotice.update();
},
});
}

View file

@ -474,8 +474,11 @@ export function setPaceCaret(
}
if (document.readyState === "complete") {
if (val === "pb" && !isAuthenticated()) {
Notifications.add("PB pace caret is unavailable without an account", 0);
if ((val === "pb" || val === "tagPb") && !isAuthenticated()) {
Notifications.add(
`Pace caret "pb" and "tag pb" are unavailable without an account`,
0
);
return false;
}
}

View file

@ -580,6 +580,37 @@ export async function getUserDailyBest<M extends Mode>(
return retval;
}
export async function getActiveTagsPB<M extends Mode>(
mode: M,
mode2: Mode2<M>,
punctuation: boolean,
numbers: boolean,
language: string,
difficulty: Difficulty,
lazyMode: boolean
): Promise<number> {
const snapshot = getSnapshot();
if (!snapshot) return 0;
let tagPbWpm = 0;
for (const tag of snapshot.tags) {
if (!tag.active) continue;
const currTagPB = await getLocalTagPB(
tag._id,
mode,
mode2,
punctuation,
numbers,
language,
difficulty,
lazyMode
);
if (currTagPB > tagPbWpm) tagPbWpm = currTagPB;
}
return tagPbWpm;
}
export async function getLocalPB<M extends Mode>(
mode: M,
mode2: Mode2<M>,

View file

@ -137,6 +137,8 @@ export async function update(): Promise<void> {
? "average"
: Config.paceCaret === "pb"
? "pb"
: Config.paceCaret === "tagPb"
? "tag pb"
: Config.paceCaret === "last"
? "last"
: Config.paceCaret === "daily"

View file

@ -82,6 +82,16 @@ export async function init(): Promise<void> {
Config.lazyMode,
Config.funbox
);
} else if (Config.paceCaret === "tagPb") {
wpm = await DB.getActiveTagsPB(
Config.mode,
mode2,
Config.punctuation,
Config.numbers,
Config.language,
Config.difficulty,
Config.lazyMode
);
} else if (Config.paceCaret === "average") {
[wpm] = await DB.getUserAverage10(
Config.mode,

View file

@ -127,6 +127,7 @@ export const PaceCaretSchema = z.enum([
"off",
"average",
"pb",
"tagPb",
"last",
"custom",
"daily",