|
| 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, useContext } from 'react'; |
| 18 | +import { useNavigate } from 'react-router-dom'; |
| 19 | +import { useTranslation } from 'react-i18next'; |
| 20 | +import { Dialog, DialogButtons } from './dialog'; |
| 21 | +import { getDeviceList } from '@/api/devices'; |
| 22 | +import { syncDeviceList } from '@/api/devicessync'; |
| 23 | +import { useSync } from '@/hooks/api'; |
| 24 | +import { useDefault } from '@/hooks/default'; |
| 25 | +import { Button } from '@/components/forms'; |
| 26 | +import { AppContext } from '@/contexts/AppContext'; |
| 27 | + |
| 28 | +type TFirmwareUpgradeRequiredDialogProps = { |
| 29 | + open: boolean; |
| 30 | + onClose: () => void; |
| 31 | +}; |
| 32 | + |
| 33 | +export const FirmwareUpgradeRequiredDialog = ({ open, onClose }: TFirmwareUpgradeRequiredDialogProps) => { |
| 34 | + const { t } = useTranslation(); |
| 35 | + const navigate = useNavigate(); |
| 36 | + const { setFirmwareUpdateDialogOpen } = useContext(AppContext); |
| 37 | + const devices = useDefault(useSync(getDeviceList, syncDeviceList), {}); |
| 38 | + const deviceIDs = Object.keys(devices); |
| 39 | + |
| 40 | + const handleUpgrade = useCallback(() => { |
| 41 | + setFirmwareUpdateDialogOpen(true); |
| 42 | + if (deviceIDs && deviceIDs.length > 0) { |
| 43 | + navigate(`/settings/device-settings/${deviceIDs[0] as string}`); |
| 44 | + } |
| 45 | + onClose(); |
| 46 | + }, [setFirmwareUpdateDialogOpen, deviceIDs, navigate, onClose]); |
| 47 | + |
| 48 | + return ( |
| 49 | + <Dialog |
| 50 | + title={t('upgradeFirmware.title')} |
| 51 | + open={open} |
| 52 | + onClose={onClose}> |
| 53 | + <p>{t('device.firmwareUpgradeRequired')}</p> |
| 54 | + <DialogButtons> |
| 55 | + <Button primary onClick={handleUpgrade}> |
| 56 | + {t('upgradeFirmware.button')} |
| 57 | + </Button> |
| 58 | + <Button secondary onClick={onClose}> |
| 59 | + {t('dialog.cancel')} |
| 60 | + </Button> |
| 61 | + </DialogButtons> |
| 62 | + </Dialog> |
| 63 | + ); |
| 64 | +}; |
0 commit comments