1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
8 ListCommunitiesResponse,
13 WebSocketJsonResponse,
16 } from 'lemmy-js-client';
17 import { WebSocketService } from '../services';
26 import { CommunityLink } from './community-link';
27 import { i18n } from '../i18next';
28 import { IsoData } from 'shared/interfaces';
30 const communityLimit = 100;
32 interface CommunitiesState {
33 communities: Community[];
39 interface CommunitiesProps {
43 export class Communities extends Component<any, CommunitiesState> {
44 private subscription: Subscription;
45 private emptyState: CommunitiesState = {
48 page: getPageFromProps(this.props),
52 constructor(props: any, context: any) {
53 super(props, context);
54 this.state = this.emptyState;
58 this.subscription = WebSocketService.Instance.subject
59 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
61 msg => this.parseMessage(msg),
62 err => console.error(err),
63 () => console.log('complete')
65 isoData = window.isoData;
67 isoData = this.context.router.staticContext;
70 this.state.site = isoData.site.site;
72 // Only fetch the data if coming from another route
73 if (isoData.path == this.context.router.route.match.path) {
74 this.state.communities = isoData.routeData[0].communities;
75 this.state.loading = false;
81 componentWillUnmount() {
83 this.subscription.unsubscribe();
87 static getDerivedStateFromProps(props: any): CommunitiesProps {
89 page: getPageFromProps(props),
93 componentDidUpdate(_: any, lastState: CommunitiesState) {
94 if (lastState.page !== this.state.page) {
95 this.setState({ loading: true });
100 get documentTitle(): string {
101 if (this.state.site) {
102 return `${i18n.t('communities')} - ${this.state.site.name}`;
110 <div class="container">
111 <Helmet title={this.documentTitle} />
112 {this.state.loading ? (
114 <svg class="icon icon-spinner spin">
115 <use xlinkHref="#icon-spinner"></use>
120 <h5>{i18n.t('list_of_communities')}</h5>
121 <div class="table-responsive">
122 <table id="community_table" class="table table-sm table-hover">
123 <thead class="pointer">
125 <th>{i18n.t('name')}</th>
126 <th>{i18n.t('category')}</th>
127 <th class="text-right">{i18n.t('subscribers')}</th>
128 <th class="text-right d-none d-lg-table-cell">
131 <th class="text-right d-none d-lg-table-cell">
138 {this.state.communities.map(community => (
141 <CommunityLink community={community} />
143 <td>{community.category_name}</td>
144 <td class="text-right">
145 {community.number_of_subscribers}
147 <td class="text-right d-none d-lg-table-cell">
148 {community.number_of_posts}
150 <td class="text-right d-none d-lg-table-cell">
151 {community.number_of_comments}
153 <td class="text-right">
154 {community.subscribed ? (
156 class="pointer btn-link"
159 this.handleUnsubscribe
162 {i18n.t('unsubscribe')}
166 class="pointer btn-link"
172 {i18n.t('subscribe')}
191 {this.state.page > 1 && (
193 class="btn btn-secondary mr-1"
194 onClick={linkEvent(this, this.prevPage)}
200 {this.state.communities.length > 0 && (
202 class="btn btn-secondary"
203 onClick={linkEvent(this, this.nextPage)}
212 updateUrl(paramUpdates: CommunitiesProps) {
213 const page = paramUpdates.page || this.state.page;
214 this.props.history.push(`/communities/page/${page}`);
217 nextPage(i: Communities) {
218 i.updateUrl({ page: i.state.page + 1 });
221 prevPage(i: Communities) {
222 i.updateUrl({ page: i.state.page - 1 });
225 handleUnsubscribe(communityId: number) {
226 let form: FollowCommunityForm = {
227 community_id: communityId,
230 WebSocketService.Instance.followCommunity(form);
233 handleSubscribe(communityId: number) {
234 let form: FollowCommunityForm = {
235 community_id: communityId,
238 WebSocketService.Instance.followCommunity(form);
242 let listCommunitiesForm: ListCommunitiesForm = {
243 sort: SortType.TopAll,
244 limit: communityLimit,
245 page: this.state.page,
248 WebSocketService.Instance.listCommunities(listCommunitiesForm);
251 static fetchInitialData(auth: string, path: string): Promise<any>[] {
252 let pathSplit = path.split('/');
253 let page = pathSplit[2] ? Number(pathSplit[2]) : 1;
254 let listCommunitiesForm: ListCommunitiesForm = {
255 sort: SortType.TopAll,
256 limit: communityLimit,
259 setAuth(listCommunitiesForm, auth);
261 return [lemmyHttp.listCommunities(listCommunitiesForm)];
264 parseMessage(msg: WebSocketJsonResponse) {
266 let res = wsJsonToRes(msg);
268 toast(i18n.t(msg.error), 'danger');
270 } else if (res.op == UserOperation.ListCommunities) {
271 let data = res.data as ListCommunitiesResponse;
272 this.state.communities = data.communities;
273 this.state.communities.sort(
274 (a, b) => b.number_of_subscribers - a.number_of_subscribers
276 this.state.loading = false;
277 window.scrollTo(0, 0);
278 this.setState(this.state);
279 } else if (res.op == UserOperation.FollowCommunity) {
280 let data = res.data as CommunityResponse;
281 let found = this.state.communities.find(c => c.id == data.community.id);
282 found.subscribed = data.community.subscribed;
283 found.number_of_subscribers = data.community.number_of_subscribers;
284 this.setState(this.state);
285 } else if (res.op == UserOperation.GetSite) {
286 let data = res.data as GetSiteResponse;
287 this.state.site = data.site;
288 this.setState(this.state);