From 3cd3c2bc2012bd51db8b12e38a7debafb5dd74e3 Mon Sep 17 00:00:00 2001 From: svngoku <32180057+svngoku@users.noreply.github.com> Date: Wed, 22 Oct 2025 19:48:13 +0200 Subject: [PATCH] Verify if the collection already exist Adding a helper function to check if the collection exists in aim to go directly to the search. --- app/page.tsx | 53 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index 2774e43..3177989 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,7 +1,5 @@ 'use client'; - -import { useState } from 'react'; -import Image from 'next/image'; +import { useState, useEffect } from 'react'; interface SearchResult { content: string; @@ -13,16 +11,49 @@ interface SearchResult { export default function Home() { const [isCreatingCollection, setIsCreatingCollection] = useState(false); const [collectionCreated, setCollectionCreated] = useState(false); + const [checkingCollection, setCheckingCollection] = useState(true); const [searchQuery, setSearchQuery] = useState(''); const [useReranker, setUseReranker] = useState(false); const [searchResults, setSearchResults] = useState([]); const [isSearching, setIsSearching] = useState(false); const [error, setError] = useState(''); + // Helper function to check if the collection exists + const checkCollectionExists = async () => { + setCheckingCollection(true); + setError(''); + try { + // POST to /api/search with a harmless query and check for a specific error + // We'll use "test" as the query, but any query would do. + const response = await fetch('/api/search', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ query: 'test', useReranker: false }), + }); + const data = await response.json(); + // If results are returned, collection exists + if (response.ok && Array.isArray(data.results)) { + setCollectionCreated(true); + } else if (data.error && /not\s*found|no such collection|does not exist/i.test(data.error)) { + setCollectionCreated(false); + } else { + // Unknown error; don't block creation + setCollectionCreated(false); + } + } catch (err) { + setCollectionCreated(false); + } finally { + setCheckingCollection(false); + } + }; + + useEffect(() => { + checkCollectionExists(); + }, []); + const createCollection = async () => { setIsCreatingCollection(true); setError(''); - try { const response = await fetch('/api/create-collection', { method: 'POST', @@ -66,6 +97,10 @@ export default function Home() { setSearchResults(data.results); } else { setError(data.error || 'Failed to search collection'); + // If the collection suddenly doesn't exist, update state + if (data.error && /not\s*found|no such collection|does not exist/i.test(data.error)) { + setCollectionCreated(false); + } } } catch (err) { setError('An error occurred while searching'); @@ -90,7 +125,13 @@ export default function Home() { )} - {!collectionCreated ? ( + {checkingCollection ? ( +
+
+ Checking if Animal Facts Collection exists... +
+
+ ) : !collectionCreated ? (

Step 1: Create Animal Facts Collection

@@ -189,4 +230,4 @@ export default function Home() {

); -} \ No newline at end of file +}