1- import { useDisclosure } from "@chakra-ui/react" ;
2- import { useCallback , useEffect , useState } from "react" ;
1+ import { useCallback , useState } from "react" ;
32import { bluetoothUniversalHex } from "../connection-stage-actions" ;
43import {
54 ConnectionFlowStep ,
65 ConnectionFlowType ,
76 ConnectionStage ,
87 useConnectionStage ,
98} from "../connection-stage-hooks" ;
9+ import { useLogging } from "../logging/logging-hooks" ;
1010import BrokenFirmwareDialog from "./BrokenFirmwareDialog" ;
1111import ConnectBatteryDialog from "./ConnectBatteryDialog" ;
1212import ConnectCableDialog , {
@@ -23,29 +23,19 @@ import TryAgainDialog from "./TryAgainDialog";
2323import UnsupportedMicrobitDialog from "./UnsupportedMicrobitDialog" ;
2424import WebUsbBluetoothUnsupportedDialog from "./WebUsbBluetoothUnsupportedDialog" ;
2525import WhatYouWillNeedDialog from "./WhatYouWillNeedDialog" ;
26- import { useLogging } from "../logging/logging-hooks" ;
2726
2827const ConnectionDialogs = ( ) => {
2928 const { stage, actions } = useConnectionStage ( ) ;
3029 const logging = useLogging ( ) ;
3130 const [ flashProgress , setFlashProgress ] = useState < number > ( 0 ) ;
32- const { isOpen, onClose : onCloseDialog , onOpen } = useDisclosure ( ) ;
3331 const [ microbitName , setMicrobitName ] = useState < string | undefined > (
3432 stage . bluetoothMicrobitName
3533 ) ;
3634 const onClose = useCallback ( ( ) => {
3735 actions . setFlowStep ( ConnectionFlowStep . None ) ;
38- onCloseDialog ( ) ;
39- } , [ actions , onCloseDialog ] ) ;
36+ } , [ actions ] ) ;
4037
41- useEffect ( ( ) => {
42- if ( stage . flowStep !== ConnectionFlowStep . None && ! isOpen ) {
43- onOpen ( ) ;
44- }
45- if ( stage . flowStep === ConnectionFlowStep . None && isOpen ) {
46- onClose ( ) ;
47- }
48- } , [ isOpen , onClose , onOpen , stage ] ) ;
38+ const isOpen = stage . flowStep !== ConnectionFlowStep . None ;
4939
5040 const progressCallback = useCallback (
5141 ( progress : number ) => {
@@ -57,51 +47,8 @@ const ConnectionDialogs = () => {
5747 [ actions , stage . flowStep ]
5848 ) ;
5949
60- const onChangeMicrobitName = useCallback (
61- ( name : string ) => {
62- actions . onChangeMicrobitName ( name ) ;
63- setMicrobitName ( name ) ;
64- } ,
65- [ actions ]
66- ) ;
67-
68- const onFlashSuccess = useCallback ( ( newStage : ConnectionStage ) => {
69- // Inferring microbit name saves the user from entering the pattern
70- // for bluetooth connection flow
71- if ( newStage . bluetoothMicrobitName ) {
72- setMicrobitName ( newStage . bluetoothMicrobitName ) ;
73- }
74- } , [ ] ) ;
75-
76- const connectAndFlash = useCallback ( async ( ) => {
77- if ( stage . flowType === ConnectionFlowType . ConnectRadioBridge ) {
78- logging . event ( {
79- type : "connect-user" ,
80- message : "radio-bridge" ,
81- } ) ;
82- }
83- await actions . connectAndflashMicrobit ( progressCallback , onFlashSuccess ) ;
84- } , [ actions , logging , onFlashSuccess , progressCallback , stage . flowType ] ) ;
85-
86- const onSkip = useCallback (
87- ( ) => actions . setFlowStep ( ConnectionFlowStep . ConnectBattery ) ,
88- [ actions ]
89- ) ;
90- const onInstructManualFlashing = useCallback (
91- ( ) => actions . setFlowStep ( ConnectionFlowStep . ManualFlashingTutorial ) ,
92- [ actions ]
93- ) ;
94-
9550 const dialogCommonProps = { isOpen, onClose } ;
9651
97- const handleConnectBluetooth = useCallback ( ( ) => {
98- logging . event ( {
99- type : "connect-user" ,
100- message : "bluetooth" ,
101- } ) ;
102- void actions . connectBluetooth ( ) ;
103- } , [ actions , logging ] ) ;
104-
10552 switch ( stage . flowStep ) {
10653 case ConnectionFlowStep . ReconnectFailedTwice :
10754 case ConnectionFlowStep . Start : {
@@ -134,12 +81,29 @@ const ConnectionDialogs = () => {
13481 onBackClick = { actions . onBackClick }
13582 onNextClick = { actions . onNextClick }
13683 config = { config }
137- onSkip = { onSkip }
84+ onSkip = { ( ) => actions . setFlowStep ( ConnectionFlowStep . ConnectBattery ) }
13885 onSwitch = { actions . switchFlowType }
13986 />
14087 ) ;
14188 }
14289 case ConnectionFlowStep . WebUsbFlashingTutorial : {
90+ const connectAndFlash = async ( ) => {
91+ const onFlashSuccess = ( newStage : ConnectionStage ) => {
92+ // Inferring microbit name saves the user from entering the pattern
93+ // for bluetooth connection flow
94+ if ( newStage . bluetoothMicrobitName ) {
95+ setMicrobitName ( newStage . bluetoothMicrobitName ) ;
96+ }
97+ } ;
98+
99+ if ( stage . flowType === ConnectionFlowType . ConnectRadioBridge ) {
100+ logging . event ( {
101+ type : "connect-user" ,
102+ message : "radio-bridge" ,
103+ } ) ;
104+ }
105+ await actions . connectAndflashMicrobit ( progressCallback , onFlashSuccess ) ;
106+ } ;
143107 return (
144108 < SelectMicrobitUsbDialog
145109 { ...dialogCommonProps }
@@ -175,11 +139,21 @@ const ConnectionDialogs = () => {
175139 onBackClick = { actions . onBackClick }
176140 onNextClick = { actions . onNextClick }
177141 microbitName = { microbitName }
178- onChangeMicrobitName = { onChangeMicrobitName }
142+ onChangeMicrobitName = { ( name : string ) => {
143+ actions . onChangeMicrobitName ( name ) ;
144+ setMicrobitName ( name ) ;
145+ } }
179146 />
180147 ) ;
181148 }
182149 case ConnectionFlowStep . ConnectBluetoothTutorial : {
150+ const handleConnectBluetooth = ( ) => {
151+ logging . event ( {
152+ type : "connect-user" ,
153+ message : "bluetooth" ,
154+ } ) ;
155+ void actions . connectBluetooth ( ) ;
156+ } ;
183157 return (
184158 < SelectMicrobitBluetoothDialog
185159 { ...dialogCommonProps }
@@ -227,7 +201,9 @@ const ConnectionDialogs = () => {
227201 return (
228202 < BrokenFirmwareDialog
229203 { ...dialogCommonProps }
230- onSkip = { onInstructManualFlashing }
204+ onSkip = { ( ) =>
205+ actions . setFlowStep ( ConnectionFlowStep . ManualFlashingTutorial )
206+ }
231207 onTryAgain = { actions . onTryAgain }
232208 />
233209 ) ;
0 commit comments