2022-06-21 21:42:29 +00:00
|
|
|
import { None, Option, Some } from "@sniptt/monads";
|
2021-07-17 20:42:55 +00:00
|
|
|
import autosize from "autosize";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
|
|
|
import { Prompt } from "inferno-router";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
CommunityView,
|
2020-12-24 01:58:27 +00:00
|
|
|
CreatePost,
|
|
|
|
EditPost,
|
2022-09-22 15:14:58 +00:00
|
|
|
Language,
|
2021-07-17 20:42:55 +00:00
|
|
|
ListingType,
|
2020-09-06 16:15:25 +00:00
|
|
|
PostResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
PostView,
|
2020-12-24 01:58:27 +00:00
|
|
|
Search,
|
2020-09-06 16:15:25 +00:00
|
|
|
SearchResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
SearchType,
|
|
|
|
SortType,
|
2022-06-21 21:42:29 +00:00
|
|
|
toUndefined,
|
2021-07-17 20:42:55 +00:00
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { pictrsUri } from "../../env";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { PostFormParams } from "../../interfaces";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-10-29 01:58:49 +00:00
|
|
|
archiveTodayUrl,
|
2022-06-21 21:42:29 +00:00
|
|
|
auth,
|
2021-07-17 20:42:55 +00:00
|
|
|
capitalizeFirstLetter,
|
|
|
|
choicesConfig,
|
|
|
|
communitySelectName,
|
|
|
|
communityToChoice,
|
2020-09-06 16:15:25 +00:00
|
|
|
debounce,
|
2021-07-17 20:42:55 +00:00
|
|
|
fetchCommunities,
|
2021-08-19 15:24:13 +00:00
|
|
|
getSiteMetadata,
|
2021-10-29 01:58:49 +00:00
|
|
|
ghostArchiveUrl,
|
2021-07-17 20:42:55 +00:00
|
|
|
isBrowser,
|
2020-09-06 16:15:25 +00:00
|
|
|
isImage,
|
2022-09-22 15:14:58 +00:00
|
|
|
myFirstDiscussionLanguageId,
|
2020-09-06 16:15:25 +00:00
|
|
|
pictrsDeleteToast,
|
2022-02-24 15:31:44 +00:00
|
|
|
relTags,
|
2021-07-17 20:42:55 +00:00
|
|
|
setupTippy,
|
|
|
|
toast,
|
2022-06-21 21:42:29 +00:00
|
|
|
trendingFetchLimit,
|
2020-09-06 16:15:25 +00:00
|
|
|
validTitle,
|
2021-07-17 20:42:55 +00:00
|
|
|
validURL,
|
2021-10-29 01:58:49 +00:00
|
|
|
webArchiveUrl,
|
2021-07-17 20:42:55 +00:00
|
|
|
wsClient,
|
2020-09-07 22:24:48 +00:00
|
|
|
wsSubscribe,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { Icon, Spinner } from "../common/icon";
|
2022-09-22 15:14:58 +00:00
|
|
|
import { LanguageSelect } from "../common/language-select";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { MarkdownTextArea } from "../common/markdown-textarea";
|
|
|
|
import { PostListings } from "./post-listings";
|
2020-09-07 22:24:48 +00:00
|
|
|
|
2021-07-17 20:42:55 +00:00
|
|
|
var Choices: any;
|
2020-09-07 22:24:48 +00:00
|
|
|
if (isBrowser()) {
|
2021-02-22 02:39:04 +00:00
|
|
|
Choices = require("choices.js");
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
const MAX_POST_TITLE_LENGTH = 200;
|
|
|
|
|
|
|
|
interface PostFormProps {
|
2022-06-21 21:42:29 +00:00
|
|
|
post_view: Option<PostView>; // If a post is given, that means this is an edit
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages: Language[];
|
2022-06-21 21:42:29 +00:00
|
|
|
communities: Option<CommunityView[]>;
|
|
|
|
params: Option<PostFormParams>;
|
2020-09-06 16:15:25 +00:00
|
|
|
onCancel?(): any;
|
2020-12-24 01:58:27 +00:00
|
|
|
onCreate?(post: PostView): any;
|
|
|
|
onEdit?(post: PostView): any;
|
2022-06-21 21:42:29 +00:00
|
|
|
enableNsfw?: boolean;
|
|
|
|
enableDownvotes?: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PostFormState {
|
2020-12-24 01:58:27 +00:00
|
|
|
postForm: CreatePost;
|
2022-06-21 21:42:29 +00:00
|
|
|
suggestedTitle: Option<string>;
|
|
|
|
suggestedPosts: Option<PostView[]>;
|
|
|
|
crossPosts: Option<PostView[]>;
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: boolean;
|
|
|
|
imageLoading: boolean;
|
2022-09-22 18:13:22 +00:00
|
|
|
communitySearchLoading: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
previewMode: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PostForm extends Component<PostFormProps, PostFormState> {
|
|
|
|
private subscription: Subscription;
|
2020-09-07 22:24:48 +00:00
|
|
|
private choices: any;
|
2020-09-06 16:15:25 +00:00
|
|
|
private emptyState: PostFormState = {
|
2022-06-21 21:42:29 +00:00
|
|
|
postForm: new CreatePost({
|
|
|
|
community_id: undefined,
|
|
|
|
name: undefined,
|
|
|
|
nsfw: Some(false),
|
|
|
|
url: None,
|
|
|
|
body: None,
|
|
|
|
honeypot: None,
|
2022-09-22 15:14:58 +00:00
|
|
|
language_id: None,
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: undefined,
|
|
|
|
}),
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: false,
|
|
|
|
imageLoading: false,
|
2022-09-22 18:13:22 +00:00
|
|
|
communitySearchLoading: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
previewMode: false,
|
2022-06-21 21:42:29 +00:00
|
|
|
suggestedTitle: None,
|
|
|
|
suggestedPosts: None,
|
|
|
|
crossPosts: None,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
2021-04-07 16:46:12 +00:00
|
|
|
this.fetchSimilarPosts = debounce(this.fetchSimilarPosts.bind(this));
|
|
|
|
this.fetchPageTitle = debounce(this.fetchPageTitle.bind(this));
|
2020-09-06 16:15:25 +00:00
|
|
|
this.handlePostBodyChange = this.handlePostBodyChange.bind(this);
|
2022-09-22 15:14:58 +00:00
|
|
|
this.handleLanguageChange = this.handleLanguageChange.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
// Means its an edit
|
2022-09-22 15:03:35 +00:00
|
|
|
if (this.props.post_view.isSome()) {
|
|
|
|
let pv = this.props.post_view.unwrap();
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
postForm: new CreatePost({
|
2022-06-21 21:42:29 +00:00
|
|
|
body: pv.post.body,
|
|
|
|
name: pv.post.name,
|
|
|
|
community_id: pv.community.id,
|
|
|
|
url: pv.post.url,
|
|
|
|
nsfw: Some(pv.post.nsfw),
|
|
|
|
honeypot: None,
|
2022-09-22 15:14:58 +00:00
|
|
|
language_id: Some(pv.post.language_id),
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: auth().unwrap(),
|
2022-09-22 15:03:35 +00:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
if (this.props.params.isSome()) {
|
|
|
|
let params = this.props.params.unwrap();
|
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
postForm: {
|
|
|
|
...this.state.postForm,
|
|
|
|
name: toUndefined(params.name),
|
|
|
|
url: params.url,
|
|
|
|
body: params.body,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
setupTippy();
|
2020-09-07 22:24:48 +00:00
|
|
|
this.setupCommunities();
|
2021-02-22 02:39:04 +00:00
|
|
|
let textarea: any = document.getElementById("post-title");
|
2021-01-27 15:29:01 +00:00
|
|
|
if (textarea) {
|
|
|
|
autosize(textarea);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
if (
|
|
|
|
!this.state.loading &&
|
|
|
|
(this.state.postForm.name ||
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.postForm.url.isSome() ||
|
|
|
|
this.state.postForm.body.isSome())
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
|
|
|
window.onbeforeunload = () => true;
|
|
|
|
} else {
|
|
|
|
window.onbeforeunload = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
/* this.choices && this.choices.destroy(); */
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2022-09-22 15:14:58 +00:00
|
|
|
let selectedLangs = this.state.postForm.language_id
|
|
|
|
.or(myFirstDiscussionLanguageId(UserService.Instance.myUserInfo))
|
|
|
|
.map(Array.of);
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Prompt
|
|
|
|
when={
|
|
|
|
!this.state.loading &&
|
|
|
|
(this.state.postForm.name ||
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.postForm.url.isSome() ||
|
|
|
|
this.state.postForm.body.isSome())
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2021-02-22 02:39:04 +00:00
|
|
|
message={i18n.t("block_leaving")}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
<form onSubmit={linkEvent(this, this.handlePostSubmit)}>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="post-url">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("url")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
|
|
|
type="url"
|
|
|
|
id="post-url"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2022-06-21 21:42:29 +00:00
|
|
|
value={toUndefined(this.state.postForm.url)}
|
2020-09-06 16:15:25 +00:00
|
|
|
onInput={linkEvent(this, this.handlePostUrlChange)}
|
|
|
|
onPaste={linkEvent(this, this.handleImageUploadPaste)}
|
|
|
|
/>
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.state.suggestedTitle.match({
|
|
|
|
some: title => (
|
|
|
|
<div
|
2022-09-22 15:03:35 +00:00
|
|
|
className="mt-1 text-muted small font-weight-bold pointer"
|
2022-06-21 21:42:29 +00:00
|
|
|
role="button"
|
|
|
|
onClick={linkEvent(this, this.copySuggestedTitle)}
|
|
|
|
>
|
2022-07-30 03:38:10 +00:00
|
|
|
{i18n.t("copy_suggested_title", { title: "" })} {title}
|
2022-06-21 21:42:29 +00:00
|
|
|
</div>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
})}
|
2020-09-06 16:15:25 +00:00
|
|
|
<form>
|
|
|
|
<label
|
|
|
|
htmlFor="file-upload"
|
|
|
|
className={`${
|
2022-06-21 21:42:29 +00:00
|
|
|
UserService.Instance.myUserInfo.isSome() && "pointer"
|
2020-09-06 16:15:25 +00:00
|
|
|
} d-inline-block float-right text-muted font-weight-bold`}
|
2021-02-22 02:39:04 +00:00
|
|
|
data-tippy-content={i18n.t("upload_image")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="image" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
id="file-upload"
|
|
|
|
type="file"
|
|
|
|
accept="image/*,video/*"
|
|
|
|
name="file"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="d-none"
|
2022-06-21 21:42:29 +00:00
|
|
|
disabled={UserService.Instance.myUserInfo.isNone()}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleImageUpload)}
|
|
|
|
/>
|
|
|
|
</form>
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.state.postForm.url.match({
|
|
|
|
some: url =>
|
|
|
|
validURL(url) && (
|
|
|
|
<div>
|
|
|
|
<a
|
|
|
|
href={`${webArchiveUrl}/save/${encodeURIComponent(
|
|
|
|
url
|
|
|
|
)}`}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
|
2022-06-21 21:42:29 +00:00
|
|
|
rel={relTags}
|
|
|
|
>
|
|
|
|
archive.org {i18n.t("archive_link")}
|
|
|
|
</a>
|
|
|
|
<a
|
|
|
|
href={`${ghostArchiveUrl}/search?term=${encodeURIComponent(
|
|
|
|
url
|
|
|
|
)}`}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
|
2022-06-21 21:42:29 +00:00
|
|
|
rel={relTags}
|
|
|
|
>
|
|
|
|
ghostarchive.org {i18n.t("archive_link")}
|
|
|
|
</a>
|
|
|
|
<a
|
|
|
|
href={`${archiveTodayUrl}/?run=1&url=${encodeURIComponent(
|
|
|
|
url
|
|
|
|
)}`}
|
2022-09-22 15:03:35 +00:00
|
|
|
className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
|
2022-06-21 21:42:29 +00:00
|
|
|
rel={relTags}
|
|
|
|
>
|
|
|
|
archive.today {i18n.t("archive_link")}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
})}
|
2021-02-11 20:35:27 +00:00
|
|
|
{this.state.imageLoading && <Spinner />}
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.state.postForm.url.match({
|
|
|
|
some: url =>
|
2022-09-22 15:03:35 +00:00
|
|
|
isImage(url) && (
|
|
|
|
<img src={url} className="img-fluid" alt="" />
|
|
|
|
),
|
2022-06-21 21:42:29 +00:00
|
|
|
none: <></>,
|
|
|
|
})}
|
|
|
|
{this.state.crossPosts.match({
|
|
|
|
some: xPosts =>
|
|
|
|
xPosts.length > 0 && (
|
|
|
|
<>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="my-1 text-muted small font-weight-bold">
|
2022-06-21 21:42:29 +00:00
|
|
|
{i18n.t("cross_posts")}
|
|
|
|
</div>
|
|
|
|
<PostListings
|
|
|
|
showCommunity
|
|
|
|
posts={xPosts}
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.props.allLanguages}
|
2022-06-21 21:42:29 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
})}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="post-title">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("title")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<textarea
|
|
|
|
value={this.state.postForm.name}
|
|
|
|
id="post-title"
|
|
|
|
onInput={linkEvent(this, this.handlePostNameChange)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className={`form-control ${
|
2021-02-22 02:39:04 +00:00
|
|
|
!validTitle(this.state.postForm.name) && "is-invalid"
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
|
|
|
required
|
2021-01-27 15:29:01 +00:00
|
|
|
rows={1}
|
2020-09-06 16:15:25 +00:00
|
|
|
minLength={3}
|
|
|
|
maxLength={MAX_POST_TITLE_LENGTH}
|
|
|
|
/>
|
|
|
|
{!validTitle(this.state.postForm.name) && (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="invalid-feedback">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("invalid_post_title")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.state.suggestedPosts.match({
|
|
|
|
some: sPosts =>
|
|
|
|
sPosts.length > 0 && (
|
|
|
|
<>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="my-1 text-muted small font-weight-bold">
|
2022-06-21 21:42:29 +00:00
|
|
|
{i18n.t("related_posts")}
|
|
|
|
</div>
|
|
|
|
<PostListings
|
2022-07-30 03:37:20 +00:00
|
|
|
showCommunity
|
2022-06-21 21:42:29 +00:00
|
|
|
posts={sPosts}
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.props.allLanguages}
|
2022-06-21 21:42:29 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
none: <></>,
|
|
|
|
})}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label className="col-sm-2 col-form-label">{i18n.t("body")}</label>
|
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<MarkdownTextArea
|
|
|
|
initialContent={this.state.postForm.body}
|
2022-09-22 15:14:58 +00:00
|
|
|
initialLanguageId={None}
|
2020-09-06 16:15:25 +00:00
|
|
|
onContentChange={this.handlePostBodyChange}
|
2022-06-21 21:42:29 +00:00
|
|
|
placeholder={None}
|
|
|
|
buttonTitle={None}
|
|
|
|
maxLength={None}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.props.allLanguages}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.props.post_view.isNone() && (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label
|
|
|
|
className="col-sm-2 col-form-label"
|
|
|
|
htmlFor="post-community"
|
|
|
|
>
|
2022-09-22 18:13:22 +00:00
|
|
|
{this.state.communitySearchLoading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
i18n.t("community")
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<select
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control"
|
2020-09-06 16:15:25 +00:00
|
|
|
id="post-community"
|
|
|
|
value={this.state.postForm.community_id}
|
|
|
|
onInput={linkEvent(this, this.handlePostCommunityChange)}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
<option>{i18n.t("select_a_community")}</option>
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.props.communities.unwrapOr([]).map(cv => (
|
2022-09-22 15:03:35 +00:00
|
|
|
<option key={cv.community.id} value={cv.community.id}>
|
2021-04-26 20:06:16 +00:00
|
|
|
{communitySelectName(cv)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{this.props.enableNsfw && (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<legend className="col-form-label col-sm-2 pt-0">
|
2021-09-18 20:40:59 +00:00
|
|
|
{i18n.t("nsfw")}
|
|
|
|
</legend>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
|
|
|
<div className="form-check">
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-check-input position-static"
|
2020-09-06 16:15:25 +00:00
|
|
|
id="post-nsfw"
|
|
|
|
type="checkbox"
|
2022-06-21 21:42:29 +00:00
|
|
|
checked={toUndefined(this.state.postForm.nsfw)}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handlePostNsfwChange)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-09-22 15:14:58 +00:00
|
|
|
<LanguageSelect
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
selectedLanguageIds={selectedLangs}
|
|
|
|
multiple={false}
|
|
|
|
onChange={this.handleLanguageChange}
|
|
|
|
/>
|
2021-10-01 14:19:47 +00:00
|
|
|
<input
|
|
|
|
tabIndex={-1}
|
|
|
|
autoComplete="false"
|
|
|
|
name="a_password"
|
|
|
|
type="text"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control honeypot"
|
2021-10-01 14:19:47 +00:00
|
|
|
id="register-honey"
|
2022-06-21 21:42:29 +00:00
|
|
|
value={toUndefined(this.state.postForm.honeypot)}
|
2021-10-01 14:19:47 +00:00
|
|
|
onInput={linkEvent(this, this.handleHoneyPotChange)}
|
|
|
|
/>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<div className="col-sm-10">
|
2020-09-06 16:15:25 +00:00
|
|
|
<button
|
|
|
|
disabled={
|
|
|
|
!this.state.postForm.community_id || this.state.loading
|
|
|
|
}
|
|
|
|
type="submit"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-secondary mr-2"
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
|
|
|
{this.state.loading ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2022-06-21 21:42:29 +00:00
|
|
|
) : this.props.post_view.isSome() ? (
|
2021-02-22 02:39:04 +00:00
|
|
|
capitalizeFirstLetter(i18n.t("save"))
|
2020-09-06 16:15:25 +00:00
|
|
|
) : (
|
2021-02-22 02:39:04 +00:00
|
|
|
capitalizeFirstLetter(i18n.t("create"))
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</button>
|
2022-06-21 21:42:29 +00:00
|
|
|
{this.props.post_view.isSome() && (
|
2020-09-06 16:15:25 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-secondary"
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(this, this.handleCancel)}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("cancel")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePostSubmit(i: PostForm, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ loading: true });
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
// Coerce empty url string to undefined
|
2022-06-21 21:42:29 +00:00
|
|
|
if (
|
|
|
|
i.state.postForm.url.isSome() &&
|
|
|
|
i.state.postForm.url.unwrapOr("blank") === ""
|
|
|
|
) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState(s => ((s.postForm.url = None), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
let pForm = i.state.postForm;
|
|
|
|
i.props.post_view.match({
|
|
|
|
some: pv => {
|
|
|
|
let form = new EditPost({
|
|
|
|
name: Some(pForm.name),
|
|
|
|
url: pForm.url,
|
|
|
|
body: pForm.body,
|
|
|
|
nsfw: pForm.nsfw,
|
|
|
|
post_id: pv.post.id,
|
2022-09-22 15:14:58 +00:00
|
|
|
language_id: Some(pv.post.language_id),
|
2022-06-21 21:42:29 +00:00
|
|
|
auth: auth().unwrap(),
|
|
|
|
});
|
|
|
|
WebSocketService.Instance.send(wsClient.editPost(form));
|
|
|
|
},
|
|
|
|
none: () => {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState(s => ((s.postForm.auth = auth().unwrap()), s));
|
|
|
|
let form = new CreatePost({ ...i.state.postForm });
|
|
|
|
WebSocketService.Instance.send(wsClient.createPost(form));
|
2022-06-21 21:42:29 +00:00
|
|
|
},
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
copySuggestedTitle(i: PostForm) {
|
2022-06-21 21:42:29 +00:00
|
|
|
i.state.suggestedTitle.match({
|
|
|
|
some: sTitle => {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState(
|
|
|
|
s => (
|
|
|
|
(s.postForm.name = sTitle.substring(0, MAX_POST_TITLE_LENGTH)), s
|
|
|
|
)
|
|
|
|
);
|
|
|
|
i.setState({ suggestedTitle: None });
|
2022-06-21 21:42:29 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
let textarea: any = document.getElementById("post-title");
|
|
|
|
autosize.update(textarea);
|
|
|
|
}, 10);
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostUrlChange(i: PostForm, event: any) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState(s => ((s.postForm.url = Some(event.target.value)), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
i.fetchPageTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchPageTitle() {
|
2022-06-21 21:42:29 +00:00
|
|
|
this.state.postForm.url.match({
|
|
|
|
some: url => {
|
|
|
|
if (validURL(url)) {
|
|
|
|
let form = new Search({
|
|
|
|
q: url,
|
|
|
|
community_id: None,
|
|
|
|
community_name: None,
|
|
|
|
creator_id: None,
|
|
|
|
type_: Some(SearchType.Url),
|
|
|
|
sort: Some(SortType.TopAll),
|
|
|
|
listing_type: Some(ListingType.All),
|
|
|
|
page: Some(1),
|
|
|
|
limit: Some(trendingFetchLimit),
|
|
|
|
auth: auth(false).ok(),
|
|
|
|
});
|
|
|
|
|
|
|
|
WebSocketService.Instance.send(wsClient.search(form));
|
|
|
|
|
|
|
|
// Fetch the page title
|
|
|
|
getSiteMetadata(url).then(d => {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ suggestedTitle: d.metadata.title });
|
2022-06-21 21:42:29 +00:00
|
|
|
});
|
|
|
|
} else {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ suggestedTitle: None, crossPosts: None });
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostNameChange(i: PostForm, event: any) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState(s => ((s.postForm.name = event.target.value), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
i.fetchSimilarPosts();
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchSimilarPosts() {
|
2022-06-21 21:42:29 +00:00
|
|
|
let form = new Search({
|
2020-09-06 16:15:25 +00:00
|
|
|
q: this.state.postForm.name,
|
2022-06-21 21:42:29 +00:00
|
|
|
type_: Some(SearchType.Posts),
|
|
|
|
sort: Some(SortType.TopAll),
|
|
|
|
listing_type: Some(ListingType.All),
|
|
|
|
community_id: Some(this.state.postForm.community_id),
|
|
|
|
community_name: None,
|
|
|
|
creator_id: None,
|
|
|
|
page: Some(1),
|
|
|
|
limit: Some(trendingFetchLimit),
|
|
|
|
auth: auth(false).ok(),
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2021-02-22 02:39:04 +00:00
|
|
|
if (this.state.postForm.name !== "") {
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.search(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ suggestedPosts: None });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePostBodyChange(val: string) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState(s => ((s.postForm.body = Some(val)), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostCommunityChange(i: PostForm, event: any) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState(
|
|
|
|
s => ((s.postForm.community_id = Number(event.target.value)), s)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostNsfwChange(i: PostForm, event: any) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState(s => ((s.postForm.nsfw = Some(event.target.checked)), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 15:14:58 +00:00
|
|
|
handleLanguageChange(val: number[]) {
|
|
|
|
this.setState(s => ((s.postForm.language_id = Some(val[0])), s));
|
|
|
|
}
|
|
|
|
|
2021-10-01 14:19:47 +00:00
|
|
|
handleHoneyPotChange(i: PostForm, event: any) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState(s => ((s.postForm.honeypot = Some(event.target.value)), s));
|
2021-10-01 14:19:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleCancel(i: PostForm) {
|
|
|
|
i.props.onCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePreviewToggle(i: PostForm, event: any) {
|
|
|
|
event.preventDefault();
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ previewMode: !i.state.previewMode });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleImageUploadPaste(i: PostForm, event: any) {
|
|
|
|
let image = event.clipboardData.files[0];
|
|
|
|
if (image) {
|
|
|
|
i.handleImageUpload(i, image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleImageUpload(i: PostForm, event: any) {
|
|
|
|
let file: any;
|
|
|
|
if (event.target) {
|
|
|
|
event.preventDefault();
|
|
|
|
file = event.target.files[0];
|
|
|
|
} else {
|
|
|
|
file = event;
|
|
|
|
}
|
|
|
|
|
|
|
|
const formData = new FormData();
|
2021-02-22 02:39:04 +00:00
|
|
|
formData.append("images[]", file);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ imageLoading: true });
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-11 03:19:43 +00:00
|
|
|
fetch(pictrsUri, {
|
2021-02-22 02:39:04 +00:00
|
|
|
method: "POST",
|
2020-09-06 16:15:25 +00:00
|
|
|
body: formData,
|
|
|
|
})
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(res => {
|
2021-02-22 02:39:04 +00:00
|
|
|
console.log("pictrs upload:");
|
2020-09-06 16:15:25 +00:00
|
|
|
console.log(res);
|
2021-02-22 02:39:04 +00:00
|
|
|
if (res.msg == "ok") {
|
2020-09-06 16:15:25 +00:00
|
|
|
let hash = res.files[0].file;
|
2020-09-11 03:19:43 +00:00
|
|
|
let url = `${pictrsUri}/${hash}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
let deleteToken = res.files[0].delete_token;
|
2020-09-11 03:19:43 +00:00
|
|
|
let deleteUrl = `${pictrsUri}/delete/${deleteToken}/${hash}`;
|
2022-06-21 21:42:29 +00:00
|
|
|
i.state.postForm.url = Some(url);
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ imageLoading: false });
|
2020-09-06 16:15:25 +00:00
|
|
|
pictrsDeleteToast(
|
2021-02-22 02:39:04 +00:00
|
|
|
i18n.t("click_to_delete_picture"),
|
|
|
|
i18n.t("picture_deleted"),
|
2020-09-06 16:15:25 +00:00
|
|
|
deleteUrl
|
|
|
|
);
|
|
|
|
} else {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ imageLoading: false });
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(JSON.stringify(res), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ imageLoading: false });
|
2021-12-02 16:46:32 +00:00
|
|
|
console.error(error);
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(error, "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
setupCommunities() {
|
|
|
|
// Set up select searching
|
|
|
|
if (isBrowser()) {
|
2021-02-22 02:39:04 +00:00
|
|
|
let selectId: any = document.getElementById("post-community");
|
2020-09-06 16:15:25 +00:00
|
|
|
if (selectId) {
|
2021-04-23 21:48:31 +00:00
|
|
|
this.choices = new Choices(selectId, choicesConfig);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.choices.passedElement.element.addEventListener(
|
2021-02-22 02:39:04 +00:00
|
|
|
"choice",
|
2020-09-06 16:15:25 +00:00
|
|
|
(e: any) => {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState(
|
|
|
|
s => (
|
|
|
|
(s.postForm.community_id = Number(e.detail.choice.value)), s
|
|
|
|
)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2022-09-22 18:13:22 +00:00
|
|
|
this.choices.passedElement.element.addEventListener("search", () => {
|
|
|
|
this.setState({ communitySearchLoading: true });
|
|
|
|
});
|
2021-04-23 21:48:31 +00:00
|
|
|
this.choices.passedElement.element.addEventListener(
|
|
|
|
"search",
|
|
|
|
debounce(async (e: any) => {
|
2021-11-17 21:23:46 +00:00
|
|
|
try {
|
|
|
|
let communities = (await fetchCommunities(e.detail.value))
|
|
|
|
.communities;
|
|
|
|
this.choices.setChoices(
|
|
|
|
communities.map(cv => communityToChoice(cv)),
|
|
|
|
"value",
|
|
|
|
"label",
|
|
|
|
true
|
|
|
|
);
|
2022-09-22 18:13:22 +00:00
|
|
|
this.setState({ communitySearchLoading: false });
|
2021-11-17 21:23:46 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2022-04-27 20:57:55 +00:00
|
|
|
}),
|
2021-04-23 21:48:31 +00:00
|
|
|
false
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
2020-09-24 13:42:20 +00:00
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
this.props.post_view.match({
|
2022-09-22 15:03:35 +00:00
|
|
|
some: pv =>
|
|
|
|
this.setState(s => ((s.postForm.community_id = pv.community.id), s)),
|
2022-06-21 21:42:29 +00:00
|
|
|
none: void 0,
|
|
|
|
});
|
|
|
|
this.props.params.match({
|
|
|
|
some: params =>
|
|
|
|
params.nameOrId.match({
|
|
|
|
some: nameOrId =>
|
|
|
|
nameOrId.match({
|
|
|
|
left: name => {
|
|
|
|
let foundCommunityId = this.props.communities
|
|
|
|
.unwrapOr([])
|
|
|
|
.find(r => r.community.name == name).community.id;
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState(
|
|
|
|
s => ((s.postForm.community_id = foundCommunityId), s)
|
|
|
|
);
|
2022-06-21 21:42:29 +00:00
|
|
|
},
|
2022-09-22 15:03:35 +00:00
|
|
|
right: id =>
|
|
|
|
this.setState(s => ((s.postForm.community_id = id), s)),
|
2022-06-21 21:42:29 +00:00
|
|
|
}),
|
|
|
|
none: void 0,
|
|
|
|
}),
|
|
|
|
none: void 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isBrowser() && this.state.postForm.community_id) {
|
|
|
|
this.choices.setChoiceByValue(
|
|
|
|
this.state.postForm.community_id.toString()
|
|
|
|
);
|
2020-09-24 13:42:20 +00:00
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
this.setState(this.state);
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2020-09-07 22:24:48 +00:00
|
|
|
if (msg.error) {
|
2022-05-23 19:22:15 +00:00
|
|
|
// Errors handled by top level pages
|
|
|
|
// toast(i18n.t(msg.error), "danger");
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ loading: false });
|
2020-09-07 22:24:48 +00:00
|
|
|
return;
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.CreatePost) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<PostResponse>(msg, PostResponse);
|
|
|
|
UserService.Instance.myUserInfo.match({
|
|
|
|
some: mui => {
|
|
|
|
if (data.post_view.creator.id == mui.local_user_view.person.id) {
|
|
|
|
this.props.onCreate(data.post_view);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.EditPost) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<PostResponse>(msg, PostResponse);
|
|
|
|
UserService.Instance.myUserInfo.match({
|
|
|
|
some: mui => {
|
|
|
|
if (data.post_view.creator.id == mui.local_user_view.person.id) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ loading: false });
|
2022-06-21 21:42:29 +00:00
|
|
|
this.props.onEdit(data.post_view);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.Search) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let data = wsJsonToRes<SearchResponse>(msg, SearchResponse);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
if (data.type_ == SearchType[SearchType.Posts]) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ suggestedPosts: Some(data.posts) });
|
2020-09-06 16:15:25 +00:00
|
|
|
} else if (data.type_ == SearchType[SearchType.Url]) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ crossPosts: Some(data.posts) });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|