Skip to content

Commit d2270ba

Browse files
committed
bugfix: timeout did not direct correct model; hide retry buttons
1 parent 79b3170 commit d2270ba

File tree

6 files changed

+48
-28
lines changed

6 files changed

+48
-28
lines changed

apps/frontend/src/app/matching/MatchingModal.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ interface MatchingModalProps {
2020
const MatchingModal: React.FC<MatchingModalProps> = ({ isOpen, close: _close }) => {
2121
const matchingState = useMatching();
2222
const [closedType, setClosedType] = useState<"finding" | "cancelled" | "joined">("finding");
23+
const [timeoutAfter, setTimeoutAfter] = useState<number>(9999);
2324
const isClosable = ["timeout", "closed"].includes(matchingState.state);
2425

2526
function close() {
2627
// clean up matching and closedType State
2728
if (matchingState.state === "timeout") {
2829
matchingState.ok();
2930
}
31+
setClosedType("finding");
3032
_close();
3133
}
3234

@@ -42,19 +44,27 @@ const MatchingModal: React.FC<MatchingModalProps> = ({ isOpen, close: _close })
4244
setClosedType("finding");
4345
}}
4446
retry={() => {}}
47+
canceledIn={timeoutAfter}
4548
/>;
4649
case "joined":
4750
return <JoinedMatchContent cancel={() => {
4851
setClosedType("cancelled");
4952
}}/>;
5053
}
5154
case 'matching':
52-
return <MatchingInProgressContent cancelMatch={() => {
53-
matchingState.cancel();
54-
setClosedType("cancelled");
55-
}}/>;
55+
return <MatchingInProgressContent
56+
cancelMatch={(timeoutAfter: number) => {
57+
setClosedType("cancelled");
58+
setTimeoutAfter(timeoutAfter);
59+
matchingState.cancel();
60+
}}
61+
timeout={(timeoutAfter: number) => {
62+
matchingState.timeout()
63+
setTimeoutAfter(timeoutAfter);
64+
}}
65+
/>;
5666
case 'cancelling':
57-
return <MatchingInProgressContent cancelMatch={() => {}}/>;
67+
return <MatchingInProgressContent cancelMatch={() => {}} timeout={() => {}}/>;
5868
case 'starting':
5969
return <FindMatchContent beginMatch={() => {}}/>
6070
case 'found':
@@ -69,7 +79,7 @@ const MatchingModal: React.FC<MatchingModalProps> = ({ isOpen, close: _close })
6979
}}
7080
/>
7181
case 'timeout':
72-
return <MatchNotFoundContent reselect={matchingState.ok} retry={() => {}}/>;
82+
return <MatchNotFoundContent reselect={matchingState.ok} retry={() => {}} timedOutIn={timeoutAfter}/>;
7383
default:
7484
throw new Error('Invalid matching state.');
7585
}

apps/frontend/src/app/matching/modalContent/MatchCancelledContent.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { formatTime } from '@/utils/DateTime';
77
interface Props {
88
retry(): void,
99
reselect(): void,
10+
canceledIn: number,
1011
}
1112

12-
const MatchCancelledContent: React.FC<Props> = ({retry, reselect}) => {
13+
const MatchCancelledContent: React.FC<Props> = ({retry, reselect, canceledIn}) => {
1314
return (
1415
<div className="match-cancelled-content">
1516
<div className="cancel-icon-container">
@@ -25,13 +26,13 @@ const MatchCancelledContent: React.FC<Props> = ({retry, reselect}) => {
2526
</div>
2627
<div className="match-status-label">Match Cancelled!</div>
2728
<div className="match-status-message">
28-
Your match request has been cancelled after waiting {formatTime(83)}
29+
Your match request has been cancelled after waiting {formatTime(canceledIn)}
2930
</div>
30-
<button className="retry-match-button"
31+
{/* <button className="retry-match-button"
3132
onClick={retry}
3233
>
3334
Retry
34-
</button>
35+
</button> */}
3536
<button className="reselect-match-options-button"
3637
onClick={reselect}
3738
>

apps/frontend/src/app/matching/modalContent/MatchNotFoundContent.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { handleReselectMatchOptions, handleRetryMatch } from '../handlers';
55
import { formatTime } from '@/utils/DateTime';
66

77
const MatchNotFoundContent: React.FC<{
8-
retry(): void
9-
reselect(): void
8+
retry(): void,
9+
reselect(): void,
10+
timedOutIn: number,
1011
}> = ({
11-
retry, reselect
12+
retry, reselect, timedOutIn
1213
}) => {
1314
return (
1415
<div className="joined-match-content">
@@ -25,13 +26,13 @@ const MatchNotFoundContent: React.FC<{
2526
</div>
2627
<div className="match-status-label">Match Not Found!</div>
2728
<div className="match-status-message">
28-
Sorry, we could not find a match after {formatTime(83)}
29+
Sorry, we could not find a match after {formatTime(timedOutIn)}
2930
</div>
30-
<button className="retry-match-button"
31+
{/* <button className="retry-match-button"
3132
onClick={retry}
3233
>
3334
Retry
34-
</button>
35+
</button> */}
3536
<button className="reselect-match-options-button"
3637
onClick={reselect}
3738
>

apps/frontend/src/app/matching/modalContent/MatchingInProgressContent.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ import { handleCancelMatch } from '../handlers';
66
import { useTimer } from "react-timer-hook"
77
import {formatTime} from '@/utils/DateTime';
88

9-
const TIMEOUT = 10;
9+
const TIMEOUT = 5;
1010

1111
interface Props {
12-
cancelMatch(): void
12+
cancelMatch(cancelledIn: number): void
13+
timeout(timeoutIn: number): void
1314
}
1415

15-
const MatchingInProgressContent: React.FC<Props> = ({cancelMatch: cancel}) => {
16+
const MatchingInProgressContent: React.FC<Props> = ({cancelMatch: cancel, timeout}) => {
1617
const time = new Date();
1718
time.setSeconds(time.getSeconds() + TIMEOUT);
1819
const { totalSeconds } = useTimer({
1920
expiryTimestamp: time,
20-
onExpire: cancel,
21+
onExpire: () => timeout(TIMEOUT),
2122
});
2223

2324
return (
@@ -29,7 +30,9 @@ const MatchingInProgressContent: React.FC<Props> = ({cancelMatch: cancel}) => {
2930
{formatTime(TIMEOUT - totalSeconds)}
3031
</div>
3132
<button className="cancel-match-button"
32-
onClick={cancel}
33+
onClick={() => {
34+
timeout(TIMEOUT);
35+
}}
3336
>
3437
Cancel
3538
</button>

apps/frontend/src/app/services/use-matching.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ export default function useMatching(): MatchState {
4343
const responseJson: MatchResponse = JSON.parse(response);
4444
console.log("got here");
4545
if (responseJson.type == "timeout") {
46-
47-
setIsSocket(false);
48-
setSte({
49-
state: "timeout",
50-
ok: cancel,
51-
})
46+
timeout();
5247
return;
5348
}
5449

@@ -73,13 +68,22 @@ export default function useMatching(): MatchState {
7368
sendJsonMessage,
7469
} = useWebSocket<MatchResponse>(MATCHING_SERVICE_URL as string, options, isSocket);
7570

71+
function timeout() {
72+
setIsSocket(false);
73+
setSte({
74+
state: "timeout",
75+
ok: cancel,
76+
});
77+
}
78+
7679
function cancel() {
7780
setIsSocket(false)
7881
setSte({
7982
state: "closed",
8083
start,
8184
})
8285
}
86+
8387
function start(request: MatchRequestParams) {
8488
setIsSocket(true)
8589
sendJsonMessage(request);
@@ -92,7 +96,7 @@ export default function useMatching(): MatchState {
9296
matchState = {state: "closed", start}
9397
break;
9498
case ReadyState.OPEN:
95-
matchState = {state: "matching", cancel}
99+
matchState = {state: "matching", cancel, timeout}
96100
break;
97101
case ReadyState.CONNECTING:
98102
matchState = {state: "starting"}

apps/frontend/src/contexts/websocketcontext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type SocketState = {
1010
} | {
1111
state: "matching";
1212
cancel(): void;
13+
timeout(): void;
1314
};
1415

1516
export type MatchState = SocketState | {

0 commit comments

Comments
 (0)