|
1 | 1 | import { type KeyId, useKeyboard } from "@keybr/keyboard";
|
2 | 2 | import { KeyLayer, VirtualKeyboard } from "@keybr/keyboard-ui";
|
| 3 | +import { Tasks } from "@keybr/lang"; |
| 4 | +import { |
| 5 | + type LineList, |
| 6 | + type Step, |
| 7 | + type TextInputSettings, |
| 8 | +} from "@keybr/textinput"; |
3 | 9 | import { type AnyEvent } from "@keybr/textinput-events";
|
| 10 | +import { StaticText } from "@keybr/textinput-ui"; |
| 11 | +import { Box, useDocumentVisibility } from "@keybr/widget"; |
4 | 12 | import { type ReactNode, useEffect, useMemo, useState } from "react";
|
5 |
| -import { ReplayState } from "../session/index.ts"; |
| 13 | +import { ReplayState, Session, type TestResult } from "../session/index.ts"; |
| 14 | +import { type CompositeSettings } from "../settings.ts"; |
| 15 | +import { Progress } from "./Progress.tsx"; |
| 16 | +import * as styles from "./Replay.module.less"; |
6 | 17 |
|
7 | 18 | export function Replay({
|
8 |
| - events, |
| 19 | + settings: { textInput, textDisplay }, |
| 20 | + result: { steps, events }, |
9 | 21 | }: {
|
10 |
| - readonly events: readonly AnyEvent[]; |
| 22 | + readonly settings: CompositeSettings; |
| 23 | + readonly result: TestResult; |
11 | 24 | }): ReactNode {
|
12 | 25 | const keyboard = useKeyboard();
|
13 |
| - const depressedKeys = useReplayState(events); |
| 26 | + const { stepper, lines, depressedKeys } = useReplayState( |
| 27 | + textInput, |
| 28 | + steps, |
| 29 | + events, |
| 30 | + ); |
14 | 31 | return (
|
15 |
| - <VirtualKeyboard keyboard={keyboard} height="16rem"> |
16 |
| - <KeyLayer depressedKeys={depressedKeys} /> |
17 |
| - </VirtualKeyboard> |
| 32 | + <div className={styles.root}> |
| 33 | + <Progress stepper={stepper} /> |
| 34 | + <Box className={styles.text} alignItems="center" justifyContent="center"> |
| 35 | + <StaticText settings={textDisplay} lines={lines} cursor={true} /> |
| 36 | + </Box> |
| 37 | + <VirtualKeyboard keyboard={keyboard} height="16rem"> |
| 38 | + <KeyLayer depressedKeys={depressedKeys} /> |
| 39 | + </VirtualKeyboard> |
| 40 | + </div> |
18 | 41 | );
|
19 | 42 | }
|
20 | 43 |
|
21 |
| -function useReplayState(events: readonly AnyEvent[]) { |
22 |
| - const stepper = useMemo(() => new ReplayState(events), [events]); |
| 44 | +function useReplayState( |
| 45 | + settings: TextInputSettings, |
| 46 | + steps: readonly Step[], |
| 47 | + events: readonly AnyEvent[], |
| 48 | +) { |
| 49 | + const stepper = useMemo( |
| 50 | + () => new ReplayState(settings, steps, events), |
| 51 | + [settings, steps, events], |
| 52 | + ); |
| 53 | + const visible = useDocumentVisibility(); |
| 54 | + const [lines, setLines] = useState<LineList>(Session.emptyLines); |
23 | 55 | const [depressedKeys, setDepressedKeys] = useState<KeyId[]>([]);
|
24 | 56 | useEffect(() => {
|
25 |
| - let id = 0; |
| 57 | + const tasks = new Tasks(); |
26 | 58 | const step = () => {
|
27 | 59 | stepper.step();
|
| 60 | + setLines(stepper.lines); |
28 | 61 | setDepressedKeys(stepper.depressedKeys);
|
29 |
| - id = window.setTimeout(step, stepper.delay); |
| 62 | + tasks.delayed(stepper.delay, step); |
30 | 63 | };
|
31 |
| - setDepressedKeys(stepper.depressedKeys); |
32 |
| - id = window.setTimeout(step, stepper.delay); |
| 64 | + if (visible) { |
| 65 | + stepper.reset(); |
| 66 | + setLines(stepper.lines); |
| 67 | + setDepressedKeys(stepper.depressedKeys); |
| 68 | + tasks.delayed(stepper.delay, step); |
| 69 | + } |
33 | 70 | return () => {
|
34 |
| - window.clearTimeout(id); |
| 71 | + tasks.cancelAll(); |
35 | 72 | };
|
36 |
| - }, [stepper]); |
37 |
| - return depressedKeys; |
| 73 | + }, [stepper, visible]); |
| 74 | + return { stepper, lines, depressedKeys }; |
38 | 75 | }
|
0 commit comments