Skip to content

Commit 40a14b7

Browse files
committed
fix: fix README and the build pipeline
1 parent c54f752 commit 40a14b7

File tree

6 files changed

+12
-49
lines changed

6 files changed

+12
-49
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Install the package via npm (or yarn):
4040
```bash
4141
npm install react-ai-translator
4242

43+
```
44+
4345
# Usage
4446

4547
Wrap your application (or a section of it) in the `TranslatorProvider` to initialize the translation model.
@@ -97,6 +99,8 @@ function App() {
9799

98100
export default App
99101

102+
```
103+
100104

101105
# How It Works
102106

lefthook.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pre-commit:
55
run: pnpm biome check --write --unsafe --staged --no-errors-on-unmatched && git add -u
66
typecheck:
77
run: pnpm tsc
8-
# build:
9-
# run: pnpm build
8+
build:
9+
run: pnpm build
1010
test:
1111
run: pnpm test:ci

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"name": "@joelshejar/react-ai-translator",
2+
"name": "@codethicket/react-ai-translator",
33
"description": "",
4-
"version": "0.0.60",
4+
"version": "0.0.62",
55
"author": "Joel Rajesh",
66
"license": "MIT",
7-
"keywords": [],
7+
"keywords": ["translations", "ai", "react", "i18n"],
88
"repository": {
99
"type": "git",
10-
"url": "https://github.com/joelshejar/practise-translations-ai"
10+
"url": "https://github.com/CodeThicket/react-ai-translator"
1111
},
1212
"scripts": {
1313
"dev": "concurrently \"pnpm build --watch\" \"pnpm storybook\" \"pnpm test\" ",

src/hooks/useTranslation.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useEffect, useRef, useState } from "react";
22

3-
// Define the possible statuses returned by the worker
43
type WorkerStatus =
54
| "initiate"
65
| "progress"
@@ -9,21 +8,18 @@ type WorkerStatus =
98
| "update"
109
| "complete";
1110

12-
// Define the structure of the progress items
1311
interface ProgressItem {
1412
file?: string;
1513
progress?: number;
1614
}
1715

18-
// Define the data structure for worker messages
1916
interface WorkerMessageData {
2017
status: WorkerStatus;
2118
output?: string;
2219
progress?: number;
2320
file?: string;
2421
}
2522

26-
// Hook return type
2723
interface UseTranslationReturn {
2824
translate: (text: string, srcLang: string, tgtLang: string) => void;
2925
translatedText: string;
@@ -36,29 +32,22 @@ interface UseTranslationReturn {
3632
const 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,

src/worker.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
// worker.ts
2-
31
import { type PipelineType, pipeline } from "@xenova/transformers";
42

5-
// Extend the global scope for a dedicated worker
63
declare const self: DedicatedWorkerGlobalScope & typeof globalThis;
74

85
/**
@@ -34,19 +31,12 @@ self.addEventListener("message", async (event: MessageEvent) => {
3431
// Retrieve the translation pipeline. When called for the first time,
3532
// this will load the pipeline and save it for future use.
3633
const translator = await MyTranslationPipeline.getInstance((x) => {
37-
// We also add a progress callback to the pipeline so that we can
38-
// track model loading.
3934
self.postMessage(x);
4035
});
4136

42-
// Log the language codes
43-
console.log(event.data.tgt_lang, event.data.src_lang, "langcode");
44-
45-
// Actually perform the translation
4637
const output = await translator?.(event.data.text, {
4738
tgt_lang: event.data.tgt_lang,
4839
src_lang: event.data.src_lang,
49-
// Allows for partial output
5040
callback_function: (x: any) => {
5141
self.postMessage({
5242
status: "update",
@@ -57,7 +47,6 @@ self.addEventListener("message", async (event: MessageEvent) => {
5747
},
5848
});
5949

60-
// Send the output back to the main thread
6150
self.postMessage({
6251
status: "complete",
6352
output: output,

tsup.config.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ const common: Options = {
1717
injectStyle: false,
1818
target: "es2021",
1919
platform: "browser",
20-
// dts: {
21-
// entry: 'src/index.js', // or whichever main file you want
22-
// },
23-
// loader: {
24-
// '.worker.js': 'file', // Handle worker files as static files
25-
// },
2620
};
2721

2822
const getPackageName = async () => {
@@ -68,9 +62,6 @@ const linkSelf = async () => {
6862

6963
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
7064
// biome-ignore lint/suspicious/noConsole: <explanation>
71-
console.log(
72-
`Run 'pnpm link ${await getPackageName()} --global' inside another project to consume this package.`,
73-
);
7465
};
7566

7667
export default defineConfig({

0 commit comments

Comments
 (0)