1 import { None, Option, Some } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
8 ListCommunitiesResponse,
15 } from "lemmy-js-client";
16 import { Subscription } from "rxjs";
17 import { InitialFetchRequest } from "shared/interfaces";
18 import { i18n } from "../../i18next";
19 import { WebSocketService } from "../../services";
22 getListingTypeFromPropsNoDefault,
32 import { HtmlTags } from "../common/html-tags";
33 import { Spinner } from "../common/icon";
34 import { ListingTypeSelect } from "../common/listing-type-select";
35 import { Paginator } from "../common/paginator";
36 import { CommunityLink } from "./community-link";
38 const communityLimit = 50;
40 interface CommunitiesState {
41 listCommunitiesResponse: Option<ListCommunitiesResponse>;
44 siteRes: GetSiteResponse;
46 listingType: ListingType;
49 interface CommunitiesProps {
50 listingType?: ListingType;
54 export class Communities extends Component<any, CommunitiesState> {
55 private subscription: Subscription;
56 private isoData = setIsoData(this.context, ListCommunitiesResponse);
57 private emptyState: CommunitiesState = {
58 listCommunitiesResponse: None,
60 page: getPageFromProps(this.props),
61 listingType: getListingTypeFromPropsNoDefault(this.props),
62 siteRes: this.isoData.site_res,
66 constructor(props: any, context: any) {
67 super(props, context);
68 this.state = this.emptyState;
69 this.handlePageChange = this.handlePageChange.bind(this);
70 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
72 this.parseMessage = this.parseMessage.bind(this);
73 this.subscription = wsSubscribe(this.parseMessage);
75 // Only fetch the data if coming from another route
76 if (this.isoData.path == this.context.router.route.match.url) {
77 let listRes = Some(this.isoData.routeData[0] as ListCommunitiesResponse);
80 listCommunitiesResponse: listRes,
88 componentWillUnmount() {
90 this.subscription.unsubscribe();
94 static getDerivedStateFromProps(props: any): CommunitiesProps {
96 listingType: getListingTypeFromPropsNoDefault(props),
97 page: getPageFromProps(props),
101 componentDidUpdate(_: any, lastState: CommunitiesState) {
103 lastState.page !== this.state.page ||
104 lastState.listingType !== this.state.listingType
106 this.setState({ loading: true });
111 get documentTitle(): string {
112 return this.state.siteRes.site_view.match({
113 some: siteView => `${i18n.t("communities")} - ${siteView.site.name}`,
120 <div className="container">
122 title={this.documentTitle}
123 path={this.context.router.route.match.url}
127 {this.state.loading ? (
133 <div className="row">
134 <div className="col-md-6">
135 <h4>{i18n.t("list_of_communities")}</h4>
136 <span className="mb-2">
138 type_={this.state.listingType}
139 showLocal={showLocal(this.isoData)}
141 onChange={this.handleListingTypeChange}
145 <div className="col-md-6">
146 <div className="float-md-right">{this.searchForm()}</div>
150 <div className="table-responsive">
153 className="table table-sm table-hover"
155 <thead className="pointer">
157 <th>{i18n.t("name")}</th>
158 <th className="text-right">{i18n.t("subscribers")}</th>
159 <th className="text-right">
160 {i18n.t("users")} / {i18n.t("month")}
162 <th className="text-right d-none d-lg-table-cell">
165 <th className="text-right d-none d-lg-table-cell">
172 {this.state.listCommunitiesResponse
173 .map(l => l.communities)
176 <tr key={cv.community.id}>
178 <CommunityLink community={cv.community} />
180 <td className="text-right">
181 {numToSI(cv.counts.subscribers)}
183 <td className="text-right">
184 {numToSI(cv.counts.users_active_month)}
186 <td className="text-right d-none d-lg-table-cell">
187 {numToSI(cv.counts.posts)}
189 <td className="text-right d-none d-lg-table-cell">
190 {numToSI(cv.counts.comments)}
192 <td className="text-right">
193 {cv.subscribed == SubscribedType.Subscribed && (
195 className="btn btn-link d-inline-block"
198 this.handleUnsubscribe
201 {i18n.t("unsubscribe")}
204 {cv.subscribed == SubscribedType.NotSubscribed && (
206 className="btn btn-link d-inline-block"
212 {i18n.t("subscribe")}
215 {cv.subscribed == SubscribedType.Pending && (
216 <div className="text-warning d-inline-block">
217 {i18n.t("subscribe_pending")}
227 page={this.state.page}
228 onChange={this.handlePageChange}
239 className="form-inline"
240 onSubmit={linkEvent(this, this.handleSearchSubmit)}
244 id="communities-search"
245 className="form-control mr-2 mb-2"
246 value={this.state.searchText}
247 placeholder={`${i18n.t("search")}...`}
248 onInput={linkEvent(this, this.handleSearchChange)}
252 <label className="sr-only" htmlFor="communities-search">
255 <button type="submit" className="btn btn-secondary mr-2 mb-2">
256 <span>{i18n.t("search")}</span>
262 updateUrl(paramUpdates: CommunitiesProps) {
263 const page = paramUpdates.page || this.state.page;
264 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
265 this.props.history.push(
266 `/communities/listing_type/${listingTypeStr}/page/${page}`
270 handlePageChange(page: number) {
271 this.updateUrl({ page });
274 handleListingTypeChange(val: ListingType) {
281 handleUnsubscribe(communityId: number) {
282 let form = new FollowCommunity({
283 community_id: communityId,
285 auth: auth().unwrap(),
287 WebSocketService.Instance.send(wsClient.followCommunity(form));
290 handleSubscribe(communityId: number) {
291 let form = new FollowCommunity({
292 community_id: communityId,
294 auth: auth().unwrap(),
296 WebSocketService.Instance.send(wsClient.followCommunity(form));
299 handleSearchChange(i: Communities, event: any) {
300 i.setState({ searchText: event.target.value });
303 handleSearchSubmit(i: Communities) {
304 const searchParamEncoded = encodeURIComponent(i.state.searchText);
305 i.context.router.history.push(
306 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
311 let listCommunitiesForm = new ListCommunities({
312 type_: Some(this.state.listingType),
313 sort: Some(SortType.TopMonth),
314 limit: Some(communityLimit),
315 page: Some(this.state.page),
316 auth: auth(false).ok(),
319 WebSocketService.Instance.send(
320 wsClient.listCommunities(listCommunitiesForm)
324 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
325 let pathSplit = req.path.split("/");
326 let type_: Option<ListingType> = Some(
327 pathSplit[3] ? ListingType[pathSplit[3]] : ListingType.Local
329 let page = Some(pathSplit[5] ? Number(pathSplit[5]) : 1);
330 let listCommunitiesForm = new ListCommunities({
332 sort: Some(SortType.TopMonth),
333 limit: Some(communityLimit),
338 return [req.client.listCommunities(listCommunitiesForm)];
341 parseMessage(msg: any) {
342 let op = wsUserOp(msg);
345 toast(i18n.t(msg.error), "danger");
347 } else if (op == UserOperation.ListCommunities) {
348 let data = wsJsonToRes<ListCommunitiesResponse>(
350 ListCommunitiesResponse
352 this.setState({ listCommunitiesResponse: Some(data), loading: false });
353 window.scrollTo(0, 0);
354 } else if (op == UserOperation.FollowCommunity) {
355 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
356 this.state.listCommunitiesResponse.match({
358 let found = res.communities.find(
359 c => c.community.id == data.community_view.community.id
361 found.subscribed = data.community_view.subscribed;
362 found.counts.subscribers = data.community_view.counts.subscribers;
366 this.setState(this.state);