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,
57 import { i18n } from '../i18next';
58 import { T } from 'inferno-i18next';
61 subscribedCommunities: Array<CommunityUser>;
62 trendingCommunities: Array<Community>;
63 siteRes: GetSiteResponse;
64 showEditSite: boolean;
67 comments: Array<Comment>;
68 listingType: ListingType;
74 export class Main extends Component<any, MainState> {
75 private subscription: Subscription;
76 private emptyState: MainState = {
77 subscribedCommunities: [],
78 trendingCommunities: [],
86 number_of_users: null,
87 number_of_posts: null,
88 number_of_comments: null,
89 number_of_communities: null,
90 enable_downvotes: null,
91 open_registration: null,
102 listingType: getListingTypeFromProps(this.props),
103 dataType: getDataTypeFromProps(this.props),
104 sort: getSortTypeFromProps(this.props),
105 page: getPageFromProps(this.props),
108 constructor(props: any, context: any) {
109 super(props, context);
111 this.state = this.emptyState;
112 this.handleEditCancel = this.handleEditCancel.bind(this);
113 this.handleSortChange = this.handleSortChange.bind(this);
114 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
115 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
117 this.subscription = WebSocketService.Instance.subject
118 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
120 msg => this.parseMessage(msg),
121 err => console.error(err),
122 () => console.log('complete')
125 WebSocketService.Instance.getSite();
127 if (UserService.Instance.user) {
128 WebSocketService.Instance.getFollowedCommunities();
131 let listCommunitiesForm: ListCommunitiesForm = {
132 sort: SortType[SortType.Hot],
136 WebSocketService.Instance.listCommunities(listCommunitiesForm);
141 componentWillUnmount() {
142 this.subscription.unsubscribe();
145 // Necessary for back button for some reason
146 componentWillReceiveProps(nextProps: any) {
148 nextProps.history.action == 'POP' ||
149 nextProps.history.action == 'PUSH'
151 this.state.listingType = getListingTypeFromProps(nextProps);
152 this.state.dataType = getDataTypeFromProps(nextProps);
153 this.state.sort = getSortTypeFromProps(nextProps);
154 this.state.page = getPageFromProps(nextProps);
155 this.setState(this.state);
162 <div class="container">
164 <main role="main" class="col-12 col-md-8">
167 <aside class="col-12 col-md-4">{this.my_sidebar()}</aside>
176 {!this.state.loading && (
178 <div class="card border-secondary mb-3">
179 <div class="card-body">
180 {this.trendingCommunities()}
181 {UserService.Instance.user &&
182 this.state.subscribedCommunities.length > 0 && (
185 <T i18nKey="subscribed_to_communities">
187 <Link class="text-body" to="/communities">
192 <ul class="list-inline">
193 {this.state.subscribedCommunities.map(community => (
194 <li class="list-inline-item">
195 <Link to={`/c/${community.community_name}`}>
196 {community.community_name}
204 class="btn btn-sm btn-secondary btn-block"
205 to="/create_community"
207 {i18n.t('create_a_community')}
219 trendingCommunities() {
223 <T i18nKey="trending_communities">
225 <Link class="text-body" to="/communities">
230 <ul class="list-inline">
231 {this.state.trendingCommunities.map(community => (
232 <li class="list-inline-item">
233 <Link to={`/c/${community.name}`}>{community.name}</Link>
244 {!this.state.showEditSite ? (
248 site={this.state.siteRes.site}
249 onCancel={this.handleEditCancel}
257 let listingTypeStr = ListingType[this.state.listingType].toLowerCase();
258 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
259 let sortStr = SortType[this.state.sort].toLowerCase();
260 this.props.history.push(
261 `/home/data_type/${dataTypeStr}/listing_type/${listingTypeStr}/sort/${sortStr}/page/${this.state.page}`
268 <div class="card border-secondary mb-3">
269 <div class="card-body">
270 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
272 <ul class="list-inline mb-1 text-muted font-weight-bold">
273 <li className="list-inline-item-action">
276 onClick={linkEvent(this, this.handleEditClick)}
277 data-tippy-content={i18n.t('edit')}
279 <svg class="icon icon-inline">
280 <use xlinkHref="#icon-edit"></use>
286 <ul class="my-2 list-inline">
287 <li className="list-inline-item badge badge-secondary">
288 {i18n.t('number_online', { count: this.state.siteRes.online })}
290 <li className="list-inline-item badge badge-secondary">
291 {i18n.t('number_of_users', {
292 count: this.state.siteRes.site.number_of_users,
295 <li className="list-inline-item badge badge-secondary">
296 {i18n.t('number_of_communities', {
297 count: this.state.siteRes.site.number_of_communities,
300 <li className="list-inline-item badge badge-secondary">
301 {i18n.t('number_of_posts', {
302 count: this.state.siteRes.site.number_of_posts,
305 <li className="list-inline-item badge badge-secondary">
306 {i18n.t('number_of_comments', {
307 count: this.state.siteRes.site.number_of_comments,
310 <li className="list-inline-item">
311 <Link className="badge badge-secondary" to="/modlog">
316 <ul class="mt-1 list-inline small mb-0">
317 <li class="list-inline-item">{i18n.t('admins')}:</li>
318 {this.state.siteRes.admins.map(admin => (
319 <li class="list-inline-item">
321 class="text-body font-weight-bold"
322 to={`/u/${admin.name}`}
324 {admin.avatar && showAvatars() && (
328 src={pictshareAvatarThumbnail(admin.avatar)}
329 class="rounded-circle mr-1"
332 <span>{admin.name}</span>
339 {this.state.siteRes.site.description && (
340 <div class="card border-secondary mb-3">
341 <div class="card-body">
344 dangerouslySetInnerHTML={mdToHtml(
345 this.state.siteRes.site.description
357 <div class="card border-secondary">
358 <div class="card-body">
360 {i18n.t('powered_by')}
361 <svg class="icon mx-2">
362 <use xlinkHref="#icon-mouse">#</use>
369 <T i18nKey="landing_0">
371 <a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
374 <a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
380 <a href={repoUrl}>#</a>
382 <a href="https://www.rust-lang.org">#</a>
383 <a href="https://actix.rs/">#</a>
384 <a href="https://infernojs.org">#</a>
385 <a href="https://www.typescriptlang.org/">#</a>
395 <div class="main-content-wrapper">
397 {this.state.loading ? (
399 <svg class="icon icon-spinner spin">
400 <use xlinkHref="#icon-spinner"></use>
414 return this.state.dataType == DataType.Post ? (
416 posts={this.state.posts}
419 sort={this.state.sort}
423 nodes={commentsToFlatNodes(this.state.comments)}
426 sortType={this.state.sort}
433 <div className="mb-3">
436 type_={this.state.dataType}
437 onChange={this.handleDataTypeChange}
442 type_={this.state.listingType}
443 onChange={this.handleListingTypeChange}
447 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
449 {this.state.listingType == ListingType.All && (
451 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
455 <svg class="icon text-muted small">
456 <use xlinkHref="#icon-rss">#</use>
460 {UserService.Instance.user &&
461 this.state.listingType == ListingType.Subscribed && (
463 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
464 SortType[this.state.sort]
469 <svg class="icon text-muted small">
470 <use xlinkHref="#icon-rss">#</use>
481 {this.state.page > 1 && (
483 class="btn btn-sm btn-secondary mr-1"
484 onClick={linkEvent(this, this.prevPage)}
489 {this.state.posts.length == fetchLimit && (
491 class="btn btn-sm btn-secondary"
492 onClick={linkEvent(this, this.nextPage)}
501 get canAdmin(): boolean {
503 UserService.Instance.user &&
504 this.state.siteRes.admins
506 .includes(UserService.Instance.user.id)
510 handleEditClick(i: Main) {
511 i.state.showEditSite = true;
516 this.state.showEditSite = false;
517 this.setState(this.state);
522 i.state.loading = true;
526 window.scrollTo(0, 0);
531 i.state.loading = true;
535 window.scrollTo(0, 0);
538 handleSortChange(val: SortType) {
539 this.state.sort = val;
541 this.state.loading = true;
542 this.setState(this.state);
545 window.scrollTo(0, 0);
548 handleListingTypeChange(val: ListingType) {
549 this.state.listingType = val;
551 this.state.loading = true;
552 this.setState(this.state);
555 window.scrollTo(0, 0);
558 handleDataTypeChange(val: DataType) {
559 this.state.dataType = val;
561 this.state.loading = true;
562 this.setState(this.state);
565 window.scrollTo(0, 0);
569 if (this.state.dataType == DataType.Post) {
570 let getPostsForm: GetPostsForm = {
571 page: this.state.page,
573 sort: SortType[this.state.sort],
574 type_: ListingType[this.state.listingType],
576 WebSocketService.Instance.getPosts(getPostsForm);
578 let getCommentsForm: GetCommentsForm = {
579 page: this.state.page,
581 sort: SortType[this.state.sort],
582 type_: ListingType[this.state.listingType],
584 WebSocketService.Instance.getComments(getCommentsForm);
588 parseMessage(msg: WebSocketJsonResponse) {
590 let res = wsJsonToRes(msg);
592 toast(i18n.t(msg.error), 'danger');
594 } else if (msg.reconnect) {
596 } else if (res.op == UserOperation.GetFollowedCommunities) {
597 let data = res.data as GetFollowedCommunitiesResponse;
598 this.state.subscribedCommunities = data.communities;
599 this.setState(this.state);
600 } else if (res.op == UserOperation.ListCommunities) {
601 let data = res.data as ListCommunitiesResponse;
602 this.state.trendingCommunities = data.communities;
603 this.setState(this.state);
604 } else if (res.op == UserOperation.GetSite) {
605 let data = res.data as GetSiteResponse;
607 // This means it hasn't been set up yet
609 this.context.router.history.push('/setup');
611 this.state.siteRes.admins = data.admins;
612 this.state.siteRes.site = data.site;
613 this.state.siteRes.banned = data.banned;
614 this.state.siteRes.online = data.online;
615 this.setState(this.state);
616 document.title = `${WebSocketService.Instance.site.name}`;
617 } else if (res.op == UserOperation.EditSite) {
618 let data = res.data as SiteResponse;
619 this.state.siteRes.site = data.site;
620 this.state.showEditSite = false;
621 this.setState(this.state);
622 } else if (res.op == UserOperation.GetPosts) {
623 let data = res.data as GetPostsResponse;
624 this.state.posts = data.posts;
625 this.state.loading = false;
626 this.setState(this.state);
628 } else if (res.op == UserOperation.CreatePost) {
629 let data = res.data as PostResponse;
631 // If you're on subscribed, only push it if you're subscribed.
632 if (this.state.listingType == ListingType.Subscribed) {
634 this.state.subscribedCommunities
635 .map(c => c.community_id)
636 .includes(data.post.community_id)
638 this.state.posts.unshift(data.post);
642 let nsfw = data.post.nsfw || data.post.community_nsfw;
644 // Don't push the post if its nsfw, and don't have that setting on
648 UserService.Instance.user &&
649 UserService.Instance.user.show_nsfw)
651 this.state.posts.unshift(data.post);
652 this.setState(this.state);
655 } else if (res.op == UserOperation.EditPost) {
656 let data = res.data as PostResponse;
657 editPostFindRes(data, this.state.posts);
658 this.setState(this.state);
659 } else if (res.op == UserOperation.CreatePostLike) {
660 let data = res.data as PostResponse;
661 createPostLikeFindRes(data, this.state.posts);
662 this.setState(this.state);
663 } else if (res.op == UserOperation.AddAdmin) {
664 let data = res.data as AddAdminResponse;
665 this.state.siteRes.admins = data.admins;
666 this.setState(this.state);
667 } else if (res.op == UserOperation.BanUser) {
668 let data = res.data as BanUserResponse;
669 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
671 // Remove the banned if its found in the list, and the action is an unban
672 if (found && !data.banned) {
673 this.state.siteRes.banned = this.state.siteRes.banned.filter(
674 i => i.id !== data.user.id
677 this.state.siteRes.banned.push(data.user);
681 .filter(p => p.creator_id == data.user.id)
682 .forEach(p => (p.banned = data.banned));
684 this.setState(this.state);
685 } else if (res.op == UserOperation.GetComments) {
686 let data = res.data as GetCommentsResponse;
687 this.state.comments = data.comments;
688 this.state.loading = false;
689 this.setState(this.state);
690 } else if (res.op == UserOperation.EditComment) {
691 let data = res.data as CommentResponse;
692 editCommentRes(data, this.state.comments);
693 this.setState(this.state);
694 } else if (res.op == UserOperation.CreateComment) {
695 let data = res.data as CommentResponse;
697 // Necessary since it might be a user reply
698 if (data.recipient_ids.length == 0) {
699 // If you're on subscribed, only push it if you're subscribed.
700 if (this.state.listingType == ListingType.Subscribed) {
702 this.state.subscribedCommunities
703 .map(c => c.community_id)
704 .includes(data.comment.community_id)
706 this.state.comments.unshift(data.comment);
709 this.state.comments.unshift(data.comment);
711 this.setState(this.state);
713 } else if (res.op == UserOperation.SaveComment) {
714 let data = res.data as CommentResponse;
715 saveCommentRes(data, this.state.comments);
716 this.setState(this.state);
717 } else if (res.op == UserOperation.CreateCommentLike) {
718 let data = res.data as CommentResponse;
719 createCommentLikeRes(data, this.state.comments);
720 this.setState(this.state);