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,
|
2021-03-15 18:09:31 +00:00
|
|
|
PersonViewSafe,
|
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 {
|
2021-07-17 20:42:55 +00:00
|
|
|
capitalizeFirstLetter,
|
2023-04-15 14:47:10 +00:00
|
|
|
Choice,
|
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,
|
2023-04-15 14:47:10 +00:00
|
|
|
QueryParams,
|
2021-07-17 20:42:55 +00:00
|
|
|
restoreScrollPosition,
|
|
|
|
routeListingTypeToEnum,
|
|
|
|
routeSearchTypeToEnum,
|
|
|
|
routeSortTypeToEnum,
|
|
|
|
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;
|
2020-09-06 16:15:25 +00:00
|
|
|
page: number;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
data: CommentView | PostView | CommunityView | PersonViewSafe;
|
|
|
|
published: string;
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const defaultSearchType = SearchType.All;
|
|
|
|
const defaultSortType = SortType.TopAll;
|
|
|
|
const defaultListingType = ListingType.All;
|
|
|
|
|
|
|
|
const searchTypes = [
|
|
|
|
SearchType.All,
|
|
|
|
SearchType.Comments,
|
|
|
|
SearchType.Posts,
|
|
|
|
SearchType.Communities,
|
|
|
|
SearchType.Users,
|
|
|
|
SearchType.Url,
|
|
|
|
];
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
const getSearchTypeFromQuery = (type_?: string): SearchType =>
|
|
|
|
routeSearchTypeToEnum(type_ ?? "", defaultSearchType);
|
|
|
|
|
|
|
|
const getSortTypeFromQuery = (sort?: string): SortType =>
|
|
|
|
routeSortTypeToEnum(sort ?? "", defaultSortType);
|
|
|
|
|
|
|
|
const getListingTypeFromQuery = (listingType?: string): ListingType =>
|
|
|
|
routeListingTypeToEnum(listingType ?? "", defaultListingType);
|
|
|
|
|
|
|
|
const postViewToCombined = (data: PostView): Combined => ({
|
|
|
|
type_: "posts",
|
|
|
|
data,
|
|
|
|
published: data.post.published,
|
|
|
|
});
|
|
|
|
|
|
|
|
const commentViewToCombined = (data: CommentView): Combined => ({
|
|
|
|
type_: "comments",
|
|
|
|
data,
|
|
|
|
published: data.comment.published,
|
|
|
|
});
|
|
|
|
|
|
|
|
const communityViewToCombined = (data: CommunityView): Combined => ({
|
|
|
|
type_: "communities",
|
|
|
|
data,
|
|
|
|
published: data.community.published,
|
|
|
|
});
|
|
|
|
|
|
|
|
const personViewSafeToCombined = (data: PersonViewSafe): Combined => ({
|
|
|
|
type_: "users",
|
|
|
|
data,
|
|
|
|
published: data.person.published,
|
|
|
|
});
|
|
|
|
|
|
|
|
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"
|
|
|
|
);
|
|
|
|
|
|
|
|
const personListing = ({ person, counts: { comment_count } }: PersonViewSafe) =>
|
|
|
|
getListing(
|
|
|
|
<PersonListing person={person} showApubName />,
|
|
|
|
comment_count,
|
|
|
|
"number_of_comments"
|
|
|
|
);
|
|
|
|
|
|
|
|
const getListing = (
|
|
|
|
listing: JSX.Element,
|
|
|
|
count: number,
|
|
|
|
translationKey: "number_of_comments" | "number_of_subscribers"
|
|
|
|
) => (
|
|
|
|
<>
|
|
|
|
<span>{listing}</span>
|
|
|
|
<span>{` - ${i18n.t(translationKey, {
|
|
|
|
count,
|
|
|
|
formattedCount: numToSI(count),
|
|
|
|
})}`}</span>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
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),
|
|
|
|
page: getIdFromString(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
|
|
|
const resolveObjectForm: ResolveObject = {
|
|
|
|
q: query,
|
|
|
|
auth,
|
2023-02-21 20:52:12 +00:00
|
|
|
};
|
2021-08-23 20:18:11 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (query !== "") {
|
|
|
|
promises.push(client.search(form));
|
|
|
|
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)}
|
|
|
|
{this.resultsCount === 0 && <span>{i18n.t("no_results")}</span>}
|
|
|
|
<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) {
|
|
|
|
case SearchType.All:
|
|
|
|
return this.all;
|
|
|
|
case SearchType.Comments:
|
|
|
|
return this.comments;
|
|
|
|
case SearchType.Posts:
|
|
|
|
case SearchType.Url:
|
|
|
|
return this.posts;
|
|
|
|
case SearchType.Communities:
|
|
|
|
return this.communities;
|
|
|
|
case SearchType.Users:
|
|
|
|
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-04-15 14:47:10 +00:00
|
|
|
if (sort === SortType.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 {
|
|
|
|
combined.sort(
|
|
|
|
(a, b) =>
|
2020-12-24 01:58:27 +00:00
|
|
|
((b.data as CommentView | PostView).counts.score |
|
|
|
|
(b.data as CommunityView).counts.subscribers |
|
2021-03-15 18:09:31 +00:00
|
|
|
(b.data as PersonViewSafe).counts.comment_score) -
|
2020-12-24 01:58:27 +00:00
|
|
|
((a.data as CommentView | PostView).counts.score |
|
|
|
|
(a.data as CommunityView).counts.subscribers |
|
2021-03-15 18:09:31 +00:00
|
|
|
(a.data as PersonViewSafe).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" && (
|
|
|
|
<div>{personListing(i.data as PersonViewSafe)}</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-04-15 14:47:10 +00:00
|
|
|
const resolveObjectForm: ResolveObject = {
|
|
|
|
q,
|
2023-02-21 20:52:12 +00:00
|
|
|
auth,
|
|
|
|
};
|
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));
|
2021-08-23 20:18:11 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.resolveObject(resolveObjectForm));
|
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) {
|
|
|
|
this.updateUrl({ sort, page: 1 });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleTypeChange(i: Search, event: any) {
|
2023-04-15 14:47:10 +00:00
|
|
|
const type = SearchType[event.target.value];
|
|
|
|
|
|
|
|
i.updateUrl({
|
|
|
|
type,
|
2020-09-06 16:15:25 +00:00
|
|
|
page: 1,
|
2023-04-15 14:47:10 +00:00
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
handlePageChange(page: number) {
|
|
|
|
this.updateUrl({ page });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleListingTypeChange(listingType: ListingType) {
|
|
|
|
this.updateUrl({
|
|
|
|
listingType,
|
2021-04-23 21:48:31 +00:00
|
|
|
page: 1,
|
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,
|
2021-04-23 21:48:31 +00:00
|
|
|
page: 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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,
|
2021-04-23 21:48:31 +00:00
|
|
|
page: 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-04-15 14:47:10 +00:00
|
|
|
page: 1,
|
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-01-04 16:56:24 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|