-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContext.jsx
More file actions
84 lines (74 loc) · 2.07 KB
/
Context.jsx
File metadata and controls
84 lines (74 loc) · 2.07 KB
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
import { createContext, useState } from "react";
import runChat from "../config/Gemini";
export const Context = createContext();
const ContextProvider = (props) => {
const [input, setInput] = useState("");
const [recentPrompt, setRecentPrompt] = useState("");
const [prevPrompts, setPrevPrompts] = useState([]);
const [showResults, setShowResults] = useState(false);
const [loading, setLoading] = useState(false);
const [resultData, setResultData] = useState("");
const delayPara = (index, nextWord) => {
setTimeout(function () {
setResultData((prev) => prev + nextWord);
}, 10 * index);
};
const newChat = () =>{
setLoading(false);
setShowResults(false)
}
const onSent = async (prompt) => {
setResultData("");
setLoading(true);
setShowResults(true);
let response;
if(prompt !== undefined){
response = await runChat(prompt);
setRecentPrompt(prompt)
}else{
setPrevPrompts(prev=>[...prev,input]);
setRecentPrompt(input);
response=await runChat(input);
}
try {
let responseArray = response.split("**");
let newResponse = "";
for (let i = 0; i < responseArray.length; i++) {
if (i === 0 || i % 2 !== 1) {
newResponse += responseArray[i];
} else {
newResponse += "<b>" + responseArray[i] + "</b>";
}
}
let newResponse2 = newResponse.split("*").join("<br/>");
let newResponseArray = newResponse2.split("");
for (let i = 0; i < newResponseArray.length; i++) {
const nextWord = newResponseArray[i];
delayPara(i, nextWord + "");
}
} catch (error) {
console.error("Error while running chat:", error);
// Handle error appropriately
} finally {
setLoading(false);
setInput("");
}
};
const contextValue = {
prevPrompts,
setPrevPrompts,
onSent,
setRecentPrompt,
recentPrompt,
input,
setInput,
showResults,
loading,
resultData,
newChat,
};
return (
<Context.Provider value={contextValue}>{props.children}</Context.Provider>
);
};
export default ContextProvider;