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>{i18n.t("category")}</th>
128 <th class="text-right">{i18n.t("subscribers")}</th>
129 <th class="text-right">
130 {i18n.t("users")} / {i18n.t("month")}
132 <th class="text-right d-none d-lg-table-cell">
135 <th class="text-right d-none d-lg-table-cell">
142 {this.state.communities.map(cv => (
145 <CommunityLink community={cv.community} />
147 <td>{cv.category.name}</td>
148 <td class="text-right">{cv.counts.subscribers}</td>
149 <td class="text-right">{cv.counts.users_active_month}</td>
150 <td class="text-right d-none d-lg-table-cell">
153 <td class="text-right d-none d-lg-table-cell">
156 <td class="text-right">
159 class="pointer btn-link"
163 this.handleUnsubscribe
166 {i18n.t("unsubscribe")}
170 class="pointer btn-link"
177 {i18n.t("subscribe")}
197 onSubmit={linkEvent(this, this.handleSearchSubmit)}
201 id="communities-search"
202 class="form-control mr-2 mb-2"
203 value={this.state.searchText}
204 placeholder={`${i18n.t("search")}...`}
205 onInput={linkEvent(this, this.handleSearchChange)}
209 <label class="sr-only" htmlFor="communities-search">
212 <button type="submit" class="btn btn-secondary mr-2 mb-2">
213 <span>{i18n.t("search")}</span>
222 {this.state.page > 1 && (
224 class="btn btn-secondary mr-1"
225 onClick={linkEvent(this, this.prevPage)}
231 {this.state.communities.length > 0 && (
233 class="btn btn-secondary"
234 onClick={linkEvent(this, this.nextPage)}
243 updateUrl(paramUpdates: CommunitiesProps) {
244 const page = paramUpdates.page || this.state.page;
245 this.props.history.push(`/communities/page/${page}`);
248 nextPage(i: Communities) {
249 i.updateUrl({ page: i.state.page + 1 });
252 prevPage(i: Communities) {
253 i.updateUrl({ page: i.state.page - 1 });
256 handleUnsubscribe(communityId: number) {
257 let form: FollowCommunity = {
258 community_id: communityId,
262 WebSocketService.Instance.send(wsClient.followCommunity(form));
265 handleSubscribe(communityId: number) {
266 let form: FollowCommunity = {
267 community_id: communityId,
271 WebSocketService.Instance.send(wsClient.followCommunity(form));
274 handleSearchChange(i: Communities, event: any) {
275 i.setState({ searchText: event.target.value });
278 handleSearchSubmit(i: Communities) {
279 const searchParamEncoded = encodeURIComponent(i.state.searchText);
280 i.context.router.history.push(
281 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/page/1`
286 let listCommunitiesForm: ListCommunities = {
287 type_: ListingType.All,
288 sort: SortType.TopAll,
289 limit: communityLimit,
290 page: this.state.page,
291 auth: authField(false),
294 WebSocketService.Instance.send(
295 wsClient.listCommunities(listCommunitiesForm)
299 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
300 let pathSplit = req.path.split("/");
301 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
302 let listCommunitiesForm: ListCommunities = {
303 type_: ListingType.All,
304 sort: SortType.TopAll,
305 limit: communityLimit,
308 setOptionalAuth(listCommunitiesForm, req.auth);
310 return [req.client.listCommunities(listCommunitiesForm)];
313 parseMessage(msg: any) {
314 let op = wsUserOp(msg);
316 toast(i18n.t(msg.error), "danger");
318 } else if (op == UserOperation.ListCommunities) {
319 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
320 this.state.communities = data.communities;
321 this.state.communities.sort(
322 (a, b) => b.counts.subscribers - a.counts.subscribers
324 this.state.loading = false;
325 window.scrollTo(0, 0);
326 this.setState(this.state);
327 } else if (op == UserOperation.FollowCommunity) {
328 let data = wsJsonToRes<CommunityResponse>(msg).data;
329 let found = this.state.communities.find(
330 c => c.community.id == data.community_view.community.id
332 found.subscribed = data.community_view.subscribed;
333 found.counts.subscribers = data.community_view.counts.subscribers;
334 this.setState(this.state);