Pārlūkot izejas kodu

Fixed bug where pressing Enter with empty search bar would redirect to search results

Paweł Malak 3 gadi atpakaļ
vecāks
revīzija
0f6d79683e

+ 1 - 0
CHANGELOG.md

@@ -1,5 +1,6 @@
 ### v2.3.0 (TBA)
 - Added custom theme editor ([#246](https://github.com/pawelmalak/flame/issues/246))
+- Fixed bug where pressing Enter with empty search bar would redirect to search results ([#325](https://github.com/pawelmalak/flame/issues/325))
 - Fixed bug where user could create empty app or bookmark which was causing page to go blank ([#332](https://github.com/pawelmalak/flame/issues/332))
 
 ### v2.2.2 (2022-03-21)

+ 11 - 8
client/src/components/SearchBar/SearchBar.tsx

@@ -64,7 +64,7 @@ export const SearchBar = (props: Props): JSX.Element => {
   };
 
   const searchHandler = (e: KeyboardEvent<HTMLInputElement>) => {
-    const { isLocal, search, query, isURL, sameTab } = searchParser(
+    const { isLocal, search, query, isURL, sameTab, rawQuery } = searchParser(
       inputRef.current.value
     );
 
@@ -90,15 +90,18 @@ export const SearchBar = (props: Props): JSX.Element => {
         } else if (bookmarkSearchResult?.[0]?.bookmarks?.length) {
           redirectUrl(bookmarkSearchResult[0].bookmarks[0].url, sameTab);
         } else {
-          // no local results -> search the internet with the default search provider
-          let template = query.template;
+          // no local results -> search the internet with the default search provider if query is not empty
 
-          if (query.prefix === 'l') {
-            template = 'https://duckduckgo.com/?q=';
-          }
+          if (!/^ *$/.test(rawQuery)) {
+            let template = query.template;
+
+            if (query.prefix === 'l') {
+              template = 'https://duckduckgo.com/?q=';
+            }
 
-          const url = `${template}${search}`;
-          redirectUrl(url, sameTab);
+            const url = `${template}${search}`;
+            redirectUrl(url, sameTab);
+          }
         }
       } else {
         // Valid query -> redirect to search results

+ 1 - 0
client/src/interfaces/SearchResult.ts

@@ -6,4 +6,5 @@ export interface SearchResult {
   sameTab: boolean;
   search: string;
   query: Query;
+  rawQuery: string;
 }

+ 1 - 0
client/src/utility/searchParser.ts

@@ -14,6 +14,7 @@ export const searchParser = (searchQuery: string): SearchResult => {
       prefix: '',
       template: '',
     },
+    rawQuery: searchQuery,
   };
 
   const { customQueries, config } = store.getState().config;