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';
43 getListingTypeFromProps,
50 createPostLikeFindRes,
55 import { i18n } from '../i18next';
56 import { T } from 'inferno-i18next';
59 subscribedCommunities: Array<CommunityUser>;
60 trendingCommunities: Array<Community>;
61 siteRes: GetSiteResponse;
62 showEditSite: boolean;
65 comments: Array<Comment>;
66 listingType: ListingType;
72 export class Main extends Component<any, MainState> {
73 private subscription: Subscription;
74 private emptyState: MainState = {
75 subscribedCommunities: [],
76 trendingCommunities: [],
84 number_of_users: null,
85 number_of_posts: null,
86 number_of_comments: null,
87 number_of_communities: null,
88 enable_downvotes: null,
89 open_registration: null,
100 listingType: getListingTypeFromProps(this.props),
101 dataType: getDataTypeFromProps(this.props),
102 sort: getSortTypeFromProps(this.props),
103 page: getPageFromProps(this.props),
106 constructor(props: any, context: any) {
107 super(props, context);
109 this.state = this.emptyState;
110 this.handleEditCancel = this.handleEditCancel.bind(this);
111 this.handleSortChange = this.handleSortChange.bind(this);
112 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
113 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
115 this.subscription = WebSocketService.Instance.subject
116 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
118 msg => this.parseMessage(msg),
119 err => console.error(err),
120 () => console.log('complete')
123 WebSocketService.Instance.getSite();
125 if (UserService.Instance.user) {
126 WebSocketService.Instance.getFollowedCommunities();
129 let listCommunitiesForm: ListCommunitiesForm = {
130 sort: SortType[SortType.Hot],
134 WebSocketService.Instance.listCommunities(listCommunitiesForm);
139 componentWillUnmount() {
140 this.subscription.unsubscribe();
143 // Necessary for back button for some reason
144 componentWillReceiveProps(nextProps: any) {
146 nextProps.history.action == 'POP' ||
147 nextProps.history.action == 'PUSH'
149 this.state.listingType = getListingTypeFromProps(nextProps);
150 this.state.dataType = getDataTypeFromProps(nextProps);
151 this.state.sort = getSortTypeFromProps(nextProps);
152 this.state.page = getPageFromProps(nextProps);
153 this.setState(this.state);
160 <div class="container">
162 <main role="main" class="col-12 col-md-8">
165 <aside class="col-12 col-md-4">{this.my_sidebar()}</aside>
174 {!this.state.loading && (
176 <div class="card border-secondary mb-3">
177 <div class="card-body">
178 {this.trendingCommunities()}
179 {UserService.Instance.user &&
180 this.state.subscribedCommunities.length > 0 && (
183 <T i18nKey="subscribed_to_communities">
185 <Link class="text-body" to="/communities">
190 <ul class="list-inline">
191 {this.state.subscribedCommunities.map(community => (
192 <li class="list-inline-item">
193 <Link to={`/c/${community.community_name}`}>
194 {community.community_name}
202 class="btn btn-sm btn-secondary btn-block"
203 to="/create_community"
205 {i18n.t('create_a_community')}
217 trendingCommunities() {
221 <T i18nKey="trending_communities">
223 <Link class="text-body" to="/communities">
228 <ul class="list-inline">
229 {this.state.trendingCommunities.map(community => (
230 <li class="list-inline-item">
231 <Link to={`/c/${community.name}`}>{community.name}</Link>
242 {!this.state.showEditSite ? (
246 site={this.state.siteRes.site}
247 onCancel={this.handleEditCancel}
255 let listingTypeStr = ListingType[this.state.listingType].toLowerCase();
256 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
257 let sortStr = SortType[this.state.sort].toLowerCase();
258 this.props.history.push(
259 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${this.state.page}`
266 <div class="card border-secondary mb-3">
267 <div class="card-body">
268 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
270 <ul class="list-inline mb-1 text-muted font-weight-bold">
271 <li className="list-inline-item-action">
274 onClick={linkEvent(this, this.handleEditClick)}
275 data-tippy-content={i18n.t('edit')}
277 <svg class="icon icon-inline">
278 <use xlinkHref="#icon-edit"></use>
284 <ul class="my-2 list-inline">
285 <li className="list-inline-item badge badge-secondary">
286 {i18n.t('number_online', { count: this.state.siteRes.online })}
288 <li className="list-inline-item badge badge-secondary">
289 {i18n.t('number_of_users', {
290 count: this.state.siteRes.site.number_of_users,
293 <li className="list-inline-item badge badge-secondary">
294 {i18n.t('number_of_communities', {
295 count: this.state.siteRes.site.number_of_communities,
298 <li className="list-inline-item badge badge-secondary">
299 {i18n.t('number_of_posts', {
300 count: this.state.siteRes.site.number_of_posts,
303 <li className="list-inline-item badge badge-secondary">
304 {i18n.t('number_of_comments', {
305 count: this.state.siteRes.site.number_of_comments,
308 <li className="list-inline-item">
309 <Link className="badge badge-secondary" to="/modlog">
314 <ul class="mt-1 list-inline small mb-0">
315 <li class="list-inline-item">{i18n.t('admins')}:</li>
316 {this.state.siteRes.admins.map(admin => (
317 <li class="list-inline-item">
321 avatar: admin.avatar,
329 {this.state.siteRes.site.description && (
330 <div class="card border-secondary mb-3">
331 <div class="card-body">
334 dangerouslySetInnerHTML={mdToHtml(
335 this.state.siteRes.site.description
347 <div class="card border-secondary">
348 <div class="card-body">
350 {i18n.t('powered_by')}
351 <svg class="icon mx-2">
352 <use xlinkHref="#icon-mouse">#</use>
359 <T i18nKey="landing_0">
361 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
364 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
370 <a href={repoUrl}>#</a>
372 <a href="https://www.rust-lang.org">#</a>
373 <a href="https://actix.rs/">#</a>
374 <a href="https://infernojs.org">#</a>
375 <a href="https://www.typescriptlang.org/">#</a>
385 <div class="main-content-wrapper">
387 {this.state.loading ? (
389 <svg class="icon icon-spinner spin">
390 <use xlinkHref="#icon-spinner"></use>
404 return this.state.dataType == DataType.Post ? (
406 posts={this.state.posts}
409 sort={this.state.sort}
413 nodes={commentsToFlatNodes(this.state.comments)}
416 sortType={this.state.sort}
424 <div className="mb-3">
427 type_={this.state.dataType}
428 onChange={this.handleDataTypeChange}
433 type_={this.state.listingType}
434 onChange={this.handleListingTypeChange}
438 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
440 {this.state.listingType == ListingType.All && (
442 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
446 <svg class="icon text-muted small">
447 <use xlinkHref="#icon-rss">#</use>
451 {UserService.Instance.user &&
452 this.state.listingType == ListingType.Subscribed && (
454 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
455 SortType[this.state.sort]
460 <svg class="icon text-muted small">
461 <use xlinkHref="#icon-rss">#</use>
472 {this.state.page > 1 && (
474 class="btn btn-sm btn-secondary mr-1"
475 onClick={linkEvent(this, this.prevPage)}
480 {this.state.posts.length == fetchLimit && (
482 class="btn btn-sm btn-secondary"
483 onClick={linkEvent(this, this.nextPage)}
492 get canAdmin(): boolean {
494 UserService.Instance.user &&
495 this.state.siteRes.admins
497 .includes(UserService.Instance.user.id)
501 handleEditClick(i: Main) {
502 i.state.showEditSite = true;
507 this.state.showEditSite = false;
508 this.setState(this.state);
513 i.state.loading = true;
517 window.scrollTo(0, 0);
522 i.state.loading = true;
526 window.scrollTo(0, 0);
529 handleSortChange(val: SortType) {
530 this.state.sort = val;
532 this.state.loading = true;
533 this.setState(this.state);
536 window.scrollTo(0, 0);
539 handleListingTypeChange(val: ListingType) {
540 this.state.listingType = val;
542 this.state.loading = true;
543 this.setState(this.state);
546 window.scrollTo(0, 0);
549 handleDataTypeChange(val: DataType) {
550 this.state.dataType = val;
552 this.state.loading = true;
553 this.setState(this.state);
556 window.scrollTo(0, 0);
560 if (this.state.dataType == DataType.Post) {
561 let getPostsForm: GetPostsForm = {
562 page: this.state.page,
564 sort: SortType[this.state.sort],
565 type_: ListingType[this.state.listingType],
567 WebSocketService.Instance.getPosts(getPostsForm);
569 let getCommentsForm: GetCommentsForm = {
570 page: this.state.page,
572 sort: SortType[this.state.sort],
573 type_: ListingType[this.state.listingType],
575 WebSocketService.Instance.getComments(getCommentsForm);
579 parseMessage(msg: WebSocketJsonResponse) {
581 let res = wsJsonToRes(msg);
583 toast(i18n.t(msg.error), 'danger');
585 } else if (msg.reconnect) {
587 } else if (res.op == UserOperation.GetFollowedCommunities) {
588 let data = res.data as GetFollowedCommunitiesResponse;
589 this.state.subscribedCommunities = data.communities;
590 this.setState(this.state);
591 } else if (res.op == UserOperation.ListCommunities) {
592 let data = res.data as ListCommunitiesResponse;
593 this.state.trendingCommunities = data.communities;
594 this.setState(this.state);
595 } else if (res.op == UserOperation.GetSite) {
596 let data = res.data as GetSiteResponse;
598 // This means it hasn't been set up yet
600 this.context.router.history.push('/setup');
602 this.state.siteRes.admins = data.admins;
603 this.state.siteRes.site = data.site;
604 this.state.siteRes.banned = data.banned;
605 this.state.siteRes.online = data.online;
606 this.setState(this.state);
607 document.title = `${WebSocketService.Instance.site.name}`;
608 } else if (res.op == UserOperation.EditSite) {
609 let data = res.data as SiteResponse;
610 this.state.siteRes.site = data.site;
611 this.state.showEditSite = false;
612 this.setState(this.state);
613 toast(i18n.t('site_saved'));
614 } else if (res.op == UserOperation.GetPosts) {
615 let data = res.data as GetPostsResponse;
616 this.state.posts = data.posts;
617 this.state.loading = false;
618 this.setState(this.state);
620 } else if (res.op == UserOperation.CreatePost) {
621 let data = res.data as PostResponse;
623 // If you're on subscribed, only push it if you're subscribed.
624 if (this.state.listingType == ListingType.Subscribed) {
626 this.state.subscribedCommunities
627 .map(c => c.community_id)
628 .includes(data.post.community_id)
630 this.state.posts.unshift(data.post);
634 let nsfw = data.post.nsfw || data.post.community_nsfw;
636 // Don't push the post if its nsfw, and don't have that setting on
640 UserService.Instance.user &&
641 UserService.Instance.user.show_nsfw)
643 this.state.posts.unshift(data.post);
646 this.setState(this.state);
647 } else if (res.op == UserOperation.EditPost) {
648 let data = res.data as PostResponse;
649 editPostFindRes(data, this.state.posts);
650 this.setState(this.state);
651 } else if (res.op == UserOperation.CreatePostLike) {
652 let data = res.data as PostResponse;
653 createPostLikeFindRes(data, this.state.posts);
654 this.setState(this.state);
655 } else if (res.op == UserOperation.AddAdmin) {
656 let data = res.data as AddAdminResponse;
657 this.state.siteRes.admins = data.admins;
658 this.setState(this.state);
659 } else if (res.op == UserOperation.BanUser) {
660 let data = res.data as BanUserResponse;
661 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
663 // Remove the banned if its found in the list, and the action is an unban
664 if (found && !data.banned) {
665 this.state.siteRes.banned = this.state.siteRes.banned.filter(
666 i => i.id !== data.user.id
669 this.state.siteRes.banned.push(data.user);
673 .filter(p => p.creator_id == data.user.id)
674 .forEach(p => (p.banned = data.banned));
676 this.setState(this.state);
677 } else if (res.op == UserOperation.GetComments) {
678 let data = res.data as GetCommentsResponse;
679 this.state.comments = data.comments;
680 this.state.loading = false;
681 this.setState(this.state);
682 } else if (res.op == UserOperation.EditComment) {
683 let data = res.data as CommentResponse;
684 editCommentRes(data, this.state.comments);
685 this.setState(this.state);
686 } else if (res.op == UserOperation.CreateComment) {
687 let data = res.data as CommentResponse;
689 // Necessary since it might be a user reply
690 if (data.recipient_ids.length == 0) {
691 // If you're on subscribed, only push it if you're subscribed.
692 if (this.state.listingType == ListingType.Subscribed) {
694 this.state.subscribedCommunities
695 .map(c => c.community_id)
696 .includes(data.comment.community_id)
698 this.state.comments.unshift(data.comment);
701 this.state.comments.unshift(data.comment);
703 this.setState(this.state);
705 } else if (res.op == UserOperation.SaveComment) {
706 let data = res.data as CommentResponse;
707 saveCommentRes(data, this.state.comments);
708 this.setState(this.state);
709 } else if (res.op == UserOperation.CreateCommentLike) {
710 let data = res.data as CommentResponse;
711 createCommentLikeRes(data, this.state.comments);
712 this.setState(this.state);