ente/src/components/RecoveryKeyModal.tsx

88 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-05-30 16:56:48 +00:00
import React, { useEffect, useState } from 'react';
import { downloadAsFile } from 'utils/file';
import { getRecoveryKey } from 'utils/crypto';
2021-04-05 07:41:31 +00:00
import constants from 'utils/strings/constants';
2021-04-07 09:22:59 +00:00
import MessageDialog from './MessageDialog';
2021-04-18 13:01:40 +00:00
import EnteSpinner from './EnteSpinner';
import styled from 'styled-components';
2021-04-05 07:41:31 +00:00
export const CodeBlock = styled.div<{ height: number }>`
display: flex;
align-items: center;
justify-content: center;
background: #1a1919;
height: ${(props) => props.height}px;
2021-08-13 02:38:38 +00:00
padding-left: 30px;
padding-right: 20px;
color: white;
margin: 20px 0;
2021-08-13 02:38:38 +00:00
width: 100%;
`;
export const FreeFlowText = styled.div`
word-wrap: break-word;
overflow-wrap: break-word;
min-width: 30%;
`;
interface Props {
2021-04-05 07:41:31 +00:00
show: boolean;
onHide: () => void;
somethingWentWrong: any;
2021-04-05 07:41:31 +00:00
}
2021-05-30 16:56:48 +00:00
function RecoveryKeyModal({ somethingWentWrong, ...props }: Props) {
2021-04-05 07:41:31 +00:00
const [recoveryKey, setRecoveryKey] = useState(null);
useEffect(() => {
2021-04-05 09:11:06 +00:00
if (!props.show) {
return;
}
2021-04-05 07:41:31 +00:00
const main = async () => {
2021-04-05 12:18:29 +00:00
const recoveryKey = await getRecoveryKey();
if (!recoveryKey) {
somethingWentWrong();
props.onHide();
}
2021-04-05 07:41:31 +00:00
setRecoveryKey(recoveryKey);
};
main();
}, [props.show]);
function onSaveClick() {
downloadAsFile(constants.RECOVERY_KEY_FILENAME, recoveryKey);
onClose();
}
function onClose() {
props.onHide();
}
2021-04-05 07:41:31 +00:00
return (
<MessageDialog
show={props.show}
onHide={onClose}
2021-05-09 06:53:56 +00:00
size="lg"
attributes={{
title: constants.DOWNLOAD_RECOVERY_KEY,
close: {
text: constants.SAVE_LATER,
variant: 'danger',
},
staticBackdrop: true,
proceed: {
text: constants.SAVE,
action: onSaveClick,
disabled: !recoveryKey,
variant: 'success',
},
2021-08-13 02:38:38 +00:00
}}>
<p>{constants.RECOVERY_KEY_DESCRIPTION}</p>
<CodeBlock height={150}>
{recoveryKey ? (
2021-08-13 02:38:38 +00:00
<FreeFlowText>{recoveryKey}</FreeFlowText>
) : (
2021-04-18 13:01:40 +00:00
<EnteSpinner />
)}
</CodeBlock>
<p>{constants.KEY_NOT_STORED_DISCLAIMER}</p>
2021-08-13 02:38:38 +00:00
</MessageDialog>
2021-04-05 07:41:31 +00:00
);
}
export default RecoveryKeyModal;