|
1 | 1 | <script lang="ts"> |
2 | 2 | import { Button } from "$lib/components/ui/button"; |
3 | 3 | import { Header } from "$lib/components/ui/header"; |
| 4 | + import * as InputOTP from "$lib/components/ui/input-otp"; |
4 | 5 | import MoveLeft from "@lucide/svelte/icons/move-left"; |
5 | 6 | import { toast } from "svelte-sonner"; |
| 7 | + import { REGEXP_ONLY_DIGITS_AND_CHARS } from "bits-ui"; |
6 | 8 |
|
7 | 9 | import { goto } from "$app/navigation"; |
8 | 10 |
|
9 | 11 | import { io, type Socket } from "socket.io-client"; |
10 | 12 | import type { RoomCreateClientToServerEvents, RoomCreateServerToClientEvents } from "$lib/mathex/schemas"; |
11 | 13 | const socket: Socket<RoomCreateServerToClientEvents, RoomCreateClientToServerEvents> = io("/rooms"); |
12 | 14 |
|
13 | | - let code: string[] = $state(Array.from({ length: 8 }).map(() => "")); |
| 15 | + let code: string = $state(""); |
14 | 16 | let lastCode: string = ""; |
15 | | - async function checkLetter(i: number) { |
16 | | - code[i] = code[i].trim(); |
17 | | - if (code[i].length > 1) { |
18 | | - let rest = code[i].split("").slice(1).join(""); |
19 | | - code[i] = code[i][0].toUpperCase(); |
20 | | - if (code[i + 1] !== undefined) { |
21 | | - code[i + 1] += rest; |
22 | | - document.getElementById("code-input" + (i + 1))?.focus(); |
23 | | - checkLetter(i + 1); |
24 | | - } |
25 | | - } else { |
26 | | - code[i] = code[i].toUpperCase(); |
27 | | - } |
28 | | -
|
29 | | - const codeStr = code.join(""); |
30 | | - if (codeStr.length === 8 && lastCode !== codeStr) { |
31 | | - lastCode = codeStr; |
| 17 | + async function setCode(newCode: string) { |
| 18 | + code = newCode.toUpperCase(); |
| 19 | + if (code.length === 8 && lastCode !== code) { |
| 20 | + lastCode = code; |
32 | 21 | toast.promise( |
33 | 22 | new Promise<void>(async (resolve, reject) => { |
34 | | - if (await socket.emitWithAck("checkRoom", codeStr)) { |
| 23 | + if (await socket.emitWithAck("checkRoom", code)) { |
35 | 24 | resolve(); |
36 | 25 | } else reject(); |
37 | 26 | }), |
38 | 27 | { |
39 | 28 | loading: "Loading...", |
40 | 29 | success() { |
41 | 30 | socket.disconnect(); |
42 | | - goto("/mathex/app/play/" + codeStr); |
| 31 | + goto("/mathex/app/play/" + code); |
43 | 32 | return "Going to room..."; |
44 | 33 | }, |
45 | 34 | error: "That room does not exist! Try typing the room ID again." |
|
52 | 41 | <div class="flex flex-col h-full w-full justify-center items-center"> |
53 | 42 | <Header size="h1">Join Room</Header> |
54 | 43 | <div class="flex gap-1 mt-2"> |
55 | | - {#each code as letter, i} |
56 | | - <!-- svelte-ignore a11y_autofocus --> |
57 | | - <input |
58 | | - type="text" |
59 | | - id="code-input{i}" |
60 | | - class="w-12 h-12 bg-gray-100 block rounded-sm text-slate-900 text-xl text-center" |
61 | | - autofocus={i === 0} |
62 | | - bind:value={code[i]} |
63 | | - onkeydown={async (e) => { |
64 | | - const prevEl = document.getElementById("code-input" + (i - 1)); |
65 | | - if (prevEl && letter === "" && e.key === "Backspace") prevEl.focus(); |
66 | | - }} |
67 | | - oninput={() => checkLetter(i)} |
68 | | - /> |
69 | | - {/each} |
| 44 | + <InputOTP.Root |
| 45 | + maxlength={8} |
| 46 | + spellcheck="false" |
| 47 | + pattern={REGEXP_ONLY_DIGITS_AND_CHARS} |
| 48 | + bind:value={() => code, setCode} |
| 49 | + > |
| 50 | + {#snippet children({ cells })} |
| 51 | + {#each cells as cell (cell)} |
| 52 | + <InputOTP.Group> |
| 53 | + <InputOTP.Slot class="bg-gray-100 text-slate-900" {cell} /> |
| 54 | + </InputOTP.Group> |
| 55 | + {/each} |
| 56 | + {/snippet} |
| 57 | + </InputOTP.Root> |
70 | 58 | </div> |
71 | 59 | <Button variant="link" class="text-white" href="/mathex/app"><MoveLeft class="mr-1" /> Back to home</Button> |
72 | 60 | </div> |
0 commit comments