11import { useEffect , useRef , useState } from "react" ;
22
3- // Define the possible statuses returned by the worker
43type WorkerStatus =
54 | "initiate"
65 | "progress"
@@ -9,21 +8,18 @@ type WorkerStatus =
98 | "update"
109 | "complete" ;
1110
12- // Define the structure of the progress items
1311interface ProgressItem {
1412 file ?: string ;
1513 progress ?: number ;
1614}
1715
18- // Define the data structure for worker messages
1916interface WorkerMessageData {
2017 status : WorkerStatus ;
2118 output ?: string ;
2219 progress ?: number ;
2320 file ?: string ;
2421}
2522
26- // Hook return type
2723interface UseTranslationReturn {
2824 translate : ( text : string , srcLang : string , tgtLang : string ) => void ;
2925 translatedText : string ;
@@ -36,29 +32,22 @@ interface UseTranslationReturn {
3632const useTranslation = ( _workerScript : string ) : UseTranslationReturn => {
3733 const worker = useRef < Worker | null > ( null ) ;
3834
39- // States for managing translation
4035 const [ loading , setLoading ] = useState < boolean > ( false ) ;
4136 const [ modelLoading , setModelLoading ] = useState < boolean > ( false ) ;
4237 const [ progress , setProgress ] = useState < ProgressItem [ ] > ( [ ] ) ;
4338 const [ translatedText , setTranslatedText ] = useState < string > ( "" ) ;
4439 const [ error , setError ] = useState < string | null > ( null ) ;
4540
46- console . log ( "hhhhh" ) ;
47-
4841 useEffect ( ( ) => {
4942 if ( ! worker . current ) {
50- // Initialize the worker
5143 worker . current = new Worker ( new URL ( "./worker.mjs" , import . meta. url ) , {
5244 type : "module" ,
5345 } ) ;
5446 }
5547
56- // Handle worker messages
5748 const onMessage = ( e : MessageEvent < WorkerMessageData > ) => {
5849 const { status, output, progress : progressValue , file } = e . data ;
5950
60- console . log ( status , "status" ) ;
61-
6251 switch ( status ) {
6352 case "initiate" : {
6453 setLoading ( true ) ;
@@ -68,7 +57,6 @@ const useTranslation = (_workerScript: string): UseTranslationReturn => {
6857 }
6958
7059 case "progress" : {
71- console . log ( "progress" ) ;
7260 setProgress ( ( prev ) =>
7361 prev . map ( ( item ) =>
7462 item . file === file ? { ...item , progress : progressValue } : item ,
@@ -78,30 +66,24 @@ const useTranslation = (_workerScript: string): UseTranslationReturn => {
7866 }
7967
8068 case "done" : {
81- console . log ( "done" ) ;
8269 setModelLoading ( false ) ;
8370 setProgress ( ( prev ) => prev . filter ( ( item ) => item . file !== file ) ) ;
8471 break ;
8572 }
8673
8774 case "ready" : {
88- console . log ( "ready" ) ;
8975 setLoading ( false ) ;
9076 break ;
9177 }
9278
9379 case "update" : {
94- console . log ( "update" ) ;
95- console . log ( output , "output" ) ;
96- // Append partial translations
9780 if ( output ) {
9881 setTranslatedText ( output ) ;
9982 }
10083 break ;
10184 }
10285
10386 case "complete" : {
104- console . log ( "complete" ) ;
10587 setLoading ( false ) ;
10688 break ;
10789 }
@@ -111,25 +93,22 @@ const useTranslation = (_workerScript: string): UseTranslationReturn => {
11193 worker . current . addEventListener ( "message" , onMessage ) ;
11294
11395 return ( ) => {
96+ // worker.current?.removeEventListener("message", onMessage)
11497 // If you want the worker to be terminated on unmount, uncomment below:
11598 // worker.current?.terminate();
11699 // worker.current = null;
117100 } ;
118101 } , [ ] ) ;
119102
120- // Function to send translation requests to the worker
121103 const translate = ( text : string , srcLang : string , tgtLang : string ) => {
122- console . log ( "kkkk" ) ;
123104 if ( ! worker . current ) {
124105 console . error ( "Worker is not initialized." ) ;
125106 return ;
126107 }
127108
128109 setLoading ( true ) ;
129110 setError ( null ) ;
130- setTranslatedText ( "" ) ; // Reset the output
131-
132- console . log ( "lllll" , text , srcLang , tgtLang ) ;
111+ setTranslatedText ( "" ) ;
133112
134113 worker . current . postMessage ( {
135114 text,
0 commit comments