Skip to content
This repository was archived by the owner on Apr 6, 2024. It is now read-only.

Commit 834d9a9

Browse files
committed
feat: upload
1 parent c12e78e commit 834d9a9

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

public/population_state_10x10_35.txt renamed to public/population_state_10x10_36.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Generation 35:
1+
Generation 36:
22
10 10
33
. . . * * . . . . *
44
. . * . * . . * . .

public/population_state_5x11_20.txt renamed to public/population_state_11x5_20.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Generation 20:
2-
5 11
2+
11 5
33
. * . * .
44
. . * . *
55
* . * . *

src/App.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import * as React from 'react'
22

33
import {Grid} from './components/Grid'
44
import {Cell} from './components/Cell'
5-
import {readCols, readRows} from './helpers/main'
65
import {Upload} from './components/Upload'
7-
import {Details} from './components/Details'
6+
import {readRows, readCols} from './helpers/main'
87

98
export const resolution = 600
109

@@ -16,29 +15,23 @@ function App() {
1615
const onFileUpload: React.ChangeEventHandler<HTMLInputElement> = event => {
1716
const file = event.target.files?.[0]
1817
if (file) {
19-
file.text().then(setText).catch(console.error)
18+
file
19+
.text()
20+
.then(text => {
21+
setText(text)
22+
setRows(readRows(text))
23+
setCols(readCols(text))
24+
})
25+
.catch(console.error)
2026
} else {
2127
console.warn('Could not find file')
2228
}
2329
}
2430

25-
React.useEffect(() => {
26-
if (text.length) {
27-
setRows(readRows(text))
28-
setCols(readCols(text))
29-
}
30-
}, [text])
31-
3231
return (
3332
<>
34-
<Details
35-
list={[
36-
{term: 'Rows:', description: rows},
37-
{term: 'Cols:', description: cols},
38-
]}
39-
/>
4033
<Upload onChange={onFileUpload} />
41-
<Grid cols={cols} rows={rows} text={text}>
34+
<Grid text={text} rows={rows} cols={cols}>
4235
{generation =>
4336
generation.map((axis, index) => {
4437
return (
@@ -48,8 +41,8 @@ function App() {
4841
<Cell
4942
key={index + Math.random()}
5043
state={state}
51-
cols={cols}
52-
rows={rows}
44+
rows={generation.length}
45+
cols={generation[0].length}
5346
/>
5447
)
5548
})}

src/components/Grid.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ import {Slider} from './Slider'
77
import {Details} from './Details'
88

99
interface Props<T> {
10-
cols: number
11-
rows: number
1210
text: string
11+
rows: number
12+
cols: number
1313
children: (data: T) => React.ReactNode
1414
}
1515

16-
export const Grid = ({cols, rows, text, children}: Props<Generation>) => {
17-
const [delay, setDelay] = React.useState(1000)
16+
export const Grid = ({text, rows, cols, children}: Props<Generation>) => {
1817
const [generation, setGeneration] = React.useState<Generation>([[]])
19-
20-
const onDelayUpdate: React.ChangeEventHandler<HTMLInputElement> = event =>
21-
setDelay(parseInt(event.target.value))
18+
const [delay, setDelay] = React.useState(1000)
2219

2320
React.useEffect(() => {
2421
setGeneration(draw(build(cols, rows), parse(text)))
@@ -31,10 +28,17 @@ export const Grid = ({cols, rows, text, children}: Props<Generation>) => {
3128
return () => clearInterval(timeout)
3229
})
3330

31+
const onDelayUpdate: React.ChangeEventHandler<HTMLInputElement> = event =>
32+
setDelay(parseInt(event.target.value))
33+
3434
return (
3535
<>
3636
<Details
37-
list={[{term: 'Generation:', description: countLiveCells(generation)}]}
37+
list={[
38+
{term: 'Generation:', description: countLiveCells(generation)},
39+
{term: 'Rows:', description: rows},
40+
{term: 'Cols:', description: cols},
41+
]}
3842
/>
3943
<Slider onChange={onDelayUpdate} value={delay} />
4044
<div

0 commit comments

Comments
 (0)