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 = 100;
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);
78 this.state.listCommunitiesResponse = listRes;
79 this.state.loading = false;
85 componentWillUnmount() {
87 this.subscription.unsubscribe();
91 static getDerivedStateFromProps(props: any): CommunitiesProps {
93 listingType: getListingTypeFromPropsNoDefault(props),
94 page: getPageFromProps(props),
98 componentDidUpdate(_: any, lastState: CommunitiesState) {
100 lastState.page !== this.state.page ||
101 lastState.listingType !== this.state.listingType
103 this.setState({ loading: true });
108 get documentTitle(): string {
109 return this.state.siteRes.site_view.match({
110 some: siteView => `${i18n.t("communities")} - ${siteView.site.name}`,
117 <div class="container">
119 title={this.documentTitle}
120 path={this.context.router.route.match.url}
124 {this.state.loading ? (
131 <div class="col-md-6">
132 <h4>{i18n.t("list_of_communities")}</h4>
135 type_={this.state.listingType}
136 showLocal={showLocal(this.isoData)}
138 onChange={this.handleListingTypeChange}
142 <div class="col-md-6">
143 <div class="float-md-right">{this.searchForm()}</div>
147 <div class="table-responsive">
148 <table id="community_table" class="table table-sm table-hover">
149 <thead class="pointer">
151 <th>{i18n.t("name")}</th>
152 <th class="text-right">{i18n.t("subscribers")}</th>
153 <th class="text-right">
154 {i18n.t("users")} / {i18n.t("month")}
156 <th class="text-right d-none d-lg-table-cell">
159 <th class="text-right d-none d-lg-table-cell">
166 {this.state.listCommunitiesResponse
167 .map(l => l.communities)
172 <CommunityLink community={cv.community} />
174 <td class="text-right">
175 {numToSI(cv.counts.subscribers)}
177 <td class="text-right">
178 {numToSI(cv.counts.users_active_month)}
180 <td class="text-right d-none d-lg-table-cell">
181 {numToSI(cv.counts.posts)}
183 <td class="text-right d-none d-lg-table-cell">
184 {numToSI(cv.counts.comments)}
186 <td class="text-right">
187 {cv.subscribed == SubscribedType.Subscribed && (
189 class="btn btn-link d-inline-block"
192 this.handleUnsubscribe
195 {i18n.t("unsubscribe")}
198 {cv.subscribed == SubscribedType.NotSubscribed && (
200 class="btn btn-link d-inline-block"
206 {i18n.t("subscribe")}
209 {cv.subscribed == SubscribedType.Pending && (
210 <div class="text-warning d-inline-block">
211 {i18n.t("subscribe_pending")}
221 page={this.state.page}
222 onChange={this.handlePageChange}
234 onSubmit={linkEvent(this, this.handleSearchSubmit)}
238 id="communities-search"
239 class="form-control mr-2 mb-2"
240 value={this.state.searchText}
241 placeholder={`${i18n.t("search")}...`}
242 onInput={linkEvent(this, this.handleSearchChange)}
246 <label class="sr-only" htmlFor="communities-search">
249 <button type="submit" class="btn btn-secondary mr-2 mb-2">
250 <span>{i18n.t("search")}</span>
256 updateUrl(paramUpdates: CommunitiesProps) {
257 const page = paramUpdates.page || this.state.page;
258 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
259 this.props.history.push(
260 `/communities/listing_type/${listingTypeStr}/page/${page}`
264 handlePageChange(page: number) {
265 this.updateUrl({ page });
268 handleListingTypeChange(val: ListingType) {
275 handleUnsubscribe(communityId: number) {
276 let form = new FollowCommunity({
277 community_id: communityId,
279 auth: auth().unwrap(),
281 WebSocketService.Instance.send(wsClient.followCommunity(form));
284 handleSubscribe(communityId: number) {
285 let form = new FollowCommunity({
286 community_id: communityId,
288 auth: auth().unwrap(),
290 WebSocketService.Instance.send(wsClient.followCommunity(form));
293 handleSearchChange(i: Communities, event: any) {
294 i.setState({ searchText: event.target.value });
297 handleSearchSubmit(i: Communities) {
298 const searchParamEncoded = encodeURIComponent(i.state.searchText);
299 i.context.router.history.push(
300 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
305 let listCommunitiesForm = new ListCommunities({
306 type_: Some(this.state.listingType),
307 sort: Some(SortType.TopMonth),
308 limit: Some(communityLimit),
309 page: Some(this.state.page),
310 auth: auth(false).ok(),
313 WebSocketService.Instance.send(
314 wsClient.listCommunities(listCommunitiesForm)
318 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
319 let pathSplit = req.path.split("/");
320 let type_: Option<ListingType> = Some(
321 pathSplit[3] ? ListingType[pathSplit[3]] : ListingType.Local
323 let page = Some(pathSplit[5] ? Number(pathSplit[5]) : 1);
324 let listCommunitiesForm = new ListCommunities({
326 sort: Some(SortType.TopMonth),
327 limit: Some(communityLimit),
332 return [req.client.listCommunities(listCommunitiesForm)];
335 parseMessage(msg: any) {
336 let op = wsUserOp(msg);
339 toast(i18n.t(msg.error), "danger");
341 } else if (op == UserOperation.ListCommunities) {
342 let data = wsJsonToRes<ListCommunitiesResponse>(
344 ListCommunitiesResponse
346 this.state.listCommunitiesResponse = Some(data);
347 this.state.loading = false;
348 window.scrollTo(0, 0);
349 this.setState(this.state);
350 } else if (op == UserOperation.FollowCommunity) {
351 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
352 this.state.listCommunitiesResponse.match({
354 let found = res.communities.find(
355 c => c.community.id == data.community_view.community.id
357 found.subscribed = data.community_view.subscribed;
358 found.counts.subscribers = data.community_view.counts.subscribers;
362 this.setState(this.state);