-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
70 lines (59 loc) · 1.89 KB
/
index.tsx
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
import React, {useState} from 'react'
import ReactDom from 'react-dom'
import styled from 'styled-components'
import {toPairs, sortBy, countBy, flatten} from 'lodash'
import isKanji from 'iskanji'
import {readAsText} from 'promise-file-reader'
import parser from 'subtitles-parser'
import Dropzone from './components/Dropzone'
import Analysis from './components/Analysis'
import Kanjis from './components/Kanjis'
import {KanjiFrequencies, Lines} from './types'
function App() {
const [kanjiFrequencies, setKanjiFrequencies] = useState<KanjiFrequencies>([])
const [lines, setLines] = useState<Lines>([])
const noKanjis = kanjiFrequencies.length === 0
return (
<Dropzone noClick={!noKanjis} onDrop={onDrop}>
<Main>
{noKanjis ? (
<Upload>Click or drag in a SRT file to analyze kanji usage</Upload>
) : (
<Kanjis {...{kanjiFrequencies}} />
)}
</Main>
{!noKanjis && <Analysis {...{kanjiFrequencies, lines}} />}
</Dropzone>
)
async function onDrop(files: File[]) {
const texts = await Promise.all(files.map(readAsText))
const allLines: Lines = flatten(
texts.map((text) => parser.fromSrt(text).map(({text}) => text))
)
const kanjis = calculateKanjiFrequencies(texts.join(''))
setLines(allLines)
setKanjiFrequencies(kanjis)
}
function calculateKanjiFrequencies(subtitles: string): KanjiFrequencies {
const pairs = toPairs(countBy(subtitles.split('').filter(isKanji)))
const sorted = sortBy(pairs, ([, count]) => -count)
return sorted
}
}
const Upload = styled.div`
font-size: 3em;
font-family: sans-serif;
cursor: pointer;
height: 100vh;
display: flex;
align-items: center;
`
const Main = styled.section`
display: flex;
flex-direction: column;
flex-wrap: wrap-reverse;
align-items: center;
height: 100vh;
width: 100%;
`
ReactDom.render(<App />, document.getElementById('app'))