16
16
17
17
import { useTranslation } from 'react-i18next' ;
18
18
import { useSync } from '@/hooks/api' ;
19
- import { connect , getState , syncState } from '@/api/bluetooth' ;
19
+ import { connect , getState , syncState , TPeripheral } from '@/api/bluetooth' ;
20
20
import { runningInIOS } from '@/utils/env' ;
21
21
import { Status } from '@/components/status/status' ;
22
22
import { ActionableItem } from '@/components/actionable-item/actionable-item' ;
23
23
import { Badge } from '@/components/badge/badge' ;
24
+ import { HorizontallyCenteredSpinner , SpinnerRingAnimated } from '@/components/spinner/SpinnerAnimation' ;
24
25
import styles from './bluetooth.module.css' ;
25
26
27
+ const isConnectedOrConnecting = ( peripheral : TPeripheral ) => {
28
+ return peripheral . connectionState === 'connecting' || peripheral . connectionState === 'connected' ;
29
+ } ;
30
+
26
31
const _Bluetooth = ( ) => {
27
32
const { t } = useTranslation ( ) ;
28
33
const state = useSync ( getState , syncState ) ;
@@ -36,35 +41,46 @@ const _Bluetooth = () => {
36
41
</ Status >
37
42
) ;
38
43
}
44
+ const hasConnection = state . peripherals . some ( isConnectedOrConnecting ) ;
39
45
return (
40
46
< >
41
47
< div className = { styles . label } >
42
48
{ t ( 'bluetooth.select' ) }
43
49
</ div >
44
50
< div className = { styles . container } >
45
- { state . scanning ? 'scanning' : null }
46
51
{ state . peripherals . map ( peripheral => {
52
+ const onClick = ! hasConnection ? ( ) => connect ( peripheral . identifier ) : undefined ;
53
+ const connectingIcon = peripheral . connectionState === 'connecting' ? (
54
+ < SpinnerRingAnimated />
55
+ ) : undefined ;
47
56
return (
48
57
< ActionableItem
49
58
key = { peripheral . identifier }
50
- onClick = { ( ) => connect ( peripheral . identifier ) } >
59
+ icon = { connectingIcon }
60
+ onClick = { onClick } >
51
61
< span >
52
62
{ peripheral . name !== '' ? peripheral . name : peripheral . identifier }
53
63
{ ' ' }
64
+ { peripheral . connectionState === 'connected' ? (
65
+ < Badge type = "success" >
66
+ { t ( 'bluetooth.connected' ) }
67
+ </ Badge >
68
+ ) : null }
54
69
{ peripheral . connectionState === 'error' ? (
55
70
< Badge type = "danger" >
56
- { t ( 'bluetooth.connectionFailed' ) }
71
+ < span style = { { whiteSpace : 'wrap' } } >
72
+ { peripheral . connectionError }
73
+ </ span >
57
74
</ Badge >
58
75
) : null }
59
- { peripheral . connectionState === 'error' ? (
60
- < p > { peripheral . connectionError } </ p >
61
- ) : peripheral . connectionState }
62
76
</ span >
63
-
64
77
</ ActionableItem >
65
78
) ;
66
79
} ) }
67
80
</ div >
81
+ { state . scanning && (
82
+ < HorizontallyCenteredSpinner />
83
+ ) }
68
84
</ >
69
85
) ;
70
86
} ;
0 commit comments