1 import { Component, linkEvent } from "inferno";
7 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";
20 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 = 50;
39 interface CommunitiesState {
40 listCommunitiesResponse?: 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);
56 state: CommunitiesState = {
58 page: getPageFromProps(this.props),
59 listingType: getListingTypeFromPropsNoDefault(this.props),
60 siteRes: this.isoData.site_res,
64 constructor(props: any, context: any) {
65 super(props, context);
66 this.handlePageChange = this.handlePageChange.bind(this);
67 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
69 this.parseMessage = this.parseMessage.bind(this);
70 this.subscription = wsSubscribe(this.parseMessage);
72 // Only fetch the data if coming from another route
73 if (this.isoData.path == this.context.router.route.match.url) {
74 let listRes = this.isoData.routeData[0] as ListCommunitiesResponse;
77 listCommunitiesResponse: listRes,
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 `${i18n.t("communities")} - ${
110 this.state.siteRes.site_view.site.name
116 <div className="container-lg">
118 title={this.documentTitle}
119 path={this.context.router.route.match.url}
121 {this.state.loading ? (
127 <div className="row">
128 <div className="col-md-6">
129 <h4>{i18n.t("list_of_communities")}</h4>
130 <span className="mb-2">
132 type_={this.state.listingType}
133 showLocal={showLocal(this.isoData)}
135 onChange={this.handleListingTypeChange}
139 <div className="col-md-6">
140 <div className="float-md-right">{this.searchForm()}</div>
144 <div className="table-responsive">
147 className="table table-sm table-hover"
149 <thead className="pointer">
151 <th>{i18n.t("name")}</th>
152 <th className="text-right">{i18n.t("subscribers")}</th>
153 <th className="text-right">
154 {i18n.t("users")} / {i18n.t("month")}
156 <th className="text-right d-none d-lg-table-cell">
159 <th className="text-right d-none d-lg-table-cell">
166 {this.state.listCommunitiesResponse?.communities.map(cv => (
167 <tr key={cv.community.id}>
169 <CommunityLink community={cv.community} />
171 <td className="text-right">
172 {numToSI(cv.counts.subscribers)}
174 <td className="text-right">
175 {numToSI(cv.counts.users_active_month)}
177 <td className="text-right d-none d-lg-table-cell">
178 {numToSI(cv.counts.posts)}
180 <td className="text-right d-none d-lg-table-cell">
181 {numToSI(cv.counts.comments)}
183 <td className="text-right">
184 {cv.subscribed == SubscribedType.Subscribed && (
186 className="btn btn-link d-inline-block"
189 this.handleUnsubscribe
192 {i18n.t("unsubscribe")}
195 {cv.subscribed == SubscribedType.NotSubscribed && (
197 className="btn btn-link d-inline-block"
203 {i18n.t("subscribe")}
206 {cv.subscribed == SubscribedType.Pending && (
207 <div className="text-warning d-inline-block">
208 {i18n.t("subscribe_pending")}
218 page={this.state.page}
219 onChange={this.handlePageChange}
230 className="form-inline"
231 onSubmit={linkEvent(this, this.handleSearchSubmit)}
235 id="communities-search"
236 className="form-control mr-2 mb-2"
237 value={this.state.searchText}
238 placeholder={`${i18n.t("search")}...`}
239 onInput={linkEvent(this, this.handleSearchChange)}
243 <label className="sr-only" htmlFor="communities-search">
246 <button type="submit" className="btn btn-secondary mr-2 mb-2">
247 <span>{i18n.t("search")}</span>
253 updateUrl(paramUpdates: CommunitiesProps) {
254 const page = paramUpdates.page || this.state.page;
255 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
256 this.props.history.push(
257 `/communities/listing_type/${listingTypeStr}/page/${page}`
261 handlePageChange(page: number) {
262 this.updateUrl({ page });
265 handleListingTypeChange(val: ListingType) {
272 handleUnsubscribe(communityId: number) {
275 let form: FollowCommunity = {
276 community_id: communityId,
280 WebSocketService.Instance.send(wsClient.followCommunity(form));
284 handleSubscribe(communityId: number) {
287 let form: FollowCommunity = {
288 community_id: communityId,
292 WebSocketService.Instance.send(wsClient.followCommunity(form));
296 handleSearchChange(i: Communities, event: any) {
297 i.setState({ searchText: event.target.value });
300 handleSearchSubmit(i: Communities) {
301 const searchParamEncoded = encodeURIComponent(i.state.searchText);
302 i.context.router.history.push(
303 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
308 let listCommunitiesForm: ListCommunities = {
309 type_: this.state.listingType,
310 sort: SortType.TopMonth,
311 limit: communityLimit,
312 page: this.state.page,
316 WebSocketService.Instance.send(
317 wsClient.listCommunities(listCommunitiesForm)
321 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
322 let pathSplit = req.path.split("/");
323 let type_: ListingType = pathSplit[3]
324 ? ListingType[pathSplit[3]]
326 let page = pathSplit[5] ? Number(pathSplit[5]) : 1;
327 let listCommunitiesForm: ListCommunities = {
329 sort: SortType.TopMonth,
330 limit: communityLimit,
335 return [req.client.listCommunities(listCommunitiesForm)];
338 parseMessage(msg: any) {
339 let op = wsUserOp(msg);
342 toast(i18n.t(msg.error), "danger");
344 } else if (op == UserOperation.ListCommunities) {
345 let data = wsJsonToRes<ListCommunitiesResponse>(msg);
346 this.setState({ listCommunitiesResponse: data, loading: false });
347 window.scrollTo(0, 0);
348 } else if (op == UserOperation.FollowCommunity) {
349 let data = wsJsonToRes<CommunityResponse>(msg);
350 let res = this.state.listCommunitiesResponse;
351 let found = res?.communities.find(
352 c => c.community.id == data.community_view.community.id
355 found.subscribed = data.community_view.subscribed;
356 found.counts.subscribers = data.community_view.counts.subscribers;
357 this.setState(this.state);