anki/ts/congrats/lib.ts
Damien Elmes c039845c16 use singleton + free functions for i18n in ts
This allows for tree shaking, and reduces the congrats page from 150k
with the old enum solution to about 80k.
2021-03-26 20:38:44 +10:00

35 lines
1.1 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import pb from "anki/backend_proto";
import { postRequest } from "anki/postrequest";
import { naturalUnit, unitAmount, unitName } from "anki/time";
import * as tr from "anki/i18n";
export async function getCongratsInfo(): Promise<pb.BackendProto.CongratsInfoOut> {
return pb.BackendProto.CongratsInfoOut.decode(
await postRequest("/_anki/congratsInfo", "")
);
}
export function buildNextLearnMsg(info: pb.BackendProto.CongratsInfoOut): string {
const secsUntil = info.secsUntilNextLearn;
// next learning card not due (/ until tomorrow)?
if (secsUntil == 0 || secsUntil > 86_400) {
return "";
}
const unit = naturalUnit(secsUntil);
const amount = Math.round(unitAmount(unit, secsUntil));
const unitStr = unitName(unit);
const nextLearnDue = tr.schedulingNextLearnDue({
amount,
unit: unitStr,
});
const remaining = tr.schedulingLearnRemaining({
remaining: info.learnRemaining,
});
return `${nextLearnDue} ${remaining}`;
}