Explorar el Código

Live search for apps

unknown hace 3 años
padre
commit
fac280ff0a

+ 17 - 5
client/src/components/SearchBar/SearchBar.tsx

@@ -28,16 +28,28 @@ const SearchBar = (props: ComponentProps): JSX.Element => {
   }, []);
   }, []);
 
 
   const searchHandler = (e: KeyboardEvent<HTMLInputElement>) => {
   const searchHandler = (e: KeyboardEvent<HTMLInputElement>) => {
-    if (e.code === 'Enter') {
-      const searchResult = searchParser(inputRef.current.value);
+    const searchResult = searchParser(inputRef.current.value);
+
+    if (searchResult.isLocal) {
+      setLocalSearch(searchResult.search);
+    }
 
 
-      if (!searchResult.prefix) {
+    if (e.code === 'Enter') {
+      if (!searchResult.query.prefix) {
         createNotification({
         createNotification({
           title: 'Error',
           title: 'Error',
           message: 'Prefix not found',
           message: 'Prefix not found',
         });
         });
       } else if (searchResult.isLocal) {
       } else if (searchResult.isLocal) {
-        setLocalSearch(searchResult.query);
+        setLocalSearch(searchResult.search);
+      } else {
+        if (searchResult.sameTab) {
+          document.location.replace(
+            `${searchResult.query.template}${searchResult.search}`
+          );
+        } else {
+          window.open(`${searchResult.query.template}${searchResult.search}`);
+        }
       }
       }
     }
     }
   };
   };
@@ -47,7 +59,7 @@ const SearchBar = (props: ComponentProps): JSX.Element => {
       ref={inputRef}
       ref={inputRef}
       type="text"
       type="text"
       className={classes.SearchBar}
       className={classes.SearchBar}
-      onKeyDown={(e) => searchHandler(e)}
+      onKeyUp={(e) => searchHandler(e)}
     />
     />
   );
   );
 };
 };

+ 5 - 2
client/src/interfaces/SearchResult.ts

@@ -1,5 +1,8 @@
+import { Query } from './Query';
+
 export interface SearchResult {
 export interface SearchResult {
   isLocal: boolean;
   isLocal: boolean;
-  prefix: null | string;
-  query: string;
+  sameTab: boolean;
+  search: string;
+  query: Query;
 }
 }

+ 10 - 11
client/src/utility/searchParser.ts

@@ -6,8 +6,13 @@ import { searchConfig } from '.';
 export const searchParser = (searchQuery: string): SearchResult => {
 export const searchParser = (searchQuery: string): SearchResult => {
   const result: SearchResult = {
   const result: SearchResult = {
     isLocal: false,
     isLocal: false,
-    prefix: null,
-    query: '',
+    sameTab: false,
+    search: '',
+    query: {
+      name: '',
+      prefix: '',
+      template: '',
+    },
   };
   };
 
 
   const splitQuery = searchQuery.match(/^\/([a-z]+)[ ](.+)$/i);
   const splitQuery = searchQuery.match(/^\/([a-z]+)[ ](.+)$/i);
@@ -23,19 +28,13 @@ export const searchParser = (searchQuery: string): SearchResult => {
   const query = queries.find((q: Query) => q.prefix === prefix);
   const query = queries.find((q: Query) => q.prefix === prefix);
 
 
   if (query) {
   if (query) {
-    result.prefix = query.prefix;
-    result.query = search;
+    result.query = query;
+    result.search = search;
 
 
     if (prefix === 'l') {
     if (prefix === 'l') {
       result.isLocal = true;
       result.isLocal = true;
     } else {
     } else {
-      const sameTab = searchConfig('searchSameTab', false);
-
-      if (sameTab) {
-        document.location.replace(`${query.template}${search}`);
-      } else {
-        window.open(`${query.template}${search}`);
-      }
+      result.sameTab = searchConfig('searchSameTab', false);
     }
     }
 
 
     return result;
     return result;