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";
31 import { HtmlTags } from "../common/html-tags";
32 import { Spinner } from "../common/icon";
33 import { ListingTypeSelect } from "../common/listing-type-select";
34 import { Paginator } from "../common/paginator";
35 import { CommunityLink } from "./community-link";
37 const communityLimit = 50;
39 interface CommunitiesState {
40 listCommunitiesResponse?: ListCommunitiesResponse;
42 siteRes: GetSiteResponse;
46 interface CommunitiesProps {
47 listingType: ListingType;
51 function getCommunitiesQueryParams() {
52 return getQueryParams<CommunitiesProps>({
53 listingType: getListingTypeFromQuery,
54 page: getPageFromString,
58 function getListingTypeFromQuery(listingType?: string): ListingType {
59 return listingType ? (listingType as ListingType) : "Local";
62 function toggleSubscribe(community_id: number, follow: boolean) {
63 const auth = myAuth();
65 const form: FollowCommunity = {
71 WebSocketService.Instance.send(wsClient.followCommunity(form));
76 const { listingType, page } = getCommunitiesQueryParams();
78 const listCommunitiesForm: ListCommunities = {
81 limit: communityLimit,
86 WebSocketService.Instance.send(wsClient.listCommunities(listCommunitiesForm));
89 export class Communities extends Component<any, CommunitiesState> {
90 private subscription?: Subscription;
91 private isoData = setIsoData(this.context);
92 state: CommunitiesState = {
94 siteRes: this.isoData.site_res,
98 constructor(props: any, context: any) {
99 super(props, context);
100 this.handlePageChange = this.handlePageChange.bind(this);
101 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
103 this.parseMessage = this.parseMessage.bind(this);
104 this.subscription = wsSubscribe(this.parseMessage);
106 // Only fetch the data if coming from another route
107 if (this.isoData.path === this.context.router.route.match.url) {
108 const listRes = this.isoData.routeData[0] as ListCommunitiesResponse;
111 listCommunitiesResponse: listRes,
119 componentWillUnmount() {
121 this.subscription?.unsubscribe();
125 get documentTitle(): string {
126 return `${i18n.t("communities")} - ${
127 this.state.siteRes.site_view.site.name
132 const { listingType, page } = getCommunitiesQueryParams();
135 <div className="container-lg">
137 title={this.documentTitle}
138 path={this.context.router.route.match.url}
140 {this.state.loading ? (
146 <div className="row">
147 <div className="col-md-6">
148 <h4>{i18n.t("list_of_communities")}</h4>
149 <span className="mb-2">
152 showLocal={showLocal(this.isoData)}
154 onChange={this.handleListingTypeChange}
158 <div className="col-md-6">
159 <div className="float-md-right">{this.searchForm()}</div>
163 <div className="table-responsive">
166 className="table table-sm table-hover"
168 <thead className="pointer">
170 <th>{i18n.t("name")}</th>
171 <th className="text-right">{i18n.t("subscribers")}</th>
172 <th className="text-right">
173 {i18n.t("users")} / {i18n.t("month")}
175 <th className="text-right d-none d-lg-table-cell">
178 <th className="text-right d-none d-lg-table-cell">
185 {this.state.listCommunitiesResponse?.communities.map(cv => (
186 <tr key={cv.community.id}>
188 <CommunityLink community={cv.community} />
190 <td className="text-right">
191 {numToSI(cv.counts.subscribers)}
193 <td className="text-right">
194 {numToSI(cv.counts.users_active_month)}
196 <td className="text-right d-none d-lg-table-cell">
197 {numToSI(cv.counts.posts)}
199 <td className="text-right d-none d-lg-table-cell">
200 {numToSI(cv.counts.comments)}
202 <td className="text-right">
203 {cv.subscribed == "Subscribed" && (
205 className="btn btn-link d-inline-block"
208 this.handleUnsubscribe
211 {i18n.t("unsubscribe")}
214 {cv.subscribed === "NotSubscribed" && (
216 className="btn btn-link d-inline-block"
222 {i18n.t("subscribe")}
225 {cv.subscribed === "Pending" && (
226 <div className="text-warning d-inline-block">
227 {i18n.t("subscribe_pending")}
236 <Paginator page={page} onChange={this.handlePageChange} />
246 className="form-inline"
247 onSubmit={linkEvent(this, this.handleSearchSubmit)}
251 id="communities-search"
252 className="form-control mr-2 mb-2"
253 value={this.state.searchText}
254 placeholder={`${i18n.t("search")}...`}
255 onInput={linkEvent(this, this.handleSearchChange)}
259 <label className="sr-only" htmlFor="communities-search">
262 <button type="submit" className="btn btn-secondary mr-2 mb-2">
263 <span>{i18n.t("search")}</span>
269 updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
270 const { listingType: urlListingType, page: urlPage } =
271 getCommunitiesQueryParams();
273 const queryParams: QueryParams<CommunitiesProps> = {
274 listingType: listingType ?? urlListingType,
275 page: (page ?? urlPage)?.toString(),
278 this.props.history.push(`/communities${getQueryString(queryParams)}`);
283 handlePageChange(page: number) {
284 this.updateUrl({ page });
287 handleListingTypeChange(val: ListingType) {
294 handleUnsubscribe(communityId: number) {
295 toggleSubscribe(communityId, false);
298 handleSubscribe(communityId: number) {
299 toggleSubscribe(communityId, true);
302 handleSearchChange(i: Communities, event: any) {
303 i.setState({ searchText: event.target.value });
306 handleSearchSubmit(i: Communities) {
307 const searchParamEncoded = encodeURIComponent(i.state.searchText);
308 i.context.router.history.push(`/search?q=${searchParamEncoded}`);
311 static fetchInitialData({
312 query: { listingType, page },
315 }: InitialFetchRequest<QueryParams<CommunitiesProps>>): Promise<any>[] {
316 const listCommunitiesForm: ListCommunities = {
317 type_: getListingTypeFromQuery(listingType),
319 limit: communityLimit,
320 page: getPageFromString(page),
324 return [client.listCommunities(listCommunitiesForm)];
327 parseMessage(msg: any) {
328 const op = wsUserOp(msg);
331 toast(i18n.t(msg.error), "danger");
332 } else if (op === UserOperation.ListCommunities) {
333 const data = wsJsonToRes<ListCommunitiesResponse>(msg);
334 this.setState({ listCommunitiesResponse: data, loading: false });
335 window.scrollTo(0, 0);
336 } else if (op === UserOperation.FollowCommunity) {
341 counts: { subscribers },
343 } = wsJsonToRes<CommunityResponse>(msg);
344 const res = this.state.listCommunitiesResponse;
345 const found = res?.communities.find(
346 ({ community: { id } }) => id == community.id
350 found.subscribed = subscribed;
351 found.counts.subscribers = subscribers;
352 this.setState(this.state);