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 {
|
2020-12-24 01:58:27 +00:00
|
|
|
CreatePost,
|
|
|
|
EditPost,
|
2022-09-22 15:14:58 +00:00
|
|
|
Language,
|
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
|
|
|
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 { i18n } from "../../i18next";
|
|
|
|
import { PostFormParams } from "../../interfaces";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2023-05-11 17:06:32 +00:00
|
|
|
Choice,
|
2021-10-29 01:58:49 +00:00
|
|
|
archiveTodayUrl,
|
2021-07-17 20:42:55 +00:00
|
|
|
capitalizeFirstLetter,
|
|
|
|
communityToChoice,
|
2020-09-06 16:15:25 +00:00
|
|
|
debounce,
|
2021-07-17 20:42:55 +00:00
|
|
|
fetchCommunities,
|
2023-04-15 14:47:10 +00:00
|
|
|
getIdFromString,
|
2021-08-19 15:24:13 +00:00
|
|
|
getSiteMetadata,
|
2021-10-29 01:58:49 +00:00
|
|
|
ghostArchiveUrl,
|
2020-09-06 16:15:25 +00:00
|
|
|
isImage,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
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,
|
2023-03-27 02:06:42 +00:00
|
|
|
uploadImage,
|
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";
|
2023-04-15 14:47:10 +00:00
|
|
|
import { SearchableSelect } from "../common/searchable-select";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { PostListings } from "./post-listings";
|
2020-09-07 22:24:48 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
const MAX_POST_TITLE_LENGTH = 200;
|
|
|
|
|
|
|
|
interface PostFormProps {
|
2023-01-04 16:56:24 +00:00
|
|
|
post_view?: PostView; // If a post is given, that means this is an edit
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages: Language[];
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages: number[];
|
2023-01-04 16:56:24 +00:00
|
|
|
params?: 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;
|
2023-04-15 14:47:10 +00:00
|
|
|
selectedCommunityChoice?: Choice;
|
|
|
|
onSelectCommunity?: (choice: Choice) => void;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PostFormState {
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
|
|
|
name?: string;
|
|
|
|
url?: string;
|
|
|
|
body?: string;
|
|
|
|
nsfw?: boolean;
|
|
|
|
language_id?: number;
|
|
|
|
community_id?: number;
|
|
|
|
honeypot?: string;
|
|
|
|
};
|
|
|
|
suggestedTitle?: string;
|
|
|
|
suggestedPosts?: PostView[];
|
|
|
|
crossPosts?: PostView[];
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: boolean;
|
|
|
|
imageLoading: boolean;
|
2022-09-22 18:13:22 +00:00
|
|
|
communitySearchLoading: boolean;
|
2023-04-15 14:47:10 +00:00
|
|
|
communitySearchOptions: Choice[];
|
2020-09-06 16:15:25 +00:00
|
|
|
previewMode: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PostForm extends Component<PostFormProps, PostFormState> {
|
2023-01-04 16:56:24 +00:00
|
|
|
private subscription?: Subscription;
|
|
|
|
state: PostFormState = {
|
|
|
|
form: {},
|
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,
|
2023-04-15 14:47:10 +00:00
|
|
|
communitySearchOptions: [],
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
constructor(props: PostFormProps, context: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
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);
|
2023-04-15 14:47:10 +00:00
|
|
|
this.handleCommunitySelect = this.handleCommunitySelect.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
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
|
2023-04-15 14:47:10 +00:00
|
|
|
const pv = this.props.post_view;
|
2023-01-04 16:56:24 +00:00
|
|
|
if (pv) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
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,
|
2023-01-04 16:56:24 +00:00
|
|
|
nsfw: pv.post.nsfw,
|
|
|
|
language_id: pv.post.language_id,
|
|
|
|
},
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const selectedCommunityChoice = this.props.selectedCommunityChoice;
|
|
|
|
|
|
|
|
if (selectedCommunityChoice) {
|
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
form: {
|
|
|
|
...this.state.form,
|
|
|
|
community_id: getIdFromString(selectedCommunityChoice.value),
|
|
|
|
},
|
|
|
|
communitySearchOptions: [selectedCommunityChoice],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const params = this.props.params;
|
2023-01-04 16:56:24 +00:00
|
|
|
if (params) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
|
|
|
...this.state.form,
|
2023-04-15 14:47:10 +00:00
|
|
|
...params,
|
2022-09-22 15:03:35 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
setupTippy();
|
2023-04-15 14:47:10 +00:00
|
|
|
const 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 &&
|
2023-01-04 16:56:24 +00:00
|
|
|
(this.state.form.name || this.state.form.url || this.state.form.body)
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
|
|
|
window.onbeforeunload = () => true;
|
|
|
|
} else {
|
2023-01-04 16:56:24 +00:00
|
|
|
window.onbeforeunload = null;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.subscription?.unsubscribe();
|
2020-09-06 16:15:25 +00:00
|
|
|
/* this.choices && this.choices.destroy(); */
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
static getDerivedStateFromProps(
|
|
|
|
{ selectedCommunityChoice }: PostFormProps,
|
|
|
|
{ form, ...restState }: PostFormState
|
|
|
|
) {
|
|
|
|
return {
|
|
|
|
...restState,
|
|
|
|
form: {
|
|
|
|
...form,
|
|
|
|
community_id: getIdFromString(selectedCommunityChoice?.value),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
render() {
|
2023-01-04 16:56:24 +00:00
|
|
|
let firstLang =
|
|
|
|
this.state.form.language_id ??
|
|
|
|
myFirstDiscussionLanguageId(
|
|
|
|
this.props.allLanguages,
|
|
|
|
this.props.siteLanguages,
|
|
|
|
UserService.Instance.myUserInfo
|
|
|
|
);
|
|
|
|
let selectedLangs = firstLang ? Array.of(firstLang) : undefined;
|
2022-09-22 15:14:58 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
let url = this.state.form.url;
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Prompt
|
|
|
|
when={
|
|
|
|
!this.state.loading &&
|
2023-01-04 16:56:24 +00:00
|
|
|
(this.state.form.name ||
|
|
|
|
this.state.form.url ||
|
|
|
|
this.state.form.body)
|
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"
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.url}
|
2020-09-06 16:15:25 +00:00
|
|
|
onInput={linkEvent(this, this.handlePostUrlChange)}
|
|
|
|
onPaste={linkEvent(this, this.handleImageUploadPaste)}
|
|
|
|
/>
|
2023-01-04 16:56:24 +00:00
|
|
|
{this.state.suggestedTitle && (
|
|
|
|
<div
|
|
|
|
className="mt-1 text-muted small font-weight-bold pointer"
|
|
|
|
role="button"
|
|
|
|
onClick={linkEvent(this, this.copySuggestedTitle)}
|
|
|
|
>
|
|
|
|
{i18n.t("copy_suggested_title", { title: "" })}{" "}
|
|
|
|
{this.state.suggestedTitle}
|
|
|
|
</div>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
<form>
|
|
|
|
<label
|
|
|
|
htmlFor="file-upload"
|
|
|
|
className={`${
|
2023-01-04 16:56:24 +00:00
|
|
|
UserService.Instance.myUserInfo && "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"
|
2023-01-04 16:56:24 +00:00
|
|
|
disabled={!UserService.Instance.myUserInfo}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={linkEvent(this, this.handleImageUpload)}
|
|
|
|
/>
|
|
|
|
</form>
|
2023-01-04 16:56:24 +00:00
|
|
|
{url && validURL(url) && (
|
|
|
|
<div>
|
|
|
|
<a
|
|
|
|
href={`${webArchiveUrl}/save/${encodeURIComponent(url)}`}
|
|
|
|
className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
|
|
|
|
rel={relTags}
|
|
|
|
>
|
|
|
|
archive.org {i18n.t("archive_link")}
|
|
|
|
</a>
|
|
|
|
<a
|
|
|
|
href={`${ghostArchiveUrl}/search?term=${encodeURIComponent(
|
|
|
|
url
|
|
|
|
)}`}
|
|
|
|
className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
|
|
|
|
rel={relTags}
|
|
|
|
>
|
|
|
|
ghostarchive.org {i18n.t("archive_link")}
|
|
|
|
</a>
|
|
|
|
<a
|
|
|
|
href={`${archiveTodayUrl}/?run=1&url=${encodeURIComponent(
|
|
|
|
url
|
|
|
|
)}`}
|
|
|
|
className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
|
|
|
|
rel={relTags}
|
|
|
|
>
|
|
|
|
archive.today {i18n.t("archive_link")}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)}
|
2021-02-11 20:35:27 +00:00
|
|
|
{this.state.imageLoading && <Spinner />}
|
2023-01-04 16:56:24 +00:00
|
|
|
{url && isImage(url) && (
|
|
|
|
<img src={url} className="img-fluid" alt="" />
|
|
|
|
)}
|
|
|
|
{this.state.crossPosts && this.state.crossPosts.length > 0 && (
|
|
|
|
<>
|
|
|
|
<div className="my-1 text-muted small font-weight-bold">
|
|
|
|
{i18n.t("cross_posts")}
|
|
|
|
</div>
|
|
|
|
<PostListings
|
|
|
|
showCommunity
|
|
|
|
posts={this.state.crossPosts}
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
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
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.name}
|
2020-09-06 16:15:25 +00:00
|
|
|
id="post-title"
|
|
|
|
onInput={linkEvent(this, this.handlePostNameChange)}
|
2022-09-22 15:03:35 +00:00
|
|
|
className={`form-control ${
|
2023-01-04 16:56:24 +00:00
|
|
|
!validTitle(this.state.form.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}
|
|
|
|
/>
|
2023-01-04 16:56:24 +00:00
|
|
|
{!validTitle(this.state.form.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>
|
|
|
|
)}
|
2023-01-04 16:56:24 +00:00
|
|
|
{this.state.suggestedPosts &&
|
|
|
|
this.state.suggestedPosts.length > 0 && (
|
|
|
|
<>
|
|
|
|
<div className="my-1 text-muted small font-weight-bold">
|
|
|
|
{i18n.t("related_posts")}
|
|
|
|
</div>
|
|
|
|
<PostListings
|
|
|
|
showCommunity
|
|
|
|
posts={this.state.suggestedPosts}
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
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
|
2023-01-04 16:56:24 +00:00
|
|
|
initialContent={this.state.form.body}
|
2020-09-06 16:15:25 +00:00
|
|
|
onContentChange={this.handlePostBodyChange}
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages={this.props.allLanguages}
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages={this.props.siteLanguages}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-01-04 16:56:24 +00:00
|
|
|
{!this.props.post_view && (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="form-group row">
|
|
|
|
<label
|
|
|
|
className="col-sm-2 col-form-label"
|
|
|
|
htmlFor="post-community"
|
|
|
|
>
|
2023-04-15 14:47:10 +00:00
|
|
|
{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">
|
2023-04-15 14:47:10 +00:00
|
|
|
<SearchableSelect
|
2020-09-06 16:15:25 +00:00
|
|
|
id="post-community"
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.community_id}
|
2023-04-15 14:47:10 +00:00
|
|
|
options={[
|
|
|
|
{
|
|
|
|
label: i18n.t("select_a_community"),
|
|
|
|
value: "",
|
|
|
|
disabled: true,
|
|
|
|
} as Choice,
|
|
|
|
].concat(this.state.communitySearchOptions)}
|
|
|
|
loading={this.state.communitySearchLoading}
|
|
|
|
onChange={this.handleCommunitySelect}
|
|
|
|
onSearch={this.handleCommunitySearch}
|
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
</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"
|
2023-01-04 16:56:24 +00:00
|
|
|
checked={this.state.form.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}
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages={this.props.siteLanguages}
|
2022-09-22 15:14:58 +00:00
|
|
|
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"
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.form.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
|
2023-01-04 16:56:24 +00:00
|
|
|
disabled={!this.state.form.community_id || this.state.loading}
|
2020-09-06 16:15:25 +00:00
|
|
|
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 />
|
2023-01-04 16:56:24 +00:00
|
|
|
) : this.props.post_view ? (
|
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>
|
2023-01-04 16:56:24 +00:00
|
|
|
{this.props.post_view && (
|
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
|
2023-01-04 16:56:24 +00:00
|
|
|
if ((i.state.form.url ?? "blank") === "") {
|
|
|
|
i.setState(s => ((s.form.url = undefined), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
let pForm = i.state.form;
|
|
|
|
let pv = i.props.post_view;
|
|
|
|
let auth = myAuth();
|
|
|
|
if (auth) {
|
|
|
|
if (pv) {
|
|
|
|
let form: EditPost = {
|
|
|
|
name: pForm.name,
|
2022-06-21 21:42:29 +00:00
|
|
|
url: pForm.url,
|
|
|
|
body: pForm.body,
|
|
|
|
nsfw: pForm.nsfw,
|
|
|
|
post_id: pv.post.id,
|
2023-01-04 16:56:24 +00:00
|
|
|
language_id: pv.post.language_id,
|
|
|
|
auth,
|
|
|
|
};
|
2022-06-21 21:42:29 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.editPost(form));
|
2023-01-04 16:56:24 +00:00
|
|
|
} else {
|
|
|
|
if (pForm.name && pForm.community_id) {
|
|
|
|
let form: CreatePost = {
|
|
|
|
name: pForm.name,
|
|
|
|
community_id: pForm.community_id,
|
|
|
|
url: pForm.url,
|
|
|
|
body: pForm.body,
|
|
|
|
nsfw: pForm.nsfw,
|
|
|
|
language_id: pForm.language_id,
|
|
|
|
honeypot: pForm.honeypot,
|
|
|
|
auth,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.send(wsClient.createPost(form));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
copySuggestedTitle(i: PostForm) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let sTitle = i.state.suggestedTitle;
|
|
|
|
if (sTitle) {
|
|
|
|
i.setState(
|
|
|
|
s => ((s.form.name = sTitle?.substring(0, MAX_POST_TITLE_LENGTH)), s)
|
|
|
|
);
|
|
|
|
i.setState({ suggestedTitle: undefined });
|
|
|
|
setTimeout(() => {
|
|
|
|
let textarea: any = document.getElementById("post-title");
|
|
|
|
autosize.update(textarea);
|
|
|
|
}, 10);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostUrlChange(i: PostForm, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState(s => ((s.form.url = event.target.value), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
i.fetchPageTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchPageTitle() {
|
2023-01-04 16:56:24 +00:00
|
|
|
let url = this.state.form.url;
|
|
|
|
if (url && validURL(url)) {
|
|
|
|
let form: Search = {
|
|
|
|
q: url,
|
2023-05-11 18:32:32 +00:00
|
|
|
type_: "Url",
|
|
|
|
sort: "TopAll",
|
|
|
|
listing_type: "All",
|
|
|
|
page: 1n,
|
2023-01-04 16:56:24 +00:00
|
|
|
limit: trendingFetchLimit,
|
|
|
|
auth: myAuth(false),
|
|
|
|
};
|
|
|
|
|
|
|
|
WebSocketService.Instance.send(wsClient.search(form));
|
|
|
|
|
|
|
|
// Fetch the page title
|
|
|
|
getSiteMetadata(url).then(d => {
|
|
|
|
this.setState({ suggestedTitle: d.metadata.title });
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({ suggestedTitle: undefined, crossPosts: undefined });
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostNameChange(i: PostForm, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState(s => ((s.form.name = event.target.value), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
i.fetchSimilarPosts();
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchSimilarPosts() {
|
2023-01-04 16:56:24 +00:00
|
|
|
let q = this.state.form.name;
|
|
|
|
if (q && q !== "") {
|
|
|
|
let form: Search = {
|
|
|
|
q,
|
2023-05-11 18:32:32 +00:00
|
|
|
type_: "Posts",
|
|
|
|
sort: "TopAll",
|
|
|
|
listing_type: "All",
|
2023-01-04 16:56:24 +00:00
|
|
|
community_id: this.state.form.community_id,
|
2023-05-11 18:32:32 +00:00
|
|
|
page: 1n,
|
2023-01-04 16:56:24 +00:00
|
|
|
limit: trendingFetchLimit,
|
|
|
|
auth: myAuth(false),
|
|
|
|
};
|
|
|
|
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.search(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState({ suggestedPosts: undefined });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePostBodyChange(val: string) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState(s => ((s.form.body = val), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostCommunityChange(i: PostForm, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState(s => ((s.form.community_id = Number(event.target.value)), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostNsfwChange(i: PostForm, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState(s => ((s.form.nsfw = event.target.checked), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 15:14:58 +00:00
|
|
|
handleLanguageChange(val: number[]) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState(s => ((s.form.language_id = val.at(0)), s));
|
2022-09-22 15:14:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 14:19:47 +00:00
|
|
|
handleHoneyPotChange(i: PostForm, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState(s => ((s.form.honeypot = event.target.value), s));
|
2021-10-01 14:19:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleCancel(i: PostForm) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.props.onCancel?.();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ imageLoading: true });
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-03-27 02:06:42 +00:00
|
|
|
uploadImage(file)
|
2020-09-06 16:15:25 +00:00
|
|
|
.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);
|
2023-03-27 02:06:42 +00:00
|
|
|
if (res.msg === "ok") {
|
|
|
|
i.state.form.url = res.url;
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ imageLoading: false });
|
2023-04-04 12:40:00 +00:00
|
|
|
pictrsDeleteToast(file.name, res.delete_url as string);
|
2020-09-06 16:15:25 +00:00
|
|
|
} 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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
handleCommunitySearch = debounce(async (text: string) => {
|
|
|
|
const { selectedCommunityChoice } = this.props;
|
|
|
|
this.setState({ communitySearchLoading: true });
|
|
|
|
|
|
|
|
const newOptions: Choice[] = [];
|
|
|
|
|
|
|
|
if (selectedCommunityChoice) {
|
|
|
|
newOptions.push(selectedCommunityChoice);
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
2020-09-24 13:42:20 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (text.length > 0) {
|
|
|
|
newOptions.push(
|
|
|
|
...(await fetchCommunities(text)).communities.map(communityToChoice)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
communitySearchOptions: newOptions,
|
|
|
|
});
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
this.setState({
|
|
|
|
communitySearchLoading: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
handleCommunitySelect(choice: Choice) {
|
|
|
|
if (this.props.onSelectCommunity) {
|
|
|
|
this.setState({
|
|
|
|
loading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.props.onSelectCommunity(choice);
|
|
|
|
|
|
|
|
this.setState({ loading: false });
|
2020-09-24 13:42:20 +00:00
|
|
|
}
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let mui = UserService.Instance.myUserInfo;
|
2020-12-24 01:58:27 +00:00
|
|
|
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) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let data = wsJsonToRes<PostResponse>(msg);
|
|
|
|
if (data.post_view.creator.id == mui?.local_user_view.person.id) {
|
|
|
|
this.props.onCreate?.(data.post_view);
|
|
|
|
}
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.EditPost) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let data = wsJsonToRes<PostResponse>(msg);
|
|
|
|
if (data.post_view.creator.id == mui?.local_user_view.person.id) {
|
|
|
|
this.setState({ loading: false });
|
|
|
|
this.props.onEdit?.(data.post_view);
|
|
|
|
}
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.Search) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let data = wsJsonToRes<SearchResponse>(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
if (data.type_ == "Posts") {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState({ suggestedPosts: data.posts });
|
2023-05-11 18:32:32 +00:00
|
|
|
} else if (data.type_ == "Url") {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState({ crossPosts: data.posts });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|