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,
22 WebSocketJsonResponse,
23 } from '../interfaces';
24 import { WebSocketService, UserService } from '../services';
25 import { PostListings } from './post-listings';
26 import { SortSelect } from './sort-select';
27 import { ListingTypeSelect } from './listing-type-select';
28 import { SiteForm } from './site-form';
35 routeListingTypeToEnum,
36 pictshareAvatarThumbnail,
40 import { i18n } from '../i18next';
41 import { T } from 'inferno-i18next';
44 subscribedCommunities: Array<CommunityUser>;
45 trendingCommunities: Array<Community>;
46 siteRes: GetSiteResponse;
47 showEditSite: boolean;
55 export class Main extends Component<any, MainState> {
56 private subscription: Subscription;
57 private emptyState: MainState = {
58 subscribedCommunities: [],
59 trendingCommunities: [],
67 number_of_users: null,
68 number_of_posts: null,
69 number_of_comments: null,
70 number_of_communities: null,
71 enable_downvotes: null,
72 open_registration: null,
82 type_: this.getListingTypeFromProps(this.props),
83 sort: this.getSortTypeFromProps(this.props),
84 page: this.getPageFromProps(this.props),
87 getListingTypeFromProps(props: any): ListingType {
88 return props.match.params.type
89 ? routeListingTypeToEnum(props.match.params.type)
90 : UserService.Instance.user
91 ? UserService.Instance.user.default_listing_type
95 getSortTypeFromProps(props: any): SortType {
96 return props.match.params.sort
97 ? routeSortTypeToEnum(props.match.params.sort)
98 : UserService.Instance.user
99 ? UserService.Instance.user.default_sort_type
103 getPageFromProps(props: any): number {
104 return props.match.params.page ? Number(props.match.params.page) : 1;
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.handleTypeChange = this.handleTypeChange.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.type_ = this.getListingTypeFromProps(nextProps);
150 this.state.sort = this.getSortTypeFromProps(nextProps);
151 this.state.page = this.getPageFromProps(nextProps);
152 this.setState(this.state);
159 <div class="container">
161 <main role="main" class="col-12 col-md-8">
164 <aside class="col-12 col-md-4">{this.my_sidebar()}</aside>
173 {!this.state.loading && (
175 <div class="card border-secondary mb-3">
176 <div class="card-body">
177 {this.trendingCommunities()}
178 {UserService.Instance.user &&
179 this.state.subscribedCommunities.length > 0 && (
182 <T i18nKey="subscribed_to_communities">
184 <Link class="text-white" to="/communities">
189 <ul class="list-inline">
190 {this.state.subscribedCommunities.map(community => (
191 <li class="list-inline-item">
192 <Link to={`/c/${community.community_name}`}>
193 {community.community_name}
201 class="btn btn-sm btn-secondary btn-block"
202 to="/create_community"
204 {i18n.t('create_a_community')}
216 trendingCommunities() {
220 <T i18nKey="trending_communities">
222 <Link class="text-white" to="/communities">
227 <ul class="list-inline">
228 {this.state.trendingCommunities.map(community => (
229 <li class="list-inline-item">
230 <Link to={`/c/${community.name}`}>{community.name}</Link>
241 {!this.state.showEditSite ? (
245 site={this.state.siteRes.site}
246 onCancel={this.handleEditCancel}
254 let typeStr = ListingType[this.state.type_].toLowerCase();
255 let sortStr = SortType[this.state.sort].toLowerCase();
256 this.props.history.push(
257 `/home/type/${typeStr}/sort/${sortStr}/page/${this.state.page}`
264 <div class="card border-secondary mb-3">
265 <div class="card-body">
266 <h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
268 <ul class="list-inline mb-1 text-muted small font-weight-bold">
269 <li className="list-inline-item">
272 onClick={linkEvent(this, this.handleEditClick)}
279 <ul class="my-2 list-inline">
280 <li className="list-inline-item badge badge-secondary">
281 {i18n.t('number_online', { count: this.state.siteRes.online })}
283 <li className="list-inline-item badge badge-secondary">
284 {i18n.t('number_of_users', {
285 count: this.state.siteRes.site.number_of_users,
288 <li className="list-inline-item badge badge-secondary">
289 {i18n.t('number_of_communities', {
290 count: this.state.siteRes.site.number_of_communities,
293 <li className="list-inline-item badge badge-secondary">
294 {i18n.t('number_of_posts', {
295 count: this.state.siteRes.site.number_of_posts,
298 <li className="list-inline-item badge badge-secondary">
299 {i18n.t('number_of_comments', {
300 count: this.state.siteRes.site.number_of_comments,
303 <li className="list-inline-item">
304 <Link className="badge badge-secondary" to="/modlog">
309 <ul class="mt-1 list-inline small mb-0">
310 <li class="list-inline-item">{i18n.t('admins')}:</li>
311 {this.state.siteRes.admins.map(admin => (
312 <li class="list-inline-item">
313 <Link class="text-info" to={`/u/${admin.name}`}>
314 {admin.avatar && showAvatars() && (
318 src={pictshareAvatarThumbnail(admin.avatar)}
319 class="rounded-circle mr-1"
322 <span>{admin.name}</span>
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">
386 {this.state.loading ? (
388 <svg class="icon icon-spinner spin">
389 <use xlinkHref="#icon-spinner"></use>
395 <PostListings posts={this.state.posts} showCommunity />
405 <div className="mb-3">
407 type_={this.state.type_}
408 onChange={this.handleTypeChange}
411 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
413 {this.state.type_ == ListingType.All && (
415 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
418 <svg class="icon mx-1 text-muted small">
419 <use xlinkHref="#icon-rss">#</use>
423 {UserService.Instance.user &&
424 this.state.type_ == ListingType.Subscribed && (
426 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
427 SortType[this.state.sort]
431 <svg class="icon mx-1 text-muted small">
432 <use xlinkHref="#icon-rss">#</use>
443 {this.state.page > 1 && (
445 class="btn btn-sm btn-secondary mr-1"
446 onClick={linkEvent(this, this.prevPage)}
451 {this.state.posts.length == fetchLimit && (
453 class="btn btn-sm btn-secondary"
454 onClick={linkEvent(this, this.nextPage)}
463 get canAdmin(): boolean {
465 UserService.Instance.user &&
466 this.state.siteRes.admins
468 .includes(UserService.Instance.user.id)
472 handleEditClick(i: Main) {
473 i.state.showEditSite = true;
478 this.state.showEditSite = false;
479 this.setState(this.state);
484 i.state.loading = true;
488 window.scrollTo(0, 0);
493 i.state.loading = true;
497 window.scrollTo(0, 0);
500 handleSortChange(val: SortType) {
501 this.state.sort = val;
503 this.state.loading = true;
504 this.setState(this.state);
507 window.scrollTo(0, 0);
510 handleTypeChange(val: ListingType) {
511 this.state.type_ = val;
513 this.state.loading = true;
514 this.setState(this.state);
517 window.scrollTo(0, 0);
521 let getPostsForm: GetPostsForm = {
522 page: this.state.page,
524 sort: SortType[this.state.sort],
525 type_: ListingType[this.state.type_],
527 WebSocketService.Instance.getPosts(getPostsForm);
530 parseMessage(msg: WebSocketJsonResponse) {
532 let res = wsJsonToRes(msg);
534 toast(i18n.t(msg.error), 'danger');
536 } else if (res.op == UserOperation.GetFollowedCommunities) {
537 let data = res.data as GetFollowedCommunitiesResponse;
538 this.state.subscribedCommunities = data.communities;
539 this.setState(this.state);
540 } else if (res.op == UserOperation.ListCommunities) {
541 let data = res.data as ListCommunitiesResponse;
542 this.state.trendingCommunities = data.communities;
543 this.setState(this.state);
544 } else if (res.op == UserOperation.GetSite) {
545 let data = res.data as GetSiteResponse;
547 // This means it hasn't been set up yet
549 this.context.router.history.push('/setup');
551 this.state.siteRes.admins = data.admins;
552 this.state.siteRes.site = data.site;
553 this.state.siteRes.banned = data.banned;
554 this.state.siteRes.online = data.online;
555 this.setState(this.state);
556 document.title = `${WebSocketService.Instance.site.name}`;
557 } else if (res.op == UserOperation.EditSite) {
558 let data = res.data as SiteResponse;
559 this.state.siteRes.site = data.site;
560 this.state.showEditSite = false;
561 this.setState(this.state);
562 } else if (res.op == UserOperation.GetPosts) {
563 let data = res.data as GetPostsResponse;
564 this.state.posts = data.posts;
565 this.state.loading = false;
566 this.setState(this.state);
567 } else if (res.op == UserOperation.CreatePost) {
568 let data = res.data as PostResponse;
570 // If you're on subscribed, only push it if you're subscribed.
571 if (this.state.type_ == ListingType.Subscribed) {
573 this.state.subscribedCommunities
574 .map(c => c.community_id)
575 .includes(data.post.community_id)
577 this.state.posts.unshift(data.post);
580 this.state.posts.unshift(data.post);
583 this.setState(this.state);
584 } else if (res.op == UserOperation.EditPost) {
585 let data = res.data as PostResponse;
586 let found = this.state.posts.find(c => c.id == data.post.id);
588 found.url = data.post.url;
589 found.name = data.post.name;
590 found.nsfw = data.post.nsfw;
592 this.setState(this.state);
593 } else if (res.op == UserOperation.CreatePostLike) {
594 let data = res.data as PostResponse;
595 let found = this.state.posts.find(c => c.id == data.post.id);
597 found.score = data.post.score;
598 found.upvotes = data.post.upvotes;
599 found.downvotes = data.post.downvotes;
600 if (data.post.my_vote !== null) {
601 found.my_vote = data.post.my_vote;
602 found.upvoteLoading = false;
603 found.downvoteLoading = false;
606 this.setState(this.state);
607 } else if (res.op == UserOperation.AddAdmin) {
608 let data = res.data as AddAdminResponse;
609 this.state.siteRes.admins = data.admins;
610 this.setState(this.state);
611 } else if (res.op == UserOperation.BanUser) {
612 let data = res.data as BanUserResponse;
613 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
615 // Remove the banned if its found in the list, and the action is an unban
616 if (found && !data.banned) {
617 this.state.siteRes.banned = this.state.siteRes.banned.filter(
618 i => i.id !== data.user.id
621 this.state.siteRes.banned.push(data.user);
625 .filter(p => p.creator_id == data.user.id)
626 .forEach(p => (p.banned = data.banned));
628 this.setState(this.state);