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>
396 posts={this.state.posts}
409 <div className="mb-3">
411 type_={this.state.type_}
412 onChange={this.handleTypeChange}
415 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
417 {this.state.type_ == ListingType.All && (
419 href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
422 <svg class="icon mx-1 text-muted small">
423 <use xlinkHref="#icon-rss">#</use>
427 {UserService.Instance.user &&
428 this.state.type_ == ListingType.Subscribed && (
430 href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
431 SortType[this.state.sort]
435 <svg class="icon mx-1 text-muted small">
436 <use xlinkHref="#icon-rss">#</use>
447 {this.state.page > 1 && (
449 class="btn btn-sm btn-secondary mr-1"
450 onClick={linkEvent(this, this.prevPage)}
455 {this.state.posts.length == fetchLimit && (
457 class="btn btn-sm btn-secondary"
458 onClick={linkEvent(this, this.nextPage)}
467 get canAdmin(): boolean {
469 UserService.Instance.user &&
470 this.state.siteRes.admins
472 .includes(UserService.Instance.user.id)
476 handleEditClick(i: Main) {
477 i.state.showEditSite = true;
482 this.state.showEditSite = false;
483 this.setState(this.state);
488 i.state.loading = true;
492 window.scrollTo(0, 0);
497 i.state.loading = true;
501 window.scrollTo(0, 0);
504 handleSortChange(val: SortType) {
505 this.state.sort = val;
507 this.state.loading = true;
508 this.setState(this.state);
511 window.scrollTo(0, 0);
514 handleTypeChange(val: ListingType) {
515 this.state.type_ = val;
517 this.state.loading = true;
518 this.setState(this.state);
521 window.scrollTo(0, 0);
525 let getPostsForm: GetPostsForm = {
526 page: this.state.page,
528 sort: SortType[this.state.sort],
529 type_: ListingType[this.state.type_],
531 WebSocketService.Instance.getPosts(getPostsForm);
534 parseMessage(msg: WebSocketJsonResponse) {
536 let res = wsJsonToRes(msg);
538 toast(i18n.t(msg.error), 'danger');
540 } else if (msg.reconnect) {
542 } else if (res.op == UserOperation.GetFollowedCommunities) {
543 let data = res.data as GetFollowedCommunitiesResponse;
544 this.state.subscribedCommunities = data.communities;
545 this.setState(this.state);
546 } else if (res.op == UserOperation.ListCommunities) {
547 let data = res.data as ListCommunitiesResponse;
548 this.state.trendingCommunities = data.communities;
549 this.setState(this.state);
550 } else if (res.op == UserOperation.GetSite) {
551 let data = res.data as GetSiteResponse;
553 // This means it hasn't been set up yet
555 this.context.router.history.push('/setup');
557 this.state.siteRes.admins = data.admins;
558 this.state.siteRes.site = data.site;
559 this.state.siteRes.banned = data.banned;
560 this.state.siteRes.online = data.online;
561 this.setState(this.state);
562 document.title = `${WebSocketService.Instance.site.name}`;
563 } else if (res.op == UserOperation.EditSite) {
564 let data = res.data as SiteResponse;
565 this.state.siteRes.site = data.site;
566 this.state.showEditSite = false;
567 this.setState(this.state);
568 } else if (res.op == UserOperation.GetPosts) {
569 let data = res.data as GetPostsResponse;
570 this.state.posts = data.posts;
571 this.state.loading = false;
572 this.setState(this.state);
573 } else if (res.op == UserOperation.CreatePost) {
574 let data = res.data as PostResponse;
576 // If you're on subscribed, only push it if you're subscribed.
577 if (this.state.type_ == ListingType.Subscribed) {
579 this.state.subscribedCommunities
580 .map(c => c.community_id)
581 .includes(data.post.community_id)
583 this.state.posts.unshift(data.post);
586 this.state.posts.unshift(data.post);
589 this.setState(this.state);
590 } else if (res.op == UserOperation.EditPost) {
591 let data = res.data as PostResponse;
592 let found = this.state.posts.find(c => c.id == data.post.id);
594 found.url = data.post.url;
595 found.name = data.post.name;
596 found.nsfw = data.post.nsfw;
598 this.setState(this.state);
600 } else if (res.op == UserOperation.CreatePostLike) {
601 let data = res.data as PostResponse;
602 let found = this.state.posts.find(c => c.id == data.post.id);
604 found.score = data.post.score;
605 found.upvotes = data.post.upvotes;
606 found.downvotes = data.post.downvotes;
607 if (data.post.my_vote !== null) {
608 found.my_vote = data.post.my_vote;
609 found.upvoteLoading = false;
610 found.downvoteLoading = false;
612 this.setState(this.state);
614 } else if (res.op == UserOperation.AddAdmin) {
615 let data = res.data as AddAdminResponse;
616 this.state.siteRes.admins = data.admins;
617 this.setState(this.state);
618 } else if (res.op == UserOperation.BanUser) {
619 let data = res.data as BanUserResponse;
620 let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
622 // Remove the banned if its found in the list, and the action is an unban
623 if (found && !data.banned) {
624 this.state.siteRes.banned = this.state.siteRes.banned.filter(
625 i => i.id !== data.user.id
628 this.state.siteRes.banned.push(data.user);
632 .filter(p => p.creator_id == data.user.id)
633 .forEach(p => (p.banned = data.banned));
635 this.setState(this.state);