-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add typing practice page (#556)
- Loading branch information
Showing
5 changed files
with
131 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useTinyForm } from "@hiogawa/tiny-form/dist/react"; | ||
import { zip } from "@hiogawa/utils"; | ||
import { z } from "zod"; | ||
import { useUrlQuerySchema } from "../../utils/loader-utils"; | ||
import { cls } from "../../utils/misc"; | ||
import { PageHandle } from "../../utils/page-handle"; | ||
|
||
export const handle: PageHandle = { | ||
navBarTitle: () => "Typing Practice", | ||
}; | ||
|
||
export default function Page() { | ||
const [query, setQuery] = useUrlQuerySchema( | ||
z.object({ | ||
test: z | ||
.string() | ||
.default("") | ||
.transform((s) => s.trim().split(/\s+/).join(" ")), | ||
}) | ||
); | ||
const form = useTinyForm({ test: query.test, answer: "" }); | ||
|
||
const matches = zip([...form.data.test], [...form.data.answer]).map( | ||
([x, y]) => ({ | ||
ok: x === y, | ||
x, | ||
y, | ||
}) | ||
); | ||
const ok = matches.every((m) => m.ok); | ||
|
||
return ( | ||
<div className="w-full flex justify-center"> | ||
<div className="w-full max-w-2xl flex flex-col"> | ||
<div className="p-6 flex flex-col gap-3"> | ||
<form | ||
className="w-full flex flex-col gap-5" | ||
onSubmit={form.handleSubmit(() => {})} | ||
> | ||
<div className="flex flex-col gap-2"> | ||
<div className="flex items-center gap-2">Reference</div> | ||
<div className="flex flex-col relative"> | ||
<textarea | ||
className={cls( | ||
"antd-input p-1", | ||
!ok && "border-colorErrorBorderHover" | ||
)} | ||
value={form.fields.test.value} | ||
onChange={(e) => { | ||
const value = e.target.value; | ||
form.fields.test.onChange(value); | ||
setQuery({ test: value }, { replace: true }); | ||
}} | ||
/> | ||
{/* use "div" and "span" with same geometry to highlight mismatch over textarea */} | ||
{/* TODO: how to hande new line? (or filter out new line from test input?) */} | ||
<div | ||
className="absolute pointer-events-none absolute p-1 border border-transparent text-transparent" | ||
data-testid="typing-mismatch-overlay" | ||
> | ||
{matches.map((m, i) => ( | ||
<span | ||
key={i} | ||
className={cls( | ||
!m.ok && "border-b-2 border-colorErrorText opacity-80" | ||
)} | ||
> | ||
{m.x} | ||
</span> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
<div className="flex flex-col gap-2"> | ||
<div className="flex items-center gap-2">Practice Input</div> | ||
<textarea | ||
className="antd-input p-1" | ||
{...form.fields.answer.props()} | ||
/> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters