1 import { Component, linkEvent } from "inferno";
7 ListCommunitiesResponse,
12 } from "lemmy-js-client";
13 import { Subscription } from "rxjs";
14 import { InitialFetchRequest } from "shared/interfaces";
15 import { i18n } from "../../i18next";
16 import { WebSocketService } from "../../services";
19 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 communities: CommunityView[];
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);
57 private emptyState: CommunitiesState = {
60 page: getPageFromProps(this.props),
61 listingType: getListingTypeFromPropsNoDefault(this.props),
62 site_view: this.isoData.site_res.site_view,
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 this.state.communities = this.isoData.routeData[0].communities;
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 `${i18n.t("communities")} - ${this.state.site_view.site.name}`;
113 <div class="container">
115 title={this.documentTitle}
116 path={this.context.router.route.match.url}
118 {this.state.loading ? (
125 <div class="col-md-6">
126 <h4>{i18n.t("list_of_communities")}</h4>
129 type_={this.state.listingType}
130 showLocal={showLocal(this.isoData)}
131 onChange={this.handleListingTypeChange}
135 <div class="col-md-6">
136 <div class="float-md-right">{this.searchForm()}</div>
140 <div class="table-responsive">
141 <table id="community_table" class="table table-sm table-hover">
142 <thead class="pointer">
144 <th>{i18n.t("name")}</th>
145 <th class="text-right">{i18n.t("subscribers")}</th>
146 <th class="text-right">
147 {i18n.t("users")} / {i18n.t("month")}
149 <th class="text-right d-none d-lg-table-cell">
152 <th class="text-right d-none d-lg-table-cell">
159 {this.state.communities.map(cv => (
162 <CommunityLink community={cv.community} />
164 <td class="text-right">
165 {numToSI(cv.counts.subscribers)}
167 <td class="text-right">
168 {numToSI(cv.counts.users_active_month)}
170 <td class="text-right d-none d-lg-table-cell">
171 {numToSI(cv.counts.posts)}
173 <td class="text-right d-none d-lg-table-cell">
174 {numToSI(cv.counts.comments)}
176 <td class="text-right">
179 class="btn btn-link d-inline-block"
182 this.handleUnsubscribe
185 {i18n.t("unsubscribe")}
189 class="btn btn-link d-inline-block"
195 {i18n.t("subscribe")}
205 page={this.state.page}
206 onChange={this.handlePageChange}
218 onSubmit={linkEvent(this, this.handleSearchSubmit)}
222 id="communities-search"
223 class="form-control mr-2 mb-2"
224 value={this.state.searchText}
225 placeholder={`${i18n.t("search")}...`}
226 onInput={linkEvent(this, this.handleSearchChange)}
230 <label class="sr-only" htmlFor="communities-search">
233 <button type="submit" class="btn btn-secondary mr-2 mb-2">
234 <span>{i18n.t("search")}</span>
240 updateUrl(paramUpdates: CommunitiesProps) {
241 const page = paramUpdates.page || this.state.page;
242 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
243 this.props.history.push(
244 `/communities/listing_type/${listingTypeStr}/page/${page}`
248 handlePageChange(page: number) {
249 this.updateUrl({ page });
252 handleListingTypeChange(val: ListingType) {
259 handleUnsubscribe(communityId: number) {
260 let form: FollowCommunity = {
261 community_id: communityId,
265 WebSocketService.Instance.send(wsClient.followCommunity(form));
268 handleSubscribe(communityId: number) {
269 let form: FollowCommunity = {
270 community_id: communityId,
274 WebSocketService.Instance.send(wsClient.followCommunity(form));
277 handleSearchChange(i: Communities, event: any) {
278 i.setState({ searchText: event.target.value });
281 handleSearchSubmit(i: Communities) {
282 const searchParamEncoded = encodeURIComponent(i.state.searchText);
283 i.context.router.history.push(
284 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
289 let listCommunitiesForm: ListCommunities = {
290 type_: this.state.listingType,
291 sort: SortType.TopMonth,
292 limit: communityLimit,
293 page: this.state.page,
294 auth: authField(false),
297 WebSocketService.Instance.send(
298 wsClient.listCommunities(listCommunitiesForm)
302 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
303 let pathSplit = req.path.split("/");
304 let type_: ListingType = pathSplit[3]
305 ? ListingType[pathSplit[3]]
307 let page = pathSplit[5] ? Number(pathSplit[5]) : 1;
308 let listCommunitiesForm: ListCommunities = {
310 sort: SortType.TopMonth,
311 limit: communityLimit,
314 setOptionalAuth(listCommunitiesForm, req.auth);
316 return [req.client.listCommunities(listCommunitiesForm)];
319 parseMessage(msg: any) {
320 let op = wsUserOp(msg);
323 toast(i18n.t(msg.error), "danger");
325 } else if (op == UserOperation.ListCommunities) {
326 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
327 this.state.communities = data.communities;
328 this.state.loading = false;
329 window.scrollTo(0, 0);
330 this.setState(this.state);
331 } else if (op == UserOperation.FollowCommunity) {
332 let data = wsJsonToRes<CommunityResponse>(msg).data;
333 let found = this.state.communities.find(
334 c => c.community.id == data.community_view.community.id
336 found.subscribed = data.community_view.subscribed;
337 found.counts.subscribers = data.community_view.counts.subscribers;
338 this.setState(this.state);