diff --git a/frontend/src/basic/Main.tsx b/frontend/src/basic/Main.tsx index 0e91ef59..51aeb783 100644 --- a/frontend/src/basic/Main.tsx +++ b/frontend/src/basic/Main.tsx @@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react'; import { HashRouter, BrowserRouter, Switch, Route } from 'react-router-dom'; import { useTheme, Box, Slide, Typography } from '@mui/material'; -import UserGenPage from './UserGenPage'; +import RobotPage from './RobotPage'; import MakerPage from './MakerPage'; import BookPage from './BookPage'; import OrderPage from './OrderPage'; @@ -349,11 +349,9 @@ const Main = ({ settings, setSettings }: MainProps): JSX.Element => { appear={slideDirection.in != undefined} >
- void; + setCurrentOrder: (state: number) => void; + robot: Robot; + setRobot: (state: Robot) => void; + fetchRobot: ({}) => void; + baseUrl: string; +} + +const RobotPage = ({ + setPage, + setCurrentOrder, + robot, + setRobot, + fetchRobot, + baseUrl, +}: RobotPageProps): JSX.Element => { + const { t } = useTranslation(); + const params = useParams(); + const theme = useTheme(); + const refCode = params.refCode; + + const [robotFound, setRobotFound] = useState(false); + const [badRequest, setBadRequest] = useState(null); + const [tokenChanged, setTokenChanged] = useState(false); + const [inputToken, setInputToken] = useState(''); + + useEffect(() => { + if (robot.nickname != null) { + setInputToken(robot.token); + } else if (robot.token) { + setInputToken(robot.token); + getGenerateRobot(robot.token); + } else { + const newToken = genBase62Token(36); + setInputToken(newToken); + getGenerateRobot(newToken); + } + }, []); + + const getGenerateRobot = (token: string) => { + const strength = tokenStrength(token); + setRobot({ ...robot, loading: true, avatarLoaded: false }); + + const requestBody = genKey(token).then(function (key) { + return { + token_sha256: sha256(token), + public_key: key.publicKeyArmored, + encrypted_private_key: key.encryptedPrivateKeyArmored, + unique_values: strength.uniqueValues, + counts: strength.counts, + length: token.length, + ref_code: refCode, + }; + }); + + requestBody.then((body) => + apiClient.post(baseUrl, '/api/user/', body).then((data: any) => { + setRobotFound(data?.found); + setBadRequest(data?.bad_request); + setCurrentOrder( + data.active_order_id + ? data.active_order_id + : data.last_order_id + ? data.last_order_id + : null, + ); + // Add nick and token to App state (token only if not a bad request) + data.bad_request + ? setRobot({ + ...robot, + avatarLoaded: true, + loading: false, + nickname: data.nickname ?? robot.nickname, + activeOrderId: data.active_order_id ?? null, + referralCode: data.referral_code ?? robot.referralCode, + earnedRewards: data.earned_rewards ?? robot.earnedRewards, + lastOrderId: data.last_order_id ?? robot.lastOrderId, + stealthInvoices: data.wants_stealth ?? robot.stealthInvoices, + tgEnabled: data.tg_enabled, + tgBotName: data.tg_bot_name, + tgToken: data.tg_token, + }) + : setRobot({ + ...robot, + nickname: data.nickname, + token, + loading: false, + activeOrderId: data.active_order_id ?? null, + lastOrderId: data.last_order_id ?? null, + referralCode: data.referral_code, + earnedRewards: data.earned_rewards ?? 0, + stealthInvoices: data.wants_stealth, + tgEnabled: data.tg_enabled, + tgBotName: data.tg_bot_name, + tgToken: data.tg_token, + bitsEntropy: data.token_bits_entropy, + shannonEntropy: data.token_shannon_entropy, + pubKey: data.public_key, + encPrivKey: data.encrypted_private_key, + copiedToken: data.found ? true : robot.copiedToken, + }) & + systemClient.setItem('robot_token', token) & + systemClient.setItem('pub_key', data.public_key.split('\n').join('\\')) & + systemClient.setItem('enc_priv_key', data.encrypted_private_key.split('\n').join('\\')); + }), + ); + }; + + const deleteRobot = () => { + apiClient.delete(baseUrl, '/api/user'); + systemClient.deleteCookie('sessionid'); + systemClient.deleteItem('robot_token'); + systemClient.deleteItem('pub_key'); + systemClient.deleteItem('enc_priv_key'); + }; + + const logoutRobot = () => {}; + const handleChangeToken = () => {}; + const handleClickSubmitToken = () => {}; + const handleClickNewRandomToken = () => {}; + + const createJsonFile = () => { + return { + token: robot.token, + token_shannon_entropy: robot.shannonEntropy, + token_bit_entropy: robot.bitsEntropy, + public_key: robot.pub_key, + encrypted_private_key: robot.enc_priv_key, + }; + }; + + return ( + + +
+ + + {robot.avatarLoaded && robot.nickname ? ( +
+ + + + {robot.nickname && systemClient.getCookie('sessionid') ? ( + + ) : ( + '' + )} + + + + + +
+
+
+ ) : ( + + )} +
+ {robotFound ? ( + + + {t('A robot avatar was found, welcome back!')} +
+
+
+ ) : ( + <> + )} + + + { + if (e.key === 'Enter') { + handleClickSubmitToken(); + } + }} + InputProps={{ + startAdornment: ( +
+ + + + + saveAsJson(robot.nickname + '.json', createJsonFile())} + > + + + + + + + + + systemClient.copyToClipboard(systemClient.getItem('robot_token')) & + setRobot({ ...robot, copiedToken: true }) + } + > + + + + + +
+ ), + endAdornment: ( + + + + + + ), + }} + /> +
+
+ + {tokenChanged ? ( + + ) : ( + +
+ +
+
+ )} +
+ + {/* + +
+ +
+ + + + + {t('Simple and Private LN P2P Exchange')} + + + + + + +
+ */} + + ); +}; + +export default RobotPage; + +// class UserGenPage extends Component { + +// handleClickNewRandomToken = () => { +// const inputToken = genBase62Token(36); +// this.setState({ +// inputToken, +// tokenHasChanged: true, +// }); +// setRobot({ ...robot, copiedToken: true }); +// }; + +// handleChangeToken = (e) => { +// this.setState({ +// inputToken: e.target.value.split(' ').join(''), +// tokenHasChanged: true, +// }); +// }; + +// handleClickSubmitToken = () => { +// this.delGeneratedUser(); +// this.getGeneratedUser(inputToken); +// this.setState({ tokenHasChanged: false }); +// setRobot({ +// ...robot, +// avatarLoaded: false, +// nickname: null, +// token: null, +// copiedToken: false, +// lastOrderId: null, +// activeOrderId: null, +// }); +// }; + +// render() { +// const { t, i18n } = this.props; +// const fontSize = this.props.theme.typography.fontSize; +// const 1.000004 = fontSize / 14; // to scale sizes, default fontSize is 14 +// return ( +// +// +//
+// +// +// {robot.avatarLoaded && robot.nickname ? ( +//
+// +// +// +// {robot.nickname && systemClient.getCookie('sessionid') ? ( +//
+// +// {robot.nickname} +// +//
+// ) : ( +// '' +// )} +//
+//
+//
+// +// +//
+//
+//
+// ) : ( +// +// )} +//
+// {this.state.found ? ( +// +// +// {this.state.found ? t('A robot avatar was found, welcome back!') : null} +//
+//
+//
+// ) : ( +// '' +// )} +// +// +// { +// if (e.key === 'Enter') { +// this.handleClickSubmitToken(); +// } +// }} +// InputProps={{ +// startAdornment: ( +//
+// +// +// +// +// +// saveAsJson( +// robot.nickname + '.json', +// this.createJsonFile(), +// ) +// } +// > +// +// +// +// +// +// +// +// +// systemClient.copyToClipboard(systemClient.getItem('robot_token')) & +// setRobot({ ...robot, copiedToken: true }) +// } +// > +// +// +// +// +// +//
+// ), +// endAdornment: ( +// +// +// +// +// +// ), +// }} +// /> +//
+//
+// +// {this.state.tokenHasChanged ? ( +// +// ) : ( +// +//
+// +//
+//
+// )} +//
+ +// +// +//
+// +//
+// +// +// +// +// {t('Simple and Private LN P2P Exchange')} +// +// +// +// +// +// +//
+// +// +// ); +// } +// } + +// export default withTranslation()(UserGenPage);