From 31243722d362d285cb0e71f8753a60d7562047ad Mon Sep 17 00:00:00 2001 From: azeeee05 Date: Mon, 8 Jun 2026 11:22:15 +0530 Subject: [PATCH] search fuunction done --- frontend/src/App.tsx | 38 ++++++-- frontend/src/components/Toolbar.tsx | 8 ++ frontend/src/index.css | 6 ++ frontend/src/lib/gatewayService.ts | 40 +++++++- frontend/src/lib/wasmLoader.ts | 3 +- frontend/src/viewer/AnnotationLayer.tsx | 20 +++- frontend/src/viewer/OverlayLayer.tsx | 94 +++++++++++++++++-- frontend/src/viewer/PDFViewer.tsx | 19 +++- frontend/src/viewer/SearchOverlayLayer.tsx | 67 +++++++------- gateway/app/routers/documents.py | 102 ++++++++++++++++++++- gateway/app/services/store.py | 13 ++- scripts/build_cpp.ps1 | 19 ++-- 12 files changed, 365 insertions(+), 64 deletions(-) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 6282708..f78705e 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -5,7 +5,7 @@ import { PDFViewer } from './viewer/PDFViewer'; import type { PDFViewerRef } from './viewer/PDFViewer'; import type { Annotation } from './viewer/AnnotationLayer'; import { gatewayService } from './lib/gatewayService'; -import type { DocumentInfo } from './lib/gatewayService'; +import type { DocumentInfo, SearchResult } from './lib/gatewayService'; import { wasmLoader } from './lib/wasmLoader'; import './App.css'; @@ -29,16 +29,30 @@ function App() { // Search State const [searchQuery, setSearchQuery] = useState(''); + const [searchResults, setSearchResults] = useState([]); const [searchResultCount, setSearchResultCount] = useState(0); const [searchCurrentMatch, setSearchCurrentMatch] = useState(0); - const handleSearch = (query: string) => { + const handleSearch = async (query: string) => { setSearchQuery(query); - // Mock search results for Phase 0 - if (query) { - setSearchResultCount(5); + if (!query || !selectedDocId) { + setSearchResults([]); + setSearchResultCount(0); setSearchCurrentMatch(0); - } else { + return; + } + + try { + const results = await gatewayService.searchDocument(selectedDocId, query); + setSearchResults(results); + setSearchResultCount(results.length); + setSearchCurrentMatch(0); + if (results.length > 0) { + viewerRef.current?.scrollToPage(results[0].pageIndex); + } + } catch (err) { + console.error("Search failed", err); + setSearchResults([]); setSearchResultCount(0); setSearchCurrentMatch(0); } @@ -46,13 +60,17 @@ function App() { const handleSearchNext = () => { if (searchResultCount > 0) { - setSearchCurrentMatch((prev) => (prev + 1) % searchResultCount); + const nextMatch = (searchCurrentMatch + 1) % searchResultCount; + setSearchCurrentMatch(nextMatch); + viewerRef.current?.scrollToPage(searchResults[nextMatch].pageIndex); } }; const handleSearchPrev = () => { if (searchResultCount > 0) { - setSearchCurrentMatch((prev) => (prev - 1 + searchResultCount) % searchResultCount); + const prevMatch = (searchCurrentMatch - 1 + searchResultCount) % searchResultCount; + setSearchCurrentMatch(prevMatch); + viewerRef.current?.scrollToPage(searchResults[prevMatch].pageIndex); } }; @@ -198,11 +216,15 @@ function App() { ref={viewerRef} documentId={activeDoc.id} totalPages={activeDoc.totalPages} + pageWidth={activeDoc.pageWidth} + pageHeight={activeDoc.pageHeight} zoom={zoom} rotation={rotation} activeTool={activeTool} annotations={annotations} searchQuery={searchQuery} + searchResults={searchResults} + searchCurrentMatch={searchCurrentMatch} onAnnotationAdded={handleAnnotationAdded} onPageVisible={setCurrentPage} /> diff --git a/frontend/src/components/Toolbar.tsx b/frontend/src/components/Toolbar.tsx index 943d30f..ceca69f 100644 --- a/frontend/src/components/Toolbar.tsx +++ b/frontend/src/components/Toolbar.tsx @@ -160,6 +160,14 @@ export const Toolbar: React.FC = ({ Highlight + +