index.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import type { PageProps } from "@ente/shared/apps/types";
  2. import CodeBlock from "@ente/shared/components/CodeBlock";
  3. import DialogTitleWithCloseButton from "@ente/shared/components/DialogBox/TitleWithCloseButton";
  4. import { getRecoveryKey } from "@ente/shared/crypto/helpers";
  5. import { downloadAsFile } from "@ente/shared/utils";
  6. import {
  7. Button,
  8. Dialog,
  9. DialogActions,
  10. DialogContent,
  11. Typography,
  12. } from "@mui/material";
  13. import * as bip39 from "bip39";
  14. import { t } from "i18next";
  15. import { useEffect, useState } from "react";
  16. import { DashedBorderWrapper } from "./styledComponents";
  17. // mobile client library only supports english.
  18. bip39.setDefaultWordlist("english");
  19. const RECOVERY_KEY_FILE_NAME = "ente-recovery-key.txt";
  20. interface Props {
  21. appContext: PageProps["appContext"];
  22. show: boolean;
  23. onHide: () => void;
  24. somethingWentWrong: any;
  25. }
  26. function RecoveryKey({ somethingWentWrong, appContext, ...props }: Props) {
  27. const [recoveryKey, setRecoveryKey] = useState(null);
  28. useEffect(() => {
  29. if (!props.show) {
  30. return;
  31. }
  32. const main = async () => {
  33. try {
  34. const recoveryKey = await getRecoveryKey();
  35. setRecoveryKey(bip39.entropyToMnemonic(recoveryKey));
  36. } catch (e) {
  37. somethingWentWrong();
  38. props.onHide();
  39. }
  40. };
  41. main();
  42. }, [props.show]);
  43. function onSaveClick() {
  44. downloadAsFile(RECOVERY_KEY_FILE_NAME, recoveryKey);
  45. props.onHide();
  46. }
  47. return (
  48. <Dialog
  49. fullScreen={appContext.isMobile}
  50. open={props.show}
  51. onClose={props.onHide}
  52. maxWidth="xs"
  53. >
  54. <DialogTitleWithCloseButton onClose={props.onHide}>
  55. {t("RECOVERY_KEY")}
  56. </DialogTitleWithCloseButton>
  57. <DialogContent>
  58. <Typography mb={3}>{t("RECOVERY_KEY_DESCRIPTION")}</Typography>
  59. <DashedBorderWrapper>
  60. <CodeBlock code={recoveryKey} />
  61. <Typography m={2}>
  62. {t("KEY_NOT_STORED_DISCLAIMER")}
  63. </Typography>
  64. </DashedBorderWrapper>
  65. </DialogContent>
  66. <DialogActions>
  67. <Button color="secondary" size="large" onClick={props.onHide}>
  68. {t("SAVE_LATER")}
  69. </Button>
  70. <Button color="accent" size="large" onClick={onSaveClick}>
  71. {t("SAVE")}
  72. </Button>
  73. </DialogActions>
  74. </Dialog>
  75. );
  76. }
  77. export default RecoveryKey;