@@ -5,19 +5,8 @@ import type { User } from "next-auth";
5
5
import { Button } from "@/components/ui/button" ;
6
6
import { useRouter } from "next/navigation" ;
7
7
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" ;
21
10
22
11
import { Heading } from "@/components/ui/heading" ;
23
12
import { saveUserResultAction } from "../_actions/user" ;
@@ -47,25 +36,16 @@ export default function Race({ user, snippet }: RaceProps) {
47
36
const lines = code . split ( "\n" ) ;
48
37
49
38
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
-
60
39
// Focus element
61
40
if ( inputElement . current !== null ) {
62
41
inputElement . current . focus ( ) ;
63
42
}
64
43
65
44
// Calculate result
66
- if ( input . length === code . length && input === code ) {
45
+ if ( input . length === code . length && input === code && ! endTime ) {
67
46
setEndTime ( new Date ( ) ) ;
68
47
}
48
+
69
49
if ( startTime && endTime ) {
70
50
const timeTaken = ( endTime . getTime ( ) - startTime . getTime ( ) ) / 1000 ;
71
51
@@ -79,8 +59,7 @@ export default function Race({ user, snippet }: RaceProps) {
79
59
accuracy : calculateAccuracy ( code . length - 1 , errorTotal ) ,
80
60
snippetId : snippet . id ,
81
61
} ) ;
82
-
83
- router . push ( "/result" ) ;
62
+ router . push ( `/result?snippetId=${ snippet . id } ` ) ;
84
63
}
85
64
86
65
// Set Errors
@@ -91,18 +70,7 @@ export default function Race({ user, snippet }: RaceProps) {
91
70
. filter ( ( index ) => index !== - 1 ) ;
92
71
return newErrors ;
93
72
} ) ;
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 ] ) ;
106
74
107
75
// Reset Race
108
76
useEffect ( ( ) => {
@@ -221,19 +189,19 @@ export default function Race({ user, snippet }: RaceProps) {
221
189
const indent = createIndent ( nextLine ) ;
222
190
223
191
// Stop at end of line if not matching
224
- if ( input . length - 1 === code . length ) {
192
+ if ( input . length === code . length ) {
225
193
return ;
226
194
}
227
195
228
196
if ( counter == ln . length - 1 ) {
229
197
setCounter ( indent . length ) ;
230
- setInput ( input + e . key + "\n" + indent ) ;
198
+ setInput ( input + e . key + indent ) ;
231
199
if ( line < lines . length - 1 ) {
232
200
setLine ( line + 1 ) ;
233
201
}
234
202
} else if ( ln . length - 1 == counter - previousLines ( lines , line ) . length ) {
235
203
setCounter ( indent . length ) ;
236
- setInput ( input + e . key + "\n" + indent ) ;
204
+ setInput ( input + e . key + indent ) ;
237
205
if ( line < lines . length - 1 ) {
238
206
setLine ( line + 1 ) ;
239
207
}
@@ -260,16 +228,9 @@ export default function Race({ user, snippet }: RaceProps) {
260
228
onClick = { focusOnLoad }
261
229
role = "none" // eslint fix - will remove the semantic meaning of an element while still exposing it to assistive technology
262
230
>
263
- < RaceTracker
264
- codeLength = { code . length }
265
- inputLength = { input . length }
266
- user = { user }
267
- />
231
+ < RaceTracker codeLength = { code . length } inputLength = { input . length } user = { user } />
268
232
< 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" />
273
234
</ div >
274
235
< Code code = { code } errors = { errors } userInput = { input } />
275
236
< input
0 commit comments