1 import { Component, linkEvent } from "inferno";
2 import { HtmlTags } from "./html-tags";
3 import { Subscription } from "rxjs";
7 ListCommunitiesResponse,
14 } from "lemmy-js-client";
15 import { WebSocketService } from "../services";
28 import { CommunityLink } from "./community-link";
29 import { Spinner } from "./icon";
30 import { i18n } from "../i18next";
31 import { InitialFetchRequest } from "shared/interfaces";
33 const communityLimit = 100;
35 interface CommunitiesState {
36 communities: CommunityView[];
43 interface CommunitiesProps {
47 export class Communities extends Component<any, CommunitiesState> {
48 private subscription: Subscription;
49 private isoData = setIsoData(this.context);
50 private emptyState: CommunitiesState = {
53 page: getPageFromProps(this.props),
54 site_view: this.isoData.site_res.site_view,
58 constructor(props: any, context: any) {
59 super(props, context);
60 this.state = this.emptyState;
62 this.parseMessage = this.parseMessage.bind(this);
63 this.subscription = wsSubscribe(this.parseMessage);
65 // Only fetch the data if coming from another route
66 if (this.isoData.path == this.context.router.route.match.url) {
67 this.state.communities = this.isoData.routeData[0].communities;
68 this.state.communities.sort(
69 (a, b) => b.counts.subscribers - a.counts.subscribers
71 this.state.loading = false;
77 componentWillUnmount() {
79 this.subscription.unsubscribe();
83 static getDerivedStateFromProps(props: any): CommunitiesProps {
85 page: getPageFromProps(props),
89 componentDidUpdate(_: any, lastState: CommunitiesState) {
90 if (lastState.page !== this.state.page) {
91 this.setState({ loading: true });
96 get documentTitle(): string {
97 return `${i18n.t("communities")} - ${this.state.site_view.site.name}`;
102 <div class="container">
104 title={this.documentTitle}
105 path={this.context.router.route.match.url}
107 {this.state.loading ? (
114 <div class="col-md-6">
115 <h4>{i18n.t("list_of_communities")}</h4>
117 <div class="col-md-6">
118 <div class="float-md-right">{this.searchForm()}</div>
122 <div class="table-responsive">
123 <table id="community_table" class="table table-sm table-hover">
124 <thead class="pointer">
126 <th>{i18n.t("name")}</th>
127 <th class="text-right">{i18n.t("subscribers")}</th>
128 <th class="text-right">
129 {i18n.t("users")} / {i18n.t("month")}
131 <th class="text-right d-none d-lg-table-cell">
134 <th class="text-right d-none d-lg-table-cell">
141 {this.state.communities.map(cv => (
144 <CommunityLink community={cv.community} />
146 <td class="text-right">{cv.counts.subscribers}</td>
147 <td class="text-right">{cv.counts.users_active_month}</td>
148 <td class="text-right d-none d-lg-table-cell">
151 <td class="text-right d-none d-lg-table-cell">
154 <td class="text-right">
157 class="pointer btn-link"
161 this.handleUnsubscribe
164 {i18n.t("unsubscribe")}
168 class="pointer btn-link"
175 {i18n.t("subscribe")}
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>
220 {this.state.page > 1 && (
222 class="btn btn-secondary mr-1"
223 onClick={linkEvent(this, this.prevPage)}
229 {this.state.communities.length > 0 && (
231 class="btn btn-secondary"
232 onClick={linkEvent(this, this.nextPage)}
241 updateUrl(paramUpdates: CommunitiesProps) {
242 const page = paramUpdates.page || this.state.page;
243 this.props.history.push(`/communities/page/${page}`);
246 nextPage(i: Communities) {
247 i.updateUrl({ page: i.state.page + 1 });
250 prevPage(i: Communities) {
251 i.updateUrl({ page: i.state.page - 1 });
254 handleUnsubscribe(communityId: number) {
255 let form: FollowCommunity = {
256 community_id: communityId,
260 WebSocketService.Instance.send(wsClient.followCommunity(form));
263 handleSubscribe(communityId: number) {
264 let form: FollowCommunity = {
265 community_id: communityId,
269 WebSocketService.Instance.send(wsClient.followCommunity(form));
272 handleSearchChange(i: Communities, event: any) {
273 i.setState({ searchText: event.target.value });
276 handleSearchSubmit(i: Communities) {
277 const searchParamEncoded = encodeURIComponent(i.state.searchText);
278 i.context.router.history.push(
279 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/page/1`
284 let listCommunitiesForm: ListCommunities = {
285 type_: ListingType.All,
286 sort: SortType.TopAll,
287 limit: communityLimit,
288 page: this.state.page,
289 auth: authField(false),
292 WebSocketService.Instance.send(
293 wsClient.listCommunities(listCommunitiesForm)
297 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
298 let pathSplit = req.path.split("/");
299 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
300 let listCommunitiesForm: ListCommunities = {
301 type_: ListingType.All,
302 sort: SortType.TopAll,
303 limit: communityLimit,
306 setOptionalAuth(listCommunitiesForm, req.auth);
308 return [req.client.listCommunities(listCommunitiesForm)];
311 parseMessage(msg: any) {
312 let op = wsUserOp(msg);
314 toast(i18n.t(msg.error), "danger");
316 } else if (op == UserOperation.ListCommunities) {
317 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
318 this.state.communities = data.communities;
319 this.state.communities.sort(
320 (a, b) => b.counts.subscribers - a.counts.subscribers
322 this.state.loading = false;
323 window.scrollTo(0, 0);
324 this.setState(this.state);
325 } else if (op == UserOperation.FollowCommunity) {
326 let data = wsJsonToRes<CommunityResponse>(msg).data;
327 let found = this.state.communities.find(
328 c => c.community.id == data.community_view.community.id
330 found.subscribed = data.community_view.subscribed;
331 found.counts.subscribers = data.community_view.counts.subscribers;
332 this.setState(this.state);