1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
16 } from 'lemmy-js-client';
17 import { WebSocketService } from '../services';
21 routeSearchTypeToEnum,
25 createPostLikeFindRes,
34 restoreScrollPosition,
36 import { PostListing } from './post-listing';
37 import { HtmlTags } from './html-tags';
38 import { UserListing } from './user-listing';
39 import { CommunityLink } from './community-link';
40 import { SortSelect } from './sort-select';
41 import { CommentNodes } from './comment-nodes';
42 import { i18n } from '../i18next';
43 import { InitialFetchRequest } from 'shared/interfaces';
45 interface SearchProps {
52 interface SearchState {
57 searchResponse: SearchResponse;
70 export class Search extends Component<any, SearchState> {
71 private isoData = setIsoData(this.context);
72 private subscription: Subscription;
73 private emptyState: SearchState = {
74 q: Search.getSearchQueryFromProps(this.props.match.params.q),
75 type_: Search.getSearchTypeFromProps(this.props.match.params.type),
76 sort: Search.getSortTypeFromProps(this.props.match.params.sort),
77 page: Search.getPageFromProps(this.props.match.params.page),
78 searchText: Search.getSearchQueryFromProps(this.props.match.params.q),
87 site: this.isoData.site_res.site_view.site,
90 static getSearchQueryFromProps(q: string): string {
91 return decodeURIComponent(q) || '';
94 static getSearchTypeFromProps(type_: string): SearchType {
95 return type_ ? routeSearchTypeToEnum(type_) : SearchType.All;
98 static getSortTypeFromProps(sort: string): SortType {
99 return sort ? routeSortTypeToEnum(sort) : SortType.TopAll;
102 static getPageFromProps(page: string): number {
103 return page ? Number(page) : 1;
106 constructor(props: any, context: any) {
107 super(props, context);
109 this.state = this.emptyState;
110 this.handleSortChange = this.handleSortChange.bind(this);
112 this.parseMessage = this.parseMessage.bind(this);
113 this.subscription = wsSubscribe(this.parseMessage);
115 // Only fetch the data if coming from another route
116 if (this.state.q != '') {
117 if (this.isoData.path == this.context.router.route.match.url) {
118 this.state.searchResponse = this.isoData.routeData[0];
119 this.state.loading = false;
126 componentWillUnmount() {
127 this.subscription.unsubscribe();
128 saveScrollPosition(this.context);
131 static getDerivedStateFromProps(props: any): SearchProps {
133 q: Search.getSearchQueryFromProps(props.match.params.q),
134 type_: Search.getSearchTypeFromProps(props.match.params.type),
135 sort: Search.getSortTypeFromProps(props.match.params.sort),
136 page: Search.getPageFromProps(props.match.params.page),
140 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
141 let pathSplit = req.path.split('/');
142 let promises: Promise<any>[] = [];
144 let form: SearchForm = {
145 q: this.getSearchQueryFromProps(pathSplit[3]),
146 type_: this.getSearchTypeFromProps(pathSplit[5]),
147 sort: this.getSortTypeFromProps(pathSplit[7]),
148 page: this.getPageFromProps(pathSplit[9]),
151 setOptionalAuth(form, req.auth);
154 promises.push(req.client.search(form));
160 componentDidUpdate(_: any, lastState: SearchState) {
162 lastState.q !== this.state.q ||
163 lastState.type_ !== this.state.type_ ||
164 lastState.sort !== this.state.sort ||
165 lastState.page !== this.state.page
167 this.setState({ loading: true, searchText: this.state.q });
172 get documentTitle(): string {
174 return `${i18n.t('search')} - ${this.state.q} - ${this.state.site.name}`;
176 return `${i18n.t('search')} - ${this.state.site.name}`;
182 <div class="container">
184 title={this.documentTitle}
185 path={this.context.router.route.match.url}
187 <h5>{i18n.t('search')}</h5>
190 {this.state.type_ == SearchType.All && this.all()}
191 {this.state.type_ == SearchType.Comments && this.comments()}
192 {this.state.type_ == SearchType.Posts && this.posts()}
193 {this.state.type_ == SearchType.Communities && this.communities()}
194 {this.state.type_ == SearchType.Users && this.users()}
195 {this.resultsCount() == 0 && <span>{i18n.t('no_results')}</span>}
205 onSubmit={linkEvent(this, this.handleSearchSubmit)}
209 class="form-control mr-2 mb-2"
210 value={this.state.searchText}
211 placeholder={`${i18n.t('search')}...`}
212 aria-label={i18n.t('search')}
213 onInput={linkEvent(this, this.handleQChange)}
217 <button type="submit" class="btn btn-secondary mr-2 mb-2">
218 {this.state.loading ? (
219 <svg class="icon icon-spinner spin">
220 <use xlinkHref="#icon-spinner"></use>
223 <span>{i18n.t('search')}</span>
232 <div className="mb-2">
234 value={this.state.type_}
235 onChange={linkEvent(this, this.handleTypeChange)}
236 class="custom-select w-auto mb-2"
237 aria-label={i18n.t('type')}
239 <option disabled aria-hidden="true">
242 <option value={SearchType.All}>{i18n.t('all')}</option>
243 <option value={SearchType.Comments}>{i18n.t('comments')}</option>
244 <option value={SearchType.Posts}>{i18n.t('posts')}</option>
245 <option value={SearchType.Communities}>
246 {i18n.t('communities')}
248 <option value={SearchType.Users}>{i18n.t('users')}</option>
252 sort={this.state.sort}
253 onChange={this.handleSortChange}
265 data: CommentView | PostView | CommunityView | UserViewSafe;
268 let comments = this.state.searchResponse.comments.map(e => {
269 return { type_: 'comments', data: e, published: e.comment.published };
271 let posts = this.state.searchResponse.posts.map(e => {
272 return { type_: 'posts', data: e, published: e.post.published };
274 let communities = this.state.searchResponse.communities.map(e => {
276 type_: 'communities',
278 published: e.community.published,
281 let users = this.state.searchResponse.users.map(e => {
282 return { type_: 'users', data: e, published: e.user.published };
285 combined.push(...comments);
286 combined.push(...posts);
287 combined.push(...communities);
288 combined.push(...users);
291 if (this.state.sort == SortType.New) {
292 combined.sort((a, b) => b.published.localeCompare(a.published));
296 ((b.data as CommentView | PostView).counts.score |
297 (b.data as CommunityView).counts.subscribers |
298 (b.data as UserViewSafe).counts.comment_score) -
299 ((a.data as CommentView | PostView).counts.score |
300 (a.data as CommunityView).counts.subscribers |
301 (a.data as UserViewSafe).counts.comment_score)
310 {i.type_ == 'posts' && (
312 key={(i.data as PostView).post.id}
313 post_view={i.data as PostView}
315 enableDownvotes={this.state.site.enable_downvotes}
316 enableNsfw={this.state.site.enable_nsfw}
319 {i.type_ == 'comments' && (
321 key={(i.data as CommentView).comment.id}
322 nodes={[{ comment_view: i.data as CommentView }]}
325 enableDownvotes={this.state.site.enable_downvotes}
328 {i.type_ == 'communities' && (
329 <div>{this.communityListing(i.data as CommunityView)}</div>
331 {i.type_ == 'users' && (
332 <div>{this.userListing(i.data as UserViewSafe)}</div>
344 nodes={commentsToFlatNodes(this.state.searchResponse.comments)}
347 enableDownvotes={this.state.site.enable_downvotes}
355 {this.state.searchResponse.posts.map(post => (
361 enableDownvotes={this.state.site.enable_downvotes}
362 enableNsfw={this.state.site.enable_nsfw}
374 {this.state.searchResponse.communities.map(community => (
376 <div class="col-12">{this.communityListing(community)}</div>
383 communityListing(community_view: CommunityView) {
387 <CommunityLink community={community_view.community} />
390 ${i18n.t('number_of_subscribers', {
391 count: community_view.counts.subscribers,
398 userListing(user_view: UserViewSafe) {
401 <UserListing user={user_view.user} showApubName />
403 <span>{` - ${i18n.t('number_of_comments', {
404 count: user_view.counts.comment_count,
412 {this.state.searchResponse.users.map(user => (
414 <div class="col-12">{this.userListing(user)}</div>
424 {this.state.page > 1 && (
426 class="btn btn-secondary mr-1"
427 onClick={linkEvent(this, this.prevPage)}
433 {this.resultsCount() > 0 && (
435 class="btn btn-secondary"
436 onClick={linkEvent(this, this.nextPage)}
445 resultsCount(): number {
446 let res = this.state.searchResponse;
449 res.comments.length +
450 res.communities.length +
455 nextPage(i: Search) {
456 i.updateUrl({ page: i.state.page + 1 });
459 prevPage(i: Search) {
460 i.updateUrl({ page: i.state.page - 1 });
464 let form: SearchForm = {
466 type_: this.state.type_,
467 sort: this.state.sort,
468 page: this.state.page,
470 auth: authField(false),
473 if (this.state.q != '') {
474 WebSocketService.Instance.send(wsClient.search(form));
478 handleSortChange(val: SortType) {
479 this.updateUrl({ sort: val, page: 1 });
482 handleTypeChange(i: Search, event: any) {
484 type_: SearchType[event.target.value],
489 handleSearchSubmit(i: Search, event: any) {
490 event.preventDefault();
492 q: i.state.searchText,
493 type_: i.state.type_,
499 handleQChange(i: Search, event: any) {
500 i.setState({ searchText: event.target.value });
503 updateUrl(paramUpdates: UrlParams) {
504 const qStr = paramUpdates.q || this.state.q;
505 const qStrEncoded = encodeURIComponent(qStr);
506 const typeStr = paramUpdates.type_ || this.state.type_;
507 const sortStr = paramUpdates.sort || this.state.sort;
508 const page = paramUpdates.page || this.state.page;
509 this.props.history.push(
510 `/search/q/${qStrEncoded}/type/${typeStr}/sort/${sortStr}/page/${page}`
514 parseMessage(msg: any) {
516 let op = wsUserOp(msg);
518 toast(i18n.t(msg.error), 'danger');
520 } else if (op == UserOperation.Search) {
521 let data = wsJsonToRes<SearchResponse>(msg).data;
522 this.state.searchResponse = data;
523 this.state.loading = false;
524 window.scrollTo(0, 0);
525 this.setState(this.state);
526 restoreScrollPosition(this.context);
527 } else if (op == UserOperation.CreateCommentLike) {
528 let data = wsJsonToRes<CommentResponse>(msg).data;
529 createCommentLikeRes(
531 this.state.searchResponse.comments
533 this.setState(this.state);
534 } else if (op == UserOperation.CreatePostLike) {
535 let data = wsJsonToRes<PostResponse>(msg).data;
536 createPostLikeFindRes(data.post_view, this.state.searchResponse.posts);
537 this.setState(this.state);