]> Untitled Git - lemmy-ui.git/blob - src/shared/components/search.tsx
Adding new site setup fields. (#840)
[lemmy-ui.git] / src / shared / components / search.tsx
1 import { None, Option, Some } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
3 import {
4   CommentResponse,
5   CommentView,
6   CommunityView,
7   GetCommunity,
8   GetCommunityResponse,
9   GetPersonDetails,
10   GetPersonDetailsResponse,
11   GetSiteResponse,
12   ListCommunities,
13   ListCommunitiesResponse,
14   ListingType,
15   PersonViewSafe,
16   PostResponse,
17   PostView,
18   ResolveObject,
19   ResolveObjectResponse,
20   Search as SearchForm,
21   SearchResponse,
22   SearchType,
23   SortType,
24   UserOperation,
25   wsJsonToRes,
26   wsUserOp,
27 } from "lemmy-js-client";
28 import { Subscription } from "rxjs";
29 import { i18n } from "../i18next";
30 import { CommentViewType, InitialFetchRequest } from "../interfaces";
31 import { WebSocketService } from "../services";
32 import {
33   auth,
34   capitalizeFirstLetter,
35   choicesConfig,
36   commentsToFlatNodes,
37   communitySelectName,
38   communityToChoice,
39   createCommentLikeRes,
40   createPostLikeFindRes,
41   debounce,
42   enableDownvotes,
43   enableNsfw,
44   fetchCommunities,
45   fetchLimit,
46   fetchUsers,
47   isBrowser,
48   numToSI,
49   personSelectName,
50   personToChoice,
51   pushNotNull,
52   restoreScrollPosition,
53   routeListingTypeToEnum,
54   routeSearchTypeToEnum,
55   routeSortTypeToEnum,
56   saveScrollPosition,
57   setIsoData,
58   showLocal,
59   toast,
60   wsClient,
61   wsSubscribe,
62 } from "../utils";
63 import { CommentNodes } from "./comment/comment-nodes";
64 import { HtmlTags } from "./common/html-tags";
65 import { Spinner } from "./common/icon";
66 import { ListingTypeSelect } from "./common/listing-type-select";
67 import { Paginator } from "./common/paginator";
68 import { SortSelect } from "./common/sort-select";
69 import { CommunityLink } from "./community/community-link";
70 import { PersonListing } from "./person/person-listing";
71 import { PostListing } from "./post/post-listing";
72
73 var Choices: any;
74 if (isBrowser()) {
75   Choices = require("choices.js");
76 }
77
78 interface SearchProps {
79   q: string;
80   type_: SearchType;
81   sort: SortType;
82   listingType: ListingType;
83   communityId: number;
84   creatorId: number;
85   page: number;
86 }
87
88 interface SearchState {
89   q: string;
90   type_: SearchType;
91   sort: SortType;
92   listingType: ListingType;
93   communityId: number;
94   creatorId: number;
95   page: number;
96   searchResponse: Option<SearchResponse>;
97   communities: CommunityView[];
98   creatorDetails: Option<GetPersonDetailsResponse>;
99   loading: boolean;
100   siteRes: GetSiteResponse;
101   searchText: string;
102   resolveObjectResponse: Option<ResolveObjectResponse>;
103 }
104
105 interface UrlParams {
106   q?: string;
107   type_?: SearchType;
108   sort?: SortType;
109   listingType?: ListingType;
110   communityId?: number;
111   creatorId?: number;
112   page?: number;
113 }
114
115 interface Combined {
116   type_: string;
117   data: CommentView | PostView | CommunityView | PersonViewSafe;
118   published: string;
119 }
120
121 export class Search extends Component<any, SearchState> {
122   private isoData = setIsoData(
123     this.context,
124     GetCommunityResponse,
125     ListCommunitiesResponse,
126     GetPersonDetailsResponse,
127     SearchResponse,
128     ResolveObjectResponse
129   );
130   private communityChoices: any;
131   private creatorChoices: any;
132   private subscription: Subscription;
133   private emptyState: SearchState = {
134     q: Search.getSearchQueryFromProps(this.props.match.params.q),
135     type_: Search.getSearchTypeFromProps(this.props.match.params.type),
136     sort: Search.getSortTypeFromProps(this.props.match.params.sort),
137     listingType: Search.getListingTypeFromProps(
138       this.props.match.params.listing_type
139     ),
140     page: Search.getPageFromProps(this.props.match.params.page),
141     searchText: Search.getSearchQueryFromProps(this.props.match.params.q),
142     communityId: Search.getCommunityIdFromProps(
143       this.props.match.params.community_id
144     ),
145     creatorId: Search.getCreatorIdFromProps(this.props.match.params.creator_id),
146     searchResponse: None,
147     resolveObjectResponse: None,
148     creatorDetails: None,
149     loading: true,
150     siteRes: this.isoData.site_res,
151     communities: [],
152   };
153
154   static getSearchQueryFromProps(q: string): string {
155     return decodeURIComponent(q) || "";
156   }
157
158   static getSearchTypeFromProps(type_: string): SearchType {
159     return type_ ? routeSearchTypeToEnum(type_) : SearchType.All;
160   }
161
162   static getSortTypeFromProps(sort: string): SortType {
163     return sort ? routeSortTypeToEnum(sort) : SortType.TopAll;
164   }
165
166   static getListingTypeFromProps(listingType: string): ListingType {
167     return listingType ? routeListingTypeToEnum(listingType) : ListingType.All;
168   }
169
170   static getCommunityIdFromProps(id: string): number {
171     return id ? Number(id) : 0;
172   }
173
174   static getCreatorIdFromProps(id: string): number {
175     return id ? Number(id) : 0;
176   }
177
178   static getPageFromProps(page: string): number {
179     return page ? Number(page) : 1;
180   }
181
182   constructor(props: any, context: any) {
183     super(props, context);
184
185     this.state = this.emptyState;
186     this.handleSortChange = this.handleSortChange.bind(this);
187     this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
188     this.handlePageChange = this.handlePageChange.bind(this);
189
190     this.parseMessage = this.parseMessage.bind(this);
191     this.subscription = wsSubscribe(this.parseMessage);
192
193     // Only fetch the data if coming from another route
194     if (this.isoData.path == this.context.router.route.match.url) {
195       let communityRes = Some(
196         this.isoData.routeData[0] as GetCommunityResponse
197       );
198       let communitiesRes = Some(
199         this.isoData.routeData[1] as ListCommunitiesResponse
200       );
201
202       // This can be single or multiple communities given
203       if (communitiesRes.isSome()) {
204         this.state = {
205           ...this.state,
206           communities: communitiesRes.unwrap().communities,
207         };
208       }
209
210       if (communityRes.isSome()) {
211         this.state = {
212           ...this.state,
213           communities: [communityRes.unwrap().community_view],
214         };
215       }
216
217       this.state = {
218         ...this.state,
219         creatorDetails: Some(
220           this.isoData.routeData[2] as GetPersonDetailsResponse
221         ),
222       };
223
224       if (this.state.q != "") {
225         this.state = {
226           ...this.state,
227           searchResponse: Some(this.isoData.routeData[3] as SearchResponse),
228           resolveObjectResponse: Some(
229             this.isoData.routeData[4] as ResolveObjectResponse
230           ),
231           loading: false,
232         };
233       } else {
234         this.search();
235       }
236     } else {
237       this.fetchCommunities();
238       this.search();
239     }
240   }
241
242   componentWillUnmount() {
243     this.subscription.unsubscribe();
244     saveScrollPosition(this.context);
245   }
246
247   componentDidMount() {
248     this.setupCommunityFilter();
249     this.setupCreatorFilter();
250   }
251
252   static getDerivedStateFromProps(props: any): SearchProps {
253     return {
254       q: Search.getSearchQueryFromProps(props.match.params.q),
255       type_: Search.getSearchTypeFromProps(props.match.params.type),
256       sort: Search.getSortTypeFromProps(props.match.params.sort),
257       listingType: Search.getListingTypeFromProps(
258         props.match.params.listing_type
259       ),
260       communityId: Search.getCommunityIdFromProps(
261         props.match.params.community_id
262       ),
263       creatorId: Search.getCreatorIdFromProps(props.match.params.creator_id),
264       page: Search.getPageFromProps(props.match.params.page),
265     };
266   }
267
268   fetchCommunities() {
269     let listCommunitiesForm = new ListCommunities({
270       type_: Some(ListingType.All),
271       sort: Some(SortType.TopAll),
272       limit: Some(fetchLimit),
273       page: None,
274       auth: auth(false).ok(),
275     });
276     WebSocketService.Instance.send(
277       wsClient.listCommunities(listCommunitiesForm)
278     );
279   }
280
281   static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
282     let pathSplit = req.path.split("/");
283     let promises: Promise<any>[] = [];
284
285     let communityId = this.getCommunityIdFromProps(pathSplit[11]);
286     let community_id: Option<number> =
287       communityId == 0 ? None : Some(communityId);
288     community_id.match({
289       some: id => {
290         let getCommunityForm = new GetCommunity({
291           id: Some(id),
292           name: None,
293           auth: req.auth,
294         });
295         promises.push(req.client.getCommunity(getCommunityForm));
296         promises.push(Promise.resolve());
297       },
298       none: () => {
299         let listCommunitiesForm = new ListCommunities({
300           type_: Some(ListingType.All),
301           sort: Some(SortType.TopAll),
302           limit: Some(fetchLimit),
303           page: None,
304           auth: req.auth,
305         });
306         promises.push(Promise.resolve());
307         promises.push(req.client.listCommunities(listCommunitiesForm));
308       },
309     });
310
311     let creatorId = this.getCreatorIdFromProps(pathSplit[13]);
312     let creator_id: Option<number> = creatorId == 0 ? None : Some(creatorId);
313     creator_id.match({
314       some: id => {
315         let getCreatorForm = new GetPersonDetails({
316           person_id: Some(id),
317           username: None,
318           sort: None,
319           page: None,
320           limit: None,
321           community_id: None,
322           saved_only: None,
323           auth: req.auth,
324         });
325         promises.push(req.client.getPersonDetails(getCreatorForm));
326       },
327       none: () => {
328         promises.push(Promise.resolve());
329       },
330     });
331
332     let form = new SearchForm({
333       q: this.getSearchQueryFromProps(pathSplit[3]),
334       community_id,
335       community_name: None,
336       creator_id,
337       type_: Some(this.getSearchTypeFromProps(pathSplit[5])),
338       sort: Some(this.getSortTypeFromProps(pathSplit[7])),
339       listing_type: Some(this.getListingTypeFromProps(pathSplit[9])),
340       page: Some(this.getPageFromProps(pathSplit[15])),
341       limit: Some(fetchLimit),
342       auth: req.auth,
343     });
344
345     let resolveObjectForm = new ResolveObject({
346       q: this.getSearchQueryFromProps(pathSplit[3]),
347       auth: req.auth,
348     });
349
350     if (form.q != "") {
351       promises.push(req.client.search(form));
352       promises.push(req.client.resolveObject(resolveObjectForm));
353     } else {
354       promises.push(Promise.resolve());
355       promises.push(Promise.resolve());
356     }
357
358     return promises;
359   }
360
361   componentDidUpdate(_: any, lastState: SearchState) {
362     if (
363       lastState.q !== this.state.q ||
364       lastState.type_ !== this.state.type_ ||
365       lastState.sort !== this.state.sort ||
366       lastState.listingType !== this.state.listingType ||
367       lastState.communityId !== this.state.communityId ||
368       lastState.creatorId !== this.state.creatorId ||
369       lastState.page !== this.state.page
370     ) {
371       this.setState({
372         loading: true,
373         searchText: this.state.q,
374         searchResponse: None,
375         resolveObjectResponse: None,
376       });
377       this.search();
378     }
379   }
380
381   get documentTitle(): string {
382     let siteName = this.state.siteRes.site_view.site.name;
383     return this.state.q
384       ? `${i18n.t("search")} - ${this.state.q} - ${siteName}`
385       : `${i18n.t("search")} - ${siteName}`;
386   }
387
388   render() {
389     return (
390       <div className="container-lg">
391         <HtmlTags
392           title={this.documentTitle}
393           path={this.context.router.route.match.url}
394           description={None}
395           image={None}
396         />
397         <h5>{i18n.t("search")}</h5>
398         {this.selects()}
399         {this.searchForm()}
400         {this.state.type_ == SearchType.All && this.all()}
401         {this.state.type_ == SearchType.Comments && this.comments()}
402         {this.state.type_ == SearchType.Posts && this.posts()}
403         {this.state.type_ == SearchType.Communities && this.communities()}
404         {this.state.type_ == SearchType.Users && this.users()}
405         {this.state.type_ == SearchType.Url && this.posts()}
406         {this.resultsCount() == 0 && <span>{i18n.t("no_results")}</span>}
407         <Paginator page={this.state.page} onChange={this.handlePageChange} />
408       </div>
409     );
410   }
411
412   searchForm() {
413     return (
414       <form
415         className="form-inline"
416         onSubmit={linkEvent(this, this.handleSearchSubmit)}
417       >
418         <input
419           type="text"
420           className="form-control mr-2 mb-2"
421           value={this.state.searchText}
422           placeholder={`${i18n.t("search")}...`}
423           aria-label={i18n.t("search")}
424           onInput={linkEvent(this, this.handleQChange)}
425           required
426           minLength={1}
427         />
428         <button type="submit" className="btn btn-secondary mr-2 mb-2">
429           {this.state.loading ? <Spinner /> : <span>{i18n.t("search")}</span>}
430         </button>
431       </form>
432     );
433   }
434
435   selects() {
436     return (
437       <div className="mb-2">
438         <select
439           value={this.state.type_}
440           onChange={linkEvent(this, this.handleTypeChange)}
441           className="custom-select w-auto mb-2"
442           aria-label={i18n.t("type")}
443         >
444           <option disabled aria-hidden="true">
445             {i18n.t("type")}
446           </option>
447           <option value={SearchType.All}>{i18n.t("all")}</option>
448           <option value={SearchType.Comments}>{i18n.t("comments")}</option>
449           <option value={SearchType.Posts}>{i18n.t("posts")}</option>
450           <option value={SearchType.Communities}>
451             {i18n.t("communities")}
452           </option>
453           <option value={SearchType.Users}>{i18n.t("users")}</option>
454           <option value={SearchType.Url}>{i18n.t("url")}</option>
455         </select>
456         <span className="ml-2">
457           <ListingTypeSelect
458             type_={this.state.listingType}
459             showLocal={showLocal(this.isoData)}
460             showSubscribed
461             onChange={this.handleListingTypeChange}
462           />
463         </span>
464         <span className="ml-2">
465           <SortSelect
466             sort={this.state.sort}
467             onChange={this.handleSortChange}
468             hideHot
469             hideMostComments
470           />
471         </span>
472         <div className="form-row">
473           {this.state.communities.length > 0 && this.communityFilter()}
474           {this.creatorFilter()}
475         </div>
476       </div>
477     );
478   }
479
480   postViewToCombined(postView: PostView): Combined {
481     return {
482       type_: "posts",
483       data: postView,
484       published: postView.post.published,
485     };
486   }
487
488   commentViewToCombined(commentView: CommentView): Combined {
489     return {
490       type_: "comments",
491       data: commentView,
492       published: commentView.comment.published,
493     };
494   }
495
496   communityViewToCombined(communityView: CommunityView): Combined {
497     return {
498       type_: "communities",
499       data: communityView,
500       published: communityView.community.published,
501     };
502   }
503
504   personViewSafeToCombined(personViewSafe: PersonViewSafe): Combined {
505     return {
506       type_: "users",
507       data: personViewSafe,
508       published: personViewSafe.person.published,
509     };
510   }
511
512   buildCombined(): Combined[] {
513     let combined: Combined[] = [];
514
515     // Push the possible resolve / federated objects first
516     this.state.resolveObjectResponse.match({
517       some: res => {
518         let resolveComment = res.comment;
519         if (resolveComment.isSome()) {
520           combined.push(this.commentViewToCombined(resolveComment.unwrap()));
521         }
522         let resolvePost = res.post;
523         if (resolvePost.isSome()) {
524           combined.push(this.postViewToCombined(resolvePost.unwrap()));
525         }
526         let resolveCommunity = res.community;
527         if (resolveCommunity.isSome()) {
528           combined.push(
529             this.communityViewToCombined(resolveCommunity.unwrap())
530           );
531         }
532         let resolveUser = res.person;
533         if (resolveUser.isSome()) {
534           combined.push(this.personViewSafeToCombined(resolveUser.unwrap()));
535         }
536       },
537       none: void 0,
538     });
539
540     // Push the search results
541     this.state.searchResponse.match({
542       some: res => {
543         pushNotNull(
544           combined,
545           res.comments?.map(e => this.commentViewToCombined(e))
546         );
547         pushNotNull(
548           combined,
549           res.posts?.map(e => this.postViewToCombined(e))
550         );
551         pushNotNull(
552           combined,
553           res.communities?.map(e => this.communityViewToCombined(e))
554         );
555         pushNotNull(
556           combined,
557           res.users?.map(e => this.personViewSafeToCombined(e))
558         );
559       },
560       none: void 0,
561     });
562
563     // Sort it
564     if (this.state.sort == SortType.New) {
565       combined.sort((a, b) => b.published.localeCompare(a.published));
566     } else {
567       combined.sort(
568         (a, b) =>
569           ((b.data as CommentView | PostView).counts.score |
570             (b.data as CommunityView).counts.subscribers |
571             (b.data as PersonViewSafe).counts.comment_score) -
572           ((a.data as CommentView | PostView).counts.score |
573             (a.data as CommunityView).counts.subscribers |
574             (a.data as PersonViewSafe).counts.comment_score)
575       );
576     }
577     return combined;
578   }
579
580   all() {
581     let combined = this.buildCombined();
582     return (
583       <div>
584         {combined.map(i => (
585           <div key={i.published} className="row">
586             <div className="col-12">
587               {i.type_ == "posts" && (
588                 <PostListing
589                   key={(i.data as PostView).post.id}
590                   post_view={i.data as PostView}
591                   duplicates={None}
592                   moderators={None}
593                   admins={None}
594                   showCommunity
595                   enableDownvotes={enableDownvotes(this.state.siteRes)}
596                   enableNsfw={enableNsfw(this.state.siteRes)}
597                   allLanguages={this.state.siteRes.all_languages}
598                   viewOnly
599                 />
600               )}
601               {i.type_ == "comments" && (
602                 <CommentNodes
603                   key={(i.data as CommentView).comment.id}
604                   nodes={[
605                     {
606                       comment_view: i.data as CommentView,
607                       children: [],
608                       depth: 0,
609                     },
610                   ]}
611                   viewType={CommentViewType.Flat}
612                   viewOnly
613                   moderators={None}
614                   admins={None}
615                   maxCommentsShown={None}
616                   locked
617                   noIndent
618                   enableDownvotes={enableDownvotes(this.state.siteRes)}
619                   allLanguages={this.state.siteRes.all_languages}
620                 />
621               )}
622               {i.type_ == "communities" && (
623                 <div>{this.communityListing(i.data as CommunityView)}</div>
624               )}
625               {i.type_ == "users" && (
626                 <div>{this.personListing(i.data as PersonViewSafe)}</div>
627               )}
628             </div>
629           </div>
630         ))}
631       </div>
632     );
633   }
634
635   comments() {
636     let comments: CommentView[] = [];
637
638     this.state.resolveObjectResponse.match({
639       some: res => pushNotNull(comments, res.comment),
640       none: void 0,
641     });
642     this.state.searchResponse.match({
643       some: res => pushNotNull(comments, res.comments),
644       none: void 0,
645     });
646
647     return (
648       <CommentNodes
649         nodes={commentsToFlatNodes(comments)}
650         viewType={CommentViewType.Flat}
651         viewOnly
652         locked
653         noIndent
654         moderators={None}
655         admins={None}
656         maxCommentsShown={None}
657         enableDownvotes={enableDownvotes(this.state.siteRes)}
658         allLanguages={this.state.siteRes.all_languages}
659       />
660     );
661   }
662
663   posts() {
664     let posts: PostView[] = [];
665
666     this.state.resolveObjectResponse.match({
667       some: res => pushNotNull(posts, res.post),
668       none: void 0,
669     });
670     this.state.searchResponse.match({
671       some: res => pushNotNull(posts, res.posts),
672       none: void 0,
673     });
674
675     return (
676       <>
677         {posts.map(pv => (
678           <div key={pv.post.id} className="row">
679             <div className="col-12">
680               <PostListing
681                 post_view={pv}
682                 showCommunity
683                 duplicates={None}
684                 moderators={None}
685                 admins={None}
686                 enableDownvotes={enableDownvotes(this.state.siteRes)}
687                 enableNsfw={enableNsfw(this.state.siteRes)}
688                 allLanguages={this.state.siteRes.all_languages}
689                 viewOnly
690               />
691             </div>
692           </div>
693         ))}
694       </>
695     );
696   }
697
698   communities() {
699     let communities: CommunityView[] = [];
700
701     this.state.resolveObjectResponse.match({
702       some: res => pushNotNull(communities, res.community),
703       none: void 0,
704     });
705     this.state.searchResponse.match({
706       some: res => pushNotNull(communities, res.communities),
707       none: void 0,
708     });
709
710     return (
711       <>
712         {communities.map(cv => (
713           <div key={cv.community.id} className="row">
714             <div className="col-12">{this.communityListing(cv)}</div>
715           </div>
716         ))}
717       </>
718     );
719   }
720
721   users() {
722     let users: PersonViewSafe[] = [];
723
724     this.state.resolveObjectResponse.match({
725       some: res => pushNotNull(users, res.person),
726       none: void 0,
727     });
728     this.state.searchResponse.match({
729       some: res => pushNotNull(users, res.users),
730       none: void 0,
731     });
732
733     return (
734       <>
735         {users.map(pvs => (
736           <div key={pvs.person.id} className="row">
737             <div className="col-12">{this.personListing(pvs)}</div>
738           </div>
739         ))}
740       </>
741     );
742   }
743
744   communityListing(community_view: CommunityView) {
745     return (
746       <>
747         <span>
748           <CommunityLink community={community_view.community} />
749         </span>
750         <span>{` -
751         ${i18n.t("number_of_subscribers", {
752           count: community_view.counts.subscribers,
753           formattedCount: numToSI(community_view.counts.subscribers),
754         })}
755       `}</span>
756       </>
757     );
758   }
759
760   personListing(person_view: PersonViewSafe) {
761     return (
762       <>
763         <span>
764           <PersonListing person={person_view.person} showApubName />
765         </span>
766         <span>{` - ${i18n.t("number_of_comments", {
767           count: person_view.counts.comment_count,
768           formattedCount: numToSI(person_view.counts.comment_count),
769         })}`}</span>
770       </>
771     );
772   }
773
774   communityFilter() {
775     return (
776       <div className="form-group col-sm-6">
777         <label className="col-form-label" htmlFor="community-filter">
778           {i18n.t("community")}
779         </label>
780         <div>
781           <select
782             className="form-control"
783             id="community-filter"
784             value={this.state.communityId}
785           >
786             <option value="0">{i18n.t("all")}</option>
787             {this.state.communities.map(cv => (
788               <option key={cv.community.id} value={cv.community.id}>
789                 {communitySelectName(cv)}
790               </option>
791             ))}
792           </select>
793         </div>
794       </div>
795     );
796   }
797
798   creatorFilter() {
799     return (
800       <div className="form-group col-sm-6">
801         <label className="col-form-label" htmlFor="creator-filter">
802           {capitalizeFirstLetter(i18n.t("creator"))}
803         </label>
804         <div>
805           <select
806             className="form-control"
807             id="creator-filter"
808             value={this.state.creatorId}
809           >
810             <option value="0">{i18n.t("all")}</option>
811             {this.state.creatorDetails.match({
812               some: creator => (
813                 <option value={creator.person_view.person.id}>
814                   {personSelectName(creator.person_view)}
815                 </option>
816               ),
817               none: <></>,
818             })}
819           </select>
820         </div>
821       </div>
822     );
823   }
824
825   resultsCount(): number {
826     let searchCount = this.state.searchResponse
827       .map(
828         r =>
829           r.posts?.length +
830           r.comments?.length +
831           r.communities?.length +
832           r.users?.length
833       )
834       .unwrapOr(0);
835
836     let resObjCount = this.state.resolveObjectResponse
837       .map(r => (r.post || r.person || r.community || r.comment ? 1 : 0))
838       .unwrapOr(0);
839
840     return resObjCount + searchCount;
841   }
842
843   handlePageChange(page: number) {
844     this.updateUrl({ page });
845   }
846
847   search() {
848     let community_id: Option<number> =
849       this.state.communityId == 0 ? None : Some(this.state.communityId);
850     let creator_id: Option<number> =
851       this.state.creatorId == 0 ? None : Some(this.state.creatorId);
852
853     let form = new SearchForm({
854       q: this.state.q,
855       community_id,
856       community_name: None,
857       creator_id,
858       type_: Some(this.state.type_),
859       sort: Some(this.state.sort),
860       listing_type: Some(this.state.listingType),
861       page: Some(this.state.page),
862       limit: Some(fetchLimit),
863       auth: auth(false).ok(),
864     });
865
866     let resolveObjectForm = new ResolveObject({
867       q: this.state.q,
868       auth: auth(false).ok(),
869     });
870
871     if (this.state.q != "") {
872       this.setState({
873         searchResponse: None,
874         resolveObjectResponse: None,
875         loading: true,
876       });
877       WebSocketService.Instance.send(wsClient.search(form));
878       WebSocketService.Instance.send(wsClient.resolveObject(resolveObjectForm));
879     }
880   }
881
882   setupCommunityFilter() {
883     if (isBrowser()) {
884       let selectId: any = document.getElementById("community-filter");
885       if (selectId) {
886         this.communityChoices = new Choices(selectId, choicesConfig);
887         this.communityChoices.passedElement.element.addEventListener(
888           "choice",
889           (e: any) => {
890             this.handleCommunityFilterChange(Number(e.detail.choice.value));
891           },
892           false
893         );
894         this.communityChoices.passedElement.element.addEventListener(
895           "search",
896           debounce(async (e: any) => {
897             try {
898               let communities = (await fetchCommunities(e.detail.value))
899                 .communities;
900               let choices = communities.map(cv => communityToChoice(cv));
901               choices.unshift({ value: "0", label: i18n.t("all") });
902               this.communityChoices.setChoices(choices, "value", "label", true);
903             } catch (err) {
904               console.error(err);
905             }
906           }),
907           false
908         );
909       }
910     }
911   }
912
913   setupCreatorFilter() {
914     if (isBrowser()) {
915       let selectId: any = document.getElementById("creator-filter");
916       if (selectId) {
917         this.creatorChoices = new Choices(selectId, choicesConfig);
918         this.creatorChoices.passedElement.element.addEventListener(
919           "choice",
920           (e: any) => {
921             this.handleCreatorFilterChange(Number(e.detail.choice.value));
922           },
923           false
924         );
925         this.creatorChoices.passedElement.element.addEventListener(
926           "search",
927           debounce(async (e: any) => {
928             try {
929               let creators = (await fetchUsers(e.detail.value)).users;
930               let choices = creators.map(pvs => personToChoice(pvs));
931               choices.unshift({ value: "0", label: i18n.t("all") });
932               this.creatorChoices.setChoices(choices, "value", "label", true);
933             } catch (err) {
934               console.log(err);
935             }
936           }),
937           false
938         );
939       }
940     }
941   }
942
943   handleSortChange(val: SortType) {
944     this.updateUrl({ sort: val, page: 1 });
945   }
946
947   handleTypeChange(i: Search, event: any) {
948     i.updateUrl({
949       type_: SearchType[event.target.value],
950       page: 1,
951     });
952   }
953
954   handleListingTypeChange(val: ListingType) {
955     this.updateUrl({
956       listingType: val,
957       page: 1,
958     });
959   }
960
961   handleCommunityFilterChange(communityId: number) {
962     this.updateUrl({
963       communityId,
964       page: 1,
965     });
966   }
967
968   handleCreatorFilterChange(creatorId: number) {
969     this.updateUrl({
970       creatorId,
971       page: 1,
972     });
973   }
974
975   handleSearchSubmit(i: Search, event: any) {
976     event.preventDefault();
977     i.updateUrl({
978       q: i.state.searchText,
979       type_: i.state.type_,
980       listingType: i.state.listingType,
981       communityId: i.state.communityId,
982       creatorId: i.state.creatorId,
983       sort: i.state.sort,
984       page: i.state.page,
985     });
986   }
987
988   handleQChange(i: Search, event: any) {
989     i.setState({ searchText: event.target.value });
990   }
991
992   updateUrl(paramUpdates: UrlParams) {
993     const qStr = paramUpdates.q || this.state.q;
994     const qStrEncoded = encodeURIComponent(qStr);
995     const typeStr = paramUpdates.type_ || this.state.type_;
996     const listingTypeStr = paramUpdates.listingType || this.state.listingType;
997     const sortStr = paramUpdates.sort || this.state.sort;
998     const communityId =
999       paramUpdates.communityId == 0
1000         ? 0
1001         : paramUpdates.communityId || this.state.communityId;
1002     const creatorId =
1003       paramUpdates.creatorId == 0
1004         ? 0
1005         : paramUpdates.creatorId || this.state.creatorId;
1006     const page = paramUpdates.page || this.state.page;
1007     this.props.history.push(
1008       `/search/q/${qStrEncoded}/type/${typeStr}/sort/${sortStr}/listing_type/${listingTypeStr}/community_id/${communityId}/creator_id/${creatorId}/page/${page}`
1009     );
1010   }
1011
1012   parseMessage(msg: any) {
1013     console.log(msg);
1014     let op = wsUserOp(msg);
1015     if (msg.error) {
1016       if (msg.error == "couldnt_find_object") {
1017         this.setState({
1018           resolveObjectResponse: Some({
1019             comment: None,
1020             post: None,
1021             community: None,
1022             person: None,
1023           }),
1024         });
1025         this.checkFinishedLoading();
1026       } else {
1027         toast(i18n.t(msg.error), "danger");
1028         return;
1029       }
1030     } else if (op == UserOperation.Search) {
1031       let data = wsJsonToRes<SearchResponse>(msg, SearchResponse);
1032       this.setState({ searchResponse: Some(data) });
1033       window.scrollTo(0, 0);
1034       this.checkFinishedLoading();
1035       restoreScrollPosition(this.context);
1036     } else if (op == UserOperation.CreateCommentLike) {
1037       let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
1038       createCommentLikeRes(
1039         data.comment_view,
1040         this.state.searchResponse.map(r => r.comments).unwrapOr([])
1041       );
1042       this.setState(this.state);
1043     } else if (op == UserOperation.CreatePostLike) {
1044       let data = wsJsonToRes<PostResponse>(msg, PostResponse);
1045       createPostLikeFindRes(
1046         data.post_view,
1047         this.state.searchResponse.map(r => r.posts).unwrapOr([])
1048       );
1049       this.setState(this.state);
1050     } else if (op == UserOperation.ListCommunities) {
1051       let data = wsJsonToRes<ListCommunitiesResponse>(
1052         msg,
1053         ListCommunitiesResponse
1054       );
1055       this.setState({ communities: data.communities });
1056       this.setupCommunityFilter();
1057     } else if (op == UserOperation.ResolveObject) {
1058       let data = wsJsonToRes<ResolveObjectResponse>(msg, ResolveObjectResponse);
1059       this.setState({ resolveObjectResponse: Some(data) });
1060       this.checkFinishedLoading();
1061     }
1062   }
1063
1064   checkFinishedLoading() {
1065     if (
1066       this.state.searchResponse.isSome() &&
1067       this.state.resolveObjectResponse.isSome()
1068     ) {
1069       this.setState({ loading: false });
1070     }
1071   }
1072 }