|
| 1 | +// @flow |
| 2 | +import React from 'react'; |
| 3 | +import styled from 'styled-components'; |
| 4 | +import { connect } from 'react-redux'; |
| 5 | +import { bindActionCreators } from 'redux'; |
| 6 | +import SockJS from 'sockjs-client'; |
| 7 | + |
| 8 | +import type { CurrentUser } from 'common/types'; |
| 9 | + |
| 10 | +import Centered from 'app/components/flex/Centered'; |
| 11 | +import Title from 'app/components/text/Title'; |
| 12 | +import SubTitle from 'app/components/text/SubTitle'; |
| 13 | +import Button from '../../components/buttons/Button'; |
| 14 | +import { currentUserSelector } from '../../store/user/selectors'; |
| 15 | +import userActionCreators from '../../store/user/actions'; |
| 16 | + |
| 17 | +const Buttons = styled.div` |
| 18 | + display: flex; |
| 19 | +
|
| 20 | + > button { |
| 21 | + margin: 1rem; |
| 22 | + } |
| 23 | +`; |
| 24 | + |
| 25 | +type State = { |
| 26 | + success: boolean, |
| 27 | + loading: boolean, |
| 28 | + error: ?string, |
| 29 | +}; |
| 30 | + |
| 31 | +type Props = { |
| 32 | + user: CurrentUser, |
| 33 | + signIn: typeof userActionCreators.signIn, |
| 34 | +}; |
| 35 | + |
| 36 | +const mapStateToProps = state => ({ |
| 37 | + user: currentUserSelector(state), |
| 38 | +}); |
| 39 | +const mapDispatchToProps = dispatch => ({ |
| 40 | + signIn: bindActionCreators(userActionCreators.signIn, dispatch), |
| 41 | +}); |
| 42 | +class CLI extends React.PureComponent { |
| 43 | + props: Props; |
| 44 | + state: State = { |
| 45 | + success: false, |
| 46 | + loading: false, |
| 47 | + error: null, |
| 48 | + }; |
| 49 | + |
| 50 | + constructor(props) { |
| 51 | + super(props); |
| 52 | + |
| 53 | + const port = this.getPort(); |
| 54 | + if (port === null) { |
| 55 | + document.location.href = '/'; |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + this.port = port; |
| 60 | + } |
| 61 | + |
| 62 | + port: number; |
| 63 | + |
| 64 | + getPort = () => { |
| 65 | + const match = document.location.search.match(/\?port=(.*)/); |
| 66 | + if (match) { |
| 67 | + const portString = match[1]; |
| 68 | + return +portString; |
| 69 | + } |
| 70 | + return null; |
| 71 | + }; |
| 72 | + |
| 73 | + $authorizeCLI = (user: CurrentUser) => |
| 74 | + new Promise((resolve, reject) => { |
| 75 | + try { |
| 76 | + this.setState({ loading: true }); |
| 77 | + const sock = new SockJS(`http://localhost:${this.port}/login`); |
| 78 | + let signedIn = false; |
| 79 | + |
| 80 | + sock.onopen = () => { |
| 81 | + signedIn = true; |
| 82 | + sock.send(JSON.stringify(user)); |
| 83 | + resolve(); |
| 84 | + }; |
| 85 | + |
| 86 | + sock.onmessage = () => { |
| 87 | + sock.close(); |
| 88 | + }; |
| 89 | + |
| 90 | + sock.onclose = () => { |
| 91 | + if (!signedIn) { |
| 92 | + reject( |
| 93 | + new Error( |
| 94 | + 'Could not connect with the CLI, please send a message to @Ives13.', |
| 95 | + ), |
| 96 | + ); |
| 97 | + } |
| 98 | + }; |
| 99 | + } catch (e) { |
| 100 | + reject(e); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + authorize = async () => { |
| 105 | + try { |
| 106 | + await this.$authorizeCLI(this.props.user); |
| 107 | + |
| 108 | + // Close window after succesfull authorization |
| 109 | + window.close(); |
| 110 | + this.setState({ success: true }); |
| 111 | + } catch (e) { |
| 112 | + this.setState({ error: e.message }); |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + signIn = async () => { |
| 117 | + try { |
| 118 | + await this.props.signIn(); |
| 119 | + await this.authorize(); |
| 120 | + } catch (e) { |
| 121 | + this.setState({ error: e.message }); |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + render() { |
| 126 | + const { user } = this.props; |
| 127 | + const { error, loading, success } = this.state; |
| 128 | + |
| 129 | + if (success) { |
| 130 | + return ( |
| 131 | + <Centered horizontal vertical> |
| 132 | + <Title>Succesfully authorized the CLI</Title> |
| 133 | + <SubTitle>You can now close this window</SubTitle> |
| 134 | + </Centered> |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + if (error) { |
| 139 | + return ( |
| 140 | + <Centered horizontal vertical> |
| 141 | + <Title>An error occured:</Title> |
| 142 | + <SubTitle>{error}</SubTitle> |
| 143 | + <Buttons> |
| 144 | + <Button href="/">Go to homepage</Button> |
| 145 | + </Buttons> |
| 146 | + </Centered> |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + if (loading) { |
| 151 | + return ( |
| 152 | + <Centered horizontal vertical> |
| 153 | + <Title>Authorizing...</Title> |
| 154 | + </Centered> |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + if (!user.jwt) { |
| 159 | + return ( |
| 160 | + <Centered horizontal vertical> |
| 161 | + <Title>Welcome to CodeSandbox!</Title> |
| 162 | + <SubTitle> |
| 163 | + To use the CLI you need to sign in with your GitHub account. |
| 164 | + </SubTitle> |
| 165 | + <Buttons> |
| 166 | + <Button onClick={this.signIn}>Sign in with Github</Button> |
| 167 | + </Buttons> |
| 168 | + </Centered> |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + return ( |
| 173 | + <Centered horizontal vertical> |
| 174 | + <Title>Hello {user.username}!</Title> |
| 175 | + <SubTitle> |
| 176 | + To make use of the CLI you need to authorize it first,<br /> you can |
| 177 | + authorize by pressing {"'"}login{"'"}. |
| 178 | + </SubTitle> |
| 179 | + <Buttons> |
| 180 | + <Button red>Cancel</Button> |
| 181 | + <Button onClick={this.authorize}>Login</Button> |
| 182 | + </Buttons> |
| 183 | + </Centered> |
| 184 | + ); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +export default connect(mapStateToProps, mapDispatchToProps)(CLI); |
0 commit comments