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 <h1 className="h4">{i18n.t("list_of_communities")}</h1>
104 <div className="row g-2 justify-content-between">
105 <div className="col-auto">
108 showLocal={showLocal(this.isoData)}
110 onChange={this.handleListingTypeChange}
113 <div className="col-auto">{this.searchForm()}</div>
116 <div className="table-responsive">
119 className="table table-sm table-hover"
121 <thead className="pointer">
123 <th>{i18n.t("name")}</th>
124 <th className="text-right">{i18n.t("subscribers")}</th>
125 <th className="text-right">
126 {i18n.t("users")} / {i18n.t("month")}
128 <th className="text-right d-none d-lg-table-cell">
131 <th className="text-right d-none d-lg-table-cell">
138 {this.state.listCommunitiesResponse.data.communities.map(
140 <tr key={cv.community.id}>
142 <CommunityLink community={cv.community} />
144 <td className="text-right">
145 {numToSI(cv.counts.subscribers)}
147 <td className="text-right">
148 {numToSI(cv.counts.users_active_month)}
150 <td className="text-right d-none d-lg-table-cell">
151 {numToSI(cv.counts.posts)}
153 <td className="text-right d-none d-lg-table-cell">
154 {numToSI(cv.counts.comments)}
156 <td className="text-right">
157 {cv.subscribed == "Subscribed" && (
159 className="btn btn-link d-inline-block"
163 communityId: cv.community.id,
169 {i18n.t("unsubscribe")}
172 {cv.subscribed === "NotSubscribed" && (
174 className="btn btn-link d-inline-block"
178 communityId: cv.community.id,
184 {i18n.t("subscribe")}
187 {cv.subscribed === "Pending" && (
188 <div className="text-warning d-inline-block">
189 {i18n.t("subscribe_pending")}
199 <Paginator page={page} onChange={this.handlePageChange} />
208 <div className="communities container-lg">
210 title={this.documentTitle}
211 path={this.context.router.route.match.url}
213 {this.renderListings()}
222 onSubmit={linkEvent(this, this.handleSearchSubmit)}
224 <div className="col-auto">
227 id="communities-search"
228 className="form-control"
229 value={this.state.searchText}
230 placeholder={`${i18n.t("search")}...`}
231 onInput={linkEvent(this, this.handleSearchChange)}
236 <div className="col-auto">
237 <label className="visually-hidden" htmlFor="communities-search">
240 <button type="submit" className="btn btn-secondary">
241 <span>{i18n.t("search")}</span>
248 async updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
249 const { listingType: urlListingType, page: urlPage } =
250 this.getCommunitiesQueryParams();
252 const queryParams: QueryParams<CommunitiesProps> = {
253 listingType: listingType ?? urlListingType,
254 page: (page ?? urlPage)?.toString(),
257 this.props.history.push(`/communities${getQueryString(queryParams)}`);
259 await this.refetch();
262 handlePageChange(page: number) {
263 this.updateUrl({ page });
266 handleListingTypeChange(val: ListingType) {
273 handleSearchChange(i: Communities, event: any) {
274 i.setState({ searchText: event.target.value });
277 handleSearchSubmit(i: Communities, event: any) {
278 event.preventDefault();
279 const searchParamEncoded = encodeURIComponent(i.state.searchText);
280 i.context.router.history.push(`/search?q=${searchParamEncoded}`);
283 static async fetchInitialData({
284 query: { listingType, page },
287 }: InitialFetchRequest<
288 QueryParams<CommunitiesProps>
289 >): Promise<CommunitiesData> {
290 const listCommunitiesForm: ListCommunities = {
291 type_: getListingTypeFromQuery(listingType),
293 limit: communityLimit,
294 page: getPageFromString(page),
299 listCommunitiesResponse: await client.listCommunities(
305 getCommunitiesQueryParams() {
306 return getQueryParams<CommunitiesProps>({
307 listingType: getListingTypeFromQuery,
308 page: getPageFromString,
312 async handleFollow(data: {
317 const res = await HttpService.client.followCommunity({
318 community_id: data.communityId,
320 auth: myAuthRequired(),
322 data.i.findAndUpdateCommunity(res);
326 this.setState({ listCommunitiesResponse: { state: "loading" } });
328 const { listingType, page } = this.getCommunitiesQueryParams();
331 listCommunitiesResponse: await HttpService.client.listCommunities({
334 limit: communityLimit,
340 window.scrollTo(0, 0);
343 findAndUpdateCommunity(res: RequestState<CommunityResponse>) {
346 s.listCommunitiesResponse.state == "success" &&
347 res.state == "success"
349 s.listCommunitiesResponse.data.communities = editCommunity(
350 res.data.community_view,
351 s.listCommunitiesResponse.data.communities