|
| 1 | +import React, { useState, useEffect, useCallback } from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { |
| 4 | + Modal, |
| 5 | + ModalOverlay, |
| 6 | + ModalContent, |
| 7 | + ModalHeader, |
| 8 | + ModalBody, |
| 9 | + Button, |
| 10 | + Flex, |
| 11 | + Alert, |
| 12 | + AlertIcon, |
| 13 | + AlertTitle, |
| 14 | + Text, |
| 15 | +} from '@chakra-ui/react'; |
| 16 | +import { useGuardianAdminApi, useGuardianDispatch } from '../../hooks'; |
| 17 | +import { hexToBlob } from '../utils/api'; |
| 18 | +import { GUARDIAN_APP_ACTION_TYPE, GuardianStatus } from '../../types/guardian'; |
| 19 | + |
| 20 | +interface BackupModalProps { |
| 21 | + isOpen: boolean; |
| 22 | + onClose: () => void; |
| 23 | +} |
| 24 | + |
| 25 | +export const BackupModal: React.FC<BackupModalProps> = ({ |
| 26 | + isOpen, |
| 27 | + onClose, |
| 28 | +}) => { |
| 29 | + const { t } = useTranslation(); |
| 30 | + const [hasDownloaded, setHasDownloaded] = useState(false); |
| 31 | + const [isDownloading, setIsDownloading] = useState(false); |
| 32 | + const [downloadError, setDownloadError] = useState<string | null>(null); |
| 33 | + const api = useGuardianAdminApi(); |
| 34 | + const dispatch = useGuardianDispatch(); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (isOpen) { |
| 38 | + setHasDownloaded(false); |
| 39 | + setDownloadError(null); |
| 40 | + } |
| 41 | + }, [isOpen]); |
| 42 | + |
| 43 | + const handleDownload = useCallback(async () => { |
| 44 | + setIsDownloading(true); |
| 45 | + setDownloadError(null); |
| 46 | + |
| 47 | + try { |
| 48 | + const response = await api.downloadGuardianBackup(); |
| 49 | + const blob = hexToBlob(response.tar_archive_bytes, 'application/x-tar'); |
| 50 | + const url = window.URL.createObjectURL(blob); |
| 51 | + const link = document.createElement('a'); |
| 52 | + link.href = url; |
| 53 | + link.download = 'guardianBackup.tar'; |
| 54 | + link.click(); |
| 55 | + |
| 56 | + setTimeout(() => URL.revokeObjectURL(url), 100); |
| 57 | + setHasDownloaded(true); |
| 58 | + } catch (error) { |
| 59 | + console.error('Error in handleDownload:', error); |
| 60 | + setDownloadError( |
| 61 | + t('federation-dashboard.danger-zone.backup.error-download') |
| 62 | + ); |
| 63 | + } finally { |
| 64 | + setIsDownloading(false); |
| 65 | + } |
| 66 | + }, [api, t]); |
| 67 | + |
| 68 | + const handleContinue = useCallback(() => { |
| 69 | + if (hasDownloaded) { |
| 70 | + dispatch({ |
| 71 | + type: GUARDIAN_APP_ACTION_TYPE.SET_STATUS, |
| 72 | + payload: GuardianStatus.Admin, |
| 73 | + }); |
| 74 | + onClose(); |
| 75 | + } |
| 76 | + }, [dispatch, hasDownloaded, onClose]); |
| 77 | + |
| 78 | + return ( |
| 79 | + <Modal |
| 80 | + isOpen={isOpen} |
| 81 | + onClose={onClose} |
| 82 | + closeOnOverlayClick={false} |
| 83 | + isCentered |
| 84 | + > |
| 85 | + <ModalOverlay /> |
| 86 | + <ModalContent> |
| 87 | + <ModalHeader alignSelf='center'> |
| 88 | + {t('federation-dashboard.danger-zone.backup.title')} |
| 89 | + </ModalHeader> |
| 90 | + <ModalBody pb={6}> |
| 91 | + <Flex direction='column' gap={4}> |
| 92 | + <Alert status='warning'> |
| 93 | + <AlertIcon /> |
| 94 | + <AlertTitle> |
| 95 | + {t('federation-dashboard.danger-zone.backup.warning-title')} |
| 96 | + </AlertTitle> |
| 97 | + </Alert> |
| 98 | + <Text mb={4}> |
| 99 | + {t('federation-dashboard.danger-zone.backup.warning-text')} |
| 100 | + </Text> |
| 101 | + {downloadError && ( |
| 102 | + <Alert status='error'> |
| 103 | + <AlertIcon /> |
| 104 | + <AlertTitle>{downloadError}</AlertTitle> |
| 105 | + </Alert> |
| 106 | + )} |
| 107 | + <Flex justifyContent='center' gap={4} direction={['column', 'row']}> |
| 108 | + <Button |
| 109 | + variant='ghost' |
| 110 | + size={['sm', 'md']} |
| 111 | + onClick={handleDownload} |
| 112 | + isDisabled={hasDownloaded || isDownloading} |
| 113 | + isLoading={isDownloading} |
| 114 | + bg='red.500' |
| 115 | + color='white' |
| 116 | + _hover={{ bg: 'red.600' }} |
| 117 | + _active={{ bg: 'red.700' }} |
| 118 | + > |
| 119 | + {hasDownloaded |
| 120 | + ? t('federation-dashboard.danger-zone.backup.downloaded') + |
| 121 | + ' ✓' |
| 122 | + : t( |
| 123 | + 'federation-dashboard.danger-zone.acknowledge-and-download' |
| 124 | + )} |
| 125 | + </Button> |
| 126 | + <Button |
| 127 | + colorScheme='blue' |
| 128 | + size={['sm', 'md']} |
| 129 | + onClick={handleContinue} |
| 130 | + isDisabled={!hasDownloaded} |
| 131 | + > |
| 132 | + {t('common.close')} |
| 133 | + </Button> |
| 134 | + </Flex> |
| 135 | + </Flex> |
| 136 | + </ModalBody> |
| 137 | + </ModalContent> |
| 138 | + </Modal> |
| 139 | + ); |
| 140 | +}; |
0 commit comments