index.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { useEffect, useState } from 'react';
  2. import { downloadAsFile } from '@ente/shared/utils';
  3. import { getRecoveryKey } from '@ente/shared/crypto/helpers';
  4. import CodeBlock from '@ente/shared/components/CodeBlock';
  5. import {
  6. Button,
  7. Dialog,
  8. DialogActions,
  9. DialogContent,
  10. Typography,
  11. } from '@mui/material';
  12. import * as bip39 from 'bip39';
  13. import { DashedBorderWrapper } from './styledComponents';
  14. import DialogTitleWithCloseButton from '@ente/shared/components/DialogBox/TitleWithCloseButton';
  15. import { t } from 'i18next';
  16. import { PageProps } from '@ente/shared/apps/types';
  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. <DialogTitleWithCloseButton onClose={props.onHide}>
  54. {t('RECOVERY_KEY')}
  55. </DialogTitleWithCloseButton>
  56. <DialogContent>
  57. <Typography mb={3}>{t('RECOVERY_KEY_DESCRIPTION')}</Typography>
  58. <DashedBorderWrapper>
  59. <CodeBlock code={recoveryKey} />
  60. <Typography m={2}>
  61. {t('KEY_NOT_STORED_DISCLAIMER')}
  62. </Typography>
  63. </DashedBorderWrapper>
  64. </DialogContent>
  65. <DialogActions>
  66. <Button color="secondary" size="large" onClick={props.onHide}>
  67. {t('SAVE_LATER')}
  68. </Button>
  69. <Button color="accent" size="large" onClick={onSaveClick}>
  70. {t('SAVE')}
  71. </Button>
  72. </DialogActions>
  73. </Dialog>
  74. );
  75. }
  76. export default RecoveryKey;