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.loading = false;
74 componentWillUnmount() {
76 this.subscription.unsubscribe();
80 static getDerivedStateFromProps(props: any): CommunitiesProps {
82 page: getPageFromProps(props),
86 componentDidUpdate(_: any, lastState: CommunitiesState) {
87 if (lastState.page !== this.state.page) {
88 this.setState({ loading: true });
93 get documentTitle(): string {
94 return `${i18n.t("communities")} - ${this.state.site_view.site.name}`;
99 <div class="container">
101 title={this.documentTitle}
102 path={this.context.router.route.match.url}
104 {this.state.loading ? (
111 <div class="col-md-6">
112 <h4>{i18n.t("list_of_communities")}</h4>
114 <div class="col-md-6">
115 <div class="float-md-right">{this.searchForm()}</div>
119 <div class="table-responsive">
120 <table id="community_table" class="table table-sm table-hover">
121 <thead class="pointer">
123 <th>{i18n.t("name")}</th>
124 <th class="text-right">{i18n.t("subscribers")}</th>
125 <th class="text-right">
126 {i18n.t("users")} / {i18n.t("month")}
128 <th class="text-right d-none d-lg-table-cell">
131 <th class="text-right d-none d-lg-table-cell">
138 {this.state.communities.map(cv => (
141 <CommunityLink community={cv.community} />
143 <td class="text-right">{cv.counts.subscribers}</td>
144 <td class="text-right">{cv.counts.users_active_month}</td>
145 <td class="text-right d-none d-lg-table-cell">
148 <td class="text-right d-none d-lg-table-cell">
151 <td class="text-right">
154 class="pointer btn-link"
158 this.handleUnsubscribe
161 {i18n.t("unsubscribe")}
165 class="pointer btn-link"
172 {i18n.t("subscribe")}
192 onSubmit={linkEvent(this, this.handleSearchSubmit)}
196 id="communities-search"
197 class="form-control mr-2 mb-2"
198 value={this.state.searchText}
199 placeholder={`${i18n.t("search")}...`}
200 onInput={linkEvent(this, this.handleSearchChange)}
204 <label class="sr-only" htmlFor="communities-search">
207 <button type="submit" class="btn btn-secondary mr-2 mb-2">
208 <span>{i18n.t("search")}</span>
217 {this.state.page > 1 && (
219 class="btn btn-secondary mr-1"
220 onClick={linkEvent(this, this.prevPage)}
226 {this.state.communities.length > 0 && (
228 class="btn btn-secondary"
229 onClick={linkEvent(this, this.nextPage)}
238 updateUrl(paramUpdates: CommunitiesProps) {
239 const page = paramUpdates.page || this.state.page;
240 this.props.history.push(`/communities/page/${page}`);
243 nextPage(i: Communities) {
244 i.updateUrl({ page: i.state.page + 1 });
247 prevPage(i: Communities) {
248 i.updateUrl({ page: i.state.page - 1 });
251 handleUnsubscribe(communityId: number) {
252 let form: FollowCommunity = {
253 community_id: communityId,
257 WebSocketService.Instance.send(wsClient.followCommunity(form));
260 handleSubscribe(communityId: number) {
261 let form: FollowCommunity = {
262 community_id: communityId,
266 WebSocketService.Instance.send(wsClient.followCommunity(form));
269 handleSearchChange(i: Communities, event: any) {
270 i.setState({ searchText: event.target.value });
273 handleSearchSubmit(i: Communities) {
274 const searchParamEncoded = encodeURIComponent(i.state.searchText);
275 i.context.router.history.push(
276 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/page/1`
281 let listCommunitiesForm: ListCommunities = {
282 type_: ListingType.All,
283 sort: SortType.TopMonth,
284 limit: communityLimit,
285 page: this.state.page,
286 auth: authField(false),
289 WebSocketService.Instance.send(
290 wsClient.listCommunities(listCommunitiesForm)
294 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
295 let pathSplit = req.path.split("/");
296 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
297 let listCommunitiesForm: ListCommunities = {
298 type_: ListingType.All,
299 sort: SortType.TopMonth,
300 limit: communityLimit,
303 setOptionalAuth(listCommunitiesForm, req.auth);
305 return [req.client.listCommunities(listCommunitiesForm)];
308 parseMessage(msg: any) {
309 let op = wsUserOp(msg);
312 toast(i18n.t(msg.error), "danger");
314 } else if (op == UserOperation.ListCommunities) {
315 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
316 this.state.communities = data.communities;
317 this.state.loading = false;
318 window.scrollTo(0, 0);
319 this.setState(this.state);
320 } else if (op == UserOperation.FollowCommunity) {
321 let data = wsJsonToRes<CommunityResponse>(msg).data;
322 let found = this.state.communities.find(
323 c => c.community.id == data.community_view.community.id
325 found.subscribed = data.community_view.subscribed;
326 found.counts.subscribers = data.community_view.counts.subscribers;
327 this.setState(this.state);