MCPcopy
hub / github.com/mckaywrigley/ai-code-translator / handleTranslate

Function handleTranslate

pages/index.tsx:20–99  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

18 const [apiKey, setApiKey] = useState<string>('');
19
20 const handleTranslate = async () => {
21 const maxCodeLength = model === 'gpt-3.5-turbo' ? 6000 : 12000;
22
23 if (!apiKey) {
24 alert('Please enter an API key.');
25 return;
26 }
27
28 if (inputLanguage === outputLanguage) {
29 alert('Please select different languages.');
30 return;
31 }
32
33 if (!inputCode) {
34 alert('Please enter some code.');
35 return;
36 }
37
38 if (inputCode.length > maxCodeLength) {
39 alert(
40 `Please enter code less than ${maxCodeLength} characters. You are currently at ${inputCode.length} characters.`,
41 );
42 return;
43 }
44
45 setLoading(true);
46 setOutputCode('');
47
48 const controller = new AbortController();
49
50 const body: TranslateBody = {
51 inputLanguage,
52 outputLanguage,
53 inputCode,
54 model,
55 apiKey,
56 };
57
58 const response = await fetch('/api/translate', {
59 method: 'POST',
60 headers: {
61 'Content-Type': 'application/json',
62 },
63 signal: controller.signal,
64 body: JSON.stringify(body),
65 });
66
67 if (!response.ok) {
68 setLoading(false);
69 alert('Something went wrong.');
70 return;
71 }
72
73 const data = response.body;
74
75 if (!data) {
76 setLoading(false);
77 alert('Something went wrong.');

Callers 1

HomeFunction · 0.85

Calls 1

copyToClipboardFunction · 0.85

Tested by

no test coverage detected