import { type MiniDialogAttributes } from "@/base/components/MiniDialog"; import { SpaceBetweenFlex } from "@/base/components/mui/Container"; import { FocusVisibleButton } from "@/base/components/mui/FocusVisibleButton"; import { errorDialogAttributes } from "@/base/components/utils/dialog"; import type { ModalVisibilityProps } from "@/base/components/utils/modal"; import { useIsSmallWidth } from "@/base/hooks"; import log from "@/base/log"; import { downloadString } from "@/base/utils/web"; import { DialogCloseIconButton } from "@/new/photos/components/mui/Dialog"; import CodeBlock from "@ente/shared/components/CodeBlock"; import { getRecoveryKey } from "@ente/shared/crypto/helpers"; import { Box, Dialog, DialogActions, DialogContent, DialogTitle, Typography, styled, } from "@mui/material"; import * as bip39 from "bip39"; import { t } from "i18next"; import { useCallback, useEffect, useState } from "react"; // mobile client library only supports english. bip39.setDefaultWordlist("english"); type RecoveryKeyProps = ModalVisibilityProps & { showMiniDialog: (attributes: MiniDialogAttributes) => void; }; export const RecoveryKey: React.FC = ({ open, onClose, showMiniDialog, }) => { const [recoveryKey, setRecoveryKey] = useState(); const fullScreen = useIsSmallWidth(); const handleLoadError = useCallback( (e: unknown) => { log.error("Failed to generate recovery key", e); showMiniDialog( errorDialogAttributes(t("recovery_key_generation_failed")), ); onClose(); }, [onClose, showMiniDialog], ); useEffect(() => { if (!open) return; void getRecoveryKeyMnemonic() .then((key) => setRecoveryKey(key)) .catch(handleLoadError); }, [open, handleLoadError]); const handleSaveClick = () => { downloadRecoveryKeyMnemonic(recoveryKey!); onClose(); }; return ( {t("recovery_key")} {t("recovery_key_description")} {t("key_not_stored_note")} {t("do_this_later")} {t("save_key")} ); }; const DashedBorderWrapper = styled(Box)(({ theme }) => ({ border: `1px dashed ${theme.palette.grey.A400}`, borderRadius: theme.spacing(1), })); const getRecoveryKeyMnemonic = async () => bip39.entropyToMnemonic(await getRecoveryKey()); const downloadRecoveryKeyMnemonic = (key: string) => downloadString(key, "ente-recovery-key.txt");