import React, { Component } from "react"; import { withTranslation } from "react-i18next"; import { Button , Tooltip, Dialog, Grid, Typography, TextField, ButtonGroup, CircularProgress, IconButton} from "@mui/material" import { Link } from 'react-router-dom' import Image from 'material-ui-image' import InfoDialog from './InfoDialog' import SmartToyIcon from '@mui/icons-material/SmartToy'; import CasinoIcon from '@mui/icons-material/Casino'; import ContentCopy from "@mui/icons-material/ContentCopy"; import RoboSatsNoTextIcon from "./icons/RoboSatsNoTextIcon" import BoltIcon from '@mui/icons-material/Bolt'; import { getCookie, writeCookie } from "../utils/cookies"; class UserGenPage extends Component { constructor(props) { super(props); this.state = { openInfo: false, tokenHasChanged: false, }; this.refCode = this.props.match.params.refCode; // Checks in parent HomePage if there is already a nick and token // Displays the existing one if (this.props.nickname != null){ this.state = { nickname: this.props.nickname, token: this.props.token? this.props.token : null, avatar_url: '/static/assets/avatars/' + this.props.nickname + '.png', loadingRobot: false } } else{ var newToken = this.genBase62Token(36) this.state = { token: newToken }; this.getGeneratedUser(newToken); } } // sort of cryptographically strong function to generate Base62 token client-side genBase62Token(length) { return window.btoa(Array.from( window.crypto.getRandomValues( new Uint8Array(length * 2))) .map((b) => String.fromCharCode(b)) .join("")).replace(/[+/]/g, "") .substring(0, length); } getGeneratedUser=(token)=>{ fetch('/api/user' + '?token=' + token + '&ref_code=' + this.refCode) .then((response) => response.json()) .then((data) => { this.setState({ nickname: data.nickname, bit_entropy: data.token_bits_entropy, avatar_url: '/static/assets/avatars/' + data.nickname + '.png', shannon_entropy: data.token_shannon_entropy, bad_request: data.bad_request, found: data.found, loadingRobot:false, }) & // Add nick and token to App state (token only if not a bad request) (data.bad_request ? this.props.setAppState({ nickname: data.nickname, avatarLoaded: false, }) : (this.props.setAppState({ nickname: data.nickname, token: token, avatarLoaded: false, })) & writeCookie("robot_token",token)) & // If the robot has been found (recovered) we assume the token is backed up (data.found ? this.props.setAppState({copiedToken:true}) : null) }); } delGeneratedUser() { const requestOptions = { method: 'DELETE', headers: {'Content-Type':'application/json', 'X-CSRFToken': getCookie('csrftoken')}, }; fetch("/api/user", requestOptions) .then((response) => response.json()); } handleClickNewRandomToken=()=>{ var token = this.genBase62Token(36); this.setState({ token: token, tokenHasChanged: true, }); this.props.setAppState({copiedToken: true}) } handleChangeToken=(e)=>{ this.setState({ token: e.target.value, tokenHasChanged: true, }) } handleClickSubmitToken=()=>{ this.delGeneratedUser(); this.getGeneratedUser(this.state.token); this.setState({loadingRobot: true, tokenHasChanged: false}); this.props.setAppState({avatarLoaded: false, nickname: null, token: null, copiedToken: false}); } handleClickOpenInfo = () => { this.setState({openInfo: true}); }; handleCloseInfo = () => { this.setState({openInfo: false}); }; InfoDialog =() =>{ return( ) } render() { const { t, i18n} = this.props; return (
{!this.state.loadingRobot ?
{this.state.nickname ? : ""}

: }
{ this.state.found ? {this.state.found ? t("A robot avatar was found, welcome back!"):null}
: "" } { if (e.key === 'Enter') { this.handleClickSubmitToken(); } }} InputProps={{ startAdornment: (navigator.clipboard.writeText(this.state.token) & this.props.setAppState({copiedToken:true}))}> , endAdornment: , }} /> {this.state.tokenHasChanged ? :
}
{t("Simple and Private LN P2P Exchange")}
); } } export default withTranslation()(UserGenPage);