Skip to content

Commit a6cf7ca

Browse files
committed
feat: format typing speed using the current setting
1 parent 69a7ba1 commit a6cf7ca

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/keybr-lesson-ui/lib/format.ts

+18-21
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ import { type LearningRate } from "@keybr/lesson";
33
import { SpeedUnit, uiProps } from "@keybr/result";
44
import { useSettings } from "@keybr/settings";
55
import { useMemo } from "react";
6-
import { type FormatNumberOptions, useIntl } from "react-intl";
6+
import { useIntl } from "react-intl";
77
import { messages } from "./intl.ts";
88

9-
const f1 = { minimumFractionDigits: 1, maximumFractionDigits: 1 };
10-
const f2 = { minimumFractionDigits: 2, maximumFractionDigits: 2 };
11-
129
export type FormatterOptions = {
1310
readonly unit?: boolean;
1411
};
@@ -28,27 +25,27 @@ export const useFormatter = (): Formatter => {
2825
return useMemo(() => {
2926
const speedUnit = settings.get(uiProps.speedUnit);
3027
const speedUnitName = formatMessage(speedUnit.name);
31-
let opts: FormatNumberOptions;
32-
switch (speedUnit) {
33-
case SpeedUnit.WPM:
34-
opts = f1;
35-
break;
36-
case SpeedUnit.WPS:
37-
opts = f2;
38-
break;
39-
case SpeedUnit.CPM:
40-
opts = f1;
41-
break;
42-
case SpeedUnit.CPS:
43-
opts = f2;
44-
break;
45-
default:
46-
throw new Error();
47-
}
28+
const f1 = { minimumFractionDigits: 1, maximumFractionDigits: 1 };
29+
const f2 = { minimumFractionDigits: 2, maximumFractionDigits: 2 };
4830
const formatSpeed = (
4931
value: number,
5032
{ unit = true }: FormatterOptions = {},
5133
): string => {
34+
let opts;
35+
switch (speedUnit) {
36+
case SpeedUnit.WPM:
37+
opts = f1;
38+
break;
39+
case SpeedUnit.WPS:
40+
opts = f2;
41+
break;
42+
case SpeedUnit.CPM:
43+
opts = f1;
44+
break;
45+
case SpeedUnit.CPS:
46+
opts = f2;
47+
break;
48+
}
5249
const s = formatNumber(speedUnit.measure(value), opts);
5350
if (unit) {
5451
return s + speedUnit.id;

packages/page-practice/lib/settings/lesson/TargetSpeedProp.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function TargetSpeedProp(): ReactNode {
7070
</Dir>
7171
</Field>
7272
<Field>
73-
<Value value={formatSpeed(targetSpeed, { unit: true })} />
73+
<Value value={formatSpeed(targetSpeed)} />
7474
</Field>
7575
</FieldList>
7676
<Explainer>

packages/page-typing-test/lib/components/Report.tsx

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
SpeedHistogram,
66
} from "@keybr/chart";
77
import { useIntlNumbers } from "@keybr/intl";
8+
import { useFormatter } from "@keybr/lesson-ui";
89
import { Screen } from "@keybr/pages-shared";
910
import {
1011
Box,
@@ -38,6 +39,7 @@ export const Report = memo(function Report({
3839
readonly onNext: () => void;
3940
}) {
4041
const { formatNumber, formatPercents } = useIntlNumbers();
42+
const { speedUnit, formatSpeed } = useFormatter();
4143

4244
useHotkeys(["Enter", onNext]);
4345

@@ -53,16 +55,19 @@ export const Report = memo(function Report({
5355
<Box alignItems="center" justifyContent="center">
5456
<Indicator
5557
name="Speed"
56-
value={<Metric value={`${formatNumber(speed / 5, 2)}`} unit="WPM" />}
57-
title="The typing speed in words per minute."
58+
value={
59+
<Metric
60+
value={formatSpeed(speed, { unit: false })}
61+
unit={speedUnit.id}
62+
/>
63+
}
5864
/>
5965
<Separator />
6066
<Indicator
6167
name="Accuracy"
6268
value={
6369
<Metric value={`${formatNumber(accuracy * 100, 2)}`} unit="%" />
6470
}
65-
title="The percentage of characters typed without errors."
6671
/>
6772
</Box>
6873

@@ -146,7 +151,7 @@ function Indicator({
146151
}: {
147152
readonly name: ReactNode;
148153
readonly value: ReactNode;
149-
readonly title: string;
154+
readonly title?: string;
150155
}) {
151156
return (
152157
<div className={styles.indicator} title={title}>

packages/page-typing-test/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@keybr/phonetic-model": "*",
2020
"@keybr/phonetic-model-loader": "*",
2121
"@keybr/rand": "*",
22+
"@keybr/result": "*",
2223
"@keybr/settings": "*",
2324
"@keybr/textinput": "*",
2425
"@keybr/textinput-events": "*",

0 commit comments

Comments
 (0)