1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from "rxjs";
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { UserOperation, Post, Comment, SortType, SearchForm, SearchResponse, SearchType } from '../interfaces';
5 import { WebSocketService } from '../services';
6 import { msgOp, fetchLimit } from '../utils';
7 import { PostListing } from './post-listing';
8 import { CommentNodes } from './comment-nodes';
10 interface SearchState {
15 searchResponse: SearchResponse;
19 export class Search extends Component<any, SearchState> {
21 private subscription: Subscription;
22 private emptyState: SearchState = {
24 type_: SearchType.Both,
25 sort: SortType.TopAll,
35 constructor(props: any, context: any) {
36 super(props, context);
38 this.state = this.emptyState;
40 this.subscription = WebSocketService.Instance.subject
41 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
43 (msg) => this.parseMessage(msg),
44 (err) => console.error(err),
45 () => console.log('complete')
50 componentWillUnmount() {
51 this.subscription.unsubscribe();
55 document.title = "Search - Lemmy";
60 <div class="container">
66 {this.state.type_ == SearchType.Both &&
69 {this.state.type_ == SearchType.Comments &&
72 {this.state.type_ == SearchType.Posts &&
85 <form class="form-inline" onSubmit={linkEvent(this, this.handleSearchSubmit)}>
86 <input type="text" class="form-control mr-2" value={this.state.q} placeholder="Search..." onInput={linkEvent(this, this.handleQChange)} required minLength={3} />
87 <button type="submit" class="btn btn-secondary mr-2">
89 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> :
99 <div className="mb-2">
100 <select value={this.state.type_} onChange={linkEvent(this, this.handleTypeChange)} class="custom-select custom-select-sm w-auto">
101 <option disabled>Type</option>
102 <option value={SearchType.Both}>Both</option>
103 <option value={SearchType.Comments}>Comments</option>
104 <option value={SearchType.Posts}>Posts</option>
106 <select value={this.state.sort} onChange={linkEvent(this, this.handleSortChange)} class="custom-select custom-select-sm w-auto ml-2">
107 <option disabled>Sort Type</option>
108 <option value={SortType.New}>New</option>
109 <option value={SortType.TopDay}>Top Day</option>
110 <option value={SortType.TopWeek}>Week</option>
111 <option value={SortType.TopMonth}>Month</option>
112 <option value={SortType.TopYear}>Year</option>
113 <option value={SortType.TopAll}>All</option>
121 let combined: Array<{type_: string, data: Comment | Post}> = [];
122 let comments = this.state.searchResponse.comments.map(e => {return {type_: "comments", data: e}});
123 let posts = this.state.searchResponse.posts.map(e => {return {type_: "posts", data: e}});
125 combined.push(...comments);
126 combined.push(...posts);
129 if (this.state.sort == SortType.New) {
130 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
132 combined.sort((a, b) => b.data.score - a.data.score);
140 ? <PostListing post={i.data as Post} showCommunity viewOnly />
141 : <CommentNodes nodes={[{comment: i.data as Comment}]} viewOnly noIndent />
153 {this.state.searchResponse.comments.map(comment =>
154 <CommentNodes nodes={[{comment: comment}]} noIndent viewOnly />
163 {this.state.searchResponse.posts.map(post =>
164 <PostListing post={post} showCommunity viewOnly />
173 {this.state.page > 1 &&
174 <button class="btn btn-sm btn-secondary mr-1" onClick={linkEvent(this, this.prevPage)}>Prev</button>
176 <button class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.nextPage)}>Next</button>
182 let res = this.state.searchResponse;
185 {res && res.op && res.posts.length == 0 && res.comments.length == 0 &&
186 <span>No Results</span>
192 nextPage(i: Search) {
198 prevPage(i: Search) {
206 let form: SearchForm = {
208 type_: SearchType[this.state.type_],
209 sort: SortType[this.state.sort],
210 page: this.state.page,
214 WebSocketService.Instance.search(form);
217 handleSortChange(i: Search, event: any) {
218 i.state.sort = Number(event.target.value);
224 handleTypeChange(i: Search, event: any) {
225 i.state.type_ = Number(event.target.value);
231 handleSearchSubmit(i: Search, event: any) {
232 event.preventDefault();
233 i.state.loading = true;
238 handleQChange(i: Search, event: any) {
239 i.state.q = event.target.value;
243 parseMessage(msg: any) {
245 let op: UserOperation = msgOp(msg);
249 } else if (op == UserOperation.Search) {
250 let res: SearchResponse = msg;
251 this.state.searchResponse = res;
252 this.state.loading = false;
253 document.title = `Search - ${this.state.q} - Lemmy`;
254 window.scrollTo(0,0);
255 this.setState(this.state);