1 import { getQueryParams, getQueryString } from "@utils/helpers";
2 import type { QueryParams } from "@utils/types";
3 import { Component, linkEvent } from "inferno";
8 ListCommunitiesResponse,
10 } from "lemmy-js-client";
11 import { i18n } from "../../i18next";
12 import { InitialFetchRequest } from "../../interfaces";
13 import { FirstLoadService } from "../../services/FirstLoadService";
14 import { HttpService, RequestState } from "../../services/HttpService";
25 import { HtmlTags } from "../common/html-tags";
26 import { Spinner } from "../common/icon";
27 import { ListingTypeSelect } from "../common/listing-type-select";
28 import { Paginator } from "../common/paginator";
29 import { CommunityLink } from "./community-link";
31 const communityLimit = 50;
33 type CommunitiesData = RouteDataResponse<{
34 listCommunitiesResponse: ListCommunitiesResponse;
37 interface CommunitiesState {
38 listCommunitiesResponse: RequestState<ListCommunitiesResponse>;
39 siteRes: GetSiteResponse;
41 isIsomorphic: boolean;
44 interface CommunitiesProps {
45 listingType: ListingType;
49 function getListingTypeFromQuery(listingType?: string): ListingType {
50 return listingType ? (listingType as ListingType) : "Local";
53 export class Communities extends Component<any, CommunitiesState> {
54 private isoData = setIsoData<CommunitiesData>(this.context);
55 state: CommunitiesState = {
56 listCommunitiesResponse: { state: "empty" },
57 siteRes: this.isoData.site_res,
62 constructor(props: any, context: any) {
63 super(props, context);
64 this.handlePageChange = this.handlePageChange.bind(this);
65 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
67 // Only fetch the data if coming from another route
68 if (FirstLoadService.isFirstLoad) {
69 const { listCommunitiesResponse } = this.isoData.routeData;
73 listCommunitiesResponse,
79 async componentDidMount() {
80 if (!this.state.isIsomorphic) {
85 get documentTitle(): string {
86 return `${i18n.t("communities")} - ${
87 this.state.siteRes.site_view.site.name
92 switch (this.state.listCommunitiesResponse.state) {
100 const { listingType, page } = this.getCommunitiesQueryParams();
103 <div className="row">
104 <div className="col-md-6">
105 <h4>{i18n.t("list_of_communities")}</h4>
106 <span className="mb-2">
109 showLocal={showLocal(this.isoData)}
111 onChange={this.handleListingTypeChange}
115 <div className="col-md-6">{this.searchForm()}</div>
118 <div className="table-responsive">
121 className="table table-sm table-hover"
123 <thead className="pointer">
125 <th>{i18n.t("name")}</th>
126 <th className="text-right">{i18n.t("subscribers")}</th>
127 <th className="text-right">
128 {i18n.t("users")} / {i18n.t("month")}
130 <th className="text-right d-none d-lg-table-cell">
133 <th className="text-right d-none d-lg-table-cell">
140 {this.state.listCommunitiesResponse.data.communities.map(
142 <tr key={cv.community.id}>
144 <CommunityLink community={cv.community} />
146 <td className="text-right">
147 {numToSI(cv.counts.subscribers)}
149 <td className="text-right">
150 {numToSI(cv.counts.users_active_month)}
152 <td className="text-right d-none d-lg-table-cell">
153 {numToSI(cv.counts.posts)}
155 <td className="text-right d-none d-lg-table-cell">
156 {numToSI(cv.counts.comments)}
158 <td className="text-right">
159 {cv.subscribed == "Subscribed" && (
161 className="btn btn-link d-inline-block"
165 communityId: cv.community.id,
171 {i18n.t("unsubscribe")}
174 {cv.subscribed === "NotSubscribed" && (
176 className="btn btn-link d-inline-block"
180 communityId: cv.community.id,
186 {i18n.t("subscribe")}
189 {cv.subscribed === "Pending" && (
190 <div className="text-warning d-inline-block">
191 {i18n.t("subscribe_pending")}
201 <Paginator page={page} onChange={this.handlePageChange} />
210 <div className="container-lg">
212 title={this.documentTitle}
213 path={this.context.router.route.match.url}
215 {this.renderListings()}
223 className="row justify-content-end"
224 onSubmit={linkEvent(this, this.handleSearchSubmit)}
226 <div className="col-auto">
229 id="communities-search"
230 className="form-control me-2 mb-2"
231 value={this.state.searchText}
232 placeholder={`${i18n.t("search")}...`}
233 onInput={linkEvent(this, this.handleSearchChange)}
238 <div className="col-auto">
239 <label className="visually-hidden" htmlFor="communities-search">
242 <button type="submit" className="btn btn-secondary mb-2">
243 <span>{i18n.t("search")}</span>
250 async updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
251 const { listingType: urlListingType, page: urlPage } =
252 this.getCommunitiesQueryParams();
254 const queryParams: QueryParams<CommunitiesProps> = {
255 listingType: listingType ?? urlListingType,
256 page: (page ?? urlPage)?.toString(),
259 this.props.history.push(`/communities${getQueryString(queryParams)}`);
261 await this.refetch();
264 handlePageChange(page: number) {
265 this.updateUrl({ page });
268 handleListingTypeChange(val: ListingType) {
275 handleSearchChange(i: Communities, event: any) {
276 i.setState({ searchText: event.target.value });
279 handleSearchSubmit(i: Communities, event: any) {
280 event.preventDefault();
281 const searchParamEncoded = encodeURIComponent(i.state.searchText);
282 i.context.router.history.push(`/search?q=${searchParamEncoded}`);
285 static async fetchInitialData({
286 query: { listingType, page },
289 }: InitialFetchRequest<
290 QueryParams<CommunitiesProps>
291 >): Promise<CommunitiesData> {
292 const listCommunitiesForm: ListCommunities = {
293 type_: getListingTypeFromQuery(listingType),
295 limit: communityLimit,
296 page: getPageFromString(page),
301 listCommunitiesResponse: await client.listCommunities(
307 getCommunitiesQueryParams() {
308 return getQueryParams<CommunitiesProps>({
309 listingType: getListingTypeFromQuery,
310 page: getPageFromString,
314 async handleFollow(data: {
319 const res = await HttpService.client.followCommunity({
320 community_id: data.communityId,
322 auth: myAuthRequired(),
324 data.i.findAndUpdateCommunity(res);
328 this.setState({ listCommunitiesResponse: { state: "loading" } });
330 const { listingType, page } = this.getCommunitiesQueryParams();
333 listCommunitiesResponse: await HttpService.client.listCommunities({
336 limit: communityLimit,
342 window.scrollTo(0, 0);
345 findAndUpdateCommunity(res: RequestState<CommunityResponse>) {
348 s.listCommunitiesResponse.state == "success" &&
349 res.state == "success"
351 s.listCommunitiesResponse.data.communities = editCommunity(
352 res.data.community_view,
353 s.listCommunitiesResponse.data.communities