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';
41 pictshareAvatarThumbnail,
44 getListingTypeFromProps,
51 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-white" 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-white" 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 small font-weight-bold">
271 <li className="list-inline-item">
274 onClick={linkEvent(this, this.handleEditClick)}
281 <ul class="my-2 list-inline">
282 <li className="list-inline-item badge badge-secondary">
283 {i18n.t('number_online', { count: this.state.siteRes.online })}
285 <li className="list-inline-item badge badge-secondary">
286 {i18n.t('number_of_users', {
287 count: this.state.siteRes.site.number_of_users,
290 <li className="list-inline-item badge badge-secondary">
291 {i18n.t('number_of_communities', {
292 count: this.state.siteRes.site.number_of_communities,
295 <li className="list-inline-item badge badge-secondary">
296 {i18n.t('number_of_posts', {
297 count: this.state.siteRes.site.number_of_posts,
300 <li className="list-inline-item badge badge-secondary">
301 {i18n.t('number_of_comments', {
302 count: this.state.siteRes.site.number_of_comments,
305 <li className="list-inline-item">
306 <Link className="badge badge-secondary" to="/modlog">
311 <ul class="mt-1 list-inline small mb-0">
312 <li class="list-inline-item">{i18n.t('admins')}:</li>
313 {this.state.siteRes.admins.map(admin => (
314 <li class="list-inline-item">
315 <Link class="text-info" to={`/u/${admin.name}`}>
316 {admin.avatar && showAvatars() && (
320 src={pictshareAvatarThumbnail(admin.avatar)}
321 class="rounded-circle mr-1"
324 <span>{admin.name}</span>
331 {this.state.siteRes.site.description && (
332 <div class="card border-secondary mb-3">
333 <div class="card-body">
336 dangerouslySetInnerHTML={mdToHtml(
337 this.state.siteRes.site.description
349 <div class="card border-secondary">
350 <div class="card-body">
352 {i18n.t('powered_by')}
353 <svg class="icon mx-2">
354 <use xlinkHref="#icon-mouse">#</use>
361 <T i18nKey="landing_0">
363 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
366 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
372 <a href={repoUrl}>#</a>
374 <a href="https://www.rust-lang.org">#</a>
375 <a href="https://actix.rs/">#</a>
376 <a href="https://infernojs.org">#</a>
377 <a href="https://www.typescriptlang.org/">#</a>
387 <div class="main-content-wrapper">
388 {this.state.loading ? (
390 <svg class="icon icon-spinner spin">
391 <use xlinkHref="#icon-spinner"></use>
406 return this.state.dataType == DataType.Post ? (
407 <PostListings posts={this.state.posts} showCommunity removeDuplicates />
410 nodes={commentsToFlatNodes(this.state.comments)}
419 <div className="mb-3">
421 type_={this.state.dataType}
422 onChange={this.handleDataTypeChange}
426 type_={this.state.listingType}
427 onChange={this.handleListingTypeChange}
431 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
433 {this.state.listingType == ListingType.All && (
435 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
438 <svg class="icon mx-1 text-muted small">
439 <use xlinkHref="#icon-rss">#</use>
443 {UserService.Instance.user &&
444 this.state.listingType == ListingType.Subscribed && (
446 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
447 SortType[this.state.sort]
451 <svg class="icon mx-1 text-muted small">
452 <use xlinkHref="#icon-rss">#</use>
463 {this.state.page > 1 && (
465 class="btn btn-sm btn-secondary mr-1"
466 onClick={linkEvent(this, this.prevPage)}
471 {this.state.posts.length == fetchLimit && (
473 class="btn btn-sm btn-secondary"
474 onClick={linkEvent(this, this.nextPage)}
483 get canAdmin(): boolean {
485 UserService.Instance.user &&
486 this.state.siteRes.admins
488 .includes(UserService.Instance.user.id)
492 handleEditClick(i: Main) {
493 i.state.showEditSite = true;
498 this.state.showEditSite = false;
499 this.setState(this.state);
504 i.state.loading = true;
508 window.scrollTo(0, 0);
513 i.state.loading = true;
517 window.scrollTo(0, 0);
520 handleSortChange(val: SortType) {
521 this.state.sort = val;
523 this.state.loading = true;
524 this.setState(this.state);
527 window.scrollTo(0, 0);
530 handleListingTypeChange(val: ListingType) {
531 this.state.listingType = val;
533 this.state.loading = true;
534 this.setState(this.state);
537 window.scrollTo(0, 0);
540 handleDataTypeChange(val: DataType) {
541 this.state.dataType = val;
543 this.state.loading = true;
544 this.setState(this.state);
547 window.scrollTo(0, 0);
551 if (this.state.dataType == DataType.Post) {
552 let getPostsForm: GetPostsForm = {
553 page: this.state.page,
555 sort: SortType[this.state.sort],
556 type_: ListingType[this.state.listingType],
558 WebSocketService.Instance.getPosts(getPostsForm);
560 let getCommentsForm: GetCommentsForm = {
561 page: this.state.page,
563 sort: SortType[this.state.sort],
564 type_: ListingType[this.state.listingType],
566 WebSocketService.Instance.getComments(getCommentsForm);
570 parseMessage(msg: WebSocketJsonResponse) {
572 let res = wsJsonToRes(msg);
574 toast(i18n.t(msg.error), 'danger');
576 } else if (msg.reconnect) {
578 } else if (res.op == UserOperation.GetFollowedCommunities) {
579 let data = res.data as GetFollowedCommunitiesResponse;
580 this.state.subscribedCommunities = data.communities;
581 this.setState(this.state);
582 } else if (res.op == UserOperation.ListCommunities) {
583 let data = res.data as ListCommunitiesResponse;
584 this.state.trendingCommunities = data.communities;
585 this.setState(this.state);
586 } else if (res.op == UserOperation.GetSite) {
587 let data = res.data as GetSiteResponse;
589 // This means it hasn't been set up yet
591 this.context.router.history.push('/setup');
593 this.state.siteRes.admins = data.admins;
594 this.state.siteRes.site = data.site;
595 this.state.siteRes.banned = data.banned;
596 this.state.siteRes.online = data.online;
597 this.setState(this.state);
598 document.title = `${WebSocketService.Instance.site.name}`;
599 } else if (res.op == UserOperation.EditSite) {
600 let data = res.data as SiteResponse;
601 this.state.siteRes.site = data.site;
602 this.state.showEditSite = false;
603 this.setState(this.state);
604 } else if (res.op == UserOperation.GetPosts) {
605 let data = res.data as GetPostsResponse;
606 this.state.posts = data.posts;
607 this.state.loading = false;
608 this.setState(this.state);
609 } else if (res.op == UserOperation.CreatePost) {
610 let data = res.data as PostResponse;
612 // If you're on subscribed, only push it if you're subscribed.
613 if (this.state.listingType == ListingType.Subscribed) {
615 this.state.subscribedCommunities
616 .map(c => c.community_id)
617 .includes(data.post.community_id)
619 this.state.posts.unshift(data.post);
622 this.state.posts.unshift(data.post);
625 this.setState(this.state);
626 } else if (res.op == UserOperation.EditPost) {
627 let data = res.data as PostResponse;
628 editPostFindRes(data, this.state.posts);
629 this.setState(this.state);
630 } else if (res.op == UserOperation.CreatePostLike) {
631 let data = res.data as PostResponse;
632 createPostLikeFindRes(data, this.state.posts);
633 this.setState(this.state);
634 } else if (res.op == UserOperation.AddAdmin) {
635 let data = res.data as AddAdminResponse;
636 this.state.siteRes.admins = data.admins;
637 this.setState(this.state);
638 } else if (res.op == UserOperation.BanUser) {
639 let data = res.data as BanUserResponse;
640 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
642 // Remove the banned if its found in the list, and the action is an unban
643 if (found && !data.banned) {
644 this.state.siteRes.banned = this.state.siteRes.banned.filter(
645 i => i.id !== data.user.id
648 this.state.siteRes.banned.push(data.user);
652 .filter(p => p.creator_id == data.user.id)
653 .forEach(p => (p.banned = data.banned));
655 this.setState(this.state);
656 } else if (res.op == UserOperation.GetComments) {
657 let data = res.data as GetCommentsResponse;
658 this.state.comments = data.comments;
659 this.state.loading = false;
660 this.setState(this.state);
661 } else if (res.op == UserOperation.EditComment) {
662 let data = res.data as CommentResponse;
663 editCommentRes(data, this.state.comments);
664 this.setState(this.state);
665 } else if (res.op == UserOperation.CreateComment) {
666 let data = res.data as CommentResponse;
668 // Necessary since it might be a user reply
669 if (data.recipient_ids.length == 0) {
670 // If you're on subscribed, only push it if you're subscribed.
671 if (this.state.listingType == ListingType.Subscribed) {
673 this.state.subscribedCommunities
674 .map(c => c.community_id)
675 .includes(data.comment.community_id)
677 this.state.comments.unshift(data.comment);
680 this.state.comments.unshift(data.comment);
682 this.setState(this.state);
684 } else if (res.op == UserOperation.SaveComment) {
685 let data = res.data as CommentResponse;
686 saveCommentRes(data, this.state.comments);
687 this.setState(this.state);
688 } else if (res.op == UserOperation.CreateCommentLike) {
689 let data = res.data as CommentResponse;
690 createCommentLikeRes(data, this.state.comments);
691 this.setState(this.state);