Skip to content

Commit dbafdf7

Browse files
committed
Implement functionality for finding a match from message
1 parent 10da8f5 commit dbafdf7

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

apps/frontend/src/app/match/page.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import { WebSocketContext } from "@/contexts/websocketcontext";
1111

1212
export default function ProfilePage(): JSX.Element {
1313
const matcher = useContext(WebSocketContext)! // ! is a null-check
14-
if (matcher.state == "matching") {
15-
16-
}
1714
let button;
1815
switch (matcher.state) {
1916
case "closed":
@@ -29,9 +26,9 @@ export default function ProfilePage(): JSX.Element {
2926
button = <Button loading>{matcher.state}</Button>
3027
break;
3128

32-
case "ready":
33-
button = <Button disabled>Ok</Button>
34-
break;
29+
// case "ready":
30+
// button = <Button disabled>Ok</Button>
31+
// break;
3532
}
3633

3734

@@ -40,6 +37,7 @@ export default function ProfilePage(): JSX.Element {
4037
<Header selectedKey={["0"]} />
4138
<Content className="content">
4239
{button}
40+
{"found" in matcher && <Button onClick={matcher.ok}>Found: {matcher.found}</Button>}
4341
<p>{matcher.state.toUpperCase()}</p>
4442
</Content>
4543
</Layout>
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client"
22

33
import { ReactNode, useEffect, useState } from "react"
4-
import { type SocketState, WebSocketContext } from "@/contexts/websocketcontext";
4+
import { type FoundState, type SocketState, WebSocketContext } from "@/contexts/websocketcontext";
55
import useWebSocket, { ReadyState } from "react-use-websocket"
66
const MATCHING_SERVICE_URL = process.env.NEXT_PUBLIC_MATCHING_SERVICE_URL;
77

@@ -11,33 +11,46 @@ if (MATCHING_SERVICE_URL == undefined) {
1111

1212
export default function WebSocketProvider({children}: {children: ReactNode}) {
1313
const [open, setOpen] = useState(false)
14+
const [found, setFound] = useState<FoundState>({})
1415
const { readyState: socketState, lastMessage, sendMessage } = useWebSocket(MATCHING_SERVICE_URL as string, {}, open);
16+
1517
function cancel() {
1618
setOpen(false)
1719
}
18-
1920
function start() {
2021
setOpen(true)
22+
sendMessage("do match");
2123
}
24+
function ok() {
25+
setFound({});
26+
}
27+
28+
// Acts as a message event hook
29+
useEffect(() => {
30+
if (lastMessage == null) {
31+
return;
32+
}
33+
setFound({found: lastMessage.data, ok })
34+
}, [lastMessage])
2235

23-
let ret: SocketState;
36+
let matchState: SocketState;
2437
switch (socketState) {
2538
case ReadyState.CLOSED:
2639
case ReadyState.UNINSTANTIATED:
27-
ret = {state: "closed", start}
40+
matchState = {state: "closed", start}
2841
break;
2942
case ReadyState.OPEN:
30-
ret = {state: "matching", cancel}
43+
matchState = {state: "matching", cancel}
3144
break;
3245
case ReadyState.CONNECTING:
33-
ret = {state: "starting"}
46+
matchState = {state: "starting"}
3447
break;
3548
case ReadyState.CLOSING:
36-
ret = {state: "cancelling"}
49+
matchState = {state: "cancelling"}
3750
break;
3851
}
3952

40-
return <WebSocketContext.Provider value={ret}>
53+
return <WebSocketContext.Provider value={{...found, ...matchState}}>
4154
{children}
4255
</WebSocketContext.Provider>
4356
}

apps/frontend/src/contexts/websocketcontext.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ export type SocketState = {
88
} | {
99
state: "matching";
1010
cancel(): void;
11-
} | {
12-
state: "ready";
13-
id: string;
11+
};
12+
13+
export type FoundState = {} | {
14+
found: string;
1415
ok(): void;
1516
};
1617

17-
export const WebSocketContext = createContext<SocketState | null>(null);
18+
type MatchState = SocketState & FoundState;
19+
20+
export const WebSocketContext = createContext<MatchState | null>(null);
1821

0 commit comments

Comments
 (0)