Skip to content

Commit d891056

Browse files
authored
Merge pull request #285 from joesabbagh1/main
#280 | Race not getting stuck anymore and redirecting correctly
2 parents 194a376 + c9703b3 commit d891056

File tree

1 file changed

+11
-50
lines changed

1 file changed

+11
-50
lines changed

src/app/race/Race.tsx

+11-50
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,8 @@ import type { User } from "next-auth";
55
import { Button } from "@/components/ui/button";
66
import { useRouter } from "next/navigation";
77
import { Snippet } from "@prisma/client";
8-
import {
9-
Tooltip,
10-
TooltipContent,
11-
TooltipProvider,
12-
TooltipTrigger,
13-
} from "@/components/ui/tooltip";
14-
import {
15-
calculateAccuracy,
16-
calculateCPM,
17-
createIndent,
18-
calculateRemainder,
19-
previousLines,
20-
} from "./utils";
8+
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
9+
import { calculateAccuracy, calculateCPM, createIndent, calculateRemainder, previousLines } from "./utils";
2110

2211
import { Heading } from "@/components/ui/heading";
2312
import { saveUserResultAction } from "../_actions/user";
@@ -47,25 +36,16 @@ export default function Race({ user, snippet }: RaceProps) {
4736
const lines = code.split("\n");
4837

4938
useEffect(() => {
50-
// Debug
51-
// console.log(JSON.stringify(input));
52-
// console.log(JSON.stringify(code));
53-
console.log(input.length);
54-
console.log(code.length);
55-
// console.log("Index: " + index);
56-
// console.log("Line Index: " + lineIndex);
57-
// console.log("Line Number: " + line);
58-
// console.log(lines[line]);
59-
6039
// Focus element
6140
if (inputElement.current !== null) {
6241
inputElement.current.focus();
6342
}
6443

6544
// Calculate result
66-
if (input.length === code.length && input === code) {
45+
if (input.length === code.length && input === code && !endTime) {
6746
setEndTime(new Date());
6847
}
48+
6949
if (startTime && endTime) {
7050
const timeTaken = (endTime.getTime() - startTime.getTime()) / 1000;
7151

@@ -79,8 +59,7 @@ export default function Race({ user, snippet }: RaceProps) {
7959
accuracy: calculateAccuracy(code.length - 1, errorTotal),
8060
snippetId: snippet.id,
8161
});
82-
83-
router.push("/result");
62+
router.push(`/result?snippetId=${snippet.id}`);
8463
}
8564

8665
// Set Errors
@@ -91,18 +70,7 @@ export default function Race({ user, snippet }: RaceProps) {
9170
.filter((index) => index !== -1);
9271
return newErrors;
9372
});
94-
}, [
95-
endTime,
96-
startTime,
97-
user,
98-
errors.length,
99-
errorTotal,
100-
code.length,
101-
router,
102-
snippet.id,
103-
input,
104-
code,
105-
]);
73+
}, [startTime, endTime, user, errors.length, errorTotal, code.length, snippet.id, input, code]);
10674

10775
// Reset Race
10876
useEffect(() => {
@@ -221,19 +189,19 @@ export default function Race({ user, snippet }: RaceProps) {
221189
const indent = createIndent(nextLine);
222190

223191
// Stop at end of line if not matching
224-
if (input.length - 1 === code.length) {
192+
if (input.length === code.length) {
225193
return;
226194
}
227195

228196
if (counter == ln.length - 1) {
229197
setCounter(indent.length);
230-
setInput(input + e.key + "\n" + indent);
198+
setInput(input + e.key + indent);
231199
if (line < lines.length - 1) {
232200
setLine(line + 1);
233201
}
234202
} else if (ln.length - 1 == counter - previousLines(lines, line).length) {
235203
setCounter(indent.length);
236-
setInput(input + e.key + "\n" + indent);
204+
setInput(input + e.key + indent);
237205
if (line < lines.length - 1) {
238206
setLine(line + 1);
239207
}
@@ -260,16 +228,9 @@ export default function Race({ user, snippet }: RaceProps) {
260228
onClick={focusOnLoad}
261229
role="none" // eslint fix - will remove the semantic meaning of an element while still exposing it to assistive technology
262230
>
263-
<RaceTracker
264-
codeLength={code.length}
265-
inputLength={input.length}
266-
user={user}
267-
/>
231+
<RaceTracker codeLength={code.length} inputLength={input.length} user={user} />
268232
<div className="mb-2 md:mb-4">
269-
<Heading
270-
title="Type this code"
271-
description="Start typing to get racing"
272-
/>
233+
<Heading title="Type this code" description="Start typing to get racing" />
273234
</div>
274235
<Code code={code} errors={errors} userInput={input} />
275236
<input

0 commit comments

Comments
 (0)