8 import { isBrowser } from "@utils/browser";
14 } from "@utils/helpers";
15 import type { QueryParams } from "@utils/types";
16 import { RouteDataResponse } from "@utils/types";
17 import { Component, linkEvent } from "inferno";
22 ListCommunitiesResponse,
24 } from "lemmy-js-client";
25 import { InitialFetchRequest } from "../../interfaces";
26 import { FirstLoadService, I18NextService } from "../../services";
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 (!isBrowser() || FirstLoadService.isFirstLoad) {
72 const { listCommunitiesResponse } = this.isoData.routeData;
76 listCommunitiesResponse,
82 async componentDidMount() {
83 if (!this.state.isIsomorphic) {
88 get documentTitle(): string {
89 return `${I18NextService.i18n.t("communities")} - ${
90 this.state.siteRes.site_view.site.name
95 switch (this.state.listCommunitiesResponse.state) {
103 const { listingType, page } = this.getCommunitiesQueryParams();
107 {I18NextService.i18n.t("list_of_communities")}
109 <div className="row g-2 justify-content-between">
110 <div className="col-auto">
113 showLocal={showLocal(this.isoData)}
115 onChange={this.handleListingTypeChange}
118 <div className="col-auto">{this.searchForm()}</div>
121 <div className="table-responsive">
124 className="table table-sm table-hover"
126 <thead className="pointer">
128 <th>{I18NextService.i18n.t("name")}</th>
129 <th className="text-right">
130 {I18NextService.i18n.t("subscribers")}
132 <th className="text-right">
133 {I18NextService.i18n.t("users")} /{" "}
134 {I18NextService.i18n.t("month")}
136 <th className="text-right d-none d-lg-table-cell">
137 {I18NextService.i18n.t("posts")}
139 <th className="text-right d-none d-lg-table-cell">
140 {I18NextService.i18n.t("comments")}
146 {this.state.listCommunitiesResponse.data.communities.map(
148 <tr key={cv.community.id}>
150 <CommunityLink community={cv.community} />
152 <td className="text-right">
153 {numToSI(cv.counts.subscribers)}
155 <td className="text-right">
156 {numToSI(cv.counts.users_active_month)}
158 <td className="text-right d-none d-lg-table-cell">
159 {numToSI(cv.counts.posts)}
161 <td className="text-right d-none d-lg-table-cell">
162 {numToSI(cv.counts.comments)}
164 <td className="text-right">
165 {cv.subscribed == "Subscribed" && (
167 className="btn btn-link d-inline-block"
171 communityId: cv.community.id,
177 {I18NextService.i18n.t("unsubscribe")}
180 {cv.subscribed === "NotSubscribed" && (
182 className="btn btn-link d-inline-block"
186 communityId: cv.community.id,
192 {I18NextService.i18n.t("subscribe")}
195 {cv.subscribed === "Pending" && (
196 <div className="text-warning d-inline-block">
197 {I18NextService.i18n.t("subscribe_pending")}
207 <Paginator page={page} onChange={this.handlePageChange} />
216 <div className="communities container-lg">
218 title={this.documentTitle}
219 path={this.context.router.route.match.url}
221 {this.renderListings()}
230 onSubmit={linkEvent(this, this.handleSearchSubmit)}
232 <div className="col-auto">
235 id="communities-search"
236 className="form-control"
237 value={this.state.searchText}
238 placeholder={`${I18NextService.i18n.t("search")}...`}
239 onInput={linkEvent(this, this.handleSearchChange)}
244 <div className="col-auto">
245 <label className="visually-hidden" htmlFor="communities-search">
246 {I18NextService.i18n.t("search")}
248 <button type="submit" className="btn btn-secondary">
249 <span>{I18NextService.i18n.t("search")}</span>
256 async updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
257 const { listingType: urlListingType, page: urlPage } =
258 this.getCommunitiesQueryParams();
260 const queryParams: QueryParams<CommunitiesProps> = {
261 listingType: listingType ?? urlListingType,
262 page: (page ?? urlPage)?.toString(),
265 this.props.history.push(`/communities${getQueryString(queryParams)}`);
267 await this.refetch();
270 handlePageChange(page: number) {
271 this.updateUrl({ page });
274 handleListingTypeChange(val: ListingType) {
281 handleSearchChange(i: Communities, event: any) {
282 i.setState({ searchText: event.target.value });
285 handleSearchSubmit(i: Communities, event: any) {
286 event.preventDefault();
287 const searchParamEncoded = encodeURIComponent(i.state.searchText);
288 i.context.router.history.push(`/search?q=${searchParamEncoded}`);
291 static async fetchInitialData({
292 query: { listingType, page },
295 }: InitialFetchRequest<
296 QueryParams<CommunitiesProps>
297 >): Promise<CommunitiesData> {
298 const listCommunitiesForm: ListCommunities = {
299 type_: getListingTypeFromQuery(listingType),
301 limit: communityLimit,
302 page: getPageFromString(page),
307 listCommunitiesResponse: await client.listCommunities(
313 getCommunitiesQueryParams() {
314 return getQueryParams<CommunitiesProps>({
315 listingType: getListingTypeFromQuery,
316 page: getPageFromString,
320 async handleFollow(data: {
325 const res = await HttpService.client.followCommunity({
326 community_id: data.communityId,
328 auth: myAuthRequired(),
330 data.i.findAndUpdateCommunity(res);
334 this.setState({ listCommunitiesResponse: { state: "loading" } });
336 const { listingType, page } = this.getCommunitiesQueryParams();
339 listCommunitiesResponse: await HttpService.client.listCommunities({
342 limit: communityLimit,
348 window.scrollTo(0, 0);
351 findAndUpdateCommunity(res: RequestState<CommunityResponse>) {
354 s.listCommunitiesResponse.state == "success" &&
355 res.state == "success"
357 s.listCommunitiesResponse.data.communities = editCommunity(
358 res.data.community_view,
359 s.listCommunitiesResponse.data.communities