1 import { None, Option, Some } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
8 ListCommunitiesResponse,
14 } from "lemmy-js-client";
15 import { Subscription } from "rxjs";
16 import { InitialFetchRequest } from "shared/interfaces";
17 import { i18n } from "../../i18next";
18 import { WebSocketService } from "../../services";
21 getListingTypeFromPropsNoDefault,
31 import { HtmlTags } from "../common/html-tags";
32 import { Spinner } from "../common/icon";
33 import { ListingTypeSelect } from "../common/listing-type-select";
34 import { Paginator } from "../common/paginator";
35 import { CommunityLink } from "./community-link";
37 const communityLimit = 100;
39 interface CommunitiesState {
40 listCommunitiesResponse: Option<ListCommunitiesResponse>;
43 siteRes: GetSiteResponse;
45 listingType: ListingType;
48 interface CommunitiesProps {
49 listingType?: ListingType;
53 export class Communities extends Component<any, CommunitiesState> {
54 private subscription: Subscription;
55 private isoData = setIsoData(this.context, ListCommunitiesResponse);
56 private emptyState: CommunitiesState = {
57 listCommunitiesResponse: None,
59 page: getPageFromProps(this.props),
60 listingType: getListingTypeFromPropsNoDefault(this.props),
61 siteRes: this.isoData.site_res,
65 constructor(props: any, context: any) {
66 super(props, context);
67 this.state = this.emptyState;
68 this.handlePageChange = this.handlePageChange.bind(this);
69 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
71 this.parseMessage = this.parseMessage.bind(this);
72 this.subscription = wsSubscribe(this.parseMessage);
74 // Only fetch the data if coming from another route
75 if (this.isoData.path == this.context.router.route.match.url) {
76 let listRes = Some(this.isoData.routeData[0] as ListCommunitiesResponse);
77 this.state.listCommunitiesResponse = listRes;
78 this.state.loading = false;
84 componentWillUnmount() {
86 this.subscription.unsubscribe();
90 static getDerivedStateFromProps(props: any): CommunitiesProps {
92 listingType: getListingTypeFromPropsNoDefault(props),
93 page: getPageFromProps(props),
97 componentDidUpdate(_: any, lastState: CommunitiesState) {
99 lastState.page !== this.state.page ||
100 lastState.listingType !== this.state.listingType
102 this.setState({ loading: true });
107 get documentTitle(): string {
108 return this.state.siteRes.site_view.match({
109 some: siteView => `${i18n.t("communities")} - ${siteView.site.name}`,
116 <div class="container">
118 title={this.documentTitle}
119 path={this.context.router.route.match.url}
123 {this.state.loading ? (
130 <div class="col-md-6">
131 <h4>{i18n.t("list_of_communities")}</h4>
134 type_={this.state.listingType}
135 showLocal={showLocal(this.isoData)}
137 onChange={this.handleListingTypeChange}
141 <div class="col-md-6">
142 <div class="float-md-right">{this.searchForm()}</div>
146 <div class="table-responsive">
147 <table id="community_table" class="table table-sm table-hover">
148 <thead class="pointer">
150 <th>{i18n.t("name")}</th>
151 <th class="text-right">{i18n.t("subscribers")}</th>
152 <th class="text-right">
153 {i18n.t("users")} / {i18n.t("month")}
155 <th class="text-right d-none d-lg-table-cell">
158 <th class="text-right d-none d-lg-table-cell">
165 {this.state.listCommunitiesResponse
166 .map(l => l.communities)
171 <CommunityLink community={cv.community} />
173 <td class="text-right">
174 {numToSI(cv.counts.subscribers)}
176 <td class="text-right">
177 {numToSI(cv.counts.users_active_month)}
179 <td class="text-right d-none d-lg-table-cell">
180 {numToSI(cv.counts.posts)}
182 <td class="text-right d-none d-lg-table-cell">
183 {numToSI(cv.counts.comments)}
185 <td class="text-right">
188 class="btn btn-link d-inline-block"
191 this.handleUnsubscribe
194 {i18n.t("unsubscribe")}
198 class="btn btn-link d-inline-block"
204 {i18n.t("subscribe")}
214 page={this.state.page}
215 onChange={this.handlePageChange}
227 onSubmit={linkEvent(this, this.handleSearchSubmit)}
231 id="communities-search"
232 class="form-control mr-2 mb-2"
233 value={this.state.searchText}
234 placeholder={`${i18n.t("search")}...`}
235 onInput={linkEvent(this, this.handleSearchChange)}
239 <label class="sr-only" htmlFor="communities-search">
242 <button type="submit" class="btn btn-secondary mr-2 mb-2">
243 <span>{i18n.t("search")}</span>
249 updateUrl(paramUpdates: CommunitiesProps) {
250 const page = paramUpdates.page || this.state.page;
251 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
252 this.props.history.push(
253 `/communities/listing_type/${listingTypeStr}/page/${page}`
257 handlePageChange(page: number) {
258 this.updateUrl({ page });
261 handleListingTypeChange(val: ListingType) {
268 handleUnsubscribe(communityId: number) {
269 let form = new FollowCommunity({
270 community_id: communityId,
272 auth: auth().unwrap(),
274 WebSocketService.Instance.send(wsClient.followCommunity(form));
277 handleSubscribe(communityId: number) {
278 let form = new FollowCommunity({
279 community_id: communityId,
281 auth: auth().unwrap(),
283 WebSocketService.Instance.send(wsClient.followCommunity(form));
286 handleSearchChange(i: Communities, event: any) {
287 i.setState({ searchText: event.target.value });
290 handleSearchSubmit(i: Communities) {
291 const searchParamEncoded = encodeURIComponent(i.state.searchText);
292 i.context.router.history.push(
293 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
298 let listCommunitiesForm = new ListCommunities({
299 type_: Some(this.state.listingType),
300 sort: Some(SortType.TopMonth),
301 limit: Some(communityLimit),
302 page: Some(this.state.page),
303 auth: auth(false).ok(),
306 WebSocketService.Instance.send(
307 wsClient.listCommunities(listCommunitiesForm)
311 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
312 let pathSplit = req.path.split("/");
313 let type_: Option<ListingType> = Some(
314 pathSplit[3] ? ListingType[pathSplit[3]] : ListingType.Local
316 let page = Some(pathSplit[5] ? Number(pathSplit[5]) : 1);
317 let listCommunitiesForm = new ListCommunities({
319 sort: Some(SortType.TopMonth),
320 limit: Some(communityLimit),
325 return [req.client.listCommunities(listCommunitiesForm)];
328 parseMessage(msg: any) {
329 let op = wsUserOp(msg);
332 toast(i18n.t(msg.error), "danger");
334 } else if (op == UserOperation.ListCommunities) {
335 let data = wsJsonToRes<ListCommunitiesResponse>(
337 ListCommunitiesResponse
339 this.state.listCommunitiesResponse = Some(data);
340 this.state.loading = false;
341 window.scrollTo(0, 0);
342 this.setState(this.state);
343 } else if (op == UserOperation.FollowCommunity) {
344 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
345 this.state.listCommunitiesResponse.match({
347 let found = res.communities.find(
348 c => c.community.id == data.community_view.community.id
350 found.subscribed = data.community_view.subscribed;
351 found.counts.subscribers = data.community_view.counts.subscribers;
355 this.setState(this.state);