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)}
132 onChange={this.handleListingTypeChange}
136 <div class="col-md-6">
137 <div class="float-md-right">{this.searchForm()}</div>
141 <div class="table-responsive">
142 <table id="community_table" class="table table-sm table-hover">
143 <thead class="pointer">
145 <th>{i18n.t("name")}</th>
146 <th class="text-right">{i18n.t("subscribers")}</th>
147 <th class="text-right">
148 {i18n.t("users")} / {i18n.t("month")}
150 <th class="text-right d-none d-lg-table-cell">
153 <th class="text-right d-none d-lg-table-cell">
160 {this.state.communities.map(cv => (
163 <CommunityLink community={cv.community} />
165 <td class="text-right">
166 {numToSI(cv.counts.subscribers)}
168 <td class="text-right">
169 {numToSI(cv.counts.users_active_month)}
171 <td class="text-right d-none d-lg-table-cell">
172 {numToSI(cv.counts.posts)}
174 <td class="text-right d-none d-lg-table-cell">
175 {numToSI(cv.counts.comments)}
177 <td class="text-right">
180 class="btn btn-link d-inline-block"
183 this.handleUnsubscribe
186 {i18n.t("unsubscribe")}
190 class="btn btn-link d-inline-block"
196 {i18n.t("subscribe")}
206 page={this.state.page}
207 onChange={this.handlePageChange}
219 onSubmit={linkEvent(this, this.handleSearchSubmit)}
223 id="communities-search"
224 class="form-control mr-2 mb-2"
225 value={this.state.searchText}
226 placeholder={`${i18n.t("search")}...`}
227 onInput={linkEvent(this, this.handleSearchChange)}
231 <label class="sr-only" htmlFor="communities-search">
234 <button type="submit" class="btn btn-secondary mr-2 mb-2">
235 <span>{i18n.t("search")}</span>
241 updateUrl(paramUpdates: CommunitiesProps) {
242 const page = paramUpdates.page || this.state.page;
243 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
244 this.props.history.push(
245 `/communities/listing_type/${listingTypeStr}/page/${page}`
249 handlePageChange(page: number) {
250 this.updateUrl({ page });
253 handleListingTypeChange(val: ListingType) {
260 handleUnsubscribe(communityId: number) {
261 let form: FollowCommunity = {
262 community_id: communityId,
266 WebSocketService.Instance.send(wsClient.followCommunity(form));
269 handleSubscribe(communityId: number) {
270 let form: FollowCommunity = {
271 community_id: communityId,
275 WebSocketService.Instance.send(wsClient.followCommunity(form));
278 handleSearchChange(i: Communities, event: any) {
279 i.setState({ searchText: event.target.value });
282 handleSearchSubmit(i: Communities) {
283 const searchParamEncoded = encodeURIComponent(i.state.searchText);
284 i.context.router.history.push(
285 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
290 let listCommunitiesForm: ListCommunities = {
291 type_: this.state.listingType,
292 sort: SortType.TopMonth,
293 limit: communityLimit,
294 page: this.state.page,
295 auth: authField(false),
298 WebSocketService.Instance.send(
299 wsClient.listCommunities(listCommunitiesForm)
303 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
304 let pathSplit = req.path.split("/");
305 let type_: ListingType = pathSplit[3]
306 ? ListingType[pathSplit[3]]
308 let page = pathSplit[5] ? Number(pathSplit[5]) : 1;
309 let listCommunitiesForm: ListCommunities = {
311 sort: SortType.TopMonth,
312 limit: communityLimit,
315 setOptionalAuth(listCommunitiesForm, req.auth);
317 return [req.client.listCommunities(listCommunitiesForm)];
320 parseMessage(msg: any) {
321 let op = wsUserOp(msg);
324 toast(i18n.t(msg.error), "danger");
326 } else if (op == UserOperation.ListCommunities) {
327 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
328 this.state.communities = data.communities;
329 this.state.loading = false;
330 window.scrollTo(0, 0);
331 this.setState(this.state);
332 } else if (op == UserOperation.FollowCommunity) {
333 let data = wsJsonToRes<CommunityResponse>(msg).data;
334 let found = this.state.communities.find(
335 c => c.community.id == data.community_view.community.id
337 found.subscribed = data.community_view.subscribed;
338 found.counts.subscribers = data.community_view.counts.subscribers;
339 this.setState(this.state);