Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use client';

import { useState } from 'react';
import Image from 'next/image';
import { useState, useEffect } from 'react';

interface SearchResult {
content: string;
Expand All @@ -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<SearchResult[]>([]);
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',
Expand Down Expand Up @@ -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');
Expand All @@ -90,7 +125,13 @@ export default function Home() {
</div>
)}

{!collectionCreated ? (
{checkingCollection ? (
<div className="text-center mb-12">
<div className="text-lg text-gray-600 dark:text-gray-300">
Checking if Animal Facts Collection exists...
</div>
</div>
) : !collectionCreated ? (
<div className="text-center mb-12">
<h2 className="text-2xl font-semibold mb-4">Step 1: Create Animal Facts Collection</h2>
<p className="text-gray-600 dark:text-gray-400 mb-6">
Expand Down Expand Up @@ -189,4 +230,4 @@ export default function Home() {
</main>
</div>
);
}
}