-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
78 lines (69 loc) · 2.31 KB
/
App.tsx
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
import { SafeAreaView, View } from "react-native";
import Header from "./src/components/Header";
import Suggestions from "./src/components/Suggestions";
import MainInput from "./src/components/MainInput";
import Result from "./src/components/Result";
import { useCallback, useMemo, useState } from "react";
import { geminiModel } from "./src/services/geminiModel";
import { useDelayedTextValue } from "./src/hooks/useDelayedTextValue";
import Empty from "./src/components/Empty";
import Loading from "./src/components/Loading";
export default function App() {
const [isLoading, setIsLoading] = useState(false);
const [textValue, setTextValue] = useState("");
const [suggestionsHide, setSuggestionsHide] = useState(false);
const [promptResult, setPromptResult] = useState("");
const [delayedValue, isFinished] = useDelayedTextValue(promptResult);
const handlePrompt = useCallback(async () => {
try {
setIsLoading(true);
setPromptResult("");
if (textValue) {
const result = await geminiModel.generateContent(textValue);
const response = result.response;
const text = response.text();
if (text) {
setTextValue("");
}
setPromptResult(text);
}
} catch (error) {
alert(error);
} finally {
setIsLoading(false);
}
}, [textValue]);
const handleTextValue = useCallback(
(value: string) => setTextValue(value),
[]
);
const hasPrompt = useMemo(
() => (promptResult.length > 0 ? true : false),
[promptResult]
);
return (
<View className="flex-1 bg-white">
<SafeAreaView className="flex-1 bg-white">
<Header />
<Suggestions
isHide={suggestionsHide}
handleSuggestionsHide={(value: boolean) => setSuggestionsHide(value)}
onSelectSuggestion={handleTextValue}
/>
{!promptResult && !isLoading && <Empty />}
{promptResult && !isLoading && (
<Result data={delayedValue} suggestionsHide={suggestionsHide} />
)}
{!promptResult && isLoading && <Loading />}
<MainInput
isLoading={false}
disabled={!isFinished}
value={textValue}
onChangeText={handleTextValue}
onComplete={handlePrompt}
hasPrompt={hasPrompt}
/>
</SafeAreaView>
</View>
);
}