Skip to content

Commit af250ef

Browse files
committed
feat: migrate mathex player ui to Svelte 5
1 parent fe4f2bc commit af250ef

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

src/routes/(mathex)/mathex/app/play/[id]/+page.svelte

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<!-- @migration-task Error while migrating Svelte code: can't migrate `let confetti = false;` to `$state` because there's a variable named state.
2-
Rename the variable and try again or migrate by hand. -->
31
<script lang="ts">
42
import { page } from "$app/state";
53
import { io, type Socket } from "socket.io-client";
@@ -24,15 +22,15 @@
2422
import LoaderCircle from "@lucide/svelte/icons/loader-circle";
2523
2624
import { Confetti } from "svelte-confetti";
27-
let confetti = false;
25+
let confetti = $state(false);
2826
2927
import DOMPurify from "dompurify";
3028
3129
const roomId = page.params.id;
3230
33-
let state: State = "connecting";
31+
let gameState: State = $state("connecting");
3432
35-
let name: string = "";
33+
let name: string = $state("");
3634
3735
import { toast } from "svelte-sonner";
3836
import type { z } from "zod";
@@ -43,39 +41,39 @@
4341
toast[type](message);
4442
});
4543
socket.on("connect", () => {
46-
if (state === "connecting") state = "choose-name";
44+
if (gameState === "connecting") gameState = "choose-name";
4745
toast.success("Connected!");
4846
});
4947
socket.on("connect_error", () => toast.error("Failed to connect! Does this room exist?"));
5048
socket.on("disconnect", () => toast.warning("Disconnected!"));
5149
52-
socket.on("lobby", () => (state = "waiting_start"));
50+
socket.on("lobby", () => (gameState = "waiting_start"));
5351
54-
let answer: z.infer<typeof Question>["data"]["solutions"][number] | null = null;
52+
let answer: z.infer<typeof Question>["data"]["solutions"][number] | null = $state(null);
5553
56-
let running: number | false = false;
57-
let runningVisible = 0;
54+
let running: number | false = $state(false);
55+
let runningVisible = $state(0);
5856
let currentQuestion: {
5957
number: number;
6058
content: string;
6159
type: z.infer<typeof Question>["type"];
62-
} = {
60+
} = $state({
6361
number: 0,
6462
content: "<p>Loading...</p>",
6563
type: "text"
66-
};
64+
});
6765
let startingTime: number | null = null;
68-
let timePassed = 0;
66+
let timePassed = $state(0);
6967
setInterval(() => {
7068
if (!startingTime) timePassed = 0;
7169
else timePassed = Date.now() - startingTime;
7270
}, 100);
7371
socket.on("gameStart", () => {
74-
state = "started";
72+
gameState = "started";
7573
startingTime = Date.now();
7674
});
7775
socket.on("gameFinish", () => {
78-
state = "finished";
76+
gameState = "finished";
7977
});
8078
socket.on("confetti", () => (confetti = true));
8179
socket.on("newQuestion", (content, type) => {
@@ -95,15 +93,15 @@
9593
});
9694
});
9795
socket.on("stopRunning", () => (running = false));
98-
let questionCount = 1;
96+
let questionCount = $state(1);
9997
socket.on("questionCount", (data) => (questionCount = data));
10098
</script>
10199

102-
{#if state === "connecting"}
100+
{#if gameState === "connecting"}
103101
<span class="animate-pulse font-bold text-5xl flex text-center items-center w-full h-full justify-center"
104102
>Connecting...</span
105103
>
106-
{:else if state === "choose-name"}
104+
{:else if gameState === "choose-name"}
107105
<div class="w-full h-full flex justify-center items-center align-middle text-center">
108106
<div class="flex flex-col rounded bg-white text-slate-900 w-min text-center p-2">
109107
{#if name}
@@ -117,11 +115,11 @@
117115
<Button class="mt-2" onclick={() => socket.emit("join", name)}>Join</Button>
118116
</div>
119117
</div>
120-
{:else if state === "waiting_start"}
118+
{:else if gameState === "waiting_start"}
121119
<span class="animate-pulse font-bold text-4xl flex text-center items-center w-full h-full justify-center"
122120
>Waiting for game to start...</span
123121
>
124-
{:else if state === "started"}
122+
{:else if gameState === "started"}
125123
<div class="p-3 bg-white text-slate-900 rounded mb-2 flex items-center">
126124
<div class="text-3xl text-center">{msToMinutesAndSeconds(timePassed)}</div>
127125
</div>
@@ -156,7 +154,7 @@
156154
>
157155
</div>
158156
{/if}
159-
{:else if state === "finished"}
157+
{:else if gameState === "finished"}
160158
<div class="rounded p-2 bg-white text-slate-900">
161159
<Header size="h1">Game finished!</Header>
162160
<p>The host may communicate more information to you via alerts. They will appear at the bottom right.</p>

0 commit comments

Comments
 (0)