]> Untitled Git - lemmy-ui.git/commitdiff
Properly debouncing tribute mentions. Fixes #633 (#639)
authorDessalines <dessalines@users.noreply.github.com>
Wed, 27 Apr 2022 20:57:55 +0000 (16:57 -0400)
committerGitHub <noreply@github.com>
Wed, 27 Apr 2022 20:57:55 +0000 (22:57 +0200)
src/shared/components/person/settings.tsx
src/shared/components/post/post-form.tsx
src/shared/components/search.tsx
src/shared/utils.ts

index 9e045e4d21c1a4e049e8513e1c66e313815957f7..6008edbfab4abc49df227552095922e679707aec 100644 (file)
@@ -799,7 +799,7 @@ export class Settings extends Component<any, SettingsState> {
             } catch (err) {
               console.error(err);
             }
-          }, 400),
+          }),
           false
         );
       }
@@ -834,7 +834,7 @@ export class Settings extends Component<any, SettingsState> {
             } catch (err) {
               console.log(err);
             }
-          }, 400),
+          }),
           false
         );
       }
index 9badb746d45108157830ed7c3664b8d2153c1852..e9c221896d74acafbe1412bbae18971512e67c70 100644 (file)
@@ -600,7 +600,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
             } catch (err) {
               console.log(err);
             }
-          }, 400),
+          }),
           false
         );
       }
index f4c78503d53baee38a94381c49712adb8c281083..f049f1c5627b68dee953162eba25ee1ca88f382a 100644 (file)
@@ -778,7 +778,7 @@ export class Search extends Component<any, SearchState> {
             } catch (err) {
               console.error(err);
             }
-          }, 400),
+          }),
           false
         );
       }
@@ -808,7 +808,7 @@ export class Search extends Component<any, SearchState> {
             } catch (err) {
               console.log(err);
             }
-          }, 400),
+          }),
           false
         );
       }
index df26970d13edca89f4c39e69cd901c5ba473e916..bf0a6d8184109b4e5ff59e07c6fc5260897c5c10 100644 (file)
@@ -20,7 +20,6 @@ import {
   PrivateMessageView,
   RegistrationApplicationView,
   Search,
-  SearchResponse,
   SearchType,
   SortType,
   UserOperation,
@@ -585,9 +584,9 @@ export function setupTribute() {
           let it: PersonTribute = item.original;
           return `[${it.key}](${it.view.person.actor_id})`;
         },
-        values: (text: string, cb: (persons: PersonTribute[]) => any) => {
-          personSearch(text, (persons: PersonTribute[]) => cb(persons));
-        },
+        values: debounce(async (text: string, cb: any) => {
+          cb(await personSearch(text));
+        }),
         allowSpaces: false,
         autocompleteMode: true,
         // TODO
@@ -602,11 +601,9 @@ export function setupTribute() {
           let it: CommunityTribute = item.original;
           return `[${it.key}](${it.view.community.actor_id})`;
         },
-        values: (text: string, cb: any) => {
-          communitySearch(text, (communities: CommunityTribute[]) =>
-            cb(communities)
-          );
-        },
+        values: debounce(async (text: string, cb: any) => {
+          cb(await communitySearch(text));
+        }),
         allowSpaces: false,
         autocompleteMode: true,
         // TODO
@@ -638,42 +635,16 @@ interface PersonTribute {
   view: PersonViewSafe;
 }
 
-function personSearch(text: string, cb: (persons: PersonTribute[]) => any) {
-  if (text) {
-    let form: Search = {
-      q: text,
-      type_: SearchType.Users,
-      sort: SortType.TopAll,
-      listing_type: ListingType.All,
-      page: 1,
-      limit: mentionDropdownFetchLimit,
-      auth: authField(false),
+async function personSearch(text: string): Promise<PersonTribute[]> {
+  let users = (await fetchUsers(text)).users;
+  let persons: PersonTribute[] = users.map(pv => {
+    let tribute: PersonTribute = {
+      key: `@${pv.person.name}@${hostname(pv.person.actor_id)}`,
+      view: pv,
     };
-
-    WebSocketService.Instance.send(wsClient.search(form));
-
-    let personSub = WebSocketService.Instance.subject.subscribe(
-      msg => {
-        let res = wsJsonToRes(msg);
-        if (res.op == UserOperation.Search) {
-          let data = res.data as SearchResponse;
-          let persons: PersonTribute[] = data.users.map(pv => {
-            let tribute: PersonTribute = {
-              key: `@${pv.person.name}@${hostname(pv.person.actor_id)}`,
-              view: pv,
-            };
-            return tribute;
-          });
-          cb(persons);
-          personSub.unsubscribe();
-        }
-      },
-      err => console.error(err),
-      () => console.log("complete")
-    );
-  } else {
-    cb([]);
-  }
+    return tribute;
+  });
+  return persons;
 }
 
 interface CommunityTribute {
@@ -681,45 +652,16 @@ interface CommunityTribute {
   view: CommunityView;
 }
 
-function communitySearch(
-  text: string,
-  cb: (communities: CommunityTribute[]) => any
-) {
-  if (text) {
-    let form: Search = {
-      q: text,
-      type_: SearchType.Communities,
-      sort: SortType.TopAll,
-      listing_type: ListingType.All,
-      page: 1,
-      limit: mentionDropdownFetchLimit,
-      auth: authField(false),
+async function communitySearch(text: string): Promise<CommunityTribute[]> {
+  let comms = (await fetchCommunities(text)).communities;
+  let communities: CommunityTribute[] = comms.map(cv => {
+    let tribute: CommunityTribute = {
+      key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
+      view: cv,
     };
-
-    WebSocketService.Instance.send(wsClient.search(form));
-
-    let communitySub = WebSocketService.Instance.subject.subscribe(
-      msg => {
-        let res = wsJsonToRes(msg);
-        if (res.op == UserOperation.Search) {
-          let data = res.data as SearchResponse;
-          let communities: CommunityTribute[] = data.communities.map(cv => {
-            let tribute: CommunityTribute = {
-              key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
-              view: cv,
-            };
-            return tribute;
-          });
-          cb(communities);
-          communitySub.unsubscribe();
-        }
-      },
-      err => console.error(err),
-      () => console.log("complete")
-    );
-  } else {
-    cb([]);
-  }
+    return tribute;
+  });
+  return communities;
 }
 
 export function getListingTypeFromProps(props: any): ListingType {