From c9d39137848e180109ee14d6aa435444cfc6dcd5 Mon Sep 17 00:00:00 2001 From: Kahtaf Alam Date: Wed, 28 Feb 2024 16:51:34 -0500 Subject: [PATCH] feat: Show loading indicator when searching for documents (#19) --- .../components/Playground/PlaygroundQuery.tsx | 65 +++++++++++-------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/selfie-ui/src/app/components/Playground/PlaygroundQuery.tsx b/selfie-ui/src/app/components/Playground/PlaygroundQuery.tsx index bf1f75a..fe875b2 100644 --- a/selfie-ui/src/app/components/Playground/PlaygroundQuery.tsx +++ b/selfie-ui/src/app/components/Playground/PlaygroundQuery.tsx @@ -1,6 +1,5 @@ -import React, { useEffect, useState } from 'react'; -import Tooltip from '../Tooltip'; -import {apiBaseUrl} from "@/app/config"; +import React, { useState } from "react"; +import { apiBaseUrl } from "@/app/config"; import useAsyncTask from "@/app/hooks/useAsyncTask"; import TaskToast from "@/app/components/TaskToast"; @@ -13,23 +12,24 @@ const fetchDocuments = async (topic: string, limit?: number, minScore?: number, const response = await fetch(url); return response.json(); } catch (error) { - console.error('Error fetching documents:', error); + console.error("Error fetching documents:", error); throw error; } }; const PlaygroundQuery = () => { const { isTaskRunning, taskMessage, executeTask } = useAsyncTask(); - const [query, setQuery] = useState(''); + const [query, setQuery] = useState(""); const [documents, setDocuments] = useState([]); - const [summary, setSummary] = useState(''); + const [summary, setSummary] = useState(""); + const [isSummaryLoading, setSummaryLoading] = useState(false); const [score, setScore] = useState(0); const [limit, setLimit] = useState(); const [minScore, setMinScore] = useState(); const [includeSummary, setIncludeSummary] = useState(true); const handleInputChange = (setter: React.Dispatch>) => (e: React.ChangeEvent) => { - const value = e.target.type === 'number' ? Number(e.target.value) || undefined : e.target.value; + const value = e.target.type === "number" ? Number(e.target.value) || undefined : e.target.value; setter(value); }; @@ -43,16 +43,18 @@ const PlaygroundQuery = () => { executeTask(async () => { setScore(0); setDocuments([]); - setSummary(''); + setSummary(""); + setSummaryLoading(true); const results = await fetchDocuments(query, limit, minScore, includeSummary); setScore(results.score); setDocuments(results.documents); setSummary(results.summary); - console.log('Searching with:', query, limit, minScore, includeSummary); - }, { - start: 'Searching...', - success: 'Search complete', - error: 'Search failed', + setSummaryLoading(false); + console.log("Searching with:", query, limit, minScore, includeSummary); + }, { + start: "Searching...", + success: "Search complete", + error: "Search failed", }); }; @@ -75,7 +77,7 @@ const PlaygroundQuery = () => { ); - } + }; return (
@@ -88,13 +90,13 @@ const PlaygroundQuery = () => {
{/**/} setLimit(Number(e.target.value) || undefined)} min="1" /> @@ -108,7 +110,7 @@ const PlaygroundQuery = () => { type="number" className="input input-sm input-bordered" placeholder="Minimum score (optional)" - value={minScore === undefined ? '' : minScore} + value={minScore === undefined ? "" : minScore} onChange={(e) => setMinScore(Number(e.target.value) || undefined)} min="0" max="1" @@ -138,20 +140,15 @@ const PlaygroundQuery = () => { onChange={handleQueryChange} />
{!!score &&
{/**/} -

{summary}

-

Result Score: {score.toFixed(2)}

+

{summary}

+

Result Score: {score.toFixed(2)}

}
{!!score &&
@@ -161,6 +158,22 @@ const PlaygroundQuery = () => { ); }; -PlaygroundQuery.displayName = 'PlaygroundQuery'; +const SearchIcon = () => + +; + +const LoadingIcon = () => + + + +; + +PlaygroundQuery.displayName = "PlaygroundQuery"; export default PlaygroundQuery;