1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
8 GetFollowedCommunitiesResponse,
10 ListCommunitiesResponse,
27 WebSocketJsonResponse,
28 } from '../interfaces';
29 import { WebSocketService, UserService } from '../services';
30 import { PostListings } from './post-listings';
31 import { CommentNodes } from './comment-nodes';
32 import { SortSelect } from './sort-select';
33 import { ListingTypeSelect } from './listing-type-select';
34 import { DataTypeSelect } from './data-type-select';
35 import { SiteForm } from './site-form';
36 import { UserListing } from './user-listing';
37 import { CommunityLink } from './community-link';
44 getListingTypeFromProps,
51 createPostLikeFindRes,
56 import { i18n } from '../i18next';
57 import { T } from 'inferno-i18next';
60 subscribedCommunities: Array<CommunityUser>;
61 trendingCommunities: Array<Community>;
62 siteRes: GetSiteResponse;
63 showEditSite: boolean;
66 comments: Array<Comment>;
67 listingType: ListingType;
73 export class Main extends Component<any, MainState> {
74 private subscription: Subscription;
75 private emptyState: MainState = {
76 subscribedCommunities: [],
77 trendingCommunities: [],
85 number_of_users: null,
86 number_of_posts: null,
87 number_of_comments: null,
88 number_of_communities: null,
89 enable_downvotes: null,
90 open_registration: null,
101 listingType: getListingTypeFromProps(this.props),
102 dataType: getDataTypeFromProps(this.props),
103 sort: getSortTypeFromProps(this.props),
104 page: getPageFromProps(this.props),
107 constructor(props: any, context: any) {
108 super(props, context);
110 this.state = this.emptyState;
111 this.handleEditCancel = this.handleEditCancel.bind(this);
112 this.handleSortChange = this.handleSortChange.bind(this);
113 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
114 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
116 this.subscription = WebSocketService.Instance.subject
117 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
119 msg => this.parseMessage(msg),
120 err => console.error(err),
121 () => console.log('complete')
124 WebSocketService.Instance.getSite();
126 if (UserService.Instance.user) {
127 WebSocketService.Instance.getFollowedCommunities();
130 let listCommunitiesForm: ListCommunitiesForm = {
131 sort: SortType[SortType.Hot],
135 WebSocketService.Instance.listCommunities(listCommunitiesForm);
140 componentWillUnmount() {
141 this.subscription.unsubscribe();
144 static getDerivedStateFromProps(props) {
146 listingType: getListingTypeFromProps(props),
147 dataType: getDataTypeFromProps(props),
148 sort: getSortTypeFromProps(props),
149 page: getPageFromProps(props),
153 componentDidUpdate(_, lastState) {
155 lastState.listingType !== this.state.listingType ||
156 lastState.dataType !== this.state.dataType ||
157 lastState.sort !== this.state.sort ||
158 lastState.page !== this.state.page
160 this.setState({ loading: true });
167 <div class="container">
169 <main role="main" class="col-12 col-md-8">
172 <aside class="col-12 col-md-4">{this.my_sidebar()}</aside>
181 {!this.state.loading && (
183 <div class="card border-secondary mb-3">
184 <div class="card-body">
185 {this.trendingCommunities()}
186 {UserService.Instance.user &&
187 this.state.subscribedCommunities.length > 0 && (
190 <T i18nKey="subscribed_to_communities">
192 <Link class="text-body" to="/communities">
197 <ul class="list-inline">
198 {this.state.subscribedCommunities.map(community => (
199 <li class="list-inline-item">
202 name: community.community_name,
203 id: community.community_id,
204 local: community.community_local,
205 actor_id: community.community_actor_id,
214 class="btn btn-sm btn-secondary btn-block"
215 to="/create_community"
217 {i18n.t('create_a_community')}
229 trendingCommunities() {
233 <T i18nKey="trending_communities">
235 <Link class="text-body" to="/communities">
240 <ul class="list-inline">
241 {this.state.trendingCommunities.map(community => (
242 <li class="list-inline-item">
243 <CommunityLink community={community} />
254 {!this.state.showEditSite ? (
258 site={this.state.siteRes.site}
259 onCancel={this.handleEditCancel}
266 updateUrl(paramUpdates: {
267 listing_type?: string;
272 const listingTypeStr =
273 paramUpdates.listing_type ||
274 ListingType[getListingTypeFromProps(this.props)].toLowerCase();
276 paramUpdates.data_type ||
277 DataType[getDataTypeFromProps(this.props)].toLowerCase();
280 SortType[getSortTypeFromProps(this.props)].toLowerCase();
281 const page = paramUpdates.page || getPageFromProps(this.props);
282 this.props.history.push(
283 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${page}`
290 <div class="card border-secondary mb-3">
291 <div class="card-body">
292 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
294 <ul class="list-inline mb-1 text-muted font-weight-bold">
295 <li className="list-inline-item-action">
298 onClick={linkEvent(this, this.handleEditClick)}
299 data-tippy-content={i18n.t('edit')}
301 <svg class="icon icon-inline">
302 <use xlinkHref="#icon-edit"></use>
308 <ul class="my-2 list-inline">
310 <li className="list-inline-item badge badge-secondary">
311 {i18n.t('number_online', { count: this.state.siteRes.online })}
314 <li className="list-inline-item badge badge-secondary">
315 {i18n.t('number_of_users', {
316 count: this.state.siteRes.site.number_of_users,
319 <li className="list-inline-item badge badge-secondary">
320 {i18n.t('number_of_communities', {
321 count: this.state.siteRes.site.number_of_communities,
324 <li className="list-inline-item badge badge-secondary">
325 {i18n.t('number_of_posts', {
326 count: this.state.siteRes.site.number_of_posts,
329 <li className="list-inline-item badge badge-secondary">
330 {i18n.t('number_of_comments', {
331 count: this.state.siteRes.site.number_of_comments,
334 <li className="list-inline-item">
335 <Link className="badge badge-secondary" to="/modlog">
340 <ul class="mt-1 list-inline small mb-0">
341 <li class="list-inline-item">{i18n.t('admins')}:</li>
342 {this.state.siteRes.admins.map(admin => (
343 <li class="list-inline-item">
347 avatar: admin.avatar,
349 actor_id: admin.actor_id,
358 {this.state.siteRes.site.description && (
359 <div class="card border-secondary mb-3">
360 <div class="card-body">
363 dangerouslySetInnerHTML={mdToHtml(
364 this.state.siteRes.site.description
376 <div class="card border-secondary">
377 <div class="card-body">
379 {i18n.t('powered_by')}
380 <svg class="icon mx-2">
381 <use xlinkHref="#icon-mouse">#</use>
388 <T i18nKey="landing_0">
390 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
393 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
394 <br class="big"></br>
398 <br class="big"></br>
399 <a href={repoUrl}>#</a>
400 <br class="big"></br>
401 <a href="https://www.rust-lang.org">#</a>
402 <a href="https://actix.rs/">#</a>
403 <a href="https://infernojs.org">#</a>
404 <a href="https://www.typescriptlang.org/">#</a>
405 <br class="big"></br>
406 <a href="https://github.com/LemmyNet/lemmy/graphs/contributors?type=a">
418 <div class="main-content-wrapper">
420 {this.state.loading ? (
422 <svg class="icon icon-spinner spin">
423 <use xlinkHref="#icon-spinner"></use>
437 return this.state.dataType == DataType.Post ? (
439 posts={this.state.posts}
442 sort={this.state.sort}
443 enableDownvotes={this.state.siteRes.site.enable_downvotes}
444 enableNsfw={this.state.siteRes.site.enable_nsfw}
448 nodes={commentsToFlatNodes(this.state.comments)}
451 sortType={this.state.sort}
453 enableDownvotes={this.state.siteRes.site.enable_downvotes}
460 <div className="mb-3">
463 type_={this.state.dataType}
464 onChange={this.handleDataTypeChange}
469 type_={this.state.listingType}
470 onChange={this.handleListingTypeChange}
474 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
476 {this.state.listingType == ListingType.All && (
478 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
483 <svg class="icon text-muted small">
484 <use xlinkHref="#icon-rss">#</use>
488 {UserService.Instance.user &&
489 this.state.listingType == ListingType.Subscribed && (
491 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
492 SortType[this.state.sort]
498 <svg class="icon text-muted small">
499 <use xlinkHref="#icon-rss">#</use>
510 {this.state.page > 1 && (
512 class="btn btn-sm btn-secondary mr-1"
513 onClick={linkEvent(this, this.prevPage)}
518 {this.state.posts.length > 0 && (
520 class="btn btn-sm btn-secondary"
521 onClick={linkEvent(this, this.nextPage)}
530 get canAdmin(): boolean {
532 UserService.Instance.user &&
533 this.state.siteRes.admins
535 .includes(UserService.Instance.user.id)
539 handleEditClick(i: Main) {
540 i.state.showEditSite = true;
545 this.state.showEditSite = false;
546 this.setState(this.state);
550 i.updateUrl({ page: this.state.page + 1 });
551 window.scrollTo(0, 0);
555 i.updateUrl({ page: this.state.page - 1 });
556 window.scrollTo(0, 0);
559 handleSortChange(val: SortType) {
560 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
561 window.scrollTo(0, 0);
564 handleListingTypeChange(val: ListingType) {
565 this.updateUrl({ listing_type: ListingType[val].toLowerCase(), page: 1 });
566 window.scrollTo(0, 0);
569 handleDataTypeChange(val: DataType) {
570 this.updateUrl({ data_type: DataType[val].toLowerCase(), page: 1 });
571 window.scrollTo(0, 0);
575 if (this.state.dataType == DataType.Post) {
576 let getPostsForm: GetPostsForm = {
577 page: this.state.page,
579 sort: SortType[this.state.sort],
580 type_: ListingType[this.state.listingType],
582 WebSocketService.Instance.getPosts(getPostsForm);
584 let getCommentsForm: GetCommentsForm = {
585 page: this.state.page,
587 sort: SortType[this.state.sort],
588 type_: ListingType[this.state.listingType],
590 WebSocketService.Instance.getComments(getCommentsForm);
594 parseMessage(msg: WebSocketJsonResponse) {
596 let res = wsJsonToRes(msg);
598 toast(i18n.t(msg.error), 'danger');
600 } else if (msg.reconnect) {
602 } else if (res.op == UserOperation.GetFollowedCommunities) {
603 let data = res.data as GetFollowedCommunitiesResponse;
604 this.state.subscribedCommunities = data.communities;
605 this.setState(this.state);
606 } else if (res.op == UserOperation.ListCommunities) {
607 let data = res.data as ListCommunitiesResponse;
608 this.state.trendingCommunities = data.communities;
609 this.setState(this.state);
610 } else if (res.op == UserOperation.GetSite) {
611 let data = res.data as GetSiteResponse;
613 // This means it hasn't been set up yet
615 this.context.router.history.push('/setup');
617 this.state.siteRes.admins = data.admins;
618 this.state.siteRes.site = data.site;
619 this.state.siteRes.banned = data.banned;
620 this.state.siteRes.online = data.online;
621 this.setState(this.state);
622 document.title = `${this.state.siteRes.site.name}`;
623 } else if (res.op == UserOperation.EditSite) {
624 let data = res.data as SiteResponse;
625 this.state.siteRes.site = data.site;
626 this.state.showEditSite = false;
627 this.setState(this.state);
628 toast(i18n.t('site_saved'));
629 } else if (res.op == UserOperation.GetPosts) {
630 let data = res.data as GetPostsResponse;
631 this.state.posts = data.posts;
632 this.state.loading = false;
633 this.setState(this.state);
635 } else if (res.op == UserOperation.CreatePost) {
636 let data = res.data as PostResponse;
638 // If you're on subscribed, only push it if you're subscribed.
639 if (this.state.listingType == ListingType.Subscribed) {
641 this.state.subscribedCommunities
642 .map(c => c.community_id)
643 .includes(data.post.community_id)
645 this.state.posts.unshift(data.post);
649 let nsfw = data.post.nsfw || data.post.community_nsfw;
651 // Don't push the post if its nsfw, and don't have that setting on
655 UserService.Instance.user &&
656 UserService.Instance.user.show_nsfw)
658 this.state.posts.unshift(data.post);
661 this.setState(this.state);
662 } else if (res.op == UserOperation.EditPost) {
663 let data = res.data as PostResponse;
664 editPostFindRes(data, this.state.posts);
665 this.setState(this.state);
666 } else if (res.op == UserOperation.CreatePostLike) {
667 let data = res.data as PostResponse;
668 createPostLikeFindRes(data, this.state.posts);
669 this.setState(this.state);
670 } else if (res.op == UserOperation.AddAdmin) {
671 let data = res.data as AddAdminResponse;
672 this.state.siteRes.admins = data.admins;
673 this.setState(this.state);
674 } else if (res.op == UserOperation.BanUser) {
675 let data = res.data as BanUserResponse;
676 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
678 // Remove the banned if its found in the list, and the action is an unban
679 if (found && !data.banned) {
680 this.state.siteRes.banned = this.state.siteRes.banned.filter(
681 i => i.id !== data.user.id
684 this.state.siteRes.banned.push(data.user);
688 .filter(p => p.creator_id == data.user.id)
689 .forEach(p => (p.banned = data.banned));
691 this.setState(this.state);
692 } else if (res.op == UserOperation.GetComments) {
693 let data = res.data as GetCommentsResponse;
694 this.state.comments = data.comments;
695 this.state.loading = false;
696 this.setState(this.state);
697 } else if (res.op == UserOperation.EditComment) {
698 let data = res.data as CommentResponse;
699 editCommentRes(data, this.state.comments);
700 this.setState(this.state);
701 } else if (res.op == UserOperation.CreateComment) {
702 let data = res.data as CommentResponse;
704 // Necessary since it might be a user reply
705 if (data.recipient_ids.length == 0) {
706 // If you're on subscribed, only push it if you're subscribed.
707 if (this.state.listingType == ListingType.Subscribed) {
709 this.state.subscribedCommunities
710 .map(c => c.community_id)
711 .includes(data.comment.community_id)
713 this.state.comments.unshift(data.comment);
716 this.state.comments.unshift(data.comment);
718 this.setState(this.state);
720 } else if (res.op == UserOperation.SaveComment) {
721 let data = res.data as CommentResponse;
722 saveCommentRes(data, this.state.comments);
723 this.setState(this.state);
724 } else if (res.op == UserOperation.CreateCommentLike) {
725 let data = res.data as CommentResponse;
726 createCommentLikeRes(data, this.state.comments);
727 this.setState(this.state);