Skip to content

Commit 95f9c09

Browse files
Added isLoading checks to the follow-up suggestions to prevent multiple API calls during an ongoing request
1 parent 0dd9491 commit 95f9c09

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

App/frontend-app/src/components/chat/chatRoom.tsx

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ interface ChatRoomProps {
5050
export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDocument, disableOptionsPanel, clearChatFlag }: ChatRoomProps) {
5151
const customStyles = useStyles();
5252
const { t } = useTranslation();
53-
const [chatSessionId, setChatSessionId] = useState<string | null>(null);
53+
const [chatSessionId, setChatSessionId] = useState<string | null>(null);
5454
const [isLoading, setIsLoading] = useState<boolean>(false);
5555
const [disableSources, setDisableSources] = useState<boolean>(false);
5656
const [error, setError] = useState<unknown>();
@@ -99,16 +99,16 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
9999
useEffect(() => {
100100
handleModelChange(DefaultChatModel)
101101
}, []);
102-
103-
// Effect to clear chat when clearChat prop changes
102+
103+
// Effect to clear chat when clearChat prop changes
104104
useEffect(() => {
105105
if (clearChatFlag) {
106106
clearChat();
107107
}
108108
}, [clearChatFlag]); // Runs whenever clearChat changes
109109

110110

111-
111+
112112
const chatContainerRef = useRef<HTMLDivElement>(null);
113113
function scrollToBottom() {
114114
if (chatContainerRef.current) {
@@ -128,23 +128,23 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
128128
setTextareaKey(prev => prev + 1);
129129
setDisableSources(true);
130130
setIsLoading(true);
131-
131+
132132
// A simple function to check if the text contains Markdown
133133
const isMarkdown = (text: string) => {
134134
const markdownPattern = /(^|\s)(#{1,6}|\*\*|__|[-*]|\d+\.\s|\[.*\]\(.*\)|```|`[^`]*`)/;
135135
return markdownPattern.test(text);
136-
};
136+
};
137137

138138
const userTimestamp = new Date();
139139
// Ensure we have a chatSessionId or create one if null/undefined
140-
141-
140+
141+
142142
let currentSessionId = chatSessionId; // Get the current value of chatSessionId
143143
if (!currentSessionId) {
144144
const newSessionId = uuidv4(); // Generate a new UUID if no session exists
145145
setChatSessionId(newSessionId); // Save it for future renders
146146
currentSessionId = newSessionId; // Immediately use the new session ID in this function
147-
147+
148148
}
149149
const markdownToHtmlString = (markdown: string) => {
150150
return renderToStaticMarkup(<ReactMarkdown>{markdown}</ReactMarkdown>);
@@ -170,7 +170,7 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
170170
| Total NPLs sold by Enterprises through December 2023 | 168,364 | FHFA Non-Performing Loan Sales Report | Page 2 |
171171
| Average delinquency of NPLs sold | 2.8 years | FHFA Non-Performing Loan Sales Report | Page 2 |
172172
| Average current mark-to-market LTV ratio of NPLs | 83% | FHFA Non-Performing Loan Sales Report | Page 2 |`;
173-
173+
174174
const htmlString = await marked.parse(markdown);
175175
const htmlString2 = markdownToHtmlString(markdown);
176176

@@ -183,18 +183,18 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
183183
}],
184184
]);
185185

186-
187-
186+
187+
188188

189189

190190
let filterByDocumentIds: string[] = [];
191-
192-
191+
192+
193193
const transformDocuments = (documents: any[]) => {
194194
return documents.map(doc => doc.documentId); // Extracting documentId from each document
195195
};
196196
const formattedDocuments = transformDocuments(selectedDocuments);
197-
197+
198198

199199
if (button === "Selected Document" && selectedDocument) {
200200
filterByDocumentIds = [selectedDocument[0].documentId];
@@ -205,13 +205,13 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
205205
}
206206

207207
try {
208-
209-
210-
208+
209+
210+
211211
const request: ChatRequest = {
212212
Question: question,
213213
chatSessionId: currentSessionId,
214-
DocumentIds: button === "All Documents" ? [] : formattedDocuments
214+
DocumentIds: button === "All Documents" ? [] : formattedDocuments
215215
// cha: history,
216216
// options: {
217217
// model: model,
@@ -221,25 +221,25 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
221221
// },
222222
// filterByDocumentIds: filterByDocumentIds,
223223
};
224-
225-
224+
225+
226226
const response: ChatApiResponse = await Completion(request);
227-
228-
227+
228+
229229

230230
setIsLoading(false);
231231

232232
const answerTimestamp = new Date();
233233

234234
try {
235235
if (response && response.answer) {
236-
236+
237237
const formattedAnswer = removeNewlines(response.answer)
238238
const chatResp = await marked.parse(formattedAnswer) // Convert to HTML if Markdown detected
239-
240-
241-
242-
239+
240+
241+
242+
243243
// Update the conversation with the formatted answer
244244
setConversationAnswers((prevAnswers) => {
245245
const newAnswers = [...prevAnswers];
@@ -279,7 +279,7 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
279279
setChatSessionId(null);
280280
};
281281

282-
const handleModelChange = (model: string) => {
282+
const handleModelChange = (model: string) => {
283283
setModel(model);
284284
};
285285

@@ -293,7 +293,7 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
293293
};
294294

295295
function handleSend(ev: TextareaSubmitEvents, data: TextareaValueData) {
296-
if (data.value.trim() !='') {
296+
if (data.value.trim() != '') {
297297
makeApiRequest(data.value);
298298
}
299299
}
@@ -340,10 +340,10 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
340340
button === "Selected Documents"
341341
? selectedDocuments.map((document) => document.documentId)
342342
: button === "Search Results"
343-
? searchResultDocuments.map((document) => document.documentId)
344-
: button === "Selected Document"
345-
? [selectedDocument[0]?.documentId || ""]
346-
: [],
343+
? searchResultDocuments.map((document) => document.documentId)
344+
: button === "Selected Document"
345+
? [selectedDocument[0]?.documentId || ""]
346+
: [],
347347
groundTruthAnswer: "",
348348
documentURLs: [],
349349
chunkTexts: [],
@@ -482,8 +482,12 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
482482
{response.suggestingQuestions.map((followUp, index) => (
483483
<Suggestion
484484
key={index}
485-
className="!mr-2 !mt-2 !text-base"
486-
onClick={() => handleFollowUpQuestion(followUp)}
485+
className={`!mr-2 !mt-2 !text-base ${isLoading ? "pointer-events-none text-gray-400" : ""}`}
486+
onClick={() => {
487+
if (!isLoading) {
488+
handleFollowUpQuestion(followUp);
489+
}
490+
}}
487491
>
488492
{followUp}
489493
</Suggestion>
@@ -594,11 +598,11 @@ export function ChatRoom({ searchResultDocuments, selectedDocuments, chatWithDoc
594598
contentAfter={undefined}
595599
/>
596600
</div>
597-
</div>
601+
</div >
598602
);
599603
}
600604
function uuidv4() {
601-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
605+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
602606
const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
603607
return v.toString(16);
604608
});

0 commit comments

Comments
 (0)