lbry-desktop/ui/component/walletBackup/view.jsx

156 lines
4.8 KiB
React
Raw Normal View History

2019-05-14 04:51:33 +02:00
// @flow
import * as React from 'react';
import { shell, remote } from 'electron';
2019-05-14 04:51:33 +02:00
import Button from 'component/button';
import CopyableText from 'component/copyableText';
import AdmZip from 'adm-zip';
import path from 'path';
2019-09-27 22:03:05 +02:00
import Card from 'component/common/card';
2019-10-01 06:53:33 +02:00
import I18nMessage from 'component/i18nMessage';
2019-05-14 04:51:33 +02:00
type Props = {
daemonSettings: {
wallet_dir: ?string,
},
};
type State = {
errorMessage: ?string,
successMessage: ?string,
};
class WalletBackup extends React.PureComponent<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
errorMessage: null,
successMessage: null,
};
}
showErrorMessage(message: string) {
this.setState({ errorMessage: message });
}
showSuccessMessage(message: string) {
this.setState({ successMessage: message });
}
clearMessages() {
this.setState({ errorMessage: null, successMessage: null });
}
backupWalletDir(lbryumWalletDir: ?string) {
this.clearMessages();
if (!lbryumWalletDir) {
this.showErrorMessage(__('No wallet folder was found.'));
return;
}
// Colon fails on Windows. Backups should be portable, so replace it on other platforms, too.
const filenameTime = new Date().toISOString().replace(/:/g, '-');
const outputFilename = [path.basename(lbryumWalletDir), '-', filenameTime, '.zip'].join('');
// Prefer placing backup in user's Downloads folder, then their home folder, and finally
// right next to the lbryum folder itself.
let outputDir = path.dirname(lbryumWalletDir);
if (remote && remote.app) {
outputDir = remote.app.getPath('downloads') || remote.app.getPath('home') || outputDir;
}
const outputPath = path.join(outputDir, outputFilename);
const zip = new AdmZip();
try {
zip.addLocalFolder(lbryumWalletDir);
} catch (err) {
2019-07-21 23:31:22 +02:00
console.error(err); // eslint-disable-line no-console
this.showErrorMessage(__('The wallet folder could not be added to the zip archive.'));
return;
}
try {
zip.writeZip(outputPath);
} catch (err) {
2019-07-21 23:31:22 +02:00
console.error(err); // eslint-disable-line no-console
this.showErrorMessage(__('There was a problem writing the zip archive to disk.'));
return;
}
2019-12-10 20:49:00 +01:00
this.showSuccessMessage(__('Saved zip archive to %outputPath%', { outputPath }));
shell.showItemInFolder(outputPath);
}
2019-05-14 04:51:33 +02:00
render() {
2019-07-21 23:31:22 +02:00
const { daemonSettings = {} } = this.props;
2019-05-14 04:51:33 +02:00
const { wallet_dir: lbryumWalletDir } = daemonSettings;
return (
2019-09-27 22:03:05 +02:00
<Card
2020-04-24 01:19:48 +02:00
title={__('Backup Your LBRY Wallet')}
2019-09-27 22:03:05 +02:00
subtitle={
<ul>
<li>
{__(
2020-04-24 01:19:48 +02:00
'Your LBRY credits, channels, and publishes are controllable by you and only you, via wallet file(s) stored locally on your computer.'
2019-09-27 22:03:05 +02:00
)}
</li>
<li>
{__(
2019-11-01 18:27:01 +01:00
'If Sync is on, LBRY will backup your wallet and preferences. If disabled, you are responsible for keeping a backup.'
2019-09-27 22:03:05 +02:00
)}
</li>
<li>
{__(
2019-10-01 06:53:33 +02:00
'However, it is easy to back up manually. To backup your wallet, make a copy of the folder listed below:'
2019-09-27 22:03:05 +02:00
)}
</li>
</ul>
}
actions={
<React.Fragment>
<CopyableText copyable={lbryumWalletDir} snackMessage={__('Path copied.')} />
<p className="help">
2019-10-01 06:53:33 +02:00
<I18nMessage
tokens={{
helpLink: (
<Button
button="link"
href="https://lbry.com/faq/how-to-backup-wallet"
label={__('see this article')}
/>
),
}}
>
2020-04-24 01:19:48 +02:00
Access to these files are equivalent to having access to your credits, channels, and publishes. Keep any copies you make of your
2019-10-01 06:53:33 +02:00
wallet in a secure place. For more details on backing up and best practices %helpLink%.
</I18nMessage>
2019-09-27 22:03:05 +02:00
</p>
<p className={'card__message card__message--error' + (this.state.errorMessage ? '' : ' hidden')}>
{this.state.errorMessage}
</p>
<p className={'card__message card__message--success' + (this.state.successMessage ? '' : ' hidden')}>
{this.state.successMessage}
</p>
<div className="card__actions">
<Button
2019-12-09 18:25:13 +01:00
button="primary"
2019-09-27 22:03:05 +02:00
label={__('Create Backup')}
onClick={() => this.backupWalletDir(lbryumWalletDir)}
/>
<Button button="link" label={__('Open Folder')} onClick={() => shell.openItem(lbryumWalletDir)} />
</div>
</React.Fragment>
}
/>
2019-05-14 04:51:33 +02:00
);
}
}
export default WalletBackup;