@@ -54,7 +54,7 @@ export function Home({ isSearchResultsPage }: HomeProps) {
5454 const navigate = useNavigate ( ) ;
5555 const location = useLocation ( ) ;
5656 const [ reset , setReset ] = useState ( false ) ; // State to trigger reset
57-
57+ const [ clearSearchBox , setClearSearchBox ] = useState ( false ) ;
5858 const [ clearAllTriggered , setClearAllTriggered ] = useState ( false ) ;
5959 const [ clearFilters , setClearFilters ] = useState ( false ) ;
6060 const [ resetSearchBox , setResetSearchBox ] = useState ( false ) ;
@@ -141,15 +141,16 @@ export function Home({ isSearchResultsPage }: HomeProps) {
141141
142142 useEffect ( ( ) => {
143143 if ( searchBoxRef . current ) {
144- if ( resetSearchBox ) {
145-
144+ if ( resetSearchBox && clearSearchBox ) {
146145 searchBoxRef . current . reset ( ) ;
147146 setResetSearchBox ( false ) ; // Reset the trigger
148147 } else {
149- const urlQuery = searchParams . get ( "q" ) ;
150- if ( urlQuery ) {
151- const decodedQuery = decodeURIComponent ( urlQuery ) ;
152- searchBoxRef . current . setValue ( decodedQuery ) ;
148+ if ( ! clearSearchBox ) {
149+ const urlQuery = searchParams . get ( "q" ) ;
150+ if ( urlQuery ) {
151+ const decodedQuery = decodeURIComponent ( urlQuery ) ;
152+ searchBoxRef . current . setValue ( decodedQuery ) ;
153+ }
153154 }
154155 }
155156 }
@@ -165,15 +166,24 @@ export function Home({ isSearchResultsPage }: HomeProps) {
165166 setSearchParams ( { } ) ; // Clear all search params when there's no query
166167 }
167168 } else {
168- const q = searchParams . get ( "q" ) ;
169- if ( q ) {
170- setQuery ( decodeURIComponent ( q ) ) ;
169+ if ( ! clearSearchBox ) {
170+ const q = searchParams . get ( "q" ) ;
171+ if ( q ) {
172+ setQuery ( decodeURIComponent ( q ) ) ;
173+ }
171174 }
172175 }
173176 } , [ isSearchResultsPage , query , searchParams , setSearchParams ] ) ;
174177
178+ useEffect ( ( ) => {
179+ const q = searchParams . get ( "q" ) ;
180+ if ( ! q ) {
181+ setClearSearchBox ( false )
182+ }
183+ } , [ searchParams ] )
184+
175185 function onSearchChanged ( searchValue : string ) : void {
176- if ( searchValue ) {
186+ if ( searchValue ) {
177187 setTextValue ( searchValue ) ;
178188 }
179189 if ( searchValue ) {
@@ -189,7 +199,7 @@ export function Home({ isSearchResultsPage }: HomeProps) {
189199 }
190200 } else {
191201 setSearchResultDocuments ( [ ] ) ;
192- setSearchParams ( "" ) ;
202+ setSearchParams ( { } ) ;
193203 setQuery ( searchValue ) ;
194204 }
195205 }
@@ -462,6 +472,7 @@ export function Home({ isSearchResultsPage }: HomeProps) {
462472 setSelectedDocuments ( [ ] ) ;
463473 setClearAllTriggered ( true ) ; // Set this flag to true
464474 setClearFilters ( true ) ;
475+ setClearSearchBox ( true ) ;
465476 // Call loadDataAsync after clearing
466477 loadDataAsync ( ) ;
467478 } , [ /* dependencies */ ] ) ;
0 commit comments