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';
15 CreatePostLikeResponse,
17 WebSocketJsonResponse,
18 } from '../interfaces';
19 import { WebSocketService } from '../services';
23 routeSearchTypeToEnum,
25 pictshareAvatarThumbnail,
29 import { PostListing } from './post-listing';
30 import { SortSelect } from './sort-select';
31 import { CommentNodes } from './comment-nodes';
32 import { i18n } from '../i18next';
33 import { T } from 'inferno-i18next';
35 interface SearchState {
40 searchResponse: SearchResponse;
44 export class Search extends Component<any, SearchState> {
45 private subscription: Subscription;
46 private emptyState: SearchState = {
47 q: this.getSearchQueryFromProps(this.props),
48 type_: this.getSearchTypeFromProps(this.props),
49 sort: this.getSortTypeFromProps(this.props),
50 page: this.getPageFromProps(this.props),
61 getSearchQueryFromProps(props: any): string {
62 return props.match.params.q ? props.match.params.q : '';
65 getSearchTypeFromProps(props: any): SearchType {
66 return props.match.params.type
67 ? routeSearchTypeToEnum(props.match.params.type)
71 getSortTypeFromProps(props: any): SortType {
72 return props.match.params.sort
73 ? routeSortTypeToEnum(props.match.params.sort)
77 getPageFromProps(props: any): number {
78 return props.match.params.page ? Number(props.match.params.page) : 1;
81 constructor(props: any, context: any) {
82 super(props, context);
84 this.state = this.emptyState;
85 this.handleSortChange = this.handleSortChange.bind(this);
87 this.subscription = WebSocketService.Instance.subject
88 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
90 msg => this.parseMessage(msg),
91 err => console.error(err),
92 () => console.log('complete')
100 componentWillUnmount() {
101 this.subscription.unsubscribe();
104 // Necessary for back button for some reason
105 componentWillReceiveProps(nextProps: any) {
107 nextProps.history.action == 'POP' ||
108 nextProps.history.action == 'PUSH'
110 this.state = this.emptyState;
111 this.state.q = this.getSearchQueryFromProps(nextProps);
112 this.state.type_ = this.getSearchTypeFromProps(nextProps);
113 this.state.sort = this.getSortTypeFromProps(nextProps);
114 this.state.page = this.getPageFromProps(nextProps);
115 this.setState(this.state);
120 componentDidMount() {
121 document.title = `${i18n.t('search')} - ${
122 WebSocketService.Instance.site.name
128 <div class="container">
130 <T i18nKey="search">#</T>
134 {this.state.type_ == SearchType.All && this.all()}
135 {this.state.type_ == SearchType.Comments && this.comments()}
136 {this.state.type_ == SearchType.Posts && this.posts()}
137 {this.state.type_ == SearchType.Communities && this.communities()}
138 {this.state.type_ == SearchType.Users && this.users()}
149 onSubmit={linkEvent(this, this.handleSearchSubmit)}
153 class="form-control mr-2"
155 placeholder={`${i18n.t('search')}...`}
156 onInput={linkEvent(this, this.handleQChange)}
160 <button type="submit" class="btn btn-secondary mr-2">
161 {this.state.loading ? (
162 <svg class="icon icon-spinner spin">
163 <use xlinkHref="#icon-spinner"></use>
167 <T i18nKey="search">#</T>
177 <div className="mb-2">
179 value={this.state.type_}
180 onChange={linkEvent(this, this.handleTypeChange)}
181 class="custom-select custom-select-sm w-auto"
184 <T i18nKey="type">#</T>
186 <option value={SearchType.All}>
187 <T i18nKey="all">#</T>
189 <option value={SearchType.Comments}>
190 <T i18nKey="comments">#</T>
192 <option value={SearchType.Posts}>
193 <T i18nKey="posts">#</T>
195 <option value={SearchType.Communities}>
196 <T i18nKey="communities">#</T>
198 <option value={SearchType.Users}>
199 <T i18nKey="users">#</T>
204 sort={this.state.sort}
205 onChange={this.handleSortChange}
214 let combined: Array<{
216 data: Comment | Post | Community | UserView;
218 let comments = this.state.searchResponse.comments.map(e => {
219 return { type_: 'comments', data: e };
221 let posts = this.state.searchResponse.posts.map(e => {
222 return { type_: 'posts', data: e };
224 let communities = this.state.searchResponse.communities.map(e => {
225 return { type_: 'communities', data: e };
227 let users = this.state.searchResponse.users.map(e => {
228 return { type_: 'users', data: e };
231 combined.push(...comments);
232 combined.push(...posts);
233 combined.push(...communities);
234 combined.push(...users);
237 if (this.state.sort == SortType.New) {
238 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
242 ((b.data as Comment | Post).score |
243 (b.data as Community).number_of_subscribers |
244 (b.data as UserView).comment_score) -
245 ((a.data as Comment | Post).score |
246 (a.data as Community).number_of_subscribers |
247 (a.data as UserView).comment_score)
256 {i.type_ == 'posts' && (
257 <PostListing post={i.data as Post} showCommunity />
259 {i.type_ == 'comments' && (
261 nodes={[{ comment: i.data as Comment }]}
266 {i.type_ == 'communities' && (
269 <Link to={`/c/${(i.data as Community).name}`}>{`/c/${
270 (i.data as Community).name
273 <span>{` - ${(i.data as Community).title} - ${
274 (i.data as Community).number_of_subscribers
275 } subscribers`}</span>
278 {i.type_ == 'users' && (
282 className="text-info"
283 to={`/u/${(i.data as UserView).name}`}
285 {(i.data as UserView).avatar && showAvatars() && (
289 src={pictshareAvatarThumbnail(
290 (i.data as UserView).avatar
292 class="rounded-circle mr-1"
295 <span>{`/u/${(i.data as UserView).name}`}</span>
299 (i.data as UserView).comment_score
300 } comment karma`}</span>
313 {this.state.searchResponse.comments.map(comment => (
316 <CommentNodes nodes={[{ comment: comment }]} locked noIndent />
327 {this.state.searchResponse.posts.map(post => (
330 <PostListing post={post} showCommunity />
338 // Todo possibly create UserListing and CommunityListing
342 {this.state.searchResponse.communities.map(community => (
347 to={`/c/${community.name}`}
348 >{`/c/${community.name}`}</Link>
350 <span>{` - ${community.title} - ${community.number_of_subscribers} subscribers`}</span>
361 {this.state.searchResponse.users.map(user => (
366 className="text-info"
367 to={`/u/${user.name}`}
368 >{`/u/${user.name}`}</Link>
370 <span>{` - ${user.comment_score} comment karma`}</span>
381 {this.state.page > 1 && (
383 class="btn btn-sm btn-secondary mr-1"
384 onClick={linkEvent(this, this.prevPage)}
386 <T i18nKey="prev">#</T>
390 class="btn btn-sm btn-secondary"
391 onClick={linkEvent(this, this.nextPage)}
393 <T i18nKey="next">#</T>
400 let res = this.state.searchResponse;
404 res.posts.length == 0 &&
405 res.comments.length == 0 &&
406 res.communities.length == 0 &&
407 res.users.length == 0 && (
409 <T i18nKey="no_results">#</T>
416 nextPage(i: Search) {
423 prevPage(i: Search) {
431 let form: SearchForm = {
433 type_: SearchType[this.state.type_],
434 sort: SortType[this.state.sort],
435 page: this.state.page,
439 if (this.state.q != '') {
440 WebSocketService.Instance.search(form);
444 handleSortChange(val: SortType) {
445 this.state.sort = val;
447 this.setState(this.state);
451 handleTypeChange(i: Search, event: any) {
452 i.state.type_ = Number(event.target.value);
458 handleSearchSubmit(i: Search, event: any) {
459 event.preventDefault();
460 i.state.loading = true;
466 handleQChange(i: Search, event: any) {
467 i.state.q = event.target.value;
472 let typeStr = SearchType[this.state.type_].toLowerCase();
473 let sortStr = SortType[this.state.sort].toLowerCase();
474 this.props.history.push(
475 `/search/q/${this.state.q}/type/${typeStr}/sort/${sortStr}/page/${this.state.page}`
479 parseMessage(msg: WebSocketJsonResponse) {
481 let res = wsJsonToRes(msg);
483 toast(i18n.t(msg.error), 'danger');
485 } else if (res.op == UserOperation.Search) {
486 let data = res.data as SearchResponse;
487 this.state.searchResponse = data;
488 this.state.loading = false;
489 document.title = `${i18n.t('search')} - ${this.state.q} - ${
490 WebSocketService.Instance.site.name
492 window.scrollTo(0, 0);
493 this.setState(this.state);
494 } else if (res.op == UserOperation.CreateCommentLike) {
495 let data = res.data as CommentResponse;
496 let found: Comment = this.state.searchResponse.comments.find(
497 c => c.id === data.comment.id
499 found.score = data.comment.score;
500 found.upvotes = data.comment.upvotes;
501 found.downvotes = data.comment.downvotes;
502 if (data.comment.my_vote !== null) {
503 found.my_vote = data.comment.my_vote;
504 found.upvoteLoading = false;
505 found.downvoteLoading = false;
507 this.setState(this.state);
508 } else if (res.op == UserOperation.CreatePostLike) {
509 let data = res.data as CreatePostLikeResponse;
510 let found = this.state.searchResponse.posts.find(
511 c => c.id == data.post.id
513 found.my_vote = data.post.my_vote;
514 found.score = data.post.score;
515 found.upvotes = data.post.upvotes;
516 found.downvotes = data.post.downvotes;
517 this.setState(this.state);