|
| 1 | +/** |
| 2 | + * Copyright 2025 Shift Crypto AG |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { useCallback, useEffect, useState } from 'react'; |
| 18 | +import { useTranslation } from 'react-i18next'; |
| 19 | +import { PointToBitBox02 } from '@/components/icon'; |
| 20 | +import { bluetoothToggleEnabled, getDeviceInfo } from '@/api/bitbox02'; |
| 21 | +import { alertUser } from '@/components/alert/Alert'; |
| 22 | +import { SettingsItem } from '@/routes/settings/components/settingsItem/settingsItem'; |
| 23 | +import { WaitDialog } from '@/components/wait-dialog/wait-dialog'; |
| 24 | +import { StyledSkeleton } from '@/routes/settings/bb02-settings'; |
| 25 | + |
| 26 | +type TBluetoothToggleEnabledSettingProps = { |
| 27 | + deviceID: string; |
| 28 | +} |
| 29 | + |
| 30 | +type TToggleEnabledWaitDialogProps = { |
| 31 | + show: boolean; |
| 32 | + enabled: boolean; |
| 33 | +} |
| 34 | + |
| 35 | +const ToggleEnabledWaitDialog = ({ show, enabled }: TToggleEnabledWaitDialogProps) => { |
| 36 | + const { t } = useTranslation(); |
| 37 | + |
| 38 | + if (!show) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + return ( |
| 43 | + <WaitDialog |
| 44 | + title={enabled ? t('bitbox02Settings.bluetoothToggleEnabled.titleEnabled') : t('bitbox02Settings.bluetoothToggleEnabled.titleDisabled')} > |
| 45 | + <p>{t('bitbox02Interact.followInstructions')}</p> |
| 46 | + <PointToBitBox02 /> |
| 47 | + </WaitDialog> |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +const BluetoothToggleEnabledSetting = ({ deviceID }: TBluetoothToggleEnabledSettingProps) => { |
| 52 | + const { t } = useTranslation(); |
| 53 | + const [show, setShow] = useState(false); |
| 54 | + const [enabled, setEnabled] = useState<undefined | boolean>(undefined); |
| 55 | + |
| 56 | + const updateEnabled = useCallback(async () => { |
| 57 | + const deviceInfoResult = await getDeviceInfo(deviceID); |
| 58 | + if (!deviceInfoResult.success) { |
| 59 | + console.error(deviceInfoResult.message); |
| 60 | + alertUser(deviceInfoResult.message || t('genericError')); |
| 61 | + return; |
| 62 | + } |
| 63 | + const bluetooth = deviceInfoResult.deviceInfo.bluetooth; |
| 64 | + if (!bluetooth) { |
| 65 | + return; |
| 66 | + } |
| 67 | + setEnabled(bluetooth.enabled); |
| 68 | + return bluetooth.enabled; |
| 69 | + }, [deviceID, t]); |
| 70 | + |
| 71 | + useEffect(() => { |
| 72 | + updateEnabled(); |
| 73 | + }, [updateEnabled]); |
| 74 | + |
| 75 | + const handleBluetoothToggleEnabled = async () => { |
| 76 | + setShow(true); |
| 77 | + const result = await bluetoothToggleEnabled(deviceID); |
| 78 | + if (!result.success) { |
| 79 | + setShow(false); |
| 80 | + console.error(result.message); |
| 81 | + alertUser(result.message || t('genericError')); |
| 82 | + return; |
| 83 | + } |
| 84 | + setShow(false); |
| 85 | + const enabled = await updateEnabled(); |
| 86 | + if (enabled === true) { |
| 87 | + alertUser(t('bitbox02Settings.bluetoothToggleEnabled.alertEnabled')); |
| 88 | + } else if (enabled === false) { |
| 89 | + alertUser(t('bitbox02Settings.bluetoothToggleEnabled.alertDisabled')); |
| 90 | + } |
| 91 | + }; |
| 92 | + if (enabled === undefined) { |
| 93 | + return <StyledSkeleton />; |
| 94 | + } |
| 95 | + return ( |
| 96 | + <> |
| 97 | + <SettingsItem |
| 98 | + settingName={enabled ? t('bitbox02Settings.bluetoothToggleEnabled.titleEnabled') : t('bitbox02Settings.bluetoothToggleEnabled.titleDisabled')} |
| 99 | + secondaryText={t('bitbox02Settings.bluetoothToggleEnabled.description')} |
| 100 | + onClick={handleBluetoothToggleEnabled} |
| 101 | + /> |
| 102 | + <ToggleEnabledWaitDialog show={show} enabled={enabled} /> |
| 103 | + </> |
| 104 | + ); |
| 105 | +}; |
| 106 | + |
| 107 | +export { BluetoothToggleEnabledSetting }; |
0 commit comments