1
1
import { useEffect , useRef , useState } from "react" ;
2
2
3
- // Define the possible statuses returned by the worker
4
3
type WorkerStatus =
5
4
| "initiate"
6
5
| "progress"
@@ -9,21 +8,18 @@ type WorkerStatus =
9
8
| "update"
10
9
| "complete" ;
11
10
12
- // Define the structure of the progress items
13
11
interface ProgressItem {
14
12
file ?: string ;
15
13
progress ?: number ;
16
14
}
17
15
18
- // Define the data structure for worker messages
19
16
interface WorkerMessageData {
20
17
status : WorkerStatus ;
21
18
output ?: string ;
22
19
progress ?: number ;
23
20
file ?: string ;
24
21
}
25
22
26
- // Hook return type
27
23
interface UseTranslationReturn {
28
24
translate : ( text : string , srcLang : string , tgtLang : string ) => void ;
29
25
translatedText : string ;
@@ -36,29 +32,22 @@ interface UseTranslationReturn {
36
32
const useTranslation = ( _workerScript : string ) : UseTranslationReturn => {
37
33
const worker = useRef < Worker | null > ( null ) ;
38
34
39
- // States for managing translation
40
35
const [ loading , setLoading ] = useState < boolean > ( false ) ;
41
36
const [ modelLoading , setModelLoading ] = useState < boolean > ( false ) ;
42
37
const [ progress , setProgress ] = useState < ProgressItem [ ] > ( [ ] ) ;
43
38
const [ translatedText , setTranslatedText ] = useState < string > ( "" ) ;
44
39
const [ error , setError ] = useState < string | null > ( null ) ;
45
40
46
- console . log ( "hhhhh" ) ;
47
-
48
41
useEffect ( ( ) => {
49
42
if ( ! worker . current ) {
50
- // Initialize the worker
51
43
worker . current = new Worker ( new URL ( "./worker.mjs" , import . meta. url ) , {
52
44
type : "module" ,
53
45
} ) ;
54
46
}
55
47
56
- // Handle worker messages
57
48
const onMessage = ( e : MessageEvent < WorkerMessageData > ) => {
58
49
const { status, output, progress : progressValue , file } = e . data ;
59
50
60
- console . log ( status , "status" ) ;
61
-
62
51
switch ( status ) {
63
52
case "initiate" : {
64
53
setLoading ( true ) ;
@@ -68,7 +57,6 @@ const useTranslation = (_workerScript: string): UseTranslationReturn => {
68
57
}
69
58
70
59
case "progress" : {
71
- console . log ( "progress" ) ;
72
60
setProgress ( ( prev ) =>
73
61
prev . map ( ( item ) =>
74
62
item . file === file ? { ...item , progress : progressValue } : item ,
@@ -78,30 +66,24 @@ const useTranslation = (_workerScript: string): UseTranslationReturn => {
78
66
}
79
67
80
68
case "done" : {
81
- console . log ( "done" ) ;
82
69
setModelLoading ( false ) ;
83
70
setProgress ( ( prev ) => prev . filter ( ( item ) => item . file !== file ) ) ;
84
71
break ;
85
72
}
86
73
87
74
case "ready" : {
88
- console . log ( "ready" ) ;
89
75
setLoading ( false ) ;
90
76
break ;
91
77
}
92
78
93
79
case "update" : {
94
- console . log ( "update" ) ;
95
- console . log ( output , "output" ) ;
96
- // Append partial translations
97
80
if ( output ) {
98
81
setTranslatedText ( output ) ;
99
82
}
100
83
break ;
101
84
}
102
85
103
86
case "complete" : {
104
- console . log ( "complete" ) ;
105
87
setLoading ( false ) ;
106
88
break ;
107
89
}
@@ -111,25 +93,22 @@ const useTranslation = (_workerScript: string): UseTranslationReturn => {
111
93
worker . current . addEventListener ( "message" , onMessage ) ;
112
94
113
95
return ( ) => {
96
+ // worker.current?.removeEventListener("message", onMessage)
114
97
// If you want the worker to be terminated on unmount, uncomment below:
115
98
// worker.current?.terminate();
116
99
// worker.current = null;
117
100
} ;
118
101
} , [ ] ) ;
119
102
120
- // Function to send translation requests to the worker
121
103
const translate = ( text : string , srcLang : string , tgtLang : string ) => {
122
- console . log ( "kkkk" ) ;
123
104
if ( ! worker . current ) {
124
105
console . error ( "Worker is not initialized." ) ;
125
106
return ;
126
107
}
127
108
128
109
setLoading ( true ) ;
129
110
setError ( null ) ;
130
- setTranslatedText ( "" ) ; // Reset the output
131
-
132
- console . log ( "lllll" , text , srcLang , tgtLang ) ;
111
+ setTranslatedText ( "" ) ;
133
112
134
113
worker . current . postMessage ( {
135
114
text,
0 commit comments