13 } from "@utils/helpers";
14 import type { QueryParams } from "@utils/types";
15 import { RouteDataResponse } from "@utils/types";
16 import { Component, linkEvent } from "inferno";
21 ListCommunitiesResponse,
23 } from "lemmy-js-client";
24 import { i18n } from "../../i18next";
25 import { InitialFetchRequest } from "../../interfaces";
26 import { FirstLoadService } from "../../services/FirstLoadService";
27 import { HttpService, RequestState } from "../../services/HttpService";
28 import { HtmlTags } from "../common/html-tags";
29 import { Spinner } from "../common/icon";
30 import { ListingTypeSelect } from "../common/listing-type-select";
31 import { Paginator } from "../common/paginator";
32 import { CommunityLink } from "./community-link";
34 const communityLimit = 50;
36 type CommunitiesData = RouteDataResponse<{
37 listCommunitiesResponse: ListCommunitiesResponse;
40 interface CommunitiesState {
41 listCommunitiesResponse: RequestState<ListCommunitiesResponse>;
42 siteRes: GetSiteResponse;
44 isIsomorphic: boolean;
47 interface CommunitiesProps {
48 listingType: ListingType;
52 function getListingTypeFromQuery(listingType?: string): ListingType {
53 return listingType ? (listingType as ListingType) : "Local";
56 export class Communities extends Component<any, CommunitiesState> {
57 private isoData = setIsoData<CommunitiesData>(this.context);
58 state: CommunitiesState = {
59 listCommunitiesResponse: { state: "empty" },
60 siteRes: this.isoData.site_res,
65 constructor(props: any, context: any) {
66 super(props, context);
67 this.handlePageChange = this.handlePageChange.bind(this);
68 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
70 // Only fetch the data if coming from another route
71 if (FirstLoadService.isFirstLoad) {
72 const { listCommunitiesResponse } = this.isoData.routeData;
76 listCommunitiesResponse,
82 async componentDidMount() {
83 if (!this.state.isIsomorphic) {
88 get documentTitle(): string {
89 return `${i18n.t("communities")} - ${
90 this.state.siteRes.site_view.site.name
95 switch (this.state.listCommunitiesResponse.state) {
103 const { listingType, page } = this.getCommunitiesQueryParams();
106 <h1 className="h4">{i18n.t("list_of_communities")}</h1>
107 <div className="row g-2 justify-content-between">
108 <div className="col-auto">
111 showLocal={showLocal(this.isoData)}
113 onChange={this.handleListingTypeChange}
116 <div className="col-auto">{this.searchForm()}</div>
119 <div className="table-responsive">
122 className="table table-sm table-hover"
124 <thead className="pointer">
126 <th>{i18n.t("name")}</th>
127 <th className="text-right">{i18n.t("subscribers")}</th>
128 <th className="text-right">
129 {i18n.t("users")} / {i18n.t("month")}
131 <th className="text-right d-none d-lg-table-cell">
134 <th className="text-right d-none d-lg-table-cell">
141 {this.state.listCommunitiesResponse.data.communities.map(
143 <tr key={cv.community.id}>
145 <CommunityLink community={cv.community} />
147 <td className="text-right">
148 {numToSI(cv.counts.subscribers)}
150 <td className="text-right">
151 {numToSI(cv.counts.users_active_month)}
153 <td className="text-right d-none d-lg-table-cell">
154 {numToSI(cv.counts.posts)}
156 <td className="text-right d-none d-lg-table-cell">
157 {numToSI(cv.counts.comments)}
159 <td className="text-right">
160 {cv.subscribed == "Subscribed" && (
162 className="btn btn-link d-inline-block"
166 communityId: cv.community.id,
172 {i18n.t("unsubscribe")}
175 {cv.subscribed === "NotSubscribed" && (
177 className="btn btn-link d-inline-block"
181 communityId: cv.community.id,
187 {i18n.t("subscribe")}
190 {cv.subscribed === "Pending" && (
191 <div className="text-warning d-inline-block">
192 {i18n.t("subscribe_pending")}
202 <Paginator page={page} onChange={this.handlePageChange} />
211 <div className="communities container-lg">
213 title={this.documentTitle}
214 path={this.context.router.route.match.url}
216 {this.renderListings()}
225 onSubmit={linkEvent(this, this.handleSearchSubmit)}
227 <div className="col-auto">
230 id="communities-search"
231 className="form-control"
232 value={this.state.searchText}
233 placeholder={`${i18n.t("search")}...`}
234 onInput={linkEvent(this, this.handleSearchChange)}
239 <div className="col-auto">
240 <label className="visually-hidden" htmlFor="communities-search">
243 <button type="submit" className="btn btn-secondary">
244 <span>{i18n.t("search")}</span>
251 async updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
252 const { listingType: urlListingType, page: urlPage } =
253 this.getCommunitiesQueryParams();
255 const queryParams: QueryParams<CommunitiesProps> = {
256 listingType: listingType ?? urlListingType,
257 page: (page ?? urlPage)?.toString(),
260 this.props.history.push(`/communities${getQueryString(queryParams)}`);
262 await this.refetch();
265 handlePageChange(page: number) {
266 this.updateUrl({ page });
269 handleListingTypeChange(val: ListingType) {
276 handleSearchChange(i: Communities, event: any) {
277 i.setState({ searchText: event.target.value });
280 handleSearchSubmit(i: Communities, event: any) {
281 event.preventDefault();
282 const searchParamEncoded = encodeURIComponent(i.state.searchText);
283 i.context.router.history.push(`/search?q=${searchParamEncoded}`);
286 static async fetchInitialData({
287 query: { listingType, page },
290 }: InitialFetchRequest<
291 QueryParams<CommunitiesProps>
292 >): Promise<CommunitiesData> {
293 const listCommunitiesForm: ListCommunities = {
294 type_: getListingTypeFromQuery(listingType),
296 limit: communityLimit,
297 page: getPageFromString(page),
302 listCommunitiesResponse: await client.listCommunities(
308 getCommunitiesQueryParams() {
309 return getQueryParams<CommunitiesProps>({
310 listingType: getListingTypeFromQuery,
311 page: getPageFromString,
315 async handleFollow(data: {
320 const res = await HttpService.client.followCommunity({
321 community_id: data.communityId,
323 auth: myAuthRequired(),
325 data.i.findAndUpdateCommunity(res);
329 this.setState({ listCommunitiesResponse: { state: "loading" } });
331 const { listingType, page } = this.getCommunitiesQueryParams();
334 listCommunitiesResponse: await HttpService.client.listCommunities({
337 limit: communityLimit,
343 window.scrollTo(0, 0);
346 findAndUpdateCommunity(res: RequestState<CommunityResponse>) {
349 s.listCommunitiesResponse.state == "success" &&
350 res.state == "success"
352 s.listCommunitiesResponse.data.communities = editCommunity(
353 res.data.community_view,
354 s.listCommunitiesResponse.data.communities