-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInformation.jsx
108 lines (91 loc) · 4.6 KB
/
Information.jsx
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
import React, { useState, useEffect, useRef } from 'react'
import Transcription from './Transcription'
import Translation from './Translation'
export default function Information(props) {
const { output, finished } = props
const [tab, setTab] = useState('transcription')
const [translation, setTranslation] = useState(null)
const [toLanguage, setToLanguage] = useState('Select language')
const [translating, setTranslating] = useState(null)
console.log(output)
const worker = useRef()
useEffect(() => {
if (!worker.current) {
worker.current = new Worker(new URL('../utils/translate.worker.js', import.meta.url), {
type: 'module'
})
}
const onMessageReceived = async (e) => {
switch (e.data.status) {
case 'initiate':
console.log('DOWNLOADING')
break;
case 'progress':
console.log('LOADING')
break;
case 'update':
setTranslation(e.data.output)
console.log(e.data.output)
break;
case 'complete':
setTranslating(false)
console.log("DONE")
break;
}
}
worker.current.addEventListener('message', onMessageReceived)
return () => worker.current.removeEventListener('message', onMessageReceived)
})
const textElement = tab === 'transcription' ? output.map(val => val.text) : translation || ''
function handleCopy() {
navigator.clipboard.writeText(textElement)
}
function handleDownload() {
const element = document.createElement("a")
const file = new Blob([textElement], { type: 'text/plain' })
element.href = URL.createObjectURL(file)
element.download = `Freescribe_${new Date().toString()}.txt`
document.body.appendChild(element)
element.click()
}
function generateTranslation() {
if (translating || toLanguage === 'Select language') {
return
}
setTranslating(true)
worker.current.postMessage({
text: output.map(val => val.text),
src_lang: 'eng_Latn',
tgt_lang: toLanguage
})
}
return (
<main className='flex-1 p-4 flex flex-col gap-3 text-center sm:gap-4 justify-center pb-20 max-w-prose w-full mx-auto'>
<h1 className='font-semibold text-4xl sm:text-5xl md:text-6xl whitespace-nowrap'>Your <span className='text-blue-400 bold'>Transcription</span></h1>
<div className='grid grid-cols-2 sm:mx-auto bg-white rounded overflow-hidden items-center p-1 blueShadow border-[2px] border-solid border-blue-300'>
<button onClick={() => setTab('transcription')} className={'px-4 rounded duration-200 py-1 ' + (tab === 'transcription' ? ' bg-blue-300 text-white' : ' text-blue-400 hover:text-blue-600')}>Transcription</button>
<button onClick={() => setTab('translation')} className={'px-4 rounded duration-200 py-1 ' + (tab === 'translation' ? ' bg-blue-300 text-white' : ' text-blue-400 hover:text-blue-600')}>Translation</button>
</div>
<div className='my-8 flex flex-col-reverse max-w-prose w-full mx-auto gap-4'>
{(!finished || translating) && (
<div className='grid place-items-center'>
<i className="fa-solid fa-spinner animate-spin"></i>
</div>
)}
{tab === 'transcription' ? (
<Transcription {...props} textElement={textElement} />
) : (
<Translation {...props} toLanguage={toLanguage} translating={translating} textElement={textElement} setTranslating={setTranslating} setTranslation={setTranslation} setToLanguage={setToLanguage} generateTranslation={generateTranslation} />
)}
</div>
<div className='flex items-center gap-4 mx-auto '>
<button onClick={handleCopy} title="Copy" className='bg-white hover:text-blue-500 duration-200 text-blue-300 px-2 aspect-square grid place-items-center rounded'>
<i className="fa-solid fa-copy"></i>
</button>
<button onClick={handleDownload} title="Download" className='bg-white hover:text-blue-500 duration-200 text-blue-300 px-2 aspect-square grid place-items-center rounded'>
<i className="fa-solid fa-download"></i>
</button>
</div>
</main>
)
}