@@ -4,26 +4,38 @@ import { useState, useEffect } from 'react';
4
4
import { useRouter } from 'next/navigation' ;
5
5
import { User , Code } from 'lucide-react' ;
6
6
import { consumeMessageFromQueue } from '@/lib/rabbitmq' ;
7
+ import { Button } from '@/components/ui/button' ;
8
+ import { useAuthStore } from '@/state/useAuthStore' ;
7
9
8
10
export default function LoadingPage ( ) {
9
11
const [ elapsedTime , setElapsedTime ] = useState ( 0 ) ;
10
12
const [ usersWaiting , setUsersWaiting ] = useState ( 4 ) ;
13
+ const [ matchStatus , setMatchStatus ] = useState ( 'searching' ) ;
11
14
const router = useRouter ( ) ;
15
+ const { user } = useAuthStore ( ) ;
12
16
13
17
const startConsumingMessages = async ( ) => {
14
18
try {
15
- await consumeMessageFromQueue ( ) . then ( ( message ) => {
16
- // This function is called when a message is consumed
17
- if ( message . status == 'matched' ) {
18
- console . log ( 'Match found, your partner is' ) ;
19
- router . push ( '/' ) ;
19
+ await consumeMessageFromQueue ( user ?. id ! ) . then ( ( message ) => {
20
+ if ( message . status === 'matched' ) {
21
+ console . log ( 'Match found, your partner is' , message . partner ) ;
22
+ setMatchStatus ( 'matched' ) ;
23
+
24
+ // setTimeout(() => {
25
+ // router.push(`/collaboration`);
26
+ // }, 2000);
20
27
} else {
21
28
console . log ( 'Match failed' ) ;
22
- router . push ( '/' ) ;
29
+ setMatchStatus ( 'failed' ) ;
30
+
31
+ // setTimeout(() => {
32
+ // router.push(`/`);
33
+ // }, 4500);
23
34
}
24
35
} ) ;
25
36
} catch ( error ) {
26
37
console . error ( 'Error consuming message:' , error ) ;
38
+ setMatchStatus ( 'error' ) ;
27
39
}
28
40
} ;
29
41
@@ -37,12 +49,17 @@ export default function LoadingPage() {
37
49
} , [ ] ) ;
38
50
39
51
useEffect ( ( ) => {
40
- if ( elapsedTime >= 60 ) {
41
- // Execute your action here
42
- console . log ( 'Elapsed time reached 60 seconds. Going back to main page' ) ;
43
- router . push ( '/' ) ;
52
+ if ( elapsedTime >= 60 && matchStatus === 'searching' ) {
53
+ console . log ( 'Elapsed time reached 60 seconds. Match timed out.' ) ;
54
+ setMatchStatus ( 'timeout' ) ;
44
55
}
45
- } , [ elapsedTime ] ) ;
56
+ } , [ elapsedTime , matchStatus ] ) ;
57
+
58
+ const handleCancel = ( ) => {
59
+ // Implement logic to cancel the matching process
60
+ console . log ( 'Matching cancelled' ) ;
61
+ router . push ( '/' ) ;
62
+ } ;
46
63
47
64
return (
48
65
< div className = "flex min-h-screen flex-col bg-[#1a1f2e] text-gray-300" >
@@ -54,30 +71,94 @@ export default function LoadingPage() {
54
71
< User className = "h-6 w-6" />
55
72
</ header >
56
73
< main className = "flex flex-grow flex-col items-center justify-center space-y-6 px-4" >
57
- < div className = "h-16 w-16 animate-spin rounded-full border-b-4 border-t-4 border-purple-500" > </ div >
58
- < h1 className = "text-2xl font-bold text-white" > Finding a match</ h1 >
59
- < p className = "max-w-md text-center text-sm" >
60
- We're pairing you with another coder. This may take a few
61
- moments.
62
- </ p >
63
- < div className = "w-full max-w-md space-y-2" >
64
- < div className = "h-1 overflow-hidden rounded-full bg-gray-700" >
65
- < div
66
- className = "h-full rounded-full bg-purple-500"
67
- style = { { width : `${ ( ( elapsedTime % 60 ) / 60 ) * 100 } %` } }
68
- > </ div >
69
- </ div >
70
- < div className = "text-center text-sm" >
71
- Time elapsed: { elapsedTime } seconds
72
- </ div >
73
- </ div >
74
- < div className = "flex items-center space-x-2 text-sm" >
75
- < User className = "h-4 w-4" />
76
- < span > { usersWaiting } users waiting</ span >
77
- </ div >
78
- < button className = "w-full max-w-md rounded-md bg-purple-600 px-4 py-2 text-white transition-colors hover:bg-purple-700" >
79
- Cancel Matching
80
- </ button >
74
+ { matchStatus === 'searching' && (
75
+ < >
76
+ < div className = "h-16 w-16 animate-spin rounded-full border-b-4 border-t-4 border-purple-500" > </ div >
77
+ < h1 className = "text-2xl font-bold text-white" > Finding a match</ h1 >
78
+ < p className = "max-w-md text-center text-sm" >
79
+ We're pairing you with another coder. This may take a few
80
+ moments.
81
+ </ p >
82
+ < div className = "w-full max-w-md space-y-2" >
83
+ < div className = "h-1 overflow-hidden rounded-full bg-gray-700" >
84
+ < div
85
+ className = "h-full rounded-full bg-purple-500"
86
+ style = { { width : `${ ( ( elapsedTime % 60 ) / 60 ) * 100 } %` } }
87
+ > </ div >
88
+ </ div >
89
+ < div className = "text-center text-sm" >
90
+ Time elapsed: { elapsedTime } seconds
91
+ </ div >
92
+ </ div >
93
+ < div className = "flex items-center space-x-2 text-sm" >
94
+ < User className = "h-4 w-4" />
95
+ < span > { usersWaiting } users waiting</ span >
96
+ </ div >
97
+ < Button
98
+ onClick = { handleCancel }
99
+ className = "w-full max-w-md bg-purple-600 hover:bg-purple-700"
100
+ >
101
+ Cancel Matching
102
+ </ Button >
103
+ </ >
104
+ ) }
105
+ { matchStatus === 'matched' && (
106
+ < >
107
+ < div className = "h-16 w-16 animate-bounce text-4xl" > 🎉</ div >
108
+ < h1 className = "text-2xl font-bold text-white" > Match Found!</ h1 >
109
+ < p className = "max-w-md text-center text-sm" >
110
+ Great news! We've found a coding partner for you. Redirecting
111
+ to your collaboration room...
112
+ </ p >
113
+ </ >
114
+ ) }
115
+ { matchStatus === 'failed' && (
116
+ < >
117
+ < div className = "h-16 w-16 text-4xl" > 😕</ div >
118
+ < h1 className = "text-2xl font-bold text-white" > Match Failed</ h1 >
119
+ < p className = "max-w-md text-center text-sm" >
120
+ We couldn't find a suitable match at this time. Please try
121
+ again later.
122
+ </ p >
123
+ < Button
124
+ onClick = { ( ) => router . push ( '/' ) }
125
+ className = "w-full max-w-md bg-purple-600 hover:bg-purple-700"
126
+ >
127
+ Return to Home
128
+ </ Button >
129
+ </ >
130
+ ) }
131
+ { matchStatus === 'timeout' && (
132
+ < >
133
+ < div className = "h-16 w-16 text-4xl" > ⏳</ div >
134
+ < h1 className = "text-2xl font-bold text-white" > Match Timed Out</ h1 >
135
+ < p className = "max-w-md text-center text-sm" >
136
+ We couldn't find a match within the time limit. Please try
137
+ again.
138
+ </ p >
139
+ < Button
140
+ onClick = { ( ) => router . push ( '/' ) }
141
+ className = "w-full max-w-md bg-purple-600 hover:bg-purple-700"
142
+ >
143
+ Return to Home
144
+ </ Button >
145
+ </ >
146
+ ) }
147
+ { matchStatus === 'error' && (
148
+ < >
149
+ < div className = "h-16 w-16 text-4xl" > ❌</ div >
150
+ < h1 className = "text-2xl font-bold text-white" > Error Occurred</ h1 >
151
+ < p className = "max-w-md text-center text-sm" >
152
+ An error occurred while finding a match. Please try again later.
153
+ </ p >
154
+ < Button
155
+ onClick = { ( ) => router . push ( '/' ) }
156
+ className = "w-full max-w-md bg-purple-600 hover:bg-purple-700"
157
+ >
158
+ Return to Home
159
+ </ Button >
160
+ </ >
161
+ ) }
81
162
< p className = "mt-4 text-sm text-gray-500" >
82
163
Tip: While you wait, why not review some coding concepts?
83
164
</ p >
0 commit comments