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),
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
84 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
86 msg => this.parseMessage(msg),
87 err => console.error(err),
88 () => console.log('complete')
96 componentWillUnmount() {
97 this.subscription.unsubscribe();
100 // Necessary for back button for some reason
101 componentWillReceiveProps(nextProps: any) {
103 nextProps.history.action == 'POP' ||
104 nextProps.history.action == 'PUSH'
106 this.state = this.emptyState;
107 this.state.q = this.getSearchQueryFromProps(nextProps);
108 this.state.type_ = this.getSearchTypeFromProps(nextProps);
109 this.state.sort = this.getSortTypeFromProps(nextProps);
110 this.state.page = this.getPageFromProps(nextProps);
111 this.setState(this.state);
116 componentDidMount() {
117 document.title = `${i18n.t('search')} - ${
118 WebSocketService.Instance.site.name
124 <div class="container">
128 <T i18nKey="search">#</T>
132 {this.state.type_ == SearchType.All && this.all()}
133 {this.state.type_ == SearchType.Comments && this.comments()}
134 {this.state.type_ == SearchType.Posts && this.posts()}
135 {this.state.type_ == SearchType.Communities && this.communities()}
136 {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)
255 {i.type_ == 'posts' && (
256 <PostListing post={i.data as Post} showCommunity viewOnly />
258 {i.type_ == 'comments' && (
260 nodes={[{ comment: i.data as Comment }]}
265 {i.type_ == 'communities' && (
268 <Link to={`/c/${(i.data as Community).name}`}>{`/c/${
269 (i.data as Community).name
272 <span>{` - ${(i.data as Community).title} - ${
273 (i.data as Community).number_of_subscribers
274 } subscribers`}</span>
277 {i.type_ == 'users' && (
281 className="text-info"
282 to={`/u/${(i.data as UserView).name}`}
284 {(i.data as UserView).avatar && showAvatars() && (
288 src={pictshareAvatarThumbnail(
289 (i.data as UserView).avatar
291 class="rounded-circle mr-1"
294 <span>{`/u/${(i.data as UserView).name}`}</span>
298 (i.data as UserView).comment_score
299 } comment karma`}</span>
311 {this.state.searchResponse.comments.map(comment => (
312 <CommentNodes nodes={[{ comment: comment }]} locked noIndent />
321 {this.state.searchResponse.posts.map(post => (
322 <PostListing post={post} showCommunity viewOnly />
328 // Todo possibly create UserListing and CommunityListing
332 {this.state.searchResponse.communities.map(community => (
335 <Link to={`/c/${community.name}`}>{`/c/${community.name}`}</Link>
337 <span>{` - ${community.title} - ${community.number_of_subscribers} subscribers`}</span>
347 {this.state.searchResponse.users.map(user => (
351 className="text-info"
352 to={`/u/${user.name}`}
353 >{`/u/${user.name}`}</Link>
355 <span>{` - ${user.comment_score} comment karma`}</span>
365 {this.state.page > 1 && (
367 class="btn btn-sm btn-secondary mr-1"
368 onClick={linkEvent(this, this.prevPage)}
370 <T i18nKey="prev">#</T>
374 class="btn btn-sm btn-secondary"
375 onClick={linkEvent(this, this.nextPage)}
377 <T i18nKey="next">#</T>
384 let res = this.state.searchResponse;
388 res.posts.length == 0 &&
389 res.comments.length == 0 &&
390 res.communities.length == 0 &&
391 res.users.length == 0 && (
393 <T i18nKey="no_results">#</T>
400 nextPage(i: Search) {
407 prevPage(i: Search) {
415 let form: SearchForm = {
417 type_: SearchType[this.state.type_],
418 sort: SortType[this.state.sort],
419 page: this.state.page,
423 if (this.state.q != '') {
424 WebSocketService.Instance.search(form);
428 handleSortChange(val: SortType) {
429 this.state.sort = val;
431 this.setState(this.state);
435 handleTypeChange(i: Search, event: any) {
436 i.state.type_ = Number(event.target.value);
442 handleSearchSubmit(i: Search, event: any) {
443 event.preventDefault();
444 i.state.loading = true;
450 handleQChange(i: Search, event: any) {
451 i.state.q = event.target.value;
456 let typeStr = SearchType[this.state.type_].toLowerCase();
457 let sortStr = SortType[this.state.sort].toLowerCase();
458 this.props.history.push(
459 `/search/q/${this.state.q}/type/${typeStr}/sort/${sortStr}/page/${this.state.page}`
463 parseMessage(msg: any) {
465 let res = wsJsonToRes(msg);
467 alert(i18n.t(res.error));
469 } else if (res.op == UserOperation.Search) {
470 let data = res.data as SearchResponse;
471 this.state.searchResponse = data;
472 this.state.loading = false;
473 document.title = `${i18n.t('search')} - ${this.state.q} - ${
474 WebSocketService.Instance.site.name
476 window.scrollTo(0, 0);
477 this.setState(this.state);