2022-06-21 21:42:29 +00:00
|
|
|
import { None, Option, Some } from "@sniptt/monads";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
|
|
|
CommunityResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
FollowCommunity,
|
2022-06-21 21:42:29 +00:00
|
|
|
GetSiteResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
ListCommunities,
|
2021-07-17 20:42:55 +00:00
|
|
|
ListCommunitiesResponse,
|
2021-01-26 15:59:38 +00:00
|
|
|
ListingType,
|
2021-07-17 20:42:55 +00:00
|
|
|
SortType,
|
2022-06-23 13:35:53 +00:00
|
|
|
SubscribedType,
|
2021-07-17 20:42:55 +00:00
|
|
|
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 { InitialFetchRequest } from "shared/interfaces";
|
|
|
|
import { i18n } from "../../i18next";
|
2021-07-19 13:04:06 +00:00
|
|
|
import { WebSocketService } from "../../services";
|
2020-09-07 03:41:46 +00:00
|
|
|
import {
|
2022-06-21 21:42:29 +00:00
|
|
|
auth,
|
2021-07-19 13:04:06 +00:00
|
|
|
getListingTypeFromPropsNoDefault,
|
2020-09-07 03:41:46 +00:00
|
|
|
getPageFromProps,
|
|
|
|
isBrowser,
|
2021-09-18 16:35:49 +00:00
|
|
|
numToSI,
|
2020-09-07 15:32:07 +00:00
|
|
|
setIsoData,
|
2021-07-18 16:17:50 +00:00
|
|
|
showLocal,
|
2021-07-17 20:42:55 +00:00
|
|
|
toast,
|
|
|
|
wsClient,
|
2020-09-07 15:32:07 +00:00
|
|
|
wsSubscribe,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
2021-07-18 16:17:50 +00:00
|
|
|
import { ListingTypeSelect } from "../common/listing-type-select";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Paginator } from "../common/paginator";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { CommunityLink } from "./community-link";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2022-07-13 14:27:43 +00:00
|
|
|
const communityLimit = 50;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface CommunitiesState {
|
2022-06-21 21:42:29 +00:00
|
|
|
listCommunitiesResponse: Option<ListCommunitiesResponse>;
|
2020-09-06 16:15:25 +00:00
|
|
|
page: number;
|
|
|
|
loading: boolean;
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2021-01-23 23:07:02 +00:00
|
|
|
searchText: string;
|
2021-07-18 16:17:50 +00:00
|
|
|
listingType: ListingType;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommunitiesProps {
|
2021-07-18 16:17:50 +00:00
|
|
|
listingType?: ListingType;
|
|
|
|
page?: number;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Communities extends Component<any, CommunitiesState> {
|
|
|
|
private subscription: Subscription;
|
2022-06-21 21:42:29 +00:00
|
|
|
private isoData = setIsoData(this.context, ListCommunitiesResponse);
|
2020-09-06 16:15:25 +00:00
|
|
|
private emptyState: CommunitiesState = {
|
2022-06-21 21:42:29 +00:00
|
|
|
listCommunitiesResponse: None,
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: true,
|
|
|
|
page: getPageFromProps(this.props),
|
2021-07-19 13:04:06 +00:00
|
|
|
listingType: getListingTypeFromPropsNoDefault(this.props),
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2021-02-22 02:39:04 +00:00
|
|
|
searchText: "",
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
this.state = this.emptyState;
|
2021-07-16 19:40:56 +00:00
|
|
|
this.handlePageChange = this.handlePageChange.bind(this);
|
2021-07-18 16:17:50 +00:00
|
|
|
this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
|
2020-09-07 03:41:46 +00:00
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
2020-09-07 15:32:07 +00:00
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-07 03:41:46 +00:00
|
|
|
// Only fetch the data if coming from another route
|
2020-09-07 15:32:07 +00:00
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let listRes = Some(this.isoData.routeData[0] as ListCommunitiesResponse);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
listCommunitiesResponse: listRes,
|
|
|
|
loading: false,
|
|
|
|
};
|
2020-09-07 03:41:46 +00:00
|
|
|
} else {
|
|
|
|
this.refetch();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-09-07 03:41:46 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getDerivedStateFromProps(props: any): CommunitiesProps {
|
|
|
|
return {
|
2021-07-19 13:04:06 +00:00
|
|
|
listingType: getListingTypeFromPropsNoDefault(props),
|
2020-09-06 16:15:25 +00:00
|
|
|
page: getPageFromProps(props),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(_: any, lastState: CommunitiesState) {
|
2021-07-18 16:17:50 +00:00
|
|
|
if (
|
|
|
|
lastState.page !== this.state.page ||
|
|
|
|
lastState.listingType !== this.state.listingType
|
|
|
|
) {
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState({ loading: true });
|
|
|
|
this.refetch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2022-11-09 19:53:07 +00:00
|
|
|
return `${i18n.t("communities")} - ${
|
|
|
|
this.state.siteRes.site_view.site.name
|
|
|
|
}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
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}
|
2022-06-21 21:42:29 +00:00
|
|
|
description={None}
|
|
|
|
image={None}
|
2020-09-11 18:09:21 +00:00
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.state.loading ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<h5>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
2020-09-06 16:15:25 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-md-6">
|
2021-02-22 02:39:04 +00:00
|
|
|
<h4>{i18n.t("list_of_communities")}</h4>
|
2022-09-22 15:03:35 +00:00
|
|
|
<span className="mb-2">
|
2021-07-18 16:17:50 +00:00
|
|
|
<ListingTypeSelect
|
|
|
|
type_={this.state.listingType}
|
|
|
|
showLocal={showLocal(this.isoData)}
|
2022-05-23 19:19:14 +00:00
|
|
|
showSubscribed
|
2021-07-18 16:17:50 +00:00
|
|
|
onChange={this.handleListingTypeChange}
|
|
|
|
/>
|
|
|
|
</span>
|
2021-01-23 23:07:02 +00:00
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-md-6">
|
|
|
|
<div className="float-md-right">{this.searchForm()}</div>
|
2021-01-23 23:07:02 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="table-responsive">
|
|
|
|
<table
|
|
|
|
id="community_table"
|
|
|
|
className="table table-sm table-hover"
|
|
|
|
>
|
|
|
|
<thead className="pointer">
|
2020-09-06 16:15:25 +00:00
|
|
|
<tr>
|
2021-02-22 02:39:04 +00:00
|
|
|
<th>{i18n.t("name")}</th>
|
2022-09-22 15:03:35 +00:00
|
|
|
<th className="text-right">{i18n.t("subscribers")}</th>
|
|
|
|
<th className="text-right">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("users")} / {i18n.t("month")}
|
2021-01-29 15:10:37 +00:00
|
|
|
</th>
|
2022-09-22 15:03:35 +00:00
|
|
|
<th className="text-right d-none d-lg-table-cell">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("posts")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</th>
|
2022-09-22 15:03:35 +00:00
|
|
|
<th className="text-right d-none d-lg-table-cell">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("comments")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</th>
|
|
|
|
<th></th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.state.listCommunitiesResponse
|
|
|
|
.map(l => l.communities)
|
|
|
|
.unwrapOr([])
|
|
|
|
.map(cv => (
|
2022-09-22 15:03:35 +00:00
|
|
|
<tr key={cv.community.id}>
|
2022-06-21 21:42:29 +00:00
|
|
|
<td>
|
|
|
|
<CommunityLink community={cv.community} />
|
|
|
|
</td>
|
2022-09-22 15:03:35 +00:00
|
|
|
<td className="text-right">
|
2022-06-21 21:42:29 +00:00
|
|
|
{numToSI(cv.counts.subscribers)}
|
|
|
|
</td>
|
2022-09-22 15:03:35 +00:00
|
|
|
<td className="text-right">
|
2022-06-21 21:42:29 +00:00
|
|
|
{numToSI(cv.counts.users_active_month)}
|
|
|
|
</td>
|
2022-09-22 15:03:35 +00:00
|
|
|
<td className="text-right d-none d-lg-table-cell">
|
2022-06-21 21:42:29 +00:00
|
|
|
{numToSI(cv.counts.posts)}
|
|
|
|
</td>
|
2022-09-22 15:03:35 +00:00
|
|
|
<td className="text-right d-none d-lg-table-cell">
|
2022-06-21 21:42:29 +00:00
|
|
|
{numToSI(cv.counts.comments)}
|
|
|
|
</td>
|
2022-09-22 15:03:35 +00:00
|
|
|
<td className="text-right">
|
2022-06-23 13:35:53 +00:00
|
|
|
{cv.subscribed == SubscribedType.Subscribed && (
|
2022-06-21 21:42:29 +00:00
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-link d-inline-block"
|
2022-06-21 21:42:29 +00:00
|
|
|
onClick={linkEvent(
|
|
|
|
cv.community.id,
|
|
|
|
this.handleUnsubscribe
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{i18n.t("unsubscribe")}
|
|
|
|
</button>
|
2022-06-23 13:35:53 +00:00
|
|
|
)}
|
|
|
|
{cv.subscribed == SubscribedType.NotSubscribed && (
|
2022-06-21 21:42:29 +00:00
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-link d-inline-block"
|
2022-06-21 21:42:29 +00:00
|
|
|
onClick={linkEvent(
|
|
|
|
cv.community.id,
|
|
|
|
this.handleSubscribe
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{i18n.t("subscribe")}
|
|
|
|
</button>
|
|
|
|
)}
|
2022-06-23 13:35:53 +00:00
|
|
|
{cv.subscribed == SubscribedType.Pending && (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="text-warning d-inline-block">
|
2022-06-23 13:35:53 +00:00
|
|
|
{i18n.t("subscribe_pending")}
|
|
|
|
</div>
|
|
|
|
)}
|
2022-06-21 21:42:29 +00:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
2020-09-06 16:15:25 +00:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2021-07-16 19:40:56 +00:00
|
|
|
<Paginator
|
|
|
|
page={this.state.page}
|
|
|
|
onChange={this.handlePageChange}
|
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-23 23:07:02 +00:00
|
|
|
searchForm() {
|
|
|
|
return (
|
|
|
|
<form
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-inline"
|
2021-01-23 23:07:02 +00:00
|
|
|
onSubmit={linkEvent(this, this.handleSearchSubmit)}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="text"
|
2021-02-09 16:21:24 +00:00
|
|
|
id="communities-search"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control mr-2 mb-2"
|
2021-01-23 23:07:02 +00:00
|
|
|
value={this.state.searchText}
|
2021-02-22 02:39:04 +00:00
|
|
|
placeholder={`${i18n.t("search")}...`}
|
2021-01-23 23:07:02 +00:00
|
|
|
onInput={linkEvent(this, this.handleSearchChange)}
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
/>
|
2022-09-22 15:03:35 +00:00
|
|
|
<label className="sr-only" htmlFor="communities-search">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("search")}
|
2021-02-09 16:21:24 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<button type="submit" className="btn btn-secondary mr-2 mb-2">
|
2021-02-22 02:39:04 +00:00
|
|
|
<span>{i18n.t("search")}</span>
|
2021-01-23 23:07:02 +00:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
updateUrl(paramUpdates: CommunitiesProps) {
|
|
|
|
const page = paramUpdates.page || this.state.page;
|
2021-07-18 16:17:50 +00:00
|
|
|
const listingTypeStr = paramUpdates.listingType || this.state.listingType;
|
|
|
|
this.props.history.push(
|
|
|
|
`/communities/listing_type/${listingTypeStr}/page/${page}`
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 19:40:56 +00:00
|
|
|
handlePageChange(page: number) {
|
|
|
|
this.updateUrl({ page });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 16:17:50 +00:00
|
|
|
handleListingTypeChange(val: ListingType) {
|
|
|
|
this.updateUrl({
|
|
|
|
listingType: val,
|
|
|
|
page: 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleUnsubscribe(communityId: number) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let form = new FollowCommunity({
|
2020-09-06 16:15:25 +00:00
|
|
|
community_id: communityId,
|
|
|
|
follow: false,
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: auth().unwrap(),
|
|
|
|
});
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.followCommunity(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubscribe(communityId: number) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let form = new FollowCommunity({
|
2020-09-06 16:15:25 +00:00
|
|
|
community_id: communityId,
|
|
|
|
follow: true,
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: auth().unwrap(),
|
|
|
|
});
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.followCommunity(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:07:02 +00:00
|
|
|
handleSearchChange(i: Communities, event: any) {
|
|
|
|
i.setState({ searchText: event.target.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSearchSubmit(i: Communities) {
|
|
|
|
const searchParamEncoded = encodeURIComponent(i.state.searchText);
|
|
|
|
i.context.router.history.push(
|
2021-04-23 21:48:31 +00:00
|
|
|
`/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
|
2021-01-23 23:07:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
refetch() {
|
2022-06-21 21:42:29 +00:00
|
|
|
let listCommunitiesForm = new ListCommunities({
|
|
|
|
type_: Some(this.state.listingType),
|
|
|
|
sort: Some(SortType.TopMonth),
|
|
|
|
limit: Some(communityLimit),
|
|
|
|
page: Some(this.state.page),
|
|
|
|
auth: auth(false).ok(),
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.listCommunities(listCommunitiesForm)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
2021-02-22 02:39:04 +00:00
|
|
|
let pathSplit = req.path.split("/");
|
2022-06-21 21:42:29 +00:00
|
|
|
let type_: Option<ListingType> = Some(
|
|
|
|
pathSplit[3] ? ListingType[pathSplit[3]] : ListingType.Local
|
|
|
|
);
|
|
|
|
let page = Some(pathSplit[5] ? Number(pathSplit[5]) : 1);
|
|
|
|
let listCommunitiesForm = new ListCommunities({
|
2021-07-18 16:17:50 +00:00
|
|
|
type_,
|
2022-06-21 21:42:29 +00:00
|
|
|
sort: Some(SortType.TopMonth),
|
|
|
|
limit: Some(communityLimit),
|
2020-09-07 03:41:46 +00:00
|
|
|
page,
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: req.auth,
|
|
|
|
});
|
2020-09-07 03:41:46 +00:00
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
return [req.client.listCommunities(listCommunitiesForm)];
|
2020-09-07 03:41:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
return;
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.ListCommunities) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<ListCommunitiesResponse>(
|
|
|
|
msg,
|
|
|
|
ListCommunitiesResponse
|
|
|
|
);
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ listCommunitiesResponse: Some(data), loading: false });
|
2020-09-06 16:15:25 +00:00
|
|
|
window.scrollTo(0, 0);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.FollowCommunity) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
|
|
|
|
this.state.listCommunitiesResponse.match({
|
|
|
|
some: res => {
|
|
|
|
let found = res.communities.find(
|
|
|
|
c => c.community.id == data.community_view.community.id
|
|
|
|
);
|
|
|
|
found.subscribed = data.community_view.subscribed;
|
|
|
|
found.counts.subscribers = data.community_view.counts.subscribers;
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|