/
CreateBrainStep.tsx
170 lines (154 loc) 路 4.98 KB
/
CreateBrainStep.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { capitalCase } from "change-case";
import { useEffect, useState } from "react";
import { KnowledgeToFeed } from "@/app/chat/[chatId]/components/ActionsBar/components";
import { useUserApi } from "@/lib/api/user/useUserApi";
import { MessageInfoBox } from "@/lib/components/ui/MessageInfoBox/MessageInfoBox";
import QuivrButton from "@/lib/components/ui/QuivrButton/QuivrButton";
import { TextInput } from "@/lib/components/ui/TextInput/TextInput";
import { useKnowledgeToFeedContext } from "@/lib/context/KnowledgeToFeedProvider/hooks/useKnowledgeToFeedContext";
import { useUserData } from "@/lib/hooks/useUserData";
import styles from "./CreateBrainStep.module.scss";
import { useBrainCreationApi } from "./hooks/useBrainCreationApi";
import { useBrainCreationContext } from "../../brainCreation-provider";
import { useBrainCreationSteps } from "../../hooks/useBrainCreationSteps";
export const CreateBrainStep = (): JSX.Element => {
const { currentStepIndex, goToPreviousStep } = useBrainCreationSteps();
const { createBrain, fields, setFields } = useBrainCreationApi();
const { creating, setCreating, currentSelectedBrain } =
useBrainCreationContext();
const [createBrainStepIndex, setCreateBrainStepIndex] = useState<number>(0);
const { knowledgeToFeed } = useKnowledgeToFeedContext();
const { userIdentityData } = useUserData();
const { updateUserIdentity } = useUserApi();
useEffect(() => {
if (currentSelectedBrain?.connection_settings) {
const newFields = Object.entries(
currentSelectedBrain.connection_settings
).map(([key, type]) => {
return { name: key, type, value: "" };
});
setFields(newFields);
}
setCreateBrainStepIndex(Number(!currentSelectedBrain?.connection_settings));
}, [currentSelectedBrain?.connection_settings]);
const handleInputChange = (name: string, value: string) => {
setFields(
fields.map((field) => (field.name === name ? { ...field, value } : field))
);
};
const previous = (): void => {
goToPreviousStep();
};
const feed = async (): Promise<void> => {
if (!userIdentityData?.onboarded) {
await updateUserIdentity({
...userIdentityData,
username: userIdentityData?.username ?? "",
onboarded: true,
});
}
setCreating(true);
createBrain();
};
const renderSettings = () => {
return (
<>
<MessageInfoBox type="warning">
{currentSelectedBrain?.information}
</MessageInfoBox>
{fields.map(({ name, value }) => (
<TextInput
key={name}
inputValue={value}
setInputValue={(inputValue) => handleInputChange(name, inputValue)}
label={capitalCase(name)}
/>
))}
</>
);
};
const renderFeedBrain = () => {
return (
<>
{!userIdentityData?.onboarded && (
<MessageInfoBox type="tutorial">
<span>
Upload documents or add URLs to add knowledges to your brain.
</span>
</MessageInfoBox>
)}
<div>
<span className={styles.title}>Feed your brain</span>
<KnowledgeToFeed hideBrainSelector={true} />
</div>
</>
);
};
const renderCreateButton = () => {
return (
<MessageInfoBox type="info">
<div className={styles.message_content}>
Click on
<QuivrButton
label="Create"
color="primary"
iconName="add"
onClick={feed}
isLoading={creating}
/>
to finish your brain creation.
</div>
</MessageInfoBox>
);
};
const renderButtons = () => {
return (
<div className={styles.buttons_wrapper}>
<QuivrButton
label="Previous step"
color="primary"
iconName="chevronLeft"
onClick={previous}
/>
{(!currentSelectedBrain?.max_files && !createBrainStepIndex) ||
createBrainStepIndex ? (
<QuivrButton
label="Create"
color="primary"
iconName="add"
onClick={feed}
disabled={
knowledgeToFeed.length === 0 && !userIdentityData?.onboarded
}
isLoading={creating}
important={true}
/>
) : (
<QuivrButton
label="Feed your brain"
color="primary"
iconName="add"
onClick={() => setCreateBrainStepIndex(1)}
isLoading={creating}
important={true}
/>
)}
</div>
);
};
if (currentStepIndex !== 2) {
return <></>;
}
return (
<div className={styles.brain_knowledge_wrapper}>
{!createBrainStepIndex && renderSettings()}
{!!currentSelectedBrain?.max_files &&
!!createBrainStepIndex &&
renderFeedBrain()}
{!currentSelectedBrain?.max_files &&
!currentSelectedBrain?.connection_settings &&
renderCreateButton()}
{renderButtons()}
</div>
);
};