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";
32 import { HtmlTags } from "../common/html-tags";
33 import { Spinner } from "../common/icon";
34 import { ListingTypeSelect } from "../common/listing-type-select";
35 import { Paginator } from "../common/paginator";
36 import { CommunityLink } from "./community-link";
38 const communityLimit = 50;
40 interface CommunitiesData {
41 listCommunitiesResponse: ListCommunitiesResponse;
44 interface CommunitiesState {
45 listCommunitiesResponse?: ListCommunitiesResponse;
47 siteRes: GetSiteResponse;
51 interface CommunitiesProps {
52 listingType: ListingType;
56 function getCommunitiesQueryParams() {
57 return getQueryParams<CommunitiesProps>({
58 listingType: getListingTypeFromQuery,
59 page: getPageFromString,
63 function getListingTypeFromQuery(listingType?: string): ListingType {
64 return listingType ? (listingType as ListingType) : "Local";
67 function toggleSubscribe(community_id: number, follow: boolean) {
68 const auth = myAuth();
70 const form: FollowCommunity = {
76 WebSocketService.Instance.send(wsClient.followCommunity(form));
81 const { listingType, page } = getCommunitiesQueryParams();
83 const listCommunitiesForm: ListCommunities = {
86 limit: communityLimit,
91 WebSocketService.Instance.send(wsClient.listCommunities(listCommunitiesForm));
94 export class Communities extends Component<any, CommunitiesState> {
95 private subscription?: Subscription;
96 private isoData = setIsoData<CommunitiesData>(this.context);
97 state: CommunitiesState = {
99 siteRes: this.isoData.site_res,
103 constructor(props: any, context: any) {
104 super(props, context);
105 this.handlePageChange = this.handlePageChange.bind(this);
106 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
108 this.parseMessage = this.parseMessage.bind(this);
109 this.subscription = wsSubscribe(this.parseMessage);
111 // Only fetch the data if coming from another route
112 if (this.isoData.path === this.context.router.route.match.url) {
113 const { listCommunitiesResponse } = this.isoData.routeData;
117 listCommunitiesResponse,
125 componentWillUnmount() {
127 this.subscription?.unsubscribe();
131 get documentTitle(): string {
132 return `${i18n.t("communities")} - ${
133 this.state.siteRes.site_view.site.name
138 const { listingType, page } = getCommunitiesQueryParams();
141 <div className="container-lg">
143 title={this.documentTitle}
144 path={this.context.router.route.match.url}
146 {this.state.loading ? (
152 <div className="row">
153 <div className="col-md-6">
154 <h4>{i18n.t("list_of_communities")}</h4>
155 <span className="mb-2">
158 showLocal={showLocal(this.isoData)}
160 onChange={this.handleListingTypeChange}
164 <div className="col-md-6">
165 <div className="float-md-right">{this.searchForm()}</div>
169 <div className="table-responsive">
172 className="table table-sm table-hover"
174 <thead className="pointer">
176 <th>{i18n.t("name")}</th>
177 <th className="text-right">{i18n.t("subscribers")}</th>
178 <th className="text-right">
179 {i18n.t("users")} / {i18n.t("month")}
181 <th className="text-right d-none d-lg-table-cell">
184 <th className="text-right d-none d-lg-table-cell">
191 {this.state.listCommunitiesResponse?.communities.map(cv => (
192 <tr key={cv.community.id}>
194 <CommunityLink community={cv.community} />
196 <td className="text-right">
197 {numToSI(cv.counts.subscribers)}
199 <td className="text-right">
200 {numToSI(cv.counts.users_active_month)}
202 <td className="text-right d-none d-lg-table-cell">
203 {numToSI(cv.counts.posts)}
205 <td className="text-right d-none d-lg-table-cell">
206 {numToSI(cv.counts.comments)}
208 <td className="text-right">
209 {cv.subscribed == "Subscribed" && (
211 className="btn btn-link d-inline-block"
214 this.handleUnsubscribe
217 {i18n.t("unsubscribe")}
220 {cv.subscribed === "NotSubscribed" && (
222 className="btn btn-link d-inline-block"
228 {i18n.t("subscribe")}
231 {cv.subscribed === "Pending" && (
232 <div className="text-warning d-inline-block">
233 {i18n.t("subscribe_pending")}
242 <Paginator page={page} onChange={this.handlePageChange} />
252 className="form-inline"
253 onSubmit={linkEvent(this, this.handleSearchSubmit)}
257 id="communities-search"
258 className="form-control mr-2 mb-2"
259 value={this.state.searchText}
260 placeholder={`${i18n.t("search")}...`}
261 onInput={linkEvent(this, this.handleSearchChange)}
265 <label className="sr-only" htmlFor="communities-search">
268 <button type="submit" className="btn btn-secondary mr-2 mb-2">
269 <span>{i18n.t("search")}</span>
275 updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
276 const { listingType: urlListingType, page: urlPage } =
277 getCommunitiesQueryParams();
279 const queryParams: QueryParams<CommunitiesProps> = {
280 listingType: listingType ?? urlListingType,
281 page: (page ?? urlPage)?.toString(),
284 this.props.history.push(`/communities${getQueryString(queryParams)}`);
289 handlePageChange(page: number) {
290 this.updateUrl({ page });
293 handleListingTypeChange(val: ListingType) {
300 handleUnsubscribe(communityId: number) {
301 toggleSubscribe(communityId, false);
304 handleSubscribe(communityId: number) {
305 toggleSubscribe(communityId, true);
308 handleSearchChange(i: Communities, event: any) {
309 i.setState({ searchText: event.target.value });
312 handleSearchSubmit(i: Communities) {
313 const searchParamEncoded = encodeURIComponent(i.state.searchText);
314 i.context.router.history.push(`/search?q=${searchParamEncoded}`);
317 static fetchInitialData({
318 query: { listingType, page },
321 }: InitialFetchRequest<
322 QueryParams<CommunitiesProps>
323 >): WithPromiseKeys<CommunitiesData> {
324 const listCommunitiesForm: ListCommunities = {
325 type_: getListingTypeFromQuery(listingType),
327 limit: communityLimit,
328 page: getPageFromString(page),
333 listCommunitiesResponse: client.listCommunities(listCommunitiesForm),
337 parseMessage(msg: any) {
338 const op = wsUserOp(msg);
341 toast(i18n.t(msg.error), "danger");
342 } else if (op === UserOperation.ListCommunities) {
343 const data = wsJsonToRes<ListCommunitiesResponse>(msg);
344 this.setState({ listCommunitiesResponse: data, loading: false });
345 window.scrollTo(0, 0);
346 } else if (op === UserOperation.FollowCommunity) {
351 counts: { subscribers },
353 } = wsJsonToRes<CommunityResponse>(msg);
354 const res = this.state.listCommunitiesResponse;
355 const found = res?.communities.find(
356 ({ community: { id } }) => id == community.id
360 found.subscribed = subscribed;
361 found.counts.subscribers = subscribers;
362 this.setState(this.state);