2023-04-15 14:47:10 +00:00
|
|
|
import type { NoOptionI18nKeys } from "i18next";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
CommentResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
CommentView,
|
|
|
|
CommunityView,
|
2021-07-17 20:42:55 +00:00
|
|
|
GetCommunity,
|
2022-06-21 21:42:29 +00:00
|
|
|
GetCommunityResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
GetPersonDetails,
|
2022-06-21 21:42:29 +00:00
|
|
|
GetPersonDetailsResponse,
|
|
|
|
GetSiteResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
ListCommunities,
|
|
|
|
ListCommunitiesResponse,
|
|
|
|
ListingType,
|
2023-05-11 18:32:32 +00:00
|
|
|
PersonView,
|
2021-07-17 20:42:55 +00:00
|
|
|
PostResponse,
|
|
|
|
PostView,
|
2021-08-23 20:18:11 +00:00
|
|
|
ResolveObject,
|
|
|
|
ResolveObjectResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
Search as SearchForm,
|
2020-09-06 16:15:25 +00:00
|
|
|
SearchResponse,
|
|
|
|
SearchType,
|
2021-07-17 20:42:55 +00:00
|
|
|
SortType,
|
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { i18n } from "../i18next";
|
2022-07-30 13:28:08 +00:00
|
|
|
import { CommentViewType, InitialFetchRequest } from "../interfaces";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { WebSocketService } from "../services";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2023-04-15 14:47:10 +00:00
|
|
|
Choice,
|
2023-05-11 17:06:32 +00:00
|
|
|
QueryParams,
|
|
|
|
capitalizeFirstLetter,
|
2021-07-17 20:42:55 +00:00
|
|
|
commentsToFlatNodes,
|
|
|
|
communityToChoice,
|
|
|
|
createCommentLikeRes,
|
|
|
|
createPostLikeFindRes,
|
2021-04-23 21:48:31 +00:00
|
|
|
debounce,
|
2022-06-21 21:42:29 +00:00
|
|
|
enableDownvotes,
|
|
|
|
enableNsfw,
|
2021-04-23 21:48:31 +00:00
|
|
|
fetchCommunities,
|
2021-07-17 20:42:55 +00:00
|
|
|
fetchLimit,
|
2021-04-23 21:48:31 +00:00
|
|
|
fetchUsers,
|
2023-04-15 14:47:10 +00:00
|
|
|
getIdFromString,
|
|
|
|
getPageFromString,
|
|
|
|
getQueryParams,
|
|
|
|
getQueryString,
|
|
|
|
getUpdatedSearchId,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
2021-09-18 16:35:49 +00:00
|
|
|
numToSI,
|
2021-07-17 20:42:55 +00:00
|
|
|
personToChoice,
|
|
|
|
restoreScrollPosition,
|
|
|
|
saveScrollPosition,
|
|
|
|
setIsoData,
|
|
|
|
showLocal,
|
|
|
|
toast,
|
|
|
|
wsClient,
|
|
|
|
wsSubscribe,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "../utils";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { CommentNodes } from "./comment/comment-nodes";
|
|
|
|
import { HtmlTags } from "./common/html-tags";
|
|
|
|
import { Spinner } from "./common/icon";
|
|
|
|
import { ListingTypeSelect } from "./common/listing-type-select";
|
|
|
|
import { Paginator } from "./common/paginator";
|
2023-04-15 14:47:10 +00:00
|
|
|
import { SearchableSelect } from "./common/searchable-select";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { SortSelect } from "./common/sort-select";
|
|
|
|
import { CommunityLink } from "./community/community-link";
|
|
|
|
import { PersonListing } from "./person/person-listing";
|
|
|
|
import { PostListing } from "./post/post-listing";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-09 02:40:36 +00:00
|
|
|
interface SearchProps {
|
2023-02-21 20:52:12 +00:00
|
|
|
q?: string;
|
2023-04-15 14:47:10 +00:00
|
|
|
type: SearchType;
|
2020-09-06 16:15:25 +00:00
|
|
|
sort: SortType;
|
2021-04-23 21:48:31 +00:00
|
|
|
listingType: ListingType;
|
2023-04-15 14:47:10 +00:00
|
|
|
communityId?: number | null;
|
|
|
|
creatorId?: number | null;
|
2023-05-11 18:32:32 +00:00
|
|
|
page: bigint;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
type FilterType = "creator" | "community";
|
|
|
|
|
2020-09-09 02:40:36 +00:00
|
|
|
interface SearchState {
|
2023-01-04 16:56:24 +00:00
|
|
|
searchResponse?: SearchResponse;
|
2021-04-23 21:48:31 +00:00
|
|
|
communities: CommunityView[];
|
2023-01-04 16:56:24 +00:00
|
|
|
creatorDetails?: GetPersonDetailsResponse;
|
2023-04-15 14:47:10 +00:00
|
|
|
searchLoading: boolean;
|
|
|
|
searchCommunitiesLoading: boolean;
|
|
|
|
searchCreatorLoading: boolean;
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2023-02-21 20:52:12 +00:00
|
|
|
searchText?: string;
|
2023-01-04 16:56:24 +00:00
|
|
|
resolveObjectResponse?: ResolveObjectResponse;
|
2023-04-15 14:47:10 +00:00
|
|
|
communitySearchOptions: Choice[];
|
|
|
|
creatorSearchOptions: Choice[];
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 20:18:11 +00:00
|
|
|
interface Combined {
|
|
|
|
type_: string;
|
2023-05-11 18:32:32 +00:00
|
|
|
data: CommentView | PostView | CommunityView | PersonView;
|
2021-08-23 20:18:11 +00:00
|
|
|
published: string;
|
|
|
|
}
|
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
const defaultSearchType = "All";
|
|
|
|
const defaultSortType = "TopAll";
|
|
|
|
const defaultListingType = "All";
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
const searchTypes = ["All", "Comments", "Posts", "Communities", "Users", "Url"];
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
const getSearchQueryParams = () =>
|
|
|
|
getQueryParams<SearchProps>({
|
|
|
|
q: getSearchQueryFromQuery,
|
|
|
|
type: getSearchTypeFromQuery,
|
|
|
|
sort: getSortTypeFromQuery,
|
|
|
|
listingType: getListingTypeFromQuery,
|
|
|
|
communityId: getIdFromString,
|
|
|
|
creatorId: getIdFromString,
|
|
|
|
page: getPageFromString,
|
|
|
|
});
|
|
|
|
|
|
|
|
const getSearchQueryFromQuery = (q?: string): string | undefined =>
|
|
|
|
q ? decodeURIComponent(q) : undefined;
|
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
function getSearchTypeFromQuery(type_?: string): SearchType {
|
|
|
|
return type_ ? (type_ as SearchType) : defaultSearchType;
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
function getSortTypeFromQuery(sort?: string): SortType {
|
|
|
|
return sort ? (sort as SortType) : defaultSortType;
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
function getListingTypeFromQuery(listingType?: string): ListingType {
|
|
|
|
return listingType ? (listingType as ListingType) : defaultListingType;
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
function postViewToCombined(data: PostView): Combined {
|
|
|
|
return {
|
|
|
|
type_: "posts",
|
|
|
|
data,
|
|
|
|
published: data.post.published,
|
|
|
|
};
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
function commentViewToCombined(data: CommentView): Combined {
|
|
|
|
return {
|
|
|
|
type_: "comments",
|
|
|
|
data,
|
|
|
|
published: data.comment.published,
|
|
|
|
};
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
function communityViewToCombined(data: CommunityView): Combined {
|
|
|
|
return {
|
|
|
|
type_: "communities",
|
|
|
|
data,
|
|
|
|
published: data.community.published,
|
|
|
|
};
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
function personViewSafeToCombined(data: PersonView): Combined {
|
|
|
|
return {
|
|
|
|
type_: "users",
|
|
|
|
data,
|
|
|
|
published: data.person.published,
|
|
|
|
};
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
const Filter = ({
|
|
|
|
filterType,
|
|
|
|
options,
|
|
|
|
onChange,
|
|
|
|
onSearch,
|
|
|
|
value,
|
|
|
|
loading,
|
|
|
|
}: {
|
|
|
|
filterType: FilterType;
|
|
|
|
options: Choice[];
|
|
|
|
onSearch: (text: string) => void;
|
|
|
|
onChange: (choice: Choice) => void;
|
|
|
|
value?: number | null;
|
|
|
|
loading: boolean;
|
|
|
|
}) => {
|
|
|
|
return (
|
|
|
|
<div className="form-group col-sm-6">
|
|
|
|
<label className="col-form-label" htmlFor={`${filterType}-filter`}>
|
|
|
|
{capitalizeFirstLetter(i18n.t(filterType))}
|
|
|
|
</label>
|
|
|
|
<SearchableSelect
|
|
|
|
id={`${filterType}-filter`}
|
|
|
|
options={[
|
|
|
|
{
|
|
|
|
label: i18n.t("all"),
|
|
|
|
value: "0",
|
|
|
|
},
|
|
|
|
].concat(options)}
|
|
|
|
value={value ?? 0}
|
|
|
|
onSearch={onSearch}
|
|
|
|
onChange={onChange}
|
|
|
|
loading={loading}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const communityListing = ({
|
|
|
|
community,
|
|
|
|
counts: { subscribers },
|
|
|
|
}: CommunityView) =>
|
|
|
|
getListing(
|
|
|
|
<CommunityLink community={community} />,
|
|
|
|
subscribers,
|
|
|
|
"number_of_subscribers"
|
|
|
|
);
|
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
const personListing = ({ person, counts: { comment_count } }: PersonView) =>
|
2023-04-15 14:47:10 +00:00
|
|
|
getListing(
|
|
|
|
<PersonListing person={person} showApubName />,
|
|
|
|
comment_count,
|
|
|
|
"number_of_comments"
|
|
|
|
);
|
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
function getListing(
|
2023-05-11 17:06:32 +00:00
|
|
|
listing: JSX.ElementClass,
|
2023-05-11 18:32:32 +00:00
|
|
|
count: bigint,
|
2023-04-15 14:47:10 +00:00
|
|
|
translationKey: "number_of_comments" | "number_of_subscribers"
|
2023-05-11 18:32:32 +00:00
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<span>{listing}</span>
|
|
|
|
<span>{` - ${i18n.t(translationKey, {
|
|
|
|
count: Number(count),
|
|
|
|
formattedCount: numToSI(count),
|
|
|
|
})}`}</span>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
export class Search extends Component<any, SearchState> {
|
2023-01-04 16:56:24 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
|
|
|
private subscription?: Subscription;
|
|
|
|
state: SearchState = {
|
2023-04-15 14:47:10 +00:00
|
|
|
searchLoading: false,
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2021-04-23 21:48:31 +00:00
|
|
|
communities: [],
|
2023-04-15 14:47:10 +00:00
|
|
|
searchCommunitiesLoading: false,
|
|
|
|
searchCreatorLoading: false,
|
|
|
|
creatorSearchOptions: [],
|
|
|
|
communitySearchOptions: [],
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.handleSortChange = this.handleSortChange.bind(this);
|
2021-04-23 21:48:31 +00:00
|
|
|
this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
|
2021-07-16 19:40:56 +00:00
|
|
|
this.handlePageChange = this.handlePageChange.bind(this);
|
2023-04-15 14:47:10 +00:00
|
|
|
this.handleCommunityFilterChange =
|
|
|
|
this.handleCommunityFilterChange.bind(this);
|
|
|
|
this.handleCreatorFilterChange = this.handleCreatorFilterChange.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-09 02:40:36 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const { q } = getSearchQueryParams();
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
searchText: q,
|
|
|
|
};
|
|
|
|
|
2020-09-09 02:40:36 +00:00
|
|
|
// Only fetch the data if coming from another route
|
2023-04-15 14:47:10 +00:00
|
|
|
if (this.isoData.path === this.context.router.route.match.url) {
|
|
|
|
const communityRes = this.isoData.routeData[0] as
|
2023-01-04 16:56:24 +00:00
|
|
|
| GetCommunityResponse
|
|
|
|
| undefined;
|
2023-04-15 14:47:10 +00:00
|
|
|
const communitiesRes = this.isoData.routeData[1] as
|
2023-01-04 16:56:24 +00:00
|
|
|
| ListCommunitiesResponse
|
|
|
|
| undefined;
|
2022-06-21 21:42:29 +00:00
|
|
|
// This can be single or multiple communities given
|
2023-01-04 16:56:24 +00:00
|
|
|
if (communitiesRes) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-01-04 16:56:24 +00:00
|
|
|
communities: communitiesRes.communities,
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
if (communityRes) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-01-04 16:56:24 +00:00
|
|
|
communities: [communityRes.community_view],
|
2023-04-15 14:47:10 +00:00
|
|
|
communitySearchOptions: [
|
|
|
|
communityToChoice(communityRes.community_view),
|
|
|
|
],
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const creatorRes = this.isoData.routeData[2] as GetPersonDetailsResponse;
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-04-15 14:47:10 +00:00
|
|
|
creatorDetails: creatorRes,
|
|
|
|
creatorSearchOptions: creatorRes
|
|
|
|
? [personToChoice(creatorRes.person_view)]
|
|
|
|
: [],
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
2021-04-23 21:48:31 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (q !== "") {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-01-04 16:56:24 +00:00
|
|
|
searchResponse: this.isoData.routeData[3] as SearchResponse,
|
|
|
|
resolveObjectResponse: this.isoData
|
|
|
|
.routeData[4] as ResolveObjectResponse,
|
2023-04-15 14:47:10 +00:00
|
|
|
searchLoading: false,
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
2020-09-09 02:40:36 +00:00
|
|
|
} else {
|
|
|
|
this.search();
|
|
|
|
}
|
2021-04-23 21:48:31 +00:00
|
|
|
} else {
|
2023-04-15 14:47:10 +00:00
|
|
|
const listCommunitiesForm: ListCommunities = {
|
|
|
|
type_: defaultListingType,
|
|
|
|
sort: defaultSortType,
|
|
|
|
limit: fetchLimit,
|
|
|
|
auth: myAuth(false),
|
|
|
|
};
|
2023-02-21 20:52:12 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.listCommunities(listCommunitiesForm)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (q) {
|
2023-02-21 20:52:12 +00:00
|
|
|
this.search();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.subscription?.unsubscribe();
|
2021-02-02 18:36:59 +00:00
|
|
|
saveScrollPosition(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
static fetchInitialData({
|
|
|
|
client,
|
|
|
|
auth,
|
|
|
|
query: { communityId, creatorId, q, type, sort, listingType, page },
|
|
|
|
}: InitialFetchRequest<QueryParams<SearchProps>>): Promise<any>[] {
|
|
|
|
const promises: Promise<any>[] = [];
|
2021-04-23 21:48:31 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const community_id = getIdFromString(communityId);
|
2023-01-04 16:56:24 +00:00
|
|
|
if (community_id) {
|
2023-04-15 14:47:10 +00:00
|
|
|
const getCommunityForm: GetCommunity = {
|
2023-01-04 16:56:24 +00:00
|
|
|
id: community_id,
|
|
|
|
auth,
|
|
|
|
};
|
2023-04-15 14:47:10 +00:00
|
|
|
promises.push(client.getCommunity(getCommunityForm));
|
2023-01-04 16:56:24 +00:00
|
|
|
promises.push(Promise.resolve());
|
|
|
|
} else {
|
2023-04-15 14:47:10 +00:00
|
|
|
const listCommunitiesForm: ListCommunities = {
|
|
|
|
type_: defaultListingType,
|
|
|
|
sort: defaultSortType,
|
2023-01-04 16:56:24 +00:00
|
|
|
limit: fetchLimit,
|
2023-04-15 14:47:10 +00:00
|
|
|
auth,
|
2023-01-04 16:56:24 +00:00
|
|
|
};
|
|
|
|
promises.push(Promise.resolve());
|
2023-04-15 14:47:10 +00:00
|
|
|
promises.push(client.listCommunities(listCommunitiesForm));
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2021-04-23 21:48:31 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const creator_id = getIdFromString(creatorId);
|
2023-01-04 16:56:24 +00:00
|
|
|
if (creator_id) {
|
2023-04-15 14:47:10 +00:00
|
|
|
const getCreatorForm: GetPersonDetails = {
|
2023-01-04 16:56:24 +00:00
|
|
|
person_id: creator_id,
|
2023-04-15 14:47:10 +00:00
|
|
|
auth,
|
2023-01-04 16:56:24 +00:00
|
|
|
};
|
2023-04-15 14:47:10 +00:00
|
|
|
promises.push(client.getPersonDetails(getCreatorForm));
|
2023-01-04 16:56:24 +00:00
|
|
|
} else {
|
|
|
|
promises.push(Promise.resolve());
|
|
|
|
}
|
2021-04-23 21:48:31 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const query = getSearchQueryFromQuery(q);
|
2023-02-21 20:52:12 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (query) {
|
|
|
|
const form: SearchForm = {
|
|
|
|
q: query,
|
2023-02-21 20:52:12 +00:00
|
|
|
community_id,
|
|
|
|
creator_id,
|
2023-04-15 14:47:10 +00:00
|
|
|
type_: getSearchTypeFromQuery(type),
|
|
|
|
sort: getSortTypeFromQuery(sort),
|
|
|
|
listing_type: getListingTypeFromQuery(listingType),
|
2023-05-11 18:32:32 +00:00
|
|
|
page: getPageFromString(page),
|
2023-02-21 20:52:12 +00:00
|
|
|
limit: fetchLimit,
|
2023-04-15 14:47:10 +00:00
|
|
|
auth,
|
2023-02-21 20:52:12 +00:00
|
|
|
};
|
2020-09-09 02:40:36 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (query !== "") {
|
|
|
|
promises.push(client.search(form));
|
2023-05-11 18:32:32 +00:00
|
|
|
if (auth) {
|
|
|
|
const resolveObjectForm: ResolveObject = {
|
|
|
|
q: query,
|
|
|
|
auth,
|
|
|
|
};
|
|
|
|
promises.push(client.resolveObject(resolveObjectForm));
|
|
|
|
}
|
2023-02-21 20:52:12 +00:00
|
|
|
} else {
|
|
|
|
promises.push(Promise.resolve());
|
|
|
|
promises.push(Promise.resolve());
|
|
|
|
}
|
2020-09-09 02:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return promises;
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
get documentTitle(): string {
|
2023-04-15 14:47:10 +00:00
|
|
|
const { q } = getSearchQueryParams();
|
|
|
|
const name = this.state.siteRes.site_view.site.name;
|
|
|
|
return `${i18n.t("search")} - ${q ? `${q} - ` : ""}${name}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-04-15 14:47:10 +00:00
|
|
|
const { type, page } = getSearchQueryParams();
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2021-02-22 02:39:04 +00:00
|
|
|
<h5>{i18n.t("search")}</h5>
|
2023-04-15 14:47:10 +00:00
|
|
|
{this.selects}
|
|
|
|
{this.searchForm}
|
|
|
|
{this.displayResults(type)}
|
2023-05-04 01:47:37 +00:00
|
|
|
{this.resultsCount === 0 && !this.state.searchLoading && (
|
|
|
|
<span>{i18n.t("no_results")}</span>
|
|
|
|
)}
|
2023-04-15 14:47:10 +00:00
|
|
|
<Paginator page={page} onChange={this.handlePageChange} />
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
displayResults(type: SearchType) {
|
|
|
|
switch (type) {
|
2023-05-11 18:32:32 +00:00
|
|
|
case "All":
|
2023-04-15 14:47:10 +00:00
|
|
|
return this.all;
|
2023-05-11 18:32:32 +00:00
|
|
|
case "Comments":
|
2023-04-15 14:47:10 +00:00
|
|
|
return this.comments;
|
2023-05-11 18:32:32 +00:00
|
|
|
case "Posts":
|
|
|
|
case "Url":
|
2023-04-15 14:47:10 +00:00
|
|
|
return this.posts;
|
2023-05-11 18:32:32 +00:00
|
|
|
case "Communities":
|
2023-04-15 14:47:10 +00:00
|
|
|
return this.communities;
|
2023-05-11 18:32:32 +00:00
|
|
|
case "Users":
|
2023-04-15 14:47:10 +00:00
|
|
|
return this.users;
|
|
|
|
default:
|
|
|
|
return <></>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get searchForm() {
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<form
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-inline"
|
2020-09-06 16:15:25 +00:00
|
|
|
onSubmit={linkEvent(this, this.handleSearchSubmit)}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="text"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control mr-2 mb-2"
|
2020-09-06 16:15:25 +00:00
|
|
|
value={this.state.searchText}
|
2021-02-22 02:39:04 +00:00
|
|
|
placeholder={`${i18n.t("search")}...`}
|
|
|
|
aria-label={i18n.t("search")}
|
2020-09-06 16:15:25 +00:00
|
|
|
onInput={linkEvent(this, this.handleQChange)}
|
|
|
|
required
|
2022-08-16 10:22:48 +00:00
|
|
|
minLength={1}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
2022-09-22 15:03:35 +00:00
|
|
|
<button type="submit" className="btn btn-secondary mr-2 mb-2">
|
2023-04-15 14:47:10 +00:00
|
|
|
{this.state.searchLoading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
<span>{i18n.t("search")}</span>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
get selects() {
|
|
|
|
const { type, listingType, sort, communityId, creatorId } =
|
|
|
|
getSearchQueryParams();
|
|
|
|
const {
|
|
|
|
communitySearchOptions,
|
|
|
|
creatorSearchOptions,
|
|
|
|
searchCommunitiesLoading,
|
|
|
|
searchCreatorLoading,
|
|
|
|
} = this.state;
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<div className="mb-2">
|
|
|
|
<select
|
2023-04-15 14:47:10 +00:00
|
|
|
value={type}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleTypeChange)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="custom-select w-auto mb-2"
|
2021-02-22 02:39:04 +00:00
|
|
|
aria-label={i18n.t("type")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-06 20:20:41 +00:00
|
|
|
<option disabled aria-hidden="true">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("type")}
|
2021-02-06 20:20:41 +00:00
|
|
|
</option>
|
2023-04-15 14:47:10 +00:00
|
|
|
{searchTypes.map(option => (
|
|
|
|
<option value={option} key={option}>
|
|
|
|
{i18n.t(option.toString().toLowerCase() as NoOptionI18nKeys)}
|
|
|
|
</option>
|
|
|
|
))}
|
2020-09-06 16:15:25 +00:00
|
|
|
</select>
|
2022-09-22 15:03:35 +00:00
|
|
|
<span className="ml-2">
|
2021-04-23 21:48:31 +00:00
|
|
|
<ListingTypeSelect
|
2023-04-15 14:47:10 +00:00
|
|
|
type_={listingType}
|
2021-04-23 21:48:31 +00:00
|
|
|
showLocal={showLocal(this.isoData)}
|
2022-05-23 19:19:14 +00:00
|
|
|
showSubscribed
|
2021-04-23 21:48:31 +00:00
|
|
|
onChange={this.handleListingTypeChange}
|
|
|
|
/>
|
|
|
|
</span>
|
2022-09-22 15:03:35 +00:00
|
|
|
<span className="ml-2">
|
2020-09-06 16:15:25 +00:00
|
|
|
<SortSelect
|
2023-04-15 14:47:10 +00:00
|
|
|
sort={sort}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={this.handleSortChange}
|
|
|
|
hideHot
|
2021-02-01 18:48:42 +00:00
|
|
|
hideMostComments
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</span>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-row">
|
2023-04-15 14:47:10 +00:00
|
|
|
{this.state.communities.length > 0 && (
|
|
|
|
<Filter
|
|
|
|
filterType="community"
|
|
|
|
onChange={this.handleCommunityFilterChange}
|
|
|
|
onSearch={this.handleCommunitySearch}
|
|
|
|
options={communitySearchOptions}
|
|
|
|
loading={searchCommunitiesLoading}
|
|
|
|
value={communityId}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<Filter
|
|
|
|
filterType="creator"
|
|
|
|
onChange={this.handleCreatorFilterChange}
|
|
|
|
onSearch={this.handleCreatorSearch}
|
|
|
|
options={creatorSearchOptions}
|
|
|
|
loading={searchCreatorLoading}
|
|
|
|
value={creatorId}
|
|
|
|
/>
|
2021-04-23 21:48:31 +00:00
|
|
|
</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-23 20:18:11 +00:00
|
|
|
buildCombined(): Combined[] {
|
2023-04-15 14:47:10 +00:00
|
|
|
const combined: Combined[] = [];
|
|
|
|
const { resolveObjectResponse, searchResponse } = this.state;
|
2021-08-23 20:18:11 +00:00
|
|
|
|
|
|
|
// Push the possible resolve / federated objects first
|
2023-04-15 14:47:10 +00:00
|
|
|
if (resolveObjectResponse) {
|
|
|
|
const { comment, post, community, person } = resolveObjectResponse;
|
|
|
|
|
|
|
|
if (comment) {
|
|
|
|
combined.push(commentViewToCombined(comment));
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
if (post) {
|
|
|
|
combined.push(postViewToCombined(post));
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
if (community) {
|
|
|
|
combined.push(communityViewToCombined(community));
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
if (person) {
|
|
|
|
combined.push(personViewSafeToCombined(person));
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-23 20:18:11 +00:00
|
|
|
|
|
|
|
// Push the search results
|
2023-04-15 14:47:10 +00:00
|
|
|
if (searchResponse) {
|
|
|
|
const { comments, posts, communities, users } = searchResponse;
|
|
|
|
|
|
|
|
combined.push(
|
|
|
|
...[
|
|
|
|
...(comments?.map(commentViewToCombined) ?? []),
|
|
|
|
...(posts?.map(postViewToCombined) ?? []),
|
|
|
|
...(communities?.map(communityViewToCombined) ?? []),
|
|
|
|
...(users?.map(personViewSafeToCombined) ?? []),
|
|
|
|
]
|
2023-01-04 16:56:24 +00:00
|
|
|
);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const { sort } = getSearchQueryParams();
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
// Sort it
|
2023-05-11 18:32:32 +00:00
|
|
|
if (sort === "New") {
|
2020-12-24 01:58:27 +00:00
|
|
|
combined.sort((a, b) => b.published.localeCompare(a.published));
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2023-05-11 18:32:32 +00:00
|
|
|
combined.sort((a, b) =>
|
|
|
|
Number(
|
2020-12-24 01:58:27 +00:00
|
|
|
((b.data as CommentView | PostView).counts.score |
|
|
|
|
(b.data as CommunityView).counts.subscribers |
|
2023-05-11 18:32:32 +00:00
|
|
|
(b.data as PersonView).counts.comment_score) -
|
|
|
|
((a.data as CommentView | PostView).counts.score |
|
|
|
|
(a.data as CommunityView).counts.subscribers |
|
|
|
|
(a.data as PersonView).counts.comment_score)
|
|
|
|
)
|
2020-09-06 16:15:25 +00:00
|
|
|
);
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2021-08-23 20:18:11 +00:00
|
|
|
return combined;
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
get all() {
|
|
|
|
const combined = this.buildCombined();
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{combined.map(i => (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div key={i.published} className="row">
|
|
|
|
<div className="col-12">
|
2023-04-15 14:47:10 +00:00
|
|
|
{i.type_ === "posts" && (
|
2020-09-06 16:15:25 +00:00
|
|
|
<PostListing
|
2020-12-24 01:58:27 +00:00
|
|
|
key={(i.data as PostView).post.id}
|
|
|
|
post_view={i.data as PostView}
|
2020-09-06 16:15:25 +00:00
|
|
|
showCommunity
|
2022-06-21 21:42:29 +00:00
|
|
|
enableDownvotes={enableDownvotes(this.state.siteRes)}
|
|
|
|
enableNsfw={enableNsfw(this.state.siteRes)}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages={this.state.siteRes.discussion_languages}
|
2022-09-21 14:05:19 +00:00
|
|
|
viewOnly
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
)}
|
2023-04-15 14:47:10 +00:00
|
|
|
{i.type_ === "comments" && (
|
2020-09-06 16:15:25 +00:00
|
|
|
<CommentNodes
|
2020-12-24 01:58:27 +00:00
|
|
|
key={(i.data as CommentView).comment.id}
|
2022-07-30 13:28:08 +00:00
|
|
|
nodes={[
|
|
|
|
{
|
|
|
|
comment_view: i.data as CommentView,
|
|
|
|
children: [],
|
|
|
|
depth: 0,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
viewType={CommentViewType.Flat}
|
2022-09-21 14:05:19 +00:00
|
|
|
viewOnly
|
2020-09-06 16:15:25 +00:00
|
|
|
locked
|
|
|
|
noIndent
|
2022-06-21 21:42:29 +00:00
|
|
|
enableDownvotes={enableDownvotes(this.state.siteRes)}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.state.siteRes.all_languages}
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages={this.state.siteRes.discussion_languages}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
)}
|
2023-04-15 14:47:10 +00:00
|
|
|
{i.type_ === "communities" && (
|
|
|
|
<div>{communityListing(i.data as CommunityView)}</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
2023-04-15 14:47:10 +00:00
|
|
|
{i.type_ === "users" && (
|
2023-05-11 18:32:32 +00:00
|
|
|
<div>{personListing(i.data as PersonView)}</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
get comments() {
|
|
|
|
const { searchResponse, resolveObjectResponse, siteRes } = this.state;
|
|
|
|
const comments = searchResponse?.comments ?? [];
|
|
|
|
|
|
|
|
if (resolveObjectResponse?.comment) {
|
|
|
|
comments.unshift(resolveObjectResponse?.comment);
|
|
|
|
}
|
2021-08-23 20:18:11 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<CommentNodes
|
2021-08-23 20:18:11 +00:00
|
|
|
nodes={commentsToFlatNodes(comments)}
|
2022-07-30 13:28:08 +00:00
|
|
|
viewType={CommentViewType.Flat}
|
2022-09-21 14:05:19 +00:00
|
|
|
viewOnly
|
2020-09-06 16:15:25 +00:00
|
|
|
locked
|
|
|
|
noIndent
|
2023-04-15 14:47:10 +00:00
|
|
|
enableDownvotes={enableDownvotes(siteRes)}
|
|
|
|
allLanguages={siteRes.all_languages}
|
|
|
|
siteLanguages={siteRes.discussion_languages}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
get posts() {
|
|
|
|
const { searchResponse, resolveObjectResponse, siteRes } = this.state;
|
|
|
|
const posts = searchResponse?.posts ?? [];
|
2021-08-23 20:18:11 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (resolveObjectResponse?.post) {
|
|
|
|
posts.unshift(resolveObjectResponse.post);
|
|
|
|
}
|
2021-08-23 20:18:11 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-09-22 15:03:35 +00:00
|
|
|
{posts.map(pv => (
|
|
|
|
<div key={pv.post.id} className="row">
|
|
|
|
<div className="col-12">
|
2020-09-06 16:15:25 +00:00
|
|
|
<PostListing
|
2022-09-22 15:03:35 +00:00
|
|
|
post_view={pv}
|
2020-09-06 16:15:25 +00:00
|
|
|
showCommunity
|
2023-04-15 14:47:10 +00:00
|
|
|
enableDownvotes={enableDownvotes(siteRes)}
|
|
|
|
enableNsfw={enableNsfw(siteRes)}
|
|
|
|
allLanguages={siteRes.all_languages}
|
|
|
|
siteLanguages={siteRes.discussion_languages}
|
2022-09-21 14:05:19 +00:00
|
|
|
viewOnly
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
get communities() {
|
|
|
|
const { searchResponse, resolveObjectResponse } = this.state;
|
|
|
|
const communities = searchResponse?.communities ?? [];
|
2021-08-23 20:18:11 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (resolveObjectResponse?.community) {
|
|
|
|
communities.unshift(resolveObjectResponse.community);
|
|
|
|
}
|
2021-08-23 20:18:11 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-09-22 15:03:35 +00:00
|
|
|
{communities.map(cv => (
|
|
|
|
<div key={cv.community.id} className="row">
|
2023-04-15 14:47:10 +00:00
|
|
|
<div className="col-12">{communityListing(cv)}</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
get users() {
|
|
|
|
const { searchResponse, resolveObjectResponse } = this.state;
|
|
|
|
const users = searchResponse?.users ?? [];
|
2021-08-23 20:18:11 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (resolveObjectResponse?.person) {
|
|
|
|
users.unshift(resolveObjectResponse.person);
|
|
|
|
}
|
2021-08-23 20:18:11 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-09-22 15:03:35 +00:00
|
|
|
{users.map(pvs => (
|
|
|
|
<div key={pvs.person.id} className="row">
|
2023-04-15 14:47:10 +00:00
|
|
|
<div className="col-12">{personListing(pvs)}</div>
|
2021-08-23 20:18:11 +00:00
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
get resultsCount(): number {
|
|
|
|
const { searchResponse: r, resolveObjectResponse: resolveRes } = this.state;
|
2023-01-04 16:56:24 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const searchCount = r
|
|
|
|
? r.posts.length +
|
|
|
|
r.comments.length +
|
|
|
|
r.communities.length +
|
|
|
|
r.users.length
|
2023-01-04 16:56:24 +00:00
|
|
|
: 0;
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const resObjCount = resolveRes
|
2023-01-04 16:56:24 +00:00
|
|
|
? resolveRes.post ||
|
|
|
|
resolveRes.person ||
|
|
|
|
resolveRes.community ||
|
|
|
|
resolveRes.comment
|
|
|
|
? 1
|
|
|
|
: 0
|
|
|
|
: 0;
|
2022-06-21 21:42:29 +00:00
|
|
|
|
|
|
|
return resObjCount + searchCount;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
search() {
|
2023-04-15 14:47:10 +00:00
|
|
|
const auth = myAuth(false);
|
|
|
|
const { searchText: q } = this.state;
|
|
|
|
const { communityId, creatorId, type, sort, listingType, page } =
|
|
|
|
getSearchQueryParams();
|
|
|
|
|
|
|
|
if (q && q !== "") {
|
|
|
|
const form: SearchForm = {
|
|
|
|
q,
|
|
|
|
community_id: communityId ?? undefined,
|
|
|
|
creator_id: creatorId ?? undefined,
|
|
|
|
type_: type,
|
|
|
|
sort,
|
|
|
|
listing_type: listingType,
|
|
|
|
page,
|
2023-02-21 20:52:12 +00:00
|
|
|
limit: fetchLimit,
|
|
|
|
auth,
|
|
|
|
};
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
if (auth) {
|
|
|
|
const resolveObjectForm: ResolveObject = {
|
|
|
|
q,
|
|
|
|
auth,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.resolveObject(resolveObjectForm)
|
|
|
|
);
|
|
|
|
}
|
2021-08-23 20:18:11 +00:00
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
2023-01-04 16:56:24 +00:00
|
|
|
searchResponse: undefined,
|
|
|
|
resolveObjectResponse: undefined,
|
2023-04-15 14:47:10 +00:00
|
|
|
searchLoading: true,
|
2022-09-22 15:03:35 +00:00
|
|
|
});
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.search(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
handleCreatorSearch = debounce(async (text: string) => {
|
|
|
|
const { creatorId } = getSearchQueryParams();
|
|
|
|
const { creatorSearchOptions } = this.state;
|
|
|
|
this.setState({
|
|
|
|
searchCreatorLoading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const newOptions: Choice[] = [];
|
|
|
|
|
|
|
|
const selectedChoice = creatorSearchOptions.find(
|
|
|
|
choice => getIdFromString(choice.value) === creatorId
|
|
|
|
);
|
|
|
|
|
|
|
|
if (selectedChoice) {
|
|
|
|
newOptions.push(selectedChoice);
|
2021-04-23 21:48:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (text.length > 0) {
|
|
|
|
newOptions.push(...(await fetchUsers(text)).users.map(personToChoice));
|
2021-04-23 21:48:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
this.setState({
|
|
|
|
searchCreatorLoading: false,
|
|
|
|
creatorSearchOptions: newOptions,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
handleCommunitySearch = debounce(async (text: string) => {
|
|
|
|
const { communityId } = getSearchQueryParams();
|
|
|
|
const { communitySearchOptions } = this.state;
|
|
|
|
this.setState({
|
|
|
|
searchCommunitiesLoading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const newOptions: Choice[] = [];
|
|
|
|
|
|
|
|
const selectedChoice = communitySearchOptions.find(
|
|
|
|
choice => getIdFromString(choice.value) === communityId
|
|
|
|
);
|
|
|
|
|
|
|
|
if (selectedChoice) {
|
|
|
|
newOptions.push(selectedChoice);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text.length > 0) {
|
|
|
|
newOptions.push(
|
|
|
|
...(await fetchCommunities(text)).communities.map(communityToChoice)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
searchCommunitiesLoading: false,
|
|
|
|
communitySearchOptions: newOptions,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
handleSortChange(sort: SortType) {
|
2023-05-11 18:32:32 +00:00
|
|
|
this.updateUrl({ sort, page: 1n });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleTypeChange(i: Search, event: any) {
|
2023-05-11 18:32:32 +00:00
|
|
|
const type = event.target.value as SearchType;
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
i.updateUrl({
|
|
|
|
type,
|
2023-05-11 18:32:32 +00:00
|
|
|
page: 1n,
|
2023-04-15 14:47:10 +00:00
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
handlePageChange(page: bigint) {
|
2023-04-15 14:47:10 +00:00
|
|
|
this.updateUrl({ page });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleListingTypeChange(listingType: ListingType) {
|
|
|
|
this.updateUrl({
|
|
|
|
listingType,
|
2023-05-11 18:32:32 +00:00
|
|
|
page: 1n,
|
2023-04-15 14:47:10 +00:00
|
|
|
});
|
2021-04-23 21:48:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
handleCommunityFilterChange({ value }: Choice) {
|
2021-04-23 21:48:31 +00:00
|
|
|
this.updateUrl({
|
2023-04-15 14:47:10 +00:00
|
|
|
communityId: getIdFromString(value) ?? null,
|
2023-05-11 18:32:32 +00:00
|
|
|
page: 1n,
|
2021-04-23 21:48:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
handleCreatorFilterChange({ value }: Choice) {
|
2021-04-23 21:48:31 +00:00
|
|
|
this.updateUrl({
|
2023-04-15 14:47:10 +00:00
|
|
|
creatorId: getIdFromString(value) ?? null,
|
2023-05-11 18:32:32 +00:00
|
|
|
page: 1n,
|
2021-04-23 21:48:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleSearchSubmit(i: Search, event: any) {
|
|
|
|
event.preventDefault();
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
i.updateUrl({
|
|
|
|
q: i.state.searchText,
|
2023-05-11 18:32:32 +00:00
|
|
|
page: 1n,
|
2020-09-06 16:15:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleQChange(i: Search, event: any) {
|
|
|
|
i.setState({ searchText: event.target.value });
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
updateUrl({
|
|
|
|
q,
|
|
|
|
type,
|
|
|
|
listingType,
|
|
|
|
sort,
|
|
|
|
communityId,
|
|
|
|
creatorId,
|
|
|
|
page,
|
|
|
|
}: Partial<SearchProps>) {
|
|
|
|
const {
|
|
|
|
q: urlQ,
|
|
|
|
type: urlType,
|
|
|
|
listingType: urlListingType,
|
|
|
|
communityId: urlCommunityId,
|
|
|
|
sort: urlSort,
|
|
|
|
creatorId: urlCreatorId,
|
|
|
|
page: urlPage,
|
|
|
|
} = getSearchQueryParams();
|
|
|
|
|
|
|
|
let query = q ?? this.state.searchText ?? urlQ;
|
|
|
|
|
|
|
|
if (query && query.length > 0) {
|
|
|
|
query = encodeURIComponent(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
const queryParams: QueryParams<SearchProps> = {
|
|
|
|
q: query,
|
|
|
|
type: type ?? urlType,
|
|
|
|
listingType: listingType ?? urlListingType,
|
|
|
|
communityId: getUpdatedSearchId(communityId, urlCommunityId),
|
|
|
|
creatorId: getUpdatedSearchId(creatorId, urlCreatorId),
|
|
|
|
page: (page ?? urlPage).toString(),
|
|
|
|
sort: sort ?? urlSort,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.props.history.push(`/search${getQueryString(queryParams)}`);
|
|
|
|
|
|
|
|
this.search();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
console.log(msg);
|
2023-04-15 14:47:10 +00:00
|
|
|
const op = wsUserOp(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2023-04-15 14:47:10 +00:00
|
|
|
if (msg.error === "couldnt_find_object") {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({
|
2023-01-04 16:56:24 +00:00
|
|
|
resolveObjectResponse: {},
|
2022-06-21 21:42:29 +00:00
|
|
|
});
|
2022-04-05 10:41:50 +00:00
|
|
|
this.checkFinishedLoading();
|
|
|
|
} else {
|
2021-08-23 20:18:11 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
} else {
|
|
|
|
switch (op) {
|
|
|
|
case UserOperation.Search: {
|
|
|
|
const searchResponse = wsJsonToRes<SearchResponse>(msg);
|
|
|
|
this.setState({ searchResponse });
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
this.checkFinishedLoading();
|
|
|
|
restoreScrollPosition(this.context);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.CreateCommentLike: {
|
|
|
|
const { comment_view } = wsJsonToRes<CommentResponse>(msg);
|
|
|
|
createCommentLikeRes(
|
|
|
|
comment_view,
|
|
|
|
this.state.searchResponse?.comments
|
|
|
|
);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.CreatePostLike: {
|
|
|
|
const { post_view } = wsJsonToRes<PostResponse>(msg);
|
|
|
|
createPostLikeFindRes(post_view, this.state.searchResponse?.posts);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.ListCommunities: {
|
|
|
|
const { communities } = wsJsonToRes<ListCommunitiesResponse>(msg);
|
|
|
|
this.setState({ communities });
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.ResolveObject: {
|
|
|
|
const resolveObjectResponse = wsJsonToRes<ResolveObjectResponse>(msg);
|
|
|
|
this.setState({ resolveObjectResponse });
|
|
|
|
this.checkFinishedLoading();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 10:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkFinishedLoading() {
|
2023-05-04 01:47:37 +00:00
|
|
|
if (this.state.searchResponse || this.state.resolveObjectResponse) {
|
2023-04-15 14:47:10 +00:00
|
|
|
this.setState({ searchLoading: false });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|