1 import { Component, linkEvent } from 'inferno';
2 import { HtmlTags } from './html-tags';
3 import { Subscription } from 'rxjs';
7 ListCommunitiesResponse,
14 } from 'lemmy-js-client';
15 import { WebSocketService } from '../services';
28 import { CommunityLink } from './community-link';
29 import { i18n } from '../i18next';
30 import { InitialFetchRequest } from 'shared/interfaces';
32 const communityLimit = 100;
34 interface CommunitiesState {
35 communities: CommunityView[];
42 interface CommunitiesProps {
46 export class Communities extends Component<any, CommunitiesState> {
47 private subscription: Subscription;
48 private isoData = setIsoData(this.context);
49 private emptyState: CommunitiesState = {
52 page: getPageFromProps(this.props),
53 site_view: this.isoData.site_res.site_view,
57 constructor(props: any, context: any) {
58 super(props, context);
59 this.state = this.emptyState;
61 this.parseMessage = this.parseMessage.bind(this);
62 this.subscription = wsSubscribe(this.parseMessage);
64 // Only fetch the data if coming from another route
65 if (this.isoData.path == this.context.router.route.match.url) {
66 this.state.communities = this.isoData.routeData[0].communities;
67 this.state.communities.sort(
68 (a, b) => b.counts.subscribers - a.counts.subscribers
70 this.state.loading = false;
76 componentWillUnmount() {
78 this.subscription.unsubscribe();
82 static getDerivedStateFromProps(props: any): CommunitiesProps {
84 page: getPageFromProps(props),
88 componentDidUpdate(_: any, lastState: CommunitiesState) {
89 if (lastState.page !== this.state.page) {
90 this.setState({ loading: true });
95 get documentTitle(): string {
96 return `${i18n.t('communities')} - ${this.state.site_view.site.name}`;
101 <div class="container">
103 title={this.documentTitle}
104 path={this.context.router.route.match.url}
106 {this.state.loading ? (
108 <svg class="icon icon-spinner spin">
109 <use xlinkHref="#icon-spinner"></use>
115 <div class="col-md-6">
116 <h4>{i18n.t('list_of_communities')}</h4>
118 <div class="col-md-6">
119 <div class="float-md-right">{this.searchForm()}</div>
123 <div class="table-responsive">
124 <table id="community_table" class="table table-sm table-hover">
125 <thead class="pointer">
127 <th>{i18n.t('name')}</th>
128 <th>{i18n.t('category')}</th>
129 <th class="text-right">{i18n.t('subscribers')}</th>
130 <th class="text-right">
131 {i18n.t('users')} / {i18n.t('month')}
133 <th class="text-right d-none d-lg-table-cell">
136 <th class="text-right d-none d-lg-table-cell">
143 {this.state.communities.map(cv => (
146 <CommunityLink community={cv.community} />
148 <td>{cv.category.name}</td>
149 <td class="text-right">{cv.counts.subscribers}</td>
150 <td class="text-right">{cv.counts.users_active_month}</td>
151 <td class="text-right d-none d-lg-table-cell">
154 <td class="text-right d-none d-lg-table-cell">
157 <td class="text-right">
160 class="pointer btn-link"
164 this.handleUnsubscribe
167 {i18n.t('unsubscribe')}
171 class="pointer btn-link"
178 {i18n.t('subscribe')}
198 onSubmit={linkEvent(this, this.handleSearchSubmit)}
202 class="form-control mr-2 mb-2"
203 value={this.state.searchText}
204 placeholder={`${i18n.t('search')}...`}
205 onInput={linkEvent(this, this.handleSearchChange)}
209 <button type="submit" class="btn btn-secondary mr-2 mb-2">
210 <span>{i18n.t('search')}</span>
219 {this.state.page > 1 && (
221 class="btn btn-secondary mr-1"
222 onClick={linkEvent(this, this.prevPage)}
228 {this.state.communities.length > 0 && (
230 class="btn btn-secondary"
231 onClick={linkEvent(this, this.nextPage)}
240 updateUrl(paramUpdates: CommunitiesProps) {
241 const page = paramUpdates.page || this.state.page;
242 this.props.history.push(`/communities/page/${page}`);
245 nextPage(i: Communities) {
246 i.updateUrl({ page: i.state.page + 1 });
249 prevPage(i: Communities) {
250 i.updateUrl({ page: i.state.page - 1 });
253 handleUnsubscribe(communityId: number) {
254 let form: FollowCommunity = {
255 community_id: communityId,
259 WebSocketService.Instance.send(wsClient.followCommunity(form));
262 handleSubscribe(communityId: number) {
263 let form: FollowCommunity = {
264 community_id: communityId,
268 WebSocketService.Instance.send(wsClient.followCommunity(form));
271 handleSearchChange(i: Communities, event: any) {
272 i.setState({ searchText: event.target.value });
275 handleSearchSubmit(i: Communities) {
276 const searchParamEncoded = encodeURIComponent(i.state.searchText);
277 i.context.router.history.push(
278 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/page/1`
283 let listCommunitiesForm: ListCommunities = {
284 type_: ListingType.All,
285 sort: SortType.TopAll,
286 limit: communityLimit,
287 page: this.state.page,
288 auth: authField(false),
291 WebSocketService.Instance.send(
292 wsClient.listCommunities(listCommunitiesForm)
296 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
297 let pathSplit = req.path.split('/');
298 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
299 let listCommunitiesForm: ListCommunities = {
300 type_: ListingType.All,
301 sort: SortType.TopAll,
302 limit: communityLimit,
305 setOptionalAuth(listCommunitiesForm, req.auth);
307 return [req.client.listCommunities(listCommunitiesForm)];
310 parseMessage(msg: any) {
311 let op = wsUserOp(msg);
313 toast(i18n.t(msg.error), 'danger');
315 } else if (op == UserOperation.ListCommunities) {
316 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
317 this.state.communities = data.communities;
318 this.state.communities.sort(
319 (a, b) => b.counts.subscribers - a.counts.subscribers
321 this.state.loading = false;
322 window.scrollTo(0, 0);
323 this.setState(this.state);
324 } else if (op == UserOperation.FollowCommunity) {
325 let data = wsJsonToRes<CommunityResponse>(msg).data;
326 let found = this.state.communities.find(
327 c => c.community.id == data.community_view.community.id
329 found.subscribed = data.community_view.subscribed;
330 found.counts.subscribers = data.community_view.counts.subscribers;
331 this.setState(this.state);