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 import { PostListing } from './post-listing';
35 import { HtmlTags } from './html-tags';
36 import { UserListing } from './user-listing';
37 import { CommunityLink } from './community-link';
38 import { SortSelect } from './sort-select';
39 import { CommentNodes } from './comment-nodes';
40 import { i18n } from '../i18next';
41 import { InitialFetchRequest } from 'shared/interfaces';
43 interface SearchProps {
50 interface SearchState {
55 searchResponse: SearchResponse;
68 export class Search extends Component<any, SearchState> {
69 private isoData = setIsoData(this.context);
70 private subscription: Subscription;
71 private emptyState: SearchState = {
72 q: Search.getSearchQueryFromProps(this.props.match.params.q),
73 type_: Search.getSearchTypeFromProps(this.props.match.params.type),
74 sort: Search.getSortTypeFromProps(this.props.match.params.sort),
75 page: Search.getPageFromProps(this.props.match.params.page),
76 searchText: Search.getSearchQueryFromProps(this.props.match.params.q),
85 site: this.isoData.site_res.site_view.site,
88 static getSearchQueryFromProps(q: string): string {
89 return decodeURIComponent(q) || '';
92 static getSearchTypeFromProps(type_: string): SearchType {
93 return type_ ? routeSearchTypeToEnum(type_) : SearchType.All;
96 static getSortTypeFromProps(sort: string): SortType {
97 return sort ? routeSortTypeToEnum(sort) : SortType.TopAll;
100 static getPageFromProps(page: string): number {
101 return page ? Number(page) : 1;
104 constructor(props: any, context: any) {
105 super(props, context);
107 this.state = this.emptyState;
108 this.handleSortChange = this.handleSortChange.bind(this);
110 this.parseMessage = this.parseMessage.bind(this);
111 this.subscription = wsSubscribe(this.parseMessage);
113 // Only fetch the data if coming from another route
114 if (this.state.q != '') {
115 if (this.isoData.path == this.context.router.route.match.url) {
116 this.state.searchResponse = this.isoData.routeData[0];
117 this.state.loading = false;
124 componentWillUnmount() {
125 this.subscription.unsubscribe();
128 static getDerivedStateFromProps(props: any): SearchProps {
130 q: Search.getSearchQueryFromProps(props.match.params.q),
131 type_: Search.getSearchTypeFromProps(props.match.params.type),
132 sort: Search.getSortTypeFromProps(props.match.params.sort),
133 page: Search.getPageFromProps(props.match.params.page),
137 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
138 let pathSplit = req.path.split('/');
139 let promises: Promise<any>[] = [];
141 let form: SearchForm = {
142 q: this.getSearchQueryFromProps(pathSplit[3]),
143 type_: this.getSearchTypeFromProps(pathSplit[5]),
144 sort: this.getSortTypeFromProps(pathSplit[7]),
145 page: this.getPageFromProps(pathSplit[9]),
148 setOptionalAuth(form, req.auth);
151 promises.push(req.client.search(form));
157 componentDidUpdate(_: any, lastState: SearchState) {
159 lastState.q !== this.state.q ||
160 lastState.type_ !== this.state.type_ ||
161 lastState.sort !== this.state.sort ||
162 lastState.page !== this.state.page
164 this.setState({ loading: true, searchText: this.state.q });
169 get documentTitle(): string {
171 return `${i18n.t('search')} - ${this.state.q} - ${this.state.site.name}`;
173 return `${i18n.t('search')} - ${this.state.site.name}`;
179 <div class="container">
181 title={this.documentTitle}
182 path={this.context.router.route.match.url}
184 <h5>{i18n.t('search')}</h5>
187 {this.state.type_ == SearchType.All && this.all()}
188 {this.state.type_ == SearchType.Comments && this.comments()}
189 {this.state.type_ == SearchType.Posts && this.posts()}
190 {this.state.type_ == SearchType.Communities && this.communities()}
191 {this.state.type_ == SearchType.Users && this.users()}
192 {this.resultsCount() == 0 && <span>{i18n.t('no_results')}</span>}
202 onSubmit={linkEvent(this, this.handleSearchSubmit)}
206 class="form-control mr-2 mb-2"
207 value={this.state.searchText}
208 placeholder={`${i18n.t('search')}...`}
209 onInput={linkEvent(this, this.handleQChange)}
213 <button type="submit" class="btn btn-secondary mr-2 mb-2">
214 {this.state.loading ? (
215 <svg class="icon icon-spinner spin">
216 <use xlinkHref="#icon-spinner"></use>
219 <span>{i18n.t('search')}</span>
228 <div className="mb-2">
230 value={this.state.type_}
231 onChange={linkEvent(this, this.handleTypeChange)}
232 class="custom-select w-auto mb-2"
234 <option disabled>{i18n.t('type')}</option>
235 <option value={SearchType.All}>{i18n.t('all')}</option>
236 <option value={SearchType.Comments}>{i18n.t('comments')}</option>
237 <option value={SearchType.Posts}>{i18n.t('posts')}</option>
238 <option value={SearchType.Communities}>
239 {i18n.t('communities')}
241 <option value={SearchType.Users}>{i18n.t('users')}</option>
245 sort={this.state.sort}
246 onChange={this.handleSortChange}
257 data: CommentView | PostView | CommunityView | UserViewSafe;
260 let comments = this.state.searchResponse.comments.map(e => {
261 return { type_: 'comments', data: e, published: e.comment.published };
263 let posts = this.state.searchResponse.posts.map(e => {
264 return { type_: 'posts', data: e, published: e.post.published };
266 let communities = this.state.searchResponse.communities.map(e => {
268 type_: 'communities',
270 published: e.community.published,
273 let users = this.state.searchResponse.users.map(e => {
274 return { type_: 'users', data: e, published: e.user.published };
277 combined.push(...comments);
278 combined.push(...posts);
279 combined.push(...communities);
280 combined.push(...users);
283 if (this.state.sort == SortType.New) {
284 combined.sort((a, b) => b.published.localeCompare(a.published));
288 ((b.data as CommentView | PostView).counts.score |
289 (b.data as CommunityView).counts.subscribers |
290 (b.data as UserViewSafe).counts.comment_score) -
291 ((a.data as CommentView | PostView).counts.score |
292 (a.data as CommunityView).counts.subscribers |
293 (a.data as UserViewSafe).counts.comment_score)
302 {i.type_ == 'posts' && (
304 key={(i.data as PostView).post.id}
305 post_view={i.data as PostView}
307 enableDownvotes={this.state.site.enable_downvotes}
308 enableNsfw={this.state.site.enable_nsfw}
311 {i.type_ == 'comments' && (
313 key={(i.data as CommentView).comment.id}
314 nodes={[{ comment_view: i.data as CommentView }]}
317 enableDownvotes={this.state.site.enable_downvotes}
320 {i.type_ == 'communities' && (
321 <div>{this.communityListing(i.data as CommunityView)}</div>
323 {i.type_ == 'users' && (
324 <div>{this.userListing(i.data as UserViewSafe)}</div>
336 nodes={commentsToFlatNodes(this.state.searchResponse.comments)}
339 enableDownvotes={this.state.site.enable_downvotes}
347 {this.state.searchResponse.posts.map(post => (
353 enableDownvotes={this.state.site.enable_downvotes}
354 enableNsfw={this.state.site.enable_nsfw}
366 {this.state.searchResponse.communities.map(community => (
368 <div class="col-12">{this.communityListing(community)}</div>
375 communityListing(community_view: CommunityView) {
379 <CommunityLink community={community_view.community} />
382 ${i18n.t('number_of_subscribers', {
383 count: community_view.counts.subscribers,
390 userListing(user_view: UserViewSafe) {
393 <UserListing user={user_view.user} showApubName />
395 <span>{` - ${i18n.t('number_of_comments', {
396 count: user_view.counts.comment_count,
404 {this.state.searchResponse.users.map(user => (
406 <div class="col-12">{this.userListing(user)}</div>
416 {this.state.page > 1 && (
418 class="btn btn-secondary mr-1"
419 onClick={linkEvent(this, this.prevPage)}
425 {this.resultsCount() > 0 && (
427 class="btn btn-secondary"
428 onClick={linkEvent(this, this.nextPage)}
437 resultsCount(): number {
438 let res = this.state.searchResponse;
441 res.comments.length +
442 res.communities.length +
447 nextPage(i: Search) {
448 i.updateUrl({ page: i.state.page + 1 });
451 prevPage(i: Search) {
452 i.updateUrl({ page: i.state.page - 1 });
456 let form: SearchForm = {
458 type_: this.state.type_,
459 sort: this.state.sort,
460 page: this.state.page,
462 auth: authField(false),
465 if (this.state.q != '') {
466 WebSocketService.Instance.send(wsClient.search(form));
470 handleSortChange(val: SortType) {
471 this.updateUrl({ sort: val, page: 1 });
474 handleTypeChange(i: Search, event: any) {
476 type_: SearchType[event.target.value],
481 handleSearchSubmit(i: Search, event: any) {
482 event.preventDefault();
484 q: i.state.searchText,
485 type_: i.state.type_,
491 handleQChange(i: Search, event: any) {
492 i.setState({ searchText: event.target.value });
495 updateUrl(paramUpdates: UrlParams) {
496 const qStr = paramUpdates.q || this.state.q;
497 const qStrEncoded = encodeURIComponent(qStr);
498 const typeStr = paramUpdates.type_ || this.state.type_;
499 const sortStr = paramUpdates.sort || this.state.sort;
500 const page = paramUpdates.page || this.state.page;
501 this.props.history.push(
502 `/search/q/${qStrEncoded}/type/${typeStr}/sort/${sortStr}/page/${page}`
506 parseMessage(msg: any) {
508 let op = wsUserOp(msg);
510 toast(i18n.t(msg.error), 'danger');
512 } else if (op == UserOperation.Search) {
513 let data = wsJsonToRes<SearchResponse>(msg).data;
514 this.state.searchResponse = data;
515 this.state.loading = false;
516 window.scrollTo(0, 0);
517 this.setState(this.state);
518 } else if (op == UserOperation.CreateCommentLike) {
519 let data = wsJsonToRes<CommentResponse>(msg).data;
520 createCommentLikeRes(
522 this.state.searchResponse.comments
524 this.setState(this.state);
525 } else if (op == UserOperation.CreatePostLike) {
526 let data = wsJsonToRes<PostResponse>(msg).data;
527 createPostLikeFindRes(data.post_view, this.state.searchResponse.posts);
528 this.setState(this.state);