]> Untitled Git - lemmy-ui.git/blob - src/shared/components/community/communities.tsx
af7d827a045f42e3426630ac00b631d0df8aacba
[lemmy-ui.git] / src / shared / components / community / communities.tsx
1 import { Component, linkEvent } from "inferno";
2 import {
3   CommunityResponse,
4   FollowCommunity,
5   GetSiteResponse,
6   ListCommunities,
7   ListCommunitiesResponse,
8   ListingType,
9   UserOperation,
10   wsJsonToRes,
11   wsUserOp,
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";
17 import {
18   QueryParams,
19   getPageFromString,
20   getQueryParams,
21   getQueryString,
22   isBrowser,
23   myAuth,
24   numToSI,
25   setIsoData,
26   showLocal,
27   toast,
28   wsClient,
29   wsSubscribe,
30 } from "../../utils";
31 import { HtmlTags } from "../common/html-tags";
32 import { Spinner } from "../common/icon";
33 import { ListingTypeSelect } from "../common/listing-type-select";
34 import { Paginator } from "../common/paginator";
35 import { CommunityLink } from "./community-link";
36
37 const communityLimit = 50n;
38
39 interface CommunitiesState {
40   listCommunitiesResponse?: ListCommunitiesResponse;
41   loading: boolean;
42   siteRes: GetSiteResponse;
43   searchText: string;
44 }
45
46 interface CommunitiesProps {
47   listingType: ListingType;
48   page: bigint;
49 }
50
51 function getCommunitiesQueryParams() {
52   return getQueryParams<CommunitiesProps>({
53     listingType: getListingTypeFromQuery,
54     page: getPageFromString,
55   });
56 }
57
58 function getListingTypeFromQuery(listingType?: string): ListingType {
59   return listingType ? (listingType as ListingType) : "Local";
60 }
61
62 function toggleSubscribe(community_id: number, follow: boolean) {
63   const auth = myAuth();
64   if (auth) {
65     const form: FollowCommunity = {
66       community_id,
67       follow,
68       auth,
69     };
70
71     WebSocketService.Instance.send(wsClient.followCommunity(form));
72   }
73 }
74
75 function refetch() {
76   const { listingType, page } = getCommunitiesQueryParams();
77
78   const listCommunitiesForm: ListCommunities = {
79     type_: listingType,
80     sort: "TopMonth",
81     limit: communityLimit,
82     page,
83     auth: myAuth(false),
84   };
85
86   WebSocketService.Instance.send(wsClient.listCommunities(listCommunitiesForm));
87 }
88
89 export class Communities extends Component<any, CommunitiesState> {
90   private subscription?: Subscription;
91   private isoData = setIsoData(this.context);
92   state: CommunitiesState = {
93     loading: true,
94     siteRes: this.isoData.site_res,
95     searchText: "",
96   };
97
98   constructor(props: any, context: any) {
99     super(props, context);
100     this.handlePageChange = this.handlePageChange.bind(this);
101     this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
102
103     this.parseMessage = this.parseMessage.bind(this);
104     this.subscription = wsSubscribe(this.parseMessage);
105
106     // Only fetch the data if coming from another route
107     if (this.isoData.path === this.context.router.route.match.url) {
108       const listRes = this.isoData.routeData[0] as ListCommunitiesResponse;
109       this.state = {
110         ...this.state,
111         listCommunitiesResponse: listRes,
112         loading: false,
113       };
114     } else {
115       refetch();
116     }
117   }
118
119   componentWillUnmount() {
120     if (isBrowser()) {
121       this.subscription?.unsubscribe();
122     }
123   }
124
125   get documentTitle(): string {
126     return `${i18n.t("communities")} - ${
127       this.state.siteRes.site_view.site.name
128     }`;
129   }
130
131   render() {
132     const { listingType, page } = getCommunitiesQueryParams();
133
134     return (
135       <div className="container-lg">
136         <HtmlTags
137           title={this.documentTitle}
138           path={this.context.router.route.match.url}
139         />
140         {this.state.loading ? (
141           <h5>
142             <Spinner large />
143           </h5>
144         ) : (
145           <div>
146             <div className="row">
147               <div className="col-md-6">
148                 <h4>{i18n.t("list_of_communities")}</h4>
149                 <span className="mb-2">
150                   <ListingTypeSelect
151                     type_={listingType}
152                     showLocal={showLocal(this.isoData)}
153                     showSubscribed
154                     onChange={this.handleListingTypeChange}
155                   />
156                 </span>
157               </div>
158               <div className="col-md-6">
159                 <div className="float-md-right">{this.searchForm()}</div>
160               </div>
161             </div>
162
163             <div className="table-responsive">
164               <table
165                 id="community_table"
166                 className="table table-sm table-hover"
167               >
168                 <thead className="pointer">
169                   <tr>
170                     <th>{i18n.t("name")}</th>
171                     <th className="text-right">{i18n.t("subscribers")}</th>
172                     <th className="text-right">
173                       {i18n.t("users")} / {i18n.t("month")}
174                     </th>
175                     <th className="text-right d-none d-lg-table-cell">
176                       {i18n.t("posts")}
177                     </th>
178                     <th className="text-right d-none d-lg-table-cell">
179                       {i18n.t("comments")}
180                     </th>
181                     <th></th>
182                   </tr>
183                 </thead>
184                 <tbody>
185                   {this.state.listCommunitiesResponse?.communities.map(cv => (
186                     <tr key={cv.community.id}>
187                       <td>
188                         <CommunityLink community={cv.community} />
189                       </td>
190                       <td className="text-right">
191                         {numToSI(cv.counts.subscribers)}
192                       </td>
193                       <td className="text-right">
194                         {numToSI(cv.counts.users_active_month)}
195                       </td>
196                       <td className="text-right d-none d-lg-table-cell">
197                         {numToSI(cv.counts.posts)}
198                       </td>
199                       <td className="text-right d-none d-lg-table-cell">
200                         {numToSI(cv.counts.comments)}
201                       </td>
202                       <td className="text-right">
203                         {cv.subscribed == "Subscribed" && (
204                           <button
205                             className="btn btn-link d-inline-block"
206                             onClick={linkEvent(
207                               cv.community.id,
208                               this.handleUnsubscribe
209                             )}
210                           >
211                             {i18n.t("unsubscribe")}
212                           </button>
213                         )}
214                         {cv.subscribed === "NotSubscribed" && (
215                           <button
216                             className="btn btn-link d-inline-block"
217                             onClick={linkEvent(
218                               cv.community.id,
219                               this.handleSubscribe
220                             )}
221                           >
222                             {i18n.t("subscribe")}
223                           </button>
224                         )}
225                         {cv.subscribed === "Pending" && (
226                           <div className="text-warning d-inline-block">
227                             {i18n.t("subscribe_pending")}
228                           </div>
229                         )}
230                       </td>
231                     </tr>
232                   ))}
233                 </tbody>
234               </table>
235             </div>
236             <Paginator page={page} onChange={this.handlePageChange} />
237           </div>
238         )}
239       </div>
240     );
241   }
242
243   searchForm() {
244     return (
245       <form
246         className="form-inline"
247         onSubmit={linkEvent(this, this.handleSearchSubmit)}
248       >
249         <input
250           type="text"
251           id="communities-search"
252           className="form-control mr-2 mb-2"
253           value={this.state.searchText}
254           placeholder={`${i18n.t("search")}...`}
255           onInput={linkEvent(this, this.handleSearchChange)}
256           required
257           minLength={3}
258         />
259         <label className="sr-only" htmlFor="communities-search">
260           {i18n.t("search")}
261         </label>
262         <button type="submit" className="btn btn-secondary mr-2 mb-2">
263           <span>{i18n.t("search")}</span>
264         </button>
265       </form>
266     );
267   }
268
269   updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
270     const { listingType: urlListingType, page: urlPage } =
271       getCommunitiesQueryParams();
272
273     const queryParams: QueryParams<CommunitiesProps> = {
274       listingType: listingType ?? urlListingType,
275       page: (page ?? urlPage)?.toString(),
276     };
277
278     this.props.history.push(`/communities${getQueryString(queryParams)}`);
279
280     refetch();
281   }
282
283   handlePageChange(page: bigint) {
284     this.updateUrl({ page });
285   }
286
287   handleListingTypeChange(val: ListingType) {
288     this.updateUrl({
289       listingType: val,
290       page: 1n,
291     });
292   }
293
294   handleUnsubscribe(communityId: number) {
295     toggleSubscribe(communityId, false);
296   }
297
298   handleSubscribe(communityId: number) {
299     toggleSubscribe(communityId, true);
300   }
301
302   handleSearchChange(i: Communities, event: any) {
303     i.setState({ searchText: event.target.value });
304   }
305
306   handleSearchSubmit(i: Communities) {
307     const searchParamEncoded = encodeURIComponent(i.state.searchText);
308     i.context.router.history.push(`/search?q=${searchParamEncoded}`);
309   }
310
311   static fetchInitialData({
312     query: { listingType, page },
313     client,
314     auth,
315   }: InitialFetchRequest<QueryParams<CommunitiesProps>>): Promise<any>[] {
316     const listCommunitiesForm: ListCommunities = {
317       type_: getListingTypeFromQuery(listingType),
318       sort: "TopMonth",
319       limit: communityLimit,
320       page: getPageFromString(page),
321       auth: auth,
322     };
323
324     return [client.listCommunities(listCommunitiesForm)];
325   }
326
327   parseMessage(msg: any) {
328     const op = wsUserOp(msg);
329     console.log(msg);
330     if (msg.error) {
331       toast(i18n.t(msg.error), "danger");
332     } else if (op === UserOperation.ListCommunities) {
333       const data = wsJsonToRes<ListCommunitiesResponse>(msg);
334       this.setState({ listCommunitiesResponse: data, loading: false });
335       window.scrollTo(0, 0);
336     } else if (op === UserOperation.FollowCommunity) {
337       const {
338         community_view: {
339           community,
340           subscribed,
341           counts: { subscribers },
342         },
343       } = wsJsonToRes<CommunityResponse>(msg);
344       const res = this.state.listCommunitiesResponse;
345       const found = res?.communities.find(
346         ({ community: { id } }) => id == community.id
347       );
348
349       if (found) {
350         found.subscribed = subscribed;
351         found.counts.subscribers = subscribers;
352         this.setState(this.state);
353       }
354     }
355   }
356 }