Skip to content

Commit bf85d97

Browse files
Handle Connect btn for different connection status in ConnectToRecordDialog (#392)
- If not connected, connect button starts connection flow. - If needs reconnection, connect button reconnects. - If it is reconnecting, connect button waits to reconnect and then closes dialog if successful. - If already connected, connect button closes dialog. - If a connection dialog opens, ConnectToRecord dialog closes.
1 parent 11adac3 commit bf85d97

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed

src/components/ConnectToRecordDialog.tsx

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,83 @@ import {
99
ModalOverlay,
1010
Text,
1111
} from "@chakra-ui/react";
12-
import { ComponentProps, useCallback } from "react";
12+
import { ComponentProps, useCallback, useEffect, useState } from "react";
1313
import { FormattedMessage } from "react-intl";
14-
import { useConnectionStage } from "../connection-stage-hooks";
14+
import {
15+
ConnectionFlowStep,
16+
useConnectionStage,
17+
} from "../connection-stage-hooks";
18+
import { ConnectionStatus } from "../connect-status-hooks";
1519

1620
const ConnectToRecordDialog = ({
1721
onClose,
1822
...rest
1923
}: Omit<ComponentProps<typeof Modal>, "children">) => {
20-
const { actions } = useConnectionStage();
24+
const {
25+
actions,
26+
status: connStatus,
27+
stage: connStage,
28+
} = useConnectionStage();
29+
const [isWaiting, setIsWaiting] = useState<boolean>(false);
2130

22-
const handleConnect = useCallback(() => {
31+
const handleOnClose = useCallback(() => {
32+
setIsWaiting(false);
2333
onClose();
24-
actions.startConnect();
25-
}, [actions, onClose]);
34+
}, [onClose]);
35+
36+
const handleConnect = useCallback(async () => {
37+
switch (connStatus) {
38+
case ConnectionStatus.FailedToConnect:
39+
case ConnectionStatus.FailedToReconnectTwice:
40+
case ConnectionStatus.FailedToSelectBluetoothDevice:
41+
case ConnectionStatus.NotConnected: {
42+
// Start connection flow.
43+
actions.startConnect();
44+
return handleOnClose();
45+
}
46+
case ConnectionStatus.ConnectionLost:
47+
case ConnectionStatus.FailedToReconnect:
48+
case ConnectionStatus.Disconnected: {
49+
// Reconnect.
50+
await actions.reconnect();
51+
return handleOnClose();
52+
}
53+
case ConnectionStatus.ReconnectingAutomatically: {
54+
// Wait for reconnection to happen.
55+
setIsWaiting(true);
56+
return;
57+
}
58+
case ConnectionStatus.Connected: {
59+
// Connected whilst dialog is up.
60+
return handleOnClose();
61+
}
62+
case ConnectionStatus.ReconnectingExplicitly:
63+
case ConnectionStatus.Connecting: {
64+
// Impossible cases.
65+
return handleOnClose();
66+
}
67+
}
68+
}, [connStatus, actions, handleOnClose]);
69+
70+
useEffect(() => {
71+
if (
72+
connStage.flowStep !== ConnectionFlowStep.None ||
73+
(isWaiting && connStatus === ConnectionStatus.Connected)
74+
) {
75+
// Close dialog if connection dialog is opened, or
76+
// once connected after waiting.
77+
handleOnClose();
78+
return;
79+
}
80+
}, [connStage.flowStep, connStatus, handleOnClose, isWaiting, onClose]);
2681

2782
return (
2883
<Modal
2984
closeOnOverlayClick={false}
3085
motionPreset="none"
3186
size="md"
3287
isCentered
33-
onClose={onClose}
88+
onClose={handleOnClose}
3489
{...rest}
3590
>
3691
<ModalOverlay>
@@ -45,7 +100,11 @@ const ConnectToRecordDialog = ({
45100
</Text>
46101
</ModalBody>
47102
<ModalFooter justifyContent="flex-end">
48-
<Button variant="primary" onClick={handleConnect}>
103+
<Button
104+
variant="primary"
105+
onClick={handleConnect}
106+
isLoading={isWaiting}
107+
>
49108
<FormattedMessage id="connect-action" />
50109
</Button>
51110
</ModalFooter>

0 commit comments

Comments
 (0)