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";
29 import { HtmlTags } from "../common/html-tags";
30 import { Spinner } from "../common/icon";
31 import { Paginator } from "../common/paginator";
32 import { CommunityLink } from "./community-link";
34 const communityLimit = 100;
36 interface CommunitiesState {
37 communities: CommunityView[];
44 interface CommunitiesProps {
48 export class Communities extends Component<any, CommunitiesState> {
49 private subscription: Subscription;
50 private isoData = setIsoData(this.context);
51 private emptyState: CommunitiesState = {
54 page: getPageFromProps(this.props),
55 site_view: this.isoData.site_res.site_view,
59 constructor(props: any, context: any) {
60 super(props, context);
61 this.state = this.emptyState;
62 this.handlePageChange = this.handlePageChange.bind(this);
64 this.parseMessage = this.parseMessage.bind(this);
65 this.subscription = wsSubscribe(this.parseMessage);
67 // Only fetch the data if coming from another route
68 if (this.isoData.path == this.context.router.route.match.url) {
69 this.state.communities = this.isoData.routeData[0].communities;
70 this.state.loading = false;
76 componentWillUnmount() {
78 this.subscription.unsubscribe();
82 static getDerivedStateFromProps(props: any): CommunitiesProps {
84 page: getPageFromProps(props),
88 componentDidUpdate(_: any, lastState: CommunitiesState) {
89 if (lastState.page !== this.state.page) {
90 this.setState({ loading: true });
95 get documentTitle(): string {
96 return `${i18n.t("communities")} - ${this.state.site_view.site.name}`;
101 <div class="container">
103 title={this.documentTitle}
104 path={this.context.router.route.match.url}
106 {this.state.loading ? (
113 <div class="col-md-6">
114 <h4>{i18n.t("list_of_communities")}</h4>
116 <div class="col-md-6">
117 <div class="float-md-right">{this.searchForm()}</div>
121 <div class="table-responsive">
122 <table id="community_table" class="table table-sm table-hover">
123 <thead class="pointer">
125 <th>{i18n.t("name")}</th>
126 <th class="text-right">{i18n.t("subscribers")}</th>
127 <th class="text-right">
128 {i18n.t("users")} / {i18n.t("month")}
130 <th class="text-right d-none d-lg-table-cell">
133 <th class="text-right d-none d-lg-table-cell">
140 {this.state.communities.map(cv => (
143 <CommunityLink community={cv.community} />
145 <td class="text-right">{cv.counts.subscribers}</td>
146 <td class="text-right">{cv.counts.users_active_month}</td>
147 <td class="text-right d-none d-lg-table-cell">
150 <td class="text-right d-none d-lg-table-cell">
153 <td class="text-right">
156 class="btn btn-link d-inline-block"
159 this.handleUnsubscribe
162 {i18n.t("unsubscribe")}
166 class="btn btn-link d-inline-block"
172 {i18n.t("subscribe")}
182 page={this.state.page}
183 onChange={this.handlePageChange}
195 onSubmit={linkEvent(this, this.handleSearchSubmit)}
199 id="communities-search"
200 class="form-control mr-2 mb-2"
201 value={this.state.searchText}
202 placeholder={`${i18n.t("search")}...`}
203 onInput={linkEvent(this, this.handleSearchChange)}
207 <label class="sr-only" htmlFor="communities-search">
210 <button type="submit" class="btn btn-secondary mr-2 mb-2">
211 <span>{i18n.t("search")}</span>
217 updateUrl(paramUpdates: CommunitiesProps) {
218 const page = paramUpdates.page || this.state.page;
219 this.props.history.push(`/communities/page/${page}`);
222 handlePageChange(page: number) {
223 this.updateUrl({ page });
226 handleUnsubscribe(communityId: number) {
227 let form: FollowCommunity = {
228 community_id: communityId,
232 WebSocketService.Instance.send(wsClient.followCommunity(form));
235 handleSubscribe(communityId: number) {
236 let form: FollowCommunity = {
237 community_id: communityId,
241 WebSocketService.Instance.send(wsClient.followCommunity(form));
244 handleSearchChange(i: Communities, event: any) {
245 i.setState({ searchText: event.target.value });
248 handleSearchSubmit(i: Communities) {
249 const searchParamEncoded = encodeURIComponent(i.state.searchText);
250 i.context.router.history.push(
251 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
256 let listCommunitiesForm: ListCommunities = {
257 type_: ListingType.All,
258 sort: SortType.TopMonth,
259 limit: communityLimit,
260 page: this.state.page,
261 auth: authField(false),
264 WebSocketService.Instance.send(
265 wsClient.listCommunities(listCommunitiesForm)
269 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
270 let pathSplit = req.path.split("/");
271 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
272 let listCommunitiesForm: ListCommunities = {
273 type_: ListingType.All,
274 sort: SortType.TopMonth,
275 limit: communityLimit,
278 setOptionalAuth(listCommunitiesForm, req.auth);
280 return [req.client.listCommunities(listCommunitiesForm)];
283 parseMessage(msg: any) {
284 let op = wsUserOp(msg);
287 toast(i18n.t(msg.error), "danger");
289 } else if (op == UserOperation.ListCommunities) {
290 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
291 this.state.communities = data.communities;
292 this.state.loading = false;
293 window.scrollTo(0, 0);
294 this.setState(this.state);
295 } else if (op == UserOperation.FollowCommunity) {
296 let data = wsJsonToRes<CommunityResponse>(msg).data;
297 let found = this.state.communities.find(
298 c => c.community.id == data.community_view.community.id
300 found.subscribed = data.community_view.subscribed;
301 found.counts.subscribers = data.community_view.counts.subscribers;
302 this.setState(this.state);