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 } from '../interfaces';
16 import { WebSocketService } from '../services';
20 routeSearchTypeToEnum,
22 pictshareAvatarThumbnail,
24 import { PostListing } from './post-listing';
25 import { SortSelect } from './sort-select';
26 import { CommentNodes } from './comment-nodes';
27 import { i18n } from '../i18next';
28 import { T } from 'inferno-i18next';
30 interface SearchState {
35 searchResponse: SearchResponse;
39 export class Search extends Component<any, SearchState> {
40 private subscription: Subscription;
41 private emptyState: SearchState = {
42 q: this.getSearchQueryFromProps(this.props),
43 type_: this.getSearchTypeFromProps(this.props),
44 sort: this.getSortTypeFromProps(this.props),
45 page: this.getPageFromProps(this.props),
57 getSearchQueryFromProps(props: any): string {
58 return props.match.params.q ? props.match.params.q : '';
61 getSearchTypeFromProps(props: any): SearchType {
62 return props.match.params.type
63 ? routeSearchTypeToEnum(props.match.params.type)
67 getSortTypeFromProps(props: any): SortType {
68 return props.match.params.sort
69 ? routeSortTypeToEnum(props.match.params.sort)
73 getPageFromProps(props: any): number {
74 return props.match.params.page ? Number(props.match.params.page) : 1;
77 constructor(props: any, context: any) {
78 super(props, context);
80 this.state = this.emptyState;
81 this.handleSortChange = this.handleSortChange.bind(this);
83 this.subscription = WebSocketService.Instance.subject
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">
135 <T i18nKey="search">#</T>
139 {this.state.type_ == SearchType.All && this.all()}
140 {this.state.type_ == SearchType.Comments && this.comments()}
141 {this.state.type_ == SearchType.Posts && this.posts()}
142 {this.state.type_ == SearchType.Communities && this.communities()}
143 {this.state.type_ == SearchType.Users && this.users()}
156 onSubmit={linkEvent(this, this.handleSearchSubmit)}
160 class="form-control mr-2"
162 placeholder={`${i18n.t('search')}...`}
163 onInput={linkEvent(this, this.handleQChange)}
167 <button type="submit" class="btn btn-secondary mr-2">
168 {this.state.loading ? (
169 <svg class="icon icon-spinner spin">
170 <use xlinkHref="#icon-spinner"></use>
174 <T i18nKey="search">#</T>
184 <div className="mb-2">
186 value={this.state.type_}
187 onChange={linkEvent(this, this.handleTypeChange)}
188 class="custom-select custom-select-sm w-auto"
191 <T i18nKey="type">#</T>
193 <option value={SearchType.All}>
194 <T i18nKey="all">#</T>
196 <option value={SearchType.Comments}>
197 <T i18nKey="comments">#</T>
199 <option value={SearchType.Posts}>
200 <T i18nKey="posts">#</T>
202 <option value={SearchType.Communities}>
203 <T i18nKey="communities">#</T>
205 <option value={SearchType.Users}>
206 <T i18nKey="users">#</T>
211 sort={this.state.sort}
212 onChange={this.handleSortChange}
221 let combined: Array<{
223 data: Comment | Post | Community | UserView;
225 let comments = this.state.searchResponse.comments.map(e => {
226 return { type_: 'comments', data: e };
228 let posts = this.state.searchResponse.posts.map(e => {
229 return { type_: 'posts', data: e };
231 let communities = this.state.searchResponse.communities.map(e => {
232 return { type_: 'communities', data: e };
234 let users = this.state.searchResponse.users.map(e => {
235 return { type_: 'users', data: e };
238 combined.push(...comments);
239 combined.push(...posts);
240 combined.push(...communities);
241 combined.push(...users);
244 if (this.state.sort == SortType.New) {
245 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
249 ((b.data as Comment | Post).score |
250 (b.data as Community).number_of_subscribers |
251 (b.data as UserView).comment_score) -
252 ((a.data as Comment | Post).score |
253 (a.data as Community).number_of_subscribers |
254 (a.data as UserView).comment_score)
262 {i.type_ == 'posts' && (
263 <PostListing post={i.data as Post} showCommunity viewOnly />
265 {i.type_ == 'comments' && (
267 nodes={[{ comment: i.data as Comment }]}
272 {i.type_ == 'communities' && (
275 <Link to={`/c/${(i.data as Community).name}`}>{`/c/${
276 (i.data as Community).name
279 <span>{` - ${(i.data as Community).title} - ${
280 (i.data as Community).number_of_subscribers
281 } subscribers`}</span>
284 {i.type_ == 'users' && (
288 className="text-info"
289 to={`/u/${(i.data as UserView).name}`}
291 {(i.data as UserView).avatar && (
295 src={pictshareAvatarThumbnail(
296 (i.data as UserView).avatar
298 class="rounded-circle mr-1"
301 <span>{`/u/${(i.data as UserView).name}`}</span>
305 (i.data as UserView).comment_score
306 } comment karma`}</span>
318 {this.state.searchResponse.comments.map(comment => (
319 <CommentNodes nodes={[{ comment: comment }]} noIndent viewOnly />
328 {this.state.searchResponse.posts.map(post => (
329 <PostListing post={post} showCommunity viewOnly />
335 // Todo possibly create UserListing and CommunityListing
339 {this.state.searchResponse.communities.map(community => (
342 <Link to={`/c/${community.name}`}>{`/c/${community.name}`}</Link>
344 <span>{` - ${community.title} - ${community.number_of_subscribers} subscribers`}</span>
354 {this.state.searchResponse.users.map(user => (
358 className="text-info"
359 to={`/u/${user.name}`}
360 >{`/u/${user.name}`}</Link>
362 <span>{` - ${user.comment_score} comment karma`}</span>
372 {this.state.page > 1 && (
374 class="btn btn-sm btn-secondary mr-1"
375 onClick={linkEvent(this, this.prevPage)}
377 <T i18nKey="prev">#</T>
381 class="btn btn-sm btn-secondary"
382 onClick={linkEvent(this, this.nextPage)}
384 <T i18nKey="next">#</T>
391 let res = this.state.searchResponse;
396 res.posts.length == 0 &&
397 res.comments.length == 0 &&
398 res.communities.length == 0 &&
399 res.users.length == 0 && (
401 <T i18nKey="no_results">#</T>
408 nextPage(i: Search) {
415 prevPage(i: Search) {
423 let form: SearchForm = {
425 type_: SearchType[this.state.type_],
426 sort: SortType[this.state.sort],
427 page: this.state.page,
431 if (this.state.q != '') {
432 WebSocketService.Instance.search(form);
436 handleSortChange(val: SortType) {
437 this.state.sort = val;
439 this.setState(this.state);
443 handleTypeChange(i: Search, event: any) {
444 i.state.type_ = Number(event.target.value);
450 handleSearchSubmit(i: Search, event: any) {
451 event.preventDefault();
452 i.state.loading = true;
458 handleQChange(i: Search, event: any) {
459 i.state.q = event.target.value;
464 let typeStr = SearchType[this.state.type_].toLowerCase();
465 let sortStr = SortType[this.state.sort].toLowerCase();
466 this.props.history.push(
467 `/search/q/${this.state.q}/type/${typeStr}/sort/${sortStr}/page/${this.state.page}`
471 parseMessage(msg: any) {
473 let op: UserOperation = msgOp(msg);
475 alert(i18n.t(msg.error));
477 } else if (op == UserOperation.Search) {
478 let res: SearchResponse = msg;
479 this.state.searchResponse = res;
480 this.state.loading = false;
481 document.title = `${i18n.t('search')} - ${this.state.q} - ${
482 WebSocketService.Instance.site.name
484 window.scrollTo(0, 0);
485 this.setState(this.state);