Skip to content

Commit

Permalink
feat: display warning when backup not done
Browse files Browse the repository at this point in the history
  • Loading branch information
krrprr committed Jan 31, 2021
1 parent 1778807 commit f44be87
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const GlobalCss = withStyles((theme: Theme) => {
})(() => null);

const electronStore = useElectronStore({});
const backupStore = useBackupStore({});
const backupStore = useBackupStore({ backupInfoLoaded: false });

function App(): ReactElement {
useEffect(() => {
Expand Down
99 changes: 99 additions & 0 deletions src/common/WarningMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {
Card,
createStyles,
Grid,
IconButton,
makeStyles,
Theme,
Typography,
} from "@material-ui/core";
import WarningIcon from "@material-ui/icons/Warning";
import React, { ReactElement } from "react";
import CloseIcon from "@material-ui/icons/Close";

type WarningMessageProps = {
message: string;
showCloseIcon?: boolean;
onClose?: () => void;
alignToStart?: boolean;
additionalButtons?: { button: ReactElement; key: string }[];
};

const useStyles = makeStyles((theme: Theme) =>
createStyles({
warningMessage: {
backgroundColor: theme.palette.warning.dark,
color: theme.palette.warning.contrastText,
marginBottom: theme.spacing(2),
padding: theme.spacing(1),
},
iconContainer: {
display: "flex",
},
})
);

const WarningMessage = (props: WarningMessageProps): ReactElement => {
const {
message,
showCloseIcon,
onClose,
alignToStart,
additionalButtons,
} = props;
const classes = useStyles();

return (
<Grid item>
<Card elevation={0} className={classes.warningMessage}>
<Grid
item
container
wrap="nowrap"
justify="space-between"
alignItems="center"
>
<Grid
item
container
spacing={1}
justify={alignToStart ? "flex-start" : "center"}
alignItems="center"
wrap="nowrap"
>
<Grid item className={classes.iconContainer}>
<WarningIcon fontSize="small" />
</Grid>
<Grid item>
<Typography variant="body2" align="center">
{message}
</Typography>
</Grid>
</Grid>
{(showCloseIcon || additionalButtons?.length) && (
<Grid item container justify="flex-end" spacing={1}>
{additionalButtons?.map((button) => (
<Grid item key={button.key}>
{button.button}
</Grid>
))}
{showCloseIcon && (
<Grid item>
<IconButton
color="inherit"
size="small"
onClick={() => (onClose ? onClose() : void 0)}
>
<CloseIcon />
</IconButton>
</Grid>
)}
</Grid>
)}
</Grid>
</Card>
</Grid>
);
};

export default WarningMessage;
5 changes: 5 additions & 0 deletions src/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Path } from "../router/Path";
import Console from "./console/Console";
import MenuItem, { MenuItemProps } from "./menu/MenuItem";
import Overview from "./overview/Overview";
// import SetupWarning from "./SetupWarning";
import Trade from "./trade/Trade";
import Tradehistory from "./tradehistory/Tradehistory";
import Wallets from "./wallet/Wallets";
Expand Down Expand Up @@ -57,6 +58,7 @@ const useStyles = makeStyles((theme: Theme) =>
padding: theme.spacing(3),
height: "100vh",
display: "flex",
flexDirection: "column",
},
})
);
Expand Down Expand Up @@ -211,6 +213,9 @@ const Dashboard = (): ReactElement => {
</Grid>
</Drawer>
<main className={classes.content}>
{/* <Grid item container>
{<SetupWarning />}
</Grid> */}
<Switch>
{menuItems.map((item) => (
<Route exact path={`${path}${item.path}`} key={item.text}>
Expand Down
102 changes: 102 additions & 0 deletions src/dashboard/SetupWarning.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
Button,
Collapse,
createStyles,
makeStyles,
Theme,
} from "@material-ui/core";
import { inject, observer } from "mobx-react";
import React, { ReactElement, useEffect, useState } from "react";
import { useHistory } from "react-router-dom";
import { interval } from "rxjs";
import { catchError, exhaustMap, filter, take } from "rxjs/operators";
import api from "../api";
import WarningMessage from "../common/WarningMessage";
import { Path } from "../router/Path";
import { BackupStore, BACKUP_STORE } from "../stores/backupStore";

type SetupWarningProps = {
backupStore?: BackupStore;
};

const useStyles = makeStyles((theme: Theme) =>
createStyles({
wrapper: {
width: "100%",
marginBottom: theme.spacing(2),
},
})
);

const SetupWarning = inject(BACKUP_STORE)(
observer(
(props: SetupWarningProps): ReactElement => {
const { backupStore } = props;
const classes = useStyles();
const history = useHistory();
const [visible, setVisible] = useState(false);
const [closeBtnVisible, setCloseBtnVisible] = useState(false);

useEffect(() => {
interval(1000).subscribe(() =>
setVisible(
backupStore!.backupInfoLoaded &&
(!!backupStore!.defaultPassword ||
!backupStore!.mnemonicShown ||
!!backupStore!.defaultBackupDirectory)
)
);
}, [backupStore]);

useEffect(() => {
const balanceSubscription = interval(5000)
.pipe(
exhaustMap(() => api.getbalance$()),
catchError((e, caught) => caught),
filter((value) => !!Object.keys(value.balances)?.length),
take(1),
filter(
(value) =>
!Object.values(value.balances).some(
(balance) => Number(balance.total_balance) > 0
)
)
)
.subscribe({
next: () => setCloseBtnVisible(true),
});
return () => balanceSubscription.unsubscribe();
}, []);

return (
<Collapse in={visible} className={classes.wrapper}>
<WarningMessage
message="Secure your funds. Setup password, store mnemonic, and save backup data."
alignToStart
showCloseIcon={closeBtnVisible}
onClose={() => setVisible(false)}
additionalButtons={[
{
button: (
<Button
size="small"
color="inherit"
variant="outlined"
onClick={() =>
history.push(`${Path.SETTINGS}${Path.INITIAL_SETUP}`)
}
>
Setup Now
</Button>
),
key: "CloseWarningBtn",
},
]}
/>
</Collapse>
);
}
)
);

export default SetupWarning;
1 change: 0 additions & 1 deletion src/dashboard/overview/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type StateType = DashboardContentState & {
const styles = () => {
return createStyles({
wrapper: {
flex: 1,
overflowY: "auto",
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/wallet/Deposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import ErrorMessage from "../../common/ErrorMessage";
import { getErrorMsg } from "../../common/errorUtil";
import Loader from "../../common/Loader";
import QrCode from "../../common/QrCode";
import WarningMessage from "../../common/WarningMessage";
import { DepositResponse } from "../../models/DepositResponse";
import { GetServiceInfoResponse } from "../../models/GetServiceInfoResponse";
import { Info } from "../../models/Info";
import Address from "./Address";
import BoltzFeeInfo from "./BoltzFeeInfo";
import CheckBoltzTransactionStatus from "./CheckBoltzTransactionStatus";
import WarningMessage from "./WarningMessage";

type DepositProps = {
currency: string;
Expand Down
59 changes: 0 additions & 59 deletions src/dashboard/wallet/WarningMessage.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/dashboard/wallet/WithdrawAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { GetServiceInfoResponse } from "../../models/GetServiceInfoResponse";
import Address from "./Address";
import BoltzFeeInfo from "./BoltzFeeInfo";
import { withdraw } from "./walletUtil";
import WarningMessage from "./WarningMessage";
import WarningMessage from "../../common/WarningMessage";

type WithdrawAddressProps = {
currency: string;
Expand Down
4 changes: 2 additions & 2 deletions src/stores/backupStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { observable } from "mobx";
import { BackupInfo } from "../models/BackupInfo";

export type Details = {
backupInfoLoaded?: boolean;
backupInfoLoaded: boolean;
defaultPassword?: boolean;
mnemonicShown?: boolean;
defaultBackupDirectory?: boolean;
Expand All @@ -24,7 +24,7 @@ export const useBackupStore = (defaultDetails: Details) => {
store.details.defaultBackupDirectory = value.backup.defaultLocation;
store.details.backupDirectory = value.backup.location;
},
get backupInfoLoaded(): boolean | undefined {
get backupInfoLoaded(): boolean {
return store.details.backupInfoLoaded;
},
get backupDirectory(): string | undefined {
Expand Down

0 comments on commit f44be87

Please sign in to comment.