@jajoosam/

Exoplanet Explorer

React

A React web app that allows you to view NASA exoplanet data

fork
loading
Files
  • src
    • App.js
  • dist
  • .babelrc
  • favicon.ico
  • index.html
  • package-lock.json
  • package.json
  • README.md
  • screenshot.png
  • webpack.config.js
  • yarn-error.log
src/App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import React from 'react'
import ReactDOM from 'react-dom'
import styled from 'styled-components'
import * as THREE from 'three'
import axios from 'axios'

const Font = "'Inconsolata', monospace"

const AppContainer = styled.div`
    width: 100vw;
    height: 100vh;
    background-color: #9ce1fe;
`

const AppHeader = styled.div`
    display: flex;
    justify-content: center;
    height: 10vh;
`

const Column = styled.div`
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
`

const AppTitle = styled.h1`
    margin-top: 5vh;
    font-family: ${Font};
    text-decoration: underline #4739c5;
`

const AppSubtitle = styled.h3`
    margin-top: 0;
    font-family: ${Font};
`

const Link = styled.a`
    color: #f68121;
    text-decoration: none;
`

const AppContent = styled.div`
    display: flex;
    width: 100vw;
    height: 90vh;
`

const Planet = styled.div`
    width: 40vw;
    margin-top: 7.5vh;
`

const PlanetData = styled.div`
    display: flex;
    flex-direction: column;
    width: 60vw;
    margin-top: 7.5vh;
    font-size: 1.1vw;
`

const PlanetName = styled.h1`
    font-family: ${Font};
    font-size: 3em;
    margin-top: 10vh;
    margin-bottom: 0;
`

const PlanetHost = styled.h2`
    font-family: ${Font};
`

const PlanetDetails = styled.h2`
    font-family: ${Font};
    margin-right: 5vw;
`

const Text = styled.span`
    color: #000;
`

const Data = styled.span`
    color: #f68121;
`

const Loader = styled.h1`
    font-family: ${Font};
    margin: 0 auto 0 auto;
    padding: 35vh 0 0 0;
`

const AppFooter = styled.div`
    font-family: ${Font};
    position: absolute;
    right: 1vw;
    bottom: 1vh;
`

class App extends React.Component {

    constructor() {
        super()
        this.state = {
            data: [],
            scene: null,
            camera: null,
            renderer: null,
            rendererWidth: 0,
            rendererHeight: 0,
            geometry: null,
            material: null,
            sphere: null,
            currentPlanet: null
        }

        this.onWindowResize = this.onWindowResize.bind(this)
        this.onSpacebar = this.onSpacebar.bind(this)
        this.getPlanetData = this.getPlanetData.bind(this)
        this.getNewPlanetData = this.getNewPlanetData.bind(this)
        this.generatePlanet = this.generatePlanet.bind(this)
    }

    componentDidMount() {
        window.addEventListener('resize', this.onWindowResize)
        window.addEventListener('keypress', this.onSpacebar)

        const self = this
        this.getPlanetData()
        setInterval(() => {
            self.getNewPlanetData()
        }, 5000)
    }

    componentWillUnmount() {
        window.removeEventListener('resize')
        window.removeEventListener('keypress')

        this.state.sphere.dispose()
        this.state.material.dispose()
        this.state.geometry.dispose()
        this.state.renderer.dispose()
        this.state.camera.dipose()
        this.state.scene.dispose()
    }

    onWindowResize() {
        const planet = document.getElementById('planet')
        if (planet) {
            this.state.renderer.setSize(planet.clientWidth, planet.clientWidth)
        }
    }

    onSpacebar(e) {
        if (e.code === 'Space') {
            this.getNewPlanetData()
        }
    }

    getPlanetData() {
        axios.get('https://exoplanetarchive.ipac.caltech.edu/cgi-bin/nstedAPI/nph-nstedAPI?', {
            params: {
                table: 'exoplanets',
                select: 'pl_hostname, pl_name, pl_rade, pl_masse, pl_dens, pl_eqt, gaia_dist, st_dist, st_lum',
                format: 'json'
            }
        })
            .then((res) => {
                this.setState({
                    data: res.data,
                    currentPlanet: res.data[0]
                }, () => {
                    this.generatePlanet()
                })
            })
            .catch((err) => {
                console.log(err)
            })
    }

    getNewPlanetData() {
        const currentData = this.state.data
        const randomNumber = Math.floor((Math.random() * currentData.length - 1) + 0)
        const randomColor = '0x' + (0x1000000 + (Math.random()) * 0xffffff).toString(16).substr(1, 6)

        if (this.state.sphere && /^0x[0-9A-F]{6}$/i.test(randomColor)) {
            this.state.sphere.material.color.setHex(randomColor)
        }
        this.setState({
            currentPlanet: currentData[randomNumber]
        })
    }

    generatePlanet() {
        const planet = document.getElementById('planet')
        const scene = new THREE.Scene()
        const camera = new THREE.PerspectiveCamera(80, 1, 1, 10)
        const renderer = new THREE.WebGLRenderer({ alpha: true })
        renderer.setSize(planet.clientWidth, planet.clientWidth)

        planet.appendChild(renderer.domElement)

        const geometry = new THREE.SphereGeometry(2, 10, 10)
        const material = new THREE.MeshBasicMaterial({ color: 0xffa2bc, wireframe: true })
        const sphere = new THREE.Mesh(geometry, material)

        this.setState({
            scene: scene,
            camera: camera,
            renderer: renderer,
            geometry: geometry,
            material: material,
            sphere: sphere
        })

        scene.add(sphere)
        camera.position.z = 5
        const animate = () => {
            requestAnimationFrame(animate)
            sphere.rotation.x += 0.01
            sphere.rotation.y += 0.01
            renderer.render(scene, camera)
        }
        animate()
    }

    render() {
        const { data, currentPlanet } = this.state
        const earthRadius = 6.371 * Math.pow(10, 6)
        const earthMass = 5.972 * Math.pow(10, 24)
        const speedOfLight = 0.307

        let planetRadius,
            planetMass,
            planetDensity,
            planetTemp,
            distanceFromEarth,
            stellarDistance,
            stellarLuminosity,
            goldilocksInner,
            goldilocksOuter,
            isHabitable

        if (currentPlanet) {
            planetRadius = currentPlanet.pl_rade
            planetMass = currentPlanet.pl_masse
            planetDensity = currentPlanet.pl_dens * 1000
            planetTemp = currentPlanet.pl_eqt
            distanceFromEarth = currentPlanet.gaia_dist
            stellarDistance = currentPlanet.st_dist * 206265
            stellarLuminosity = currentPlanet.st_lum
            goldilocksInner = Math.sqrt(stellarLuminosity / 1.1)
            goldilocksOuter = Math.sqrt(stellarLuminosity / 0.53)
            isHabitable = stellarDistance <= goldilocksOuter && stellarDistance >= goldilocksInner ? true : false
        }

        return (
            <AppContainer>
                <AppHeader>
                    <Column>
                        <AppTitle>Exoplanet Explorer</AppTitle>
                        <AppSubtitle>
                            Press <Link href='https://repl.it/talk/challenge/Announcing-contest-SPACE/15339'>&lt;SPACE&gt;</Link> for a new exoplanet!
                        </AppSubtitle>
                    </Column>
                </AppHeader>
                <AppContent>
                    {data.length && currentPlanet ?
                        <React.Fragment>
                            <Planet id='planet' />
                            <PlanetData>
                                <PlanetName>
                                    {currentPlanet.pl_name}
                                </PlanetName>
                                <PlanetHost>
                                    is located in the {currentPlanet.pl_hostname} star system.
                                </PlanetHost>
                                <PlanetDetails>
                                    {planetRadius ? <Text>It has a radius of <Data>{(planetRadius * earthRadius).toPrecision(2)}</Data> m, which is <Data>{planetRadius}</Data> Earth radii. </Text> :
                                        <Text>Its radius is unknown or its data is unavailable. </Text>}
                                    {planetMass ? <Text>Its mass is <Data>{(planetMass * earthMass).toPrecision(3)}</Data> kg, equivalent to <Data>{planetMass}</Data> Earth masses. </Text> :
                                        <Text>Its mass is unknown or its data is unavailable. </Text>}
                                    {planetDensity ? <Text>Its density is <Data>{planetDensity}</Data> kg/m^3. </Text> :
                                        <Text>Its density is unknown or its data is unavailable. </Text>}
                                    {planetTemp ? <Text>It has a planet equilibrium temperature of <Data>{planetTemp}</Data> K.</Text> :
                                        <Text>Its planet equilibrium temperature is unknown or its data is unavailable.</Text>}
                                </PlanetDetails>
                                <PlanetDetails>
                                    {distanceFromEarth ? <Text>It would take <Data>{(distanceFromEarth / speedOfLight).toPrecision(2)}</Data> years to travel there at the speed of light. &#128171;</Text> :
                                        <Text>The time needed to travel there at the speed of light is unknown or the data is unavailable.</Text>}
                                </PlanetDetails>
                                <PlanetDetails>
                                    {isHabitable ? <Text>Based on the data, this planet is in the Goldilocks zone &#9989;</Text> :
                                        <Text>Based on the data, this planet is not in the Goldilocks zone &#128683;</Text>}
                                </PlanetDetails>
                            </PlanetData>
                        </React.Fragment> :
                        <Loader>Loading data from NASA... &#128640;</Loader>
                    }
                </AppContent>
                <AppFooter>
                    <Text>Made by <Link href='https://twitter.com/farrelmahaztra'>@farrelmahaztra</Link> with the <Link href='https://api.nasa.gov/api.html#exoPlanet'>NASA exoplanet API</Link></Text>
                </AppFooter>
            </AppContainer>
        )
    }
}

ReactDOM.render(<App />, document.getElementById('root'))