1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
17 WebSocketJsonResponse,
18 } from '../interfaces';
19 import { WebSocketService } from '../services';
23 routeSearchTypeToEnum,
27 createPostLikeFindRes,
30 import { PostListing } from './post-listing';
31 import { UserListing } from './user-listing';
32 import { CommunityLink } from './community-link';
33 import { SortSelect } from './sort-select';
34 import { CommentNodes } from './comment-nodes';
35 import { i18n } from '../i18next';
37 interface SearchState {
42 searchResponse: SearchResponse;
46 export class Search extends Component<any, SearchState> {
47 private subscription: Subscription;
48 private emptyState: SearchState = {
49 q: this.getSearchQueryFromProps(this.props),
50 type_: this.getSearchTypeFromProps(this.props),
51 sort: this.getSortTypeFromProps(this.props),
52 page: this.getPageFromProps(this.props),
63 getSearchQueryFromProps(props: any): string {
64 return props.match.params.q ? props.match.params.q : '';
67 getSearchTypeFromProps(props: any): SearchType {
68 return props.match.params.type
69 ? routeSearchTypeToEnum(props.match.params.type)
73 getSortTypeFromProps(props: any): SortType {
74 return props.match.params.sort
75 ? routeSortTypeToEnum(props.match.params.sort)
79 getPageFromProps(props: any): number {
80 return props.match.params.page ? Number(props.match.params.page) : 1;
83 constructor(props: any, context: any) {
84 super(props, context);
86 this.state = this.emptyState;
87 this.handleSortChange = this.handleSortChange.bind(this);
89 this.subscription = WebSocketService.Instance.subject
90 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
92 msg => this.parseMessage(msg),
93 err => console.error(err),
94 () => console.log('complete')
102 componentWillUnmount() {
103 this.subscription.unsubscribe();
106 // Necessary for back button for some reason
107 componentWillReceiveProps(nextProps: any) {
109 nextProps.history.action == 'POP' ||
110 nextProps.history.action == 'PUSH'
112 this.state.q = this.getSearchQueryFromProps(nextProps);
113 this.state.type_ = this.getSearchTypeFromProps(nextProps);
114 this.state.sort = this.getSortTypeFromProps(nextProps);
115 this.state.page = this.getPageFromProps(nextProps);
116 this.setState(this.state);
121 componentDidMount() {
122 document.title = `${i18n.t('search')} - ${
123 WebSocketService.Instance.site.name
129 <div class="container">
130 <h5>{i18n.t('search')}</h5>
133 {this.state.type_ == SearchType.All && this.all()}
134 {this.state.type_ == SearchType.Comments && this.comments()}
135 {this.state.type_ == SearchType.Posts && this.posts()}
136 {this.state.type_ == SearchType.Communities && this.communities()}
137 {this.state.type_ == SearchType.Users && this.users()}
148 onSubmit={linkEvent(this, this.handleSearchSubmit)}
152 class="form-control mr-2"
154 placeholder={`${i18n.t('search')}...`}
155 onInput={linkEvent(this, this.handleQChange)}
159 <button type="submit" class="btn btn-secondary mr-2">
160 {this.state.loading ? (
161 <svg class="icon icon-spinner spin">
162 <use xlinkHref="#icon-spinner"></use>
165 <span>{i18n.t('search')}</span>
174 <div className="mb-2">
176 value={this.state.type_}
177 onChange={linkEvent(this, this.handleTypeChange)}
178 class="custom-select custom-select-sm w-auto"
180 <option disabled>{i18n.t('type')}</option>
181 <option value={SearchType.All}>{i18n.t('all')}</option>
182 <option value={SearchType.Comments}>{i18n.t('comments')}</option>
183 <option value={SearchType.Posts}>{i18n.t('posts')}</option>
184 <option value={SearchType.Communities}>
185 {i18n.t('communities')}
187 <option value={SearchType.Users}>{i18n.t('users')}</option>
191 sort={this.state.sort}
192 onChange={this.handleSortChange}
201 let combined: Array<{
203 data: Comment | Post | Community | UserView;
205 let comments = this.state.searchResponse.comments.map(e => {
206 return { type_: 'comments', data: e };
208 let posts = this.state.searchResponse.posts.map(e => {
209 return { type_: 'posts', data: e };
211 let communities = this.state.searchResponse.communities.map(e => {
212 return { type_: 'communities', data: e };
214 let users = this.state.searchResponse.users.map(e => {
215 return { type_: 'users', data: e };
218 combined.push(...comments);
219 combined.push(...posts);
220 combined.push(...communities);
221 combined.push(...users);
224 if (this.state.sort == SortType.New) {
225 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
229 ((b.data as Comment | Post).score |
230 (b.data as Community).number_of_subscribers |
231 (b.data as UserView).comment_score) -
232 ((a.data as Comment | Post).score |
233 (a.data as Community).number_of_subscribers |
234 (a.data as UserView).comment_score)
243 {i.type_ == 'posts' && (
244 <PostListing post={i.data as Post} showCommunity />
246 {i.type_ == 'comments' && (
248 nodes={[{ comment: i.data as Comment }]}
253 {i.type_ == 'communities' && (
254 <div>{this.communityListing(i.data as Community)}</div>
256 {i.type_ == 'users' && (
261 name: (i.data as UserView).name,
262 avatar: (i.data as UserView).avatar,
278 nodes={commentsToFlatNodes(this.state.searchResponse.comments)}
288 {this.state.searchResponse.posts.map(post => (
291 <PostListing post={post} showCommunity />
299 // Todo possibly create UserListing and CommunityListing
303 {this.state.searchResponse.communities.map(community => (
305 <div class="col-12">{this.communityListing(community)}</div>
312 communityListing(community: Community) {
316 <CommunityLink community={community} />
318 <span>{` - ${community.title} -
319 ${i18n.t('number_of_subscribers', {
320 count: community.number_of_subscribers,
330 {this.state.searchResponse.users.map(user => (
335 className="text-info"
336 to={`/u/${user.name}`}
337 >{`/u/${user.name}`}</Link>
349 {this.state.page > 1 && (
351 class="btn btn-sm btn-secondary mr-1"
352 onClick={linkEvent(this, this.prevPage)}
358 class="btn btn-sm btn-secondary"
359 onClick={linkEvent(this, this.nextPage)}
368 let res = this.state.searchResponse;
372 res.posts.length == 0 &&
373 res.comments.length == 0 &&
374 res.communities.length == 0 &&
375 res.users.length == 0 && <span>{i18n.t('no_results')}</span>}
380 nextPage(i: Search) {
387 prevPage(i: Search) {
395 let form: SearchForm = {
397 type_: SearchType[this.state.type_],
398 sort: SortType[this.state.sort],
399 page: this.state.page,
403 if (this.state.q != '') {
404 WebSocketService.Instance.search(form);
408 handleSortChange(val: SortType) {
409 this.state.sort = val;
411 this.setState(this.state);
415 handleTypeChange(i: Search, event: any) {
416 i.state.type_ = Number(event.target.value);
422 handleSearchSubmit(i: Search, event: any) {
423 event.preventDefault();
424 i.state.loading = true;
430 handleQChange(i: Search, event: any) {
431 i.state.q = event.target.value;
436 let typeStr = SearchType[this.state.type_].toLowerCase();
437 let sortStr = SortType[this.state.sort].toLowerCase();
438 this.props.history.push(
439 `/search/q/${this.state.q}/type/${typeStr}/sort/${sortStr}/page/${this.state.page}`
443 parseMessage(msg: WebSocketJsonResponse) {
445 let res = wsJsonToRes(msg);
447 toast(i18n.t(msg.error), 'danger');
449 } else if (res.op == UserOperation.Search) {
450 let data = res.data as SearchResponse;
451 this.state.searchResponse = data;
452 this.state.loading = false;
453 document.title = `${i18n.t('search')} - ${this.state.q} - ${
454 WebSocketService.Instance.site.name
456 window.scrollTo(0, 0);
457 this.setState(this.state);
458 } else if (res.op == UserOperation.CreateCommentLike) {
459 let data = res.data as CommentResponse;
460 createCommentLikeRes(data, this.state.searchResponse.comments);
461 this.setState(this.state);
462 } else if (res.op == UserOperation.CreatePostLike) {
463 let data = res.data as PostResponse;
464 createPostLikeFindRes(data, this.state.searchResponse.posts);
465 this.setState(this.state);