2019-04-21 17:38:57 -04:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-04-05 15:14:54 -04:00
|
|
|
import { Link } from 'inferno-router';
|
2019-10-18 20:20:27 -04:00
|
|
|
import { Subscription } from 'rxjs';
|
2019-04-05 15:14:54 -04:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-10-18 20:20:27 -04:00
|
|
|
import {
|
|
|
|
UserOperation,
|
|
|
|
CommunityUser,
|
|
|
|
GetFollowedCommunitiesResponse,
|
|
|
|
ListCommunitiesForm,
|
|
|
|
ListCommunitiesResponse,
|
|
|
|
Community,
|
|
|
|
SortType,
|
|
|
|
GetSiteResponse,
|
|
|
|
ListingType,
|
|
|
|
SiteResponse,
|
|
|
|
GetPostsResponse,
|
2020-01-31 20:02:20 -05:00
|
|
|
PostResponse,
|
2019-10-18 20:20:27 -04:00
|
|
|
Post,
|
|
|
|
GetPostsForm,
|
2020-01-31 20:02:20 -05:00
|
|
|
AddAdminResponse,
|
|
|
|
BanUserResponse,
|
2020-01-19 00:38:45 -05:00
|
|
|
WebSocketJsonResponse,
|
2019-10-18 20:20:27 -04:00
|
|
|
} from '../interfaces';
|
2019-04-05 15:14:54 -04:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
|
|
|
import { PostListings } from './post-listings';
|
2019-10-20 20:49:13 -04:00
|
|
|
import { SortSelect } from './sort-select';
|
2019-10-21 00:21:54 -04:00
|
|
|
import { ListingTypeSelect } from './listing-type-select';
|
2019-04-21 17:38:57 -04:00
|
|
|
import { SiteForm } from './site-form';
|
2019-10-18 20:20:27 -04:00
|
|
|
import {
|
2020-01-18 23:54:10 -05:00
|
|
|
wsJsonToRes,
|
2019-10-18 20:20:27 -04:00
|
|
|
repoUrl,
|
|
|
|
mdToHtml,
|
|
|
|
fetchLimit,
|
|
|
|
routeSortTypeToEnum,
|
|
|
|
routeListingTypeToEnum,
|
2019-12-29 15:39:48 -05:00
|
|
|
pictshareAvatarThumbnail,
|
2020-01-02 16:55:54 -05:00
|
|
|
showAvatars,
|
2020-01-22 22:29:11 -05:00
|
|
|
toast,
|
2019-10-18 20:20:27 -04:00
|
|
|
} from '../utils';
|
2019-08-09 20:14:43 -04:00
|
|
|
import { i18n } from '../i18next';
|
|
|
|
import { T } from 'inferno-i18next';
|
2019-04-20 15:42:52 -04:00
|
|
|
|
|
|
|
interface MainState {
|
2019-04-05 15:14:54 -04:00
|
|
|
subscribedCommunities: Array<CommunityUser>;
|
2019-04-10 02:19:12 -04:00
|
|
|
trendingCommunities: Array<Community>;
|
2020-01-31 20:02:20 -05:00
|
|
|
siteRes: GetSiteResponse;
|
2019-04-21 17:38:57 -04:00
|
|
|
showEditSite: boolean;
|
2019-04-08 17:46:09 -04:00
|
|
|
loading: boolean;
|
2019-04-28 20:19:04 -04:00
|
|
|
posts: Array<Post>;
|
|
|
|
type_: ListingType;
|
|
|
|
sort: SortType;
|
|
|
|
page: number;
|
2019-04-05 15:14:54 -04:00
|
|
|
}
|
|
|
|
|
2019-04-28 20:19:04 -04:00
|
|
|
export class Main extends Component<any, MainState> {
|
2019-04-05 15:14:54 -04:00
|
|
|
private subscription: Subscription;
|
2019-04-20 15:42:52 -04:00
|
|
|
private emptyState: MainState = {
|
2019-04-08 17:46:09 -04:00
|
|
|
subscribedCommunities: [],
|
2019-04-10 02:19:12 -04:00
|
|
|
trendingCommunities: [],
|
2020-01-31 20:02:20 -05:00
|
|
|
siteRes: {
|
2019-04-16 19:04:23 -04:00
|
|
|
site: {
|
|
|
|
id: null,
|
|
|
|
name: null,
|
|
|
|
creator_id: null,
|
|
|
|
creator_name: null,
|
|
|
|
published: null,
|
|
|
|
number_of_users: null,
|
|
|
|
number_of_posts: null,
|
|
|
|
number_of_comments: null,
|
2019-08-29 00:28:06 -04:00
|
|
|
number_of_communities: null,
|
2019-12-29 15:39:48 -05:00
|
|
|
enable_downvotes: null,
|
|
|
|
open_registration: null,
|
|
|
|
enable_nsfw: null,
|
2019-04-16 19:04:23 -04:00
|
|
|
},
|
|
|
|
admins: [],
|
|
|
|
banned: [],
|
2019-09-13 12:09:01 -04:00
|
|
|
online: null,
|
2019-04-16 19:04:23 -04:00
|
|
|
},
|
2019-04-21 17:38:57 -04:00
|
|
|
showEditSite: false,
|
2019-04-28 20:19:04 -04:00
|
|
|
loading: true,
|
|
|
|
posts: [],
|
|
|
|
type_: this.getListingTypeFromProps(this.props),
|
|
|
|
sort: this.getSortTypeFromProps(this.props),
|
|
|
|
page: this.getPageFromProps(this.props),
|
2019-10-18 20:20:27 -04:00
|
|
|
};
|
2019-04-28 20:19:04 -04:00
|
|
|
|
|
|
|
getListingTypeFromProps(props: any): ListingType {
|
2019-10-18 20:20:27 -04:00
|
|
|
return props.match.params.type
|
|
|
|
? routeListingTypeToEnum(props.match.params.type)
|
|
|
|
: UserService.Instance.user
|
2019-10-21 00:21:54 -04:00
|
|
|
? UserService.Instance.user.default_listing_type
|
2019-10-18 20:20:27 -04:00
|
|
|
: ListingType.All;
|
2019-04-28 20:19:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getSortTypeFromProps(props: any): SortType {
|
2019-10-18 20:20:27 -04:00
|
|
|
return props.match.params.sort
|
|
|
|
? routeSortTypeToEnum(props.match.params.sort)
|
2019-10-21 00:21:54 -04:00
|
|
|
: UserService.Instance.user
|
|
|
|
? UserService.Instance.user.default_sort_type
|
2019-10-18 20:20:27 -04:00
|
|
|
: SortType.Hot;
|
2019-04-28 20:19:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getPageFromProps(props: any): number {
|
2019-10-18 20:20:27 -04:00
|
|
|
return props.match.params.page ? Number(props.match.params.page) : 1;
|
2019-04-05 15:14:54 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 01:19:02 -04:00
|
|
|
constructor(props: any, context: any) {
|
2019-04-05 15:14:54 -04:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
2019-06-01 18:12:10 -04:00
|
|
|
this.handleEditCancel = this.handleEditCancel.bind(this);
|
2019-10-20 20:49:13 -04:00
|
|
|
this.handleSortChange = this.handleSortChange.bind(this);
|
2019-10-21 00:21:54 -04:00
|
|
|
this.handleTypeChange = this.handleTypeChange.bind(this);
|
2019-04-05 15:14:54 -04:00
|
|
|
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2020-01-15 17:28:59 -05:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
2019-10-18 20:20:27 -04:00
|
|
|
.subscribe(
|
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
2019-04-05 15:14:54 -04:00
|
|
|
() => console.log('complete')
|
2019-10-18 20:20:27 -04:00
|
|
|
);
|
2019-04-05 15:14:54 -04:00
|
|
|
|
2019-04-16 19:04:23 -04:00
|
|
|
WebSocketService.Instance.getSite();
|
|
|
|
|
|
|
|
if (UserService.Instance.user) {
|
2019-04-05 15:14:54 -04:00
|
|
|
WebSocketService.Instance.getFollowedCommunities();
|
|
|
|
}
|
2019-04-10 02:19:12 -04:00
|
|
|
|
|
|
|
let listCommunitiesForm: ListCommunitiesForm = {
|
2019-05-02 01:26:31 -04:00
|
|
|
sort: SortType[SortType.Hot],
|
2019-10-18 20:20:27 -04:00
|
|
|
limit: 6,
|
|
|
|
};
|
2019-04-10 02:19:12 -04:00
|
|
|
|
|
|
|
WebSocketService.Instance.listCommunities(listCommunitiesForm);
|
2019-04-21 17:38:57 -04:00
|
|
|
|
2020-01-31 20:02:20 -05:00
|
|
|
this.fetchPosts();
|
2019-04-05 15:14:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2019-04-28 20:19:04 -04:00
|
|
|
// Necessary for back button for some reason
|
|
|
|
componentWillReceiveProps(nextProps: any) {
|
2019-10-18 20:20:27 -04:00
|
|
|
if (
|
|
|
|
nextProps.history.action == 'POP' ||
|
|
|
|
nextProps.history.action == 'PUSH'
|
|
|
|
) {
|
2019-04-28 20:19:04 -04:00
|
|
|
this.state.type_ = this.getListingTypeFromProps(nextProps);
|
|
|
|
this.state.sort = this.getSortTypeFromProps(nextProps);
|
|
|
|
this.state.page = this.getPageFromProps(nextProps);
|
2019-09-05 21:34:10 -04:00
|
|
|
this.setState(this.state);
|
2019-04-28 20:19:04 -04:00
|
|
|
this.fetchPosts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 15:14:54 -04:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
|
|
|
<div class="row">
|
2020-02-02 13:50:44 -05:00
|
|
|
<main role="main" class="col-12 col-md-8">
|
|
|
|
{this.posts()}
|
|
|
|
</main>
|
2020-01-31 15:52:27 -05:00
|
|
|
<aside class="col-12 col-md-4">{this.my_sidebar()}</aside>
|
2019-08-09 20:14:43 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-10-18 20:20:27 -04:00
|
|
|
);
|
2019-08-09 20:14:43 -04:00
|
|
|
}
|
2019-10-18 20:20:27 -04:00
|
|
|
|
2019-08-09 20:14:43 -04:00
|
|
|
my_sidebar() {
|
2019-10-18 20:20:27 -04:00
|
|
|
return (
|
2019-08-09 20:14:43 -04:00
|
|
|
<div>
|
2019-10-18 20:20:27 -04:00
|
|
|
{!this.state.loading && (
|
2019-08-09 20:14:43 -04:00
|
|
|
<div>
|
2019-08-19 22:37:32 -04:00
|
|
|
<div class="card border-secondary mb-3">
|
|
|
|
<div class="card-body">
|
|
|
|
{this.trendingCommunities()}
|
2019-10-18 20:20:27 -04:00
|
|
|
{UserService.Instance.user &&
|
|
|
|
this.state.subscribedCommunities.length > 0 && (
|
|
|
|
<div>
|
|
|
|
<h5>
|
|
|
|
<T i18nKey="subscribed_to_communities">
|
|
|
|
#
|
|
|
|
<Link class="text-white" to="/communities">
|
|
|
|
#
|
|
|
|
</Link>
|
|
|
|
</T>
|
|
|
|
</h5>
|
|
|
|
<ul class="list-inline">
|
|
|
|
{this.state.subscribedCommunities.map(community => (
|
|
|
|
<li class="list-inline-item">
|
|
|
|
<Link to={`/c/${community.community_name}`}>
|
|
|
|
{community.community_name}
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<Link
|
|
|
|
class="btn btn-sm btn-secondary btn-block"
|
|
|
|
to="/create_community"
|
|
|
|
>
|
2020-02-02 13:50:44 -05:00
|
|
|
{i18n.t('create_a_community')}
|
2019-08-19 22:37:32 -04:00
|
|
|
</Link>
|
2019-04-28 20:19:04 -04:00
|
|
|
</div>
|
2019-08-09 20:14:43 -04:00
|
|
|
</div>
|
2019-08-19 22:37:32 -04:00
|
|
|
{this.sidebar()}
|
|
|
|
{this.landing()}
|
|
|
|
</div>
|
2019-10-18 20:20:27 -04:00
|
|
|
)}
|
2019-04-05 15:14:54 -04:00
|
|
|
</div>
|
2019-10-18 20:20:27 -04:00
|
|
|
);
|
2019-04-05 15:14:54 -04:00
|
|
|
}
|
|
|
|
|
2019-04-10 02:19:12 -04:00
|
|
|
trendingCommunities() {
|
|
|
|
return (
|
|
|
|
<div>
|
2019-08-09 20:14:43 -04:00
|
|
|
<h5>
|
2019-10-18 20:20:27 -04:00
|
|
|
<T i18nKey="trending_communities">
|
|
|
|
#
|
|
|
|
<Link class="text-white" to="/communities">
|
|
|
|
#
|
|
|
|
</Link>
|
|
|
|
</T>
|
2019-08-09 20:14:43 -04:00
|
|
|
</h5>
|
2019-10-18 20:20:27 -04:00
|
|
|
<ul class="list-inline">
|
|
|
|
{this.state.trendingCommunities.map(community => (
|
|
|
|
<li class="list-inline-item">
|
|
|
|
<Link to={`/c/${community.name}`}>{community.name}</Link>
|
|
|
|
</li>
|
|
|
|
))}
|
2019-04-10 02:19:12 -04:00
|
|
|
</ul>
|
|
|
|
</div>
|
2019-10-18 20:20:27 -04:00
|
|
|
);
|
2019-04-10 02:19:12 -04:00
|
|
|
}
|
|
|
|
|
2019-04-21 17:38:57 -04:00
|
|
|
sidebar() {
|
2019-04-09 17:21:19 -04:00
|
|
|
return (
|
2019-08-20 00:45:06 -04:00
|
|
|
<div>
|
2019-10-18 20:20:27 -04:00
|
|
|
{!this.state.showEditSite ? (
|
|
|
|
this.siteInfo()
|
|
|
|
) : (
|
2019-08-20 00:45:06 -04:00
|
|
|
<SiteForm
|
2020-01-31 20:02:20 -05:00
|
|
|
site={this.state.siteRes.site}
|
2019-10-18 20:20:27 -04:00
|
|
|
onCancel={this.handleEditCancel}
|
2019-08-20 00:45:06 -04:00
|
|
|
/>
|
2019-10-18 20:20:27 -04:00
|
|
|
)}
|
2019-04-21 17:38:57 -04:00
|
|
|
</div>
|
2019-10-18 20:20:27 -04:00
|
|
|
);
|
2019-04-21 17:38:57 -04:00
|
|
|
}
|
|
|
|
|
2019-04-28 20:19:04 -04:00
|
|
|
updateUrl() {
|
|
|
|
let typeStr = ListingType[this.state.type_].toLowerCase();
|
|
|
|
let sortStr = SortType[this.state.sort].toLowerCase();
|
2019-10-18 20:20:27 -04:00
|
|
|
this.props.history.push(
|
|
|
|
`/home/type/${typeStr}/sort/${sortStr}/page/${this.state.page}`
|
|
|
|
);
|
2019-04-28 20:19:04 -04:00
|
|
|
}
|
|
|
|
|
2019-04-21 17:38:57 -04:00
|
|
|
siteInfo() {
|
|
|
|
return (
|
|
|
|
<div>
|
2019-08-20 00:45:06 -04:00
|
|
|
<div class="card border-secondary mb-3">
|
|
|
|
<div class="card-body">
|
2020-01-31 20:02:20 -05:00
|
|
|
<h5 class="mb-0">{`${this.state.siteRes.site.name}`}</h5>
|
2019-10-18 20:20:27 -04:00
|
|
|
{this.canAdmin && (
|
|
|
|
<ul class="list-inline mb-1 text-muted small font-weight-bold">
|
2019-08-20 00:45:06 -04:00
|
|
|
<li className="list-inline-item">
|
2019-10-18 20:20:27 -04:00
|
|
|
<span
|
|
|
|
class="pointer"
|
|
|
|
onClick={linkEvent(this, this.handleEditClick)}
|
|
|
|
>
|
2020-02-02 13:50:44 -05:00
|
|
|
{i18n.t('edit')}
|
2019-08-20 00:45:06 -04:00
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2019-10-18 20:20:27 -04:00
|
|
|
)}
|
2019-08-20 00:45:06 -04:00
|
|
|
<ul class="my-2 list-inline">
|
2019-09-13 12:09:01 -04:00
|
|
|
<li className="list-inline-item badge badge-secondary">
|
2020-02-02 14:37:19 -05:00
|
|
|
{i18n.t('number_online', { count: this.state.siteRes.online })}
|
2019-09-13 12:09:01 -04:00
|
|
|
</li>
|
2019-08-20 00:45:06 -04:00
|
|
|
<li className="list-inline-item badge badge-secondary">
|
2020-02-02 13:50:44 -05:00
|
|
|
{i18n.t('number_of_users', {
|
2020-02-02 14:37:19 -05:00
|
|
|
count: this.state.siteRes.site.number_of_users,
|
2020-02-02 13:50:44 -05:00
|
|
|
})}
|
2019-08-20 00:45:06 -04:00
|
|
|
</li>
|
2019-08-29 00:28:06 -04:00
|
|
|
<li className="list-inline-item badge badge-secondary">
|
2020-02-02 13:50:44 -05:00
|
|
|
{i18n.t('number_of_communities', {
|
2020-02-02 14:37:19 -05:00
|
|
|
count: this.state.siteRes.site.number_of_communities,
|
2020-02-02 13:50:44 -05:00
|
|
|
})}
|
2019-08-29 00:28:06 -04:00
|
|
|
</li>
|
2019-08-20 00:45:06 -04:00
|
|
|
<li className="list-inline-item badge badge-secondary">
|
2020-02-02 13:50:44 -05:00
|
|
|
{i18n.t('number_of_posts', {
|
2020-02-02 14:37:19 -05:00
|
|
|
count: this.state.siteRes.site.number_of_posts,
|
2020-02-02 13:50:44 -05:00
|
|
|
})}
|
2019-08-20 00:45:06 -04:00
|
|
|
</li>
|
|
|
|
<li className="list-inline-item badge badge-secondary">
|
2020-02-02 13:50:44 -05:00
|
|
|
{i18n.t('number_of_comments', {
|
2020-02-02 14:37:19 -05:00
|
|
|
count: this.state.siteRes.site.number_of_comments,
|
2020-02-02 13:50:44 -05:00
|
|
|
})}
|
2019-08-20 00:45:06 -04:00
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<Link className="badge badge-secondary" to="/modlog">
|
2020-02-02 13:50:44 -05:00
|
|
|
{i18n.t('modlog')}
|
2019-08-20 00:45:06 -04:00
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2019-10-18 20:20:27 -04:00
|
|
|
<ul class="mt-1 list-inline small mb-0">
|
2020-02-02 13:50:44 -05:00
|
|
|
<li class="list-inline-item">{i18n.t('admins')}:</li>
|
2020-01-31 20:02:20 -05:00
|
|
|
{this.state.siteRes.admins.map(admin => (
|
2019-10-18 20:20:27 -04:00
|
|
|
<li class="list-inline-item">
|
|
|
|
<Link class="text-info" to={`/u/${admin.name}`}>
|
2020-01-02 16:55:54 -05:00
|
|
|
{admin.avatar && showAvatars() && (
|
2019-12-29 15:39:48 -05:00
|
|
|
<img
|
|
|
|
height="32"
|
|
|
|
width="32"
|
|
|
|
src={pictshareAvatarThumbnail(admin.avatar)}
|
|
|
|
class="rounded-circle mr-1"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<span>{admin.name}</span>
|
2019-10-18 20:20:27 -04:00
|
|
|
</Link>
|
2019-08-20 00:45:06 -04:00
|
|
|
</li>
|
2019-10-18 20:20:27 -04:00
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-01-31 20:02:20 -05:00
|
|
|
{this.state.siteRes.site.description && (
|
2019-10-18 20:20:27 -04:00
|
|
|
<div class="card border-secondary mb-3">
|
|
|
|
<div class="card-body">
|
|
|
|
<div
|
|
|
|
className="md-div"
|
|
|
|
dangerouslySetInnerHTML={mdToHtml(
|
2020-01-31 20:02:20 -05:00
|
|
|
this.state.siteRes.site.description
|
2019-08-20 00:45:06 -04:00
|
|
|
)}
|
2019-10-18 20:20:27 -04:00
|
|
|
/>
|
2019-08-20 00:45:06 -04:00
|
|
|
</div>
|
2019-04-16 19:04:23 -04:00
|
|
|
</div>
|
2019-10-18 20:20:27 -04:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
2019-04-21 17:38:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
landing() {
|
|
|
|
return (
|
2019-08-19 22:37:32 -04:00
|
|
|
<div class="card border-secondary">
|
|
|
|
<div class="card-body">
|
|
|
|
<h5>
|
2020-02-02 13:50:44 -05:00
|
|
|
{i18n.t('powered_by')}
|
2019-10-18 20:20:27 -04:00
|
|
|
<svg class="icon mx-2">
|
|
|
|
<use xlinkHref="#icon-mouse">#</use>
|
|
|
|
</svg>
|
|
|
|
<a href={repoUrl}>
|
|
|
|
Lemmy<sup>beta</sup>
|
|
|
|
</a>
|
2019-08-19 22:37:32 -04:00
|
|
|
</h5>
|
2019-08-20 00:45:06 -04:00
|
|
|
<p class="mb-0">
|
2019-10-18 20:20:27 -04:00
|
|
|
<T i18nKey="landing_0">
|
|
|
|
#
|
|
|
|
<a href="https://en.wikipedia.org/wiki/Social_network_aggregation">
|
|
|
|
#
|
|
|
|
</a>
|
|
|
|
<a href="https://en.wikipedia.org/wiki/Fediverse">#</a>
|
|
|
|
<br></br>
|
|
|
|
<code>#</code>
|
|
|
|
<br></br>
|
|
|
|
<b>#</b>
|
|
|
|
<br></br>
|
|
|
|
<a href={repoUrl}>#</a>
|
|
|
|
<br></br>
|
|
|
|
<a href="https://www.rust-lang.org">#</a>
|
|
|
|
<a href="https://actix.rs/">#</a>
|
|
|
|
<a href="https://infernojs.org">#</a>
|
|
|
|
<a href="https://www.typescriptlang.org/">#</a>
|
|
|
|
</T>
|
|
|
|
</p>
|
|
|
|
</div>
|
2019-04-09 17:21:19 -04:00
|
|
|
</div>
|
2019-10-18 20:20:27 -04:00
|
|
|
);
|
2019-04-28 20:19:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
posts() {
|
|
|
|
return (
|
2020-01-31 15:52:27 -05:00
|
|
|
<div class="main-content-wrapper">
|
2019-10-18 20:20:27 -04:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<div>
|
|
|
|
{this.selects()}
|
2020-02-03 19:52:39 -05:00
|
|
|
<PostListings
|
|
|
|
posts={this.state.posts}
|
|
|
|
showCommunity
|
|
|
|
removeDuplicates
|
|
|
|
/>
|
2019-10-18 20:20:27 -04:00
|
|
|
{this.paginator()}
|
|
|
|
</div>
|
|
|
|
)}
|
2019-04-28 20:19:04 -04:00
|
|
|
</div>
|
2019-10-18 20:20:27 -04:00
|
|
|
);
|
2019-04-28 20:19:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
selects() {
|
|
|
|
return (
|
2019-08-19 22:37:32 -04:00
|
|
|
<div className="mb-3">
|
2019-10-21 00:21:54 -04:00
|
|
|
<ListingTypeSelect
|
|
|
|
type_={this.state.type_}
|
|
|
|
onChange={this.handleTypeChange}
|
|
|
|
/>
|
2019-12-01 20:21:19 -05:00
|
|
|
<span class="mx-2">
|
2019-10-20 20:49:13 -04:00
|
|
|
<SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
|
|
|
|
</span>
|
2019-12-01 20:21:19 -05:00
|
|
|
{this.state.type_ == ListingType.All && (
|
2019-12-02 01:20:01 -05:00
|
|
|
<a
|
|
|
|
href={`/feeds/all.xml?sort=${SortType[this.state.sort]}`}
|
|
|
|
target="_blank"
|
|
|
|
>
|
2019-12-01 20:21:19 -05:00
|
|
|
<svg class="icon mx-1 text-muted small">
|
|
|
|
<use xlinkHref="#icon-rss">#</use>
|
|
|
|
</svg>
|
|
|
|
</a>
|
|
|
|
)}
|
2019-12-07 19:39:43 -05:00
|
|
|
{UserService.Instance.user &&
|
|
|
|
this.state.type_ == ListingType.Subscribed && (
|
|
|
|
<a
|
|
|
|
href={`/feeds/front/${UserService.Instance.auth}.xml?sort=${
|
|
|
|
SortType[this.state.sort]
|
|
|
|
}`}
|
|
|
|
target="_blank"
|
|
|
|
>
|
|
|
|
<svg class="icon mx-1 text-muted small">
|
|
|
|
<use xlinkHref="#icon-rss">#</use>
|
|
|
|
</svg>
|
|
|
|
</a>
|
|
|
|
)}
|
2019-04-28 20:19:04 -04:00
|
|
|
</div>
|
2019-10-18 20:20:27 -04:00
|
|
|
);
|
2019-04-09 17:21:19 -04:00
|
|
|
}
|
2019-04-05 15:14:54 -04:00
|
|
|
|
2019-04-28 20:19:04 -04:00
|
|
|
paginator() {
|
|
|
|
return (
|
2019-08-18 01:00:11 -04:00
|
|
|
<div class="my-2">
|
2019-10-18 20:20:27 -04:00
|
|
|
{this.state.page > 1 && (
|
|
|
|
<button
|
|
|
|
class="btn btn-sm btn-secondary mr-1"
|
|
|
|
onClick={linkEvent(this, this.prevPage)}
|
|
|
|
>
|
2020-02-02 13:50:44 -05:00
|
|
|
{i18n.t('prev')}
|
2019-10-18 20:20:27 -04:00
|
|
|
</button>
|
|
|
|
)}
|
2020-01-19 16:47:54 -05:00
|
|
|
{this.state.posts.length == fetchLimit && (
|
|
|
|
<button
|
|
|
|
class="btn btn-sm btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.nextPage)}
|
|
|
|
>
|
2020-02-02 13:50:44 -05:00
|
|
|
{i18n.t('next')}
|
2020-01-19 16:47:54 -05:00
|
|
|
</button>
|
|
|
|
)}
|
2019-04-28 20:19:04 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-21 17:38:57 -04:00
|
|
|
get canAdmin(): boolean {
|
2019-10-18 20:20:27 -04:00
|
|
|
return (
|
|
|
|
UserService.Instance.user &&
|
2020-01-31 20:02:20 -05:00
|
|
|
this.state.siteRes.admins
|
2019-10-18 20:20:27 -04:00
|
|
|
.map(a => a.id)
|
|
|
|
.includes(UserService.Instance.user.id)
|
|
|
|
);
|
2019-04-21 17:38:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
handleEditClick(i: Main) {
|
|
|
|
i.state.showEditSite = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEditCancel() {
|
|
|
|
this.state.showEditSite = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2019-10-18 20:20:27 -04:00
|
|
|
nextPage(i: Main) {
|
2019-04-28 20:19:04 -04:00
|
|
|
i.state.page++;
|
2019-08-29 19:49:58 -04:00
|
|
|
i.state.loading = true;
|
2019-04-28 20:19:04 -04:00
|
|
|
i.setState(i.state);
|
|
|
|
i.updateUrl();
|
|
|
|
i.fetchPosts();
|
2019-10-18 20:20:27 -04:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-28 20:19:04 -04:00
|
|
|
}
|
|
|
|
|
2019-10-18 20:20:27 -04:00
|
|
|
prevPage(i: Main) {
|
2019-04-28 20:19:04 -04:00
|
|
|
i.state.page--;
|
2019-08-29 19:49:58 -04:00
|
|
|
i.state.loading = true;
|
2019-04-28 20:19:04 -04:00
|
|
|
i.setState(i.state);
|
|
|
|
i.updateUrl();
|
|
|
|
i.fetchPosts();
|
2019-10-18 20:20:27 -04:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-28 20:19:04 -04:00
|
|
|
}
|
|
|
|
|
2019-10-20 20:49:13 -04:00
|
|
|
handleSortChange(val: SortType) {
|
|
|
|
this.state.sort = val;
|
|
|
|
this.state.page = 1;
|
|
|
|
this.state.loading = true;
|
|
|
|
this.setState(this.state);
|
|
|
|
this.updateUrl();
|
|
|
|
this.fetchPosts();
|
2019-10-18 20:20:27 -04:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-28 20:19:04 -04:00
|
|
|
}
|
|
|
|
|
2019-10-21 00:21:54 -04:00
|
|
|
handleTypeChange(val: ListingType) {
|
|
|
|
this.state.type_ = val;
|
|
|
|
this.state.page = 1;
|
|
|
|
this.state.loading = true;
|
|
|
|
this.setState(this.state);
|
|
|
|
this.updateUrl();
|
|
|
|
this.fetchPosts();
|
2019-10-18 20:20:27 -04:00
|
|
|
window.scrollTo(0, 0);
|
2019-10-13 15:37:10 -04:00
|
|
|
}
|
|
|
|
|
2019-04-28 20:19:04 -04:00
|
|
|
fetchPosts() {
|
|
|
|
let getPostsForm: GetPostsForm = {
|
|
|
|
page: this.state.page,
|
|
|
|
limit: fetchLimit,
|
|
|
|
sort: SortType[this.state.sort],
|
2019-10-18 20:20:27 -04:00
|
|
|
type_: ListingType[this.state.type_],
|
|
|
|
};
|
2019-04-28 20:19:04 -04:00
|
|
|
WebSocketService.Instance.getPosts(getPostsForm);
|
|
|
|
}
|
|
|
|
|
2020-01-19 00:38:45 -05:00
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
2019-04-05 15:14:54 -04:00
|
|
|
console.log(msg);
|
2020-01-18 23:54:10 -05:00
|
|
|
let res = wsJsonToRes(msg);
|
2020-01-25 09:58:53 -05:00
|
|
|
if (msg.error) {
|
2020-01-22 22:29:11 -05:00
|
|
|
toast(i18n.t(msg.error), 'danger');
|
2019-04-05 15:14:54 -04:00
|
|
|
return;
|
2020-02-04 11:19:05 -05:00
|
|
|
} else if (msg.reconnect) {
|
|
|
|
this.fetchPosts();
|
2020-01-18 23:54:10 -05:00
|
|
|
} else if (res.op == UserOperation.GetFollowedCommunities) {
|
|
|
|
let data = res.data as GetFollowedCommunitiesResponse;
|
|
|
|
this.state.subscribedCommunities = data.communities;
|
2019-04-05 15:14:54 -04:00
|
|
|
this.setState(this.state);
|
2020-01-18 23:54:10 -05:00
|
|
|
} else if (res.op == UserOperation.ListCommunities) {
|
|
|
|
let data = res.data as ListCommunitiesResponse;
|
|
|
|
this.state.trendingCommunities = data.communities;
|
2019-04-10 02:19:12 -04:00
|
|
|
this.setState(this.state);
|
2020-01-18 23:54:10 -05:00
|
|
|
} else if (res.op == UserOperation.GetSite) {
|
|
|
|
let data = res.data as GetSiteResponse;
|
2019-04-16 19:04:23 -04:00
|
|
|
|
|
|
|
// This means it hasn't been set up yet
|
2020-01-18 23:54:10 -05:00
|
|
|
if (!data.site) {
|
2019-10-18 20:20:27 -04:00
|
|
|
this.context.router.history.push('/setup');
|
2019-04-16 19:04:23 -04:00
|
|
|
}
|
2020-01-31 20:02:20 -05:00
|
|
|
this.state.siteRes.admins = data.admins;
|
|
|
|
this.state.siteRes.site = data.site;
|
|
|
|
this.state.siteRes.banned = data.banned;
|
|
|
|
this.state.siteRes.online = data.online;
|
2019-04-16 19:04:23 -04:00
|
|
|
this.setState(this.state);
|
2019-06-02 21:35:46 -04:00
|
|
|
document.title = `${WebSocketService.Instance.site.name}`;
|
2020-01-18 23:54:10 -05:00
|
|
|
} else if (res.op == UserOperation.EditSite) {
|
|
|
|
let data = res.data as SiteResponse;
|
2020-01-31 20:02:20 -05:00
|
|
|
this.state.siteRes.site = data.site;
|
2019-04-21 17:38:57 -04:00
|
|
|
this.state.showEditSite = false;
|
|
|
|
this.setState(this.state);
|
2020-01-18 23:54:10 -05:00
|
|
|
} else if (res.op == UserOperation.GetPosts) {
|
|
|
|
let data = res.data as GetPostsResponse;
|
|
|
|
this.state.posts = data.posts;
|
2019-04-28 20:19:04 -04:00
|
|
|
this.state.loading = false;
|
2020-01-31 20:02:20 -05:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.CreatePost) {
|
|
|
|
let data = res.data as PostResponse;
|
|
|
|
|
|
|
|
// If you're on subscribed, only push it if you're subscribed.
|
|
|
|
if (this.state.type_ == ListingType.Subscribed) {
|
|
|
|
if (
|
|
|
|
this.state.subscribedCommunities
|
|
|
|
.map(c => c.community_id)
|
|
|
|
.includes(data.post.community_id)
|
|
|
|
) {
|
|
|
|
this.state.posts.unshift(data.post);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.state.posts.unshift(data.post);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.EditPost) {
|
|
|
|
let data = res.data as PostResponse;
|
|
|
|
let found = this.state.posts.find(c => c.id == data.post.id);
|
2020-02-05 14:24:35 -05:00
|
|
|
if (found) {
|
|
|
|
found.url = data.post.url;
|
|
|
|
found.name = data.post.name;
|
|
|
|
found.nsfw = data.post.nsfw;
|
2020-01-31 20:02:20 -05:00
|
|
|
|
2020-02-05 14:24:35 -05:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
2020-01-18 23:54:10 -05:00
|
|
|
} else if (res.op == UserOperation.CreatePostLike) {
|
2020-02-02 12:45:41 -05:00
|
|
|
let data = res.data as PostResponse;
|
2020-01-18 23:54:10 -05:00
|
|
|
let found = this.state.posts.find(c => c.id == data.post.id);
|
2020-02-05 14:24:35 -05:00
|
|
|
if (found) {
|
|
|
|
found.score = data.post.score;
|
|
|
|
found.upvotes = data.post.upvotes;
|
|
|
|
found.downvotes = data.post.downvotes;
|
|
|
|
if (data.post.my_vote !== null) {
|
|
|
|
found.my_vote = data.post.my_vote;
|
|
|
|
found.upvoteLoading = false;
|
|
|
|
found.downvoteLoading = false;
|
|
|
|
}
|
|
|
|
this.setState(this.state);
|
2020-01-31 20:02:20 -05:00
|
|
|
}
|
|
|
|
} else if (res.op == UserOperation.AddAdmin) {
|
|
|
|
let data = res.data as AddAdminResponse;
|
|
|
|
this.state.siteRes.admins = data.admins;
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (res.op == UserOperation.BanUser) {
|
|
|
|
let data = res.data as BanUserResponse;
|
|
|
|
let found = this.state.siteRes.banned.find(u => (u.id = data.user.id));
|
|
|
|
|
|
|
|
// Remove the banned if its found in the list, and the action is an unban
|
|
|
|
if (found && !data.banned) {
|
|
|
|
this.state.siteRes.banned = this.state.siteRes.banned.filter(
|
|
|
|
i => i.id !== data.user.id
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.state.siteRes.banned.push(data.user);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state.posts
|
|
|
|
.filter(p => p.creator_id == data.user.id)
|
|
|
|
.forEach(p => (p.banned = data.banned));
|
|
|
|
|
2019-04-28 20:19:04 -04:00
|
|
|
this.setState(this.state);
|
2019-04-21 17:38:57 -04:00
|
|
|
}
|
2019-04-05 15:14:54 -04:00
|
|
|
}
|
|
|
|
}
|