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,
25 import { PostListing } from './post-listing';
26 import { SortSelect } from './sort-select';
27 import { CommentNodes } from './comment-nodes';
28 import { i18n } from '../i18next';
29 import { T } from 'inferno-i18next';
31 interface SearchState {
36 searchResponse: SearchResponse;
40 export class Search extends Component<any, SearchState> {
41 private subscription: Subscription;
42 private emptyState: SearchState = {
43 q: this.getSearchQueryFromProps(this.props),
44 type_: this.getSearchTypeFromProps(this.props),
45 sort: this.getSortTypeFromProps(this.props),
46 page: this.getPageFromProps(this.props),
58 getSearchQueryFromProps(props: any): string {
59 return props.match.params.q ? props.match.params.q : '';
62 getSearchTypeFromProps(props: any): SearchType {
63 return props.match.params.type
64 ? routeSearchTypeToEnum(props.match.params.type)
68 getSortTypeFromProps(props: any): SortType {
69 return props.match.params.sort
70 ? routeSortTypeToEnum(props.match.params.sort)
74 getPageFromProps(props: any): number {
75 return props.match.params.page ? Number(props.match.params.page) : 1;
78 constructor(props: any, context: any) {
79 super(props, context);
81 this.state = this.emptyState;
82 this.handleSortChange = this.handleSortChange.bind(this);
84 this.subscription = WebSocketService.Instance.subject
94 msg => this.parseMessage(msg),
95 err => console.error(err),
96 () => console.log('complete')
104 componentWillUnmount() {
105 this.subscription.unsubscribe();
108 // Necessary for back button for some reason
109 componentWillReceiveProps(nextProps: any) {
111 nextProps.history.action == 'POP' ||
112 nextProps.history.action == 'PUSH'
114 this.state = this.emptyState;
115 this.state.q = this.getSearchQueryFromProps(nextProps);
116 this.state.type_ = this.getSearchTypeFromProps(nextProps);
117 this.state.sort = this.getSortTypeFromProps(nextProps);
118 this.state.page = this.getPageFromProps(nextProps);
119 this.setState(this.state);
124 componentDidMount() {
125 document.title = `${i18n.t('search')} - ${
126 WebSocketService.Instance.site.name
132 <div class="container">
136 <T i18nKey="search">#</T>
140 {this.state.type_ == SearchType.All && this.all()}
141 {this.state.type_ == SearchType.Comments && this.comments()}
142 {this.state.type_ == SearchType.Posts && this.posts()}
143 {this.state.type_ == SearchType.Communities && this.communities()}
144 {this.state.type_ == SearchType.Users && this.users()}
157 onSubmit={linkEvent(this, this.handleSearchSubmit)}
161 class="form-control mr-2"
163 placeholder={`${i18n.t('search')}...`}
164 onInput={linkEvent(this, this.handleQChange)}
168 <button type="submit" class="btn btn-secondary mr-2">
169 {this.state.loading ? (
170 <svg class="icon icon-spinner spin">
171 <use xlinkHref="#icon-spinner"></use>
175 <T i18nKey="search">#</T>
185 <div className="mb-2">
187 value={this.state.type_}
188 onChange={linkEvent(this, this.handleTypeChange)}
189 class="custom-select custom-select-sm w-auto"
192 <T i18nKey="type">#</T>
194 <option value={SearchType.All}>
195 <T i18nKey="all">#</T>
197 <option value={SearchType.Comments}>
198 <T i18nKey="comments">#</T>
200 <option value={SearchType.Posts}>
201 <T i18nKey="posts">#</T>
203 <option value={SearchType.Communities}>
204 <T i18nKey="communities">#</T>
206 <option value={SearchType.Users}>
207 <T i18nKey="users">#</T>
212 sort={this.state.sort}
213 onChange={this.handleSortChange}
222 let combined: Array<{
224 data: Comment | Post | Community | UserView;
226 let comments = this.state.searchResponse.comments.map(e => {
227 return { type_: 'comments', data: e };
229 let posts = this.state.searchResponse.posts.map(e => {
230 return { type_: 'posts', data: e };
232 let communities = this.state.searchResponse.communities.map(e => {
233 return { type_: 'communities', data: e };
235 let users = this.state.searchResponse.users.map(e => {
236 return { type_: 'users', data: e };
239 combined.push(...comments);
240 combined.push(...posts);
241 combined.push(...communities);
242 combined.push(...users);
245 if (this.state.sort == SortType.New) {
246 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
250 ((b.data as Comment | Post).score |
251 (b.data as Community).number_of_subscribers |
252 (b.data as UserView).comment_score) -
253 ((a.data as Comment | Post).score |
254 (a.data as Community).number_of_subscribers |
255 (a.data as UserView).comment_score)
263 {i.type_ == 'posts' && (
264 <PostListing post={i.data as Post} showCommunity viewOnly />
266 {i.type_ == 'comments' && (
268 nodes={[{ comment: i.data as Comment }]}
273 {i.type_ == 'communities' && (
276 <Link to={`/c/${(i.data as Community).name}`}>{`/c/${
277 (i.data as Community).name
280 <span>{` - ${(i.data as Community).title} - ${
281 (i.data as Community).number_of_subscribers
282 } subscribers`}</span>
285 {i.type_ == 'users' && (
289 className="text-info"
290 to={`/u/${(i.data as UserView).name}`}
292 {(i.data as UserView).avatar && showAvatars() && (
296 src={pictshareAvatarThumbnail(
297 (i.data as UserView).avatar
299 class="rounded-circle mr-1"
302 <span>{`/u/${(i.data as UserView).name}`}</span>
306 (i.data as UserView).comment_score
307 } comment karma`}</span>
319 {this.state.searchResponse.comments.map(comment => (
320 <CommentNodes nodes={[{ comment: comment }]} noIndent viewOnly />
329 {this.state.searchResponse.posts.map(post => (
330 <PostListing post={post} showCommunity viewOnly />
336 // Todo possibly create UserListing and CommunityListing
340 {this.state.searchResponse.communities.map(community => (
343 <Link to={`/c/${community.name}`}>{`/c/${community.name}`}</Link>
345 <span>{` - ${community.title} - ${community.number_of_subscribers} subscribers`}</span>
355 {this.state.searchResponse.users.map(user => (
359 className="text-info"
360 to={`/u/${user.name}`}
361 >{`/u/${user.name}`}</Link>
363 <span>{` - ${user.comment_score} comment karma`}</span>
373 {this.state.page > 1 && (
375 class="btn btn-sm btn-secondary mr-1"
376 onClick={linkEvent(this, this.prevPage)}
378 <T i18nKey="prev">#</T>
382 class="btn btn-sm btn-secondary"
383 onClick={linkEvent(this, this.nextPage)}
385 <T i18nKey="next">#</T>
392 let res = this.state.searchResponse;
397 res.posts.length == 0 &&
398 res.comments.length == 0 &&
399 res.communities.length == 0 &&
400 res.users.length == 0 && (
402 <T i18nKey="no_results">#</T>
409 nextPage(i: Search) {
416 prevPage(i: Search) {
424 let form: SearchForm = {
426 type_: SearchType[this.state.type_],
427 sort: SortType[this.state.sort],
428 page: this.state.page,
432 if (this.state.q != '') {
433 WebSocketService.Instance.search(form);
437 handleSortChange(val: SortType) {
438 this.state.sort = val;
440 this.setState(this.state);
444 handleTypeChange(i: Search, event: any) {
445 i.state.type_ = Number(event.target.value);
451 handleSearchSubmit(i: Search, event: any) {
452 event.preventDefault();
453 i.state.loading = true;
459 handleQChange(i: Search, event: any) {
460 i.state.q = event.target.value;
465 let typeStr = SearchType[this.state.type_].toLowerCase();
466 let sortStr = SortType[this.state.sort].toLowerCase();
467 this.props.history.push(
468 `/search/q/${this.state.q}/type/${typeStr}/sort/${sortStr}/page/${this.state.page}`
472 parseMessage(msg: any) {
474 let op: UserOperation = msgOp(msg);
476 alert(i18n.t(msg.error));
478 } else if (op == UserOperation.Search) {
479 let res: SearchResponse = msg;
480 this.state.searchResponse = res;
481 this.state.loading = false;
482 document.title = `${i18n.t('search')} - ${this.state.q} - ${
483 WebSocketService.Instance.site.name
485 window.scrollTo(0, 0);
486 this.setState(this.state);