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,
25 pictrsAvatarThumbnail,
29 createPostLikeFindRes,
32 import { PostListing } from './post-listing';
33 import { UserListing } from './user-listing';
34 import { SortSelect } from './sort-select';
35 import { CommentNodes } from './comment-nodes';
36 import { i18n } from '../i18next';
38 interface SearchState {
43 searchResponse: SearchResponse;
47 export class Search extends Component<any, SearchState> {
48 private subscription: Subscription;
49 private emptyState: SearchState = {
50 q: this.getSearchQueryFromProps(this.props),
51 type_: this.getSearchTypeFromProps(this.props),
52 sort: this.getSortTypeFromProps(this.props),
53 page: this.getPageFromProps(this.props),
64 getSearchQueryFromProps(props: any): string {
65 return props.match.params.q ? props.match.params.q : '';
68 getSearchTypeFromProps(props: any): SearchType {
69 return props.match.params.type
70 ? routeSearchTypeToEnum(props.match.params.type)
74 getSortTypeFromProps(props: any): SortType {
75 return props.match.params.sort
76 ? routeSortTypeToEnum(props.match.params.sort)
80 getPageFromProps(props: any): number {
81 return props.match.params.page ? Number(props.match.params.page) : 1;
84 constructor(props: any, context: any) {
85 super(props, context);
87 this.state = this.emptyState;
88 this.handleSortChange = this.handleSortChange.bind(this);
90 this.subscription = WebSocketService.Instance.subject
91 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
93 msg => this.parseMessage(msg),
94 err => console.error(err),
95 () => console.log('complete')
103 componentWillUnmount() {
104 this.subscription.unsubscribe();
107 // Necessary for back button for some reason
108 componentWillReceiveProps(nextProps: any) {
110 nextProps.history.action == 'POP' ||
111 nextProps.history.action == 'PUSH'
113 this.state = this.emptyState;
114 this.state.q = this.getSearchQueryFromProps(nextProps);
115 this.state.type_ = this.getSearchTypeFromProps(nextProps);
116 this.state.sort = this.getSortTypeFromProps(nextProps);
117 this.state.page = this.getPageFromProps(nextProps);
118 this.setState(this.state);
123 componentDidMount() {
124 document.title = `${i18n.t('search')} - ${
125 WebSocketService.Instance.site.name
131 <div class="container">
132 <h5>{i18n.t('search')}</h5>
135 {this.state.type_ == SearchType.All && this.all()}
136 {this.state.type_ == SearchType.Comments && this.comments()}
137 {this.state.type_ == SearchType.Posts && this.posts()}
138 {this.state.type_ == SearchType.Communities && this.communities()}
139 {this.state.type_ == SearchType.Users && this.users()}
150 onSubmit={linkEvent(this, this.handleSearchSubmit)}
154 class="form-control mr-2"
156 placeholder={`${i18n.t('search')}...`}
157 onInput={linkEvent(this, this.handleQChange)}
161 <button type="submit" class="btn btn-secondary mr-2">
162 {this.state.loading ? (
163 <svg class="icon icon-spinner spin">
164 <use xlinkHref="#icon-spinner"></use>
167 <span>{i18n.t('search')}</span>
176 <div className="mb-2">
178 value={this.state.type_}
179 onChange={linkEvent(this, this.handleTypeChange)}
180 class="custom-select custom-select-sm w-auto"
182 <option disabled>{i18n.t('type')}</option>
183 <option value={SearchType.All}>{i18n.t('all')}</option>
184 <option value={SearchType.Comments}>{i18n.t('comments')}</option>
185 <option value={SearchType.Posts}>{i18n.t('posts')}</option>
186 <option value={SearchType.Communities}>
187 {i18n.t('communities')}
189 <option value={SearchType.Users}>{i18n.t('users')}</option>
193 sort={this.state.sort}
194 onChange={this.handleSortChange}
203 let combined: Array<{
205 data: Comment | Post | Community | UserView;
207 let comments = this.state.searchResponse.comments.map(e => {
208 return { type_: 'comments', data: e };
210 let posts = this.state.searchResponse.posts.map(e => {
211 return { type_: 'posts', data: e };
213 let communities = this.state.searchResponse.communities.map(e => {
214 return { type_: 'communities', data: e };
216 let users = this.state.searchResponse.users.map(e => {
217 return { type_: 'users', data: e };
220 combined.push(...comments);
221 combined.push(...posts);
222 combined.push(...communities);
223 combined.push(...users);
226 if (this.state.sort == SortType.New) {
227 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
231 ((b.data as Comment | Post).score |
232 (b.data as Community).number_of_subscribers |
233 (b.data as UserView).comment_score) -
234 ((a.data as Comment | Post).score |
235 (a.data as Community).number_of_subscribers |
236 (a.data as UserView).comment_score)
245 {i.type_ == 'posts' && (
246 <PostListing post={i.data as Post} showCommunity />
248 {i.type_ == 'comments' && (
250 nodes={[{ comment: i.data as Comment }]}
255 {i.type_ == 'communities' && (
258 <Link to={`/c/${(i.data as Community).name}`}>{`/c/${
259 (i.data as Community).name
262 <span>{` - ${(i.data as Community).title} - ${
263 (i.data as Community).number_of_subscribers
264 } subscribers`}</span>
267 {i.type_ == 'users' && (
272 name: (i.data as UserView).name,
273 avatar: (i.data as UserView).avatar,
278 (i.data as UserView).comment_score
279 } comment karma`}</span>
292 nodes={commentsToFlatNodes(this.state.searchResponse.comments)}
302 {this.state.searchResponse.posts.map(post => (
305 <PostListing post={post} showCommunity />
313 // Todo possibly create UserListing and CommunityListing
317 {this.state.searchResponse.communities.map(community => (
322 to={`/c/${community.name}`}
323 >{`/c/${community.name}`}</Link>
325 <span>{` - ${community.title} - ${community.number_of_subscribers} subscribers`}</span>
336 {this.state.searchResponse.users.map(user => (
341 className="text-info"
342 to={`/u/${user.name}`}
343 >{`/u/${user.name}`}</Link>
345 <span>{` - ${user.comment_score} comment karma`}</span>
356 {this.state.page > 1 && (
358 class="btn btn-sm btn-secondary mr-1"
359 onClick={linkEvent(this, this.prevPage)}
365 class="btn btn-sm btn-secondary"
366 onClick={linkEvent(this, this.nextPage)}
375 let res = this.state.searchResponse;
379 res.posts.length == 0 &&
380 res.comments.length == 0 &&
381 res.communities.length == 0 &&
382 res.users.length == 0 && <span>{i18n.t('no_results')}</span>}
387 nextPage(i: Search) {
394 prevPage(i: Search) {
402 let form: SearchForm = {
404 type_: SearchType[this.state.type_],
405 sort: SortType[this.state.sort],
406 page: this.state.page,
410 if (this.state.q != '') {
411 WebSocketService.Instance.search(form);
415 handleSortChange(val: SortType) {
416 this.state.sort = val;
418 this.setState(this.state);
422 handleTypeChange(i: Search, event: any) {
423 i.state.type_ = Number(event.target.value);
429 handleSearchSubmit(i: Search, event: any) {
430 event.preventDefault();
431 i.state.loading = true;
437 handleQChange(i: Search, event: any) {
438 i.state.q = event.target.value;
443 let typeStr = SearchType[this.state.type_].toLowerCase();
444 let sortStr = SortType[this.state.sort].toLowerCase();
445 this.props.history.push(
446 `/search/q/${this.state.q}/type/${typeStr}/sort/${sortStr}/page/${this.state.page}`
450 parseMessage(msg: WebSocketJsonResponse) {
452 let res = wsJsonToRes(msg);
454 toast(i18n.t(msg.error), 'danger');
456 } else if (res.op == UserOperation.Search) {
457 let data = res.data as SearchResponse;
458 this.state.searchResponse = data;
459 this.state.loading = false;
460 document.title = `${i18n.t('search')} - ${this.state.q} - ${
461 WebSocketService.Instance.site.name
463 window.scrollTo(0, 0);
464 this.setState(this.state);
465 } else if (res.op == UserOperation.CreateCommentLike) {
466 let data = res.data as CommentResponse;
467 createCommentLikeRes(data, this.state.searchResponse.comments);
468 this.setState(this.state);
469 } else if (res.op == UserOperation.CreatePostLike) {
470 let data = res.data as PostResponse;
471 createPostLikeFindRes(data, this.state.searchResponse.posts);
472 this.setState(this.state);