2023-06-21 22:28:24 +00:00
|
|
|
import {
|
|
|
|
communityToChoice,
|
|
|
|
fetchCommunities,
|
|
|
|
myAuth,
|
|
|
|
myAuthRequired,
|
|
|
|
} from "@utils/app";
|
|
|
|
import {
|
|
|
|
capitalizeFirstLetter,
|
|
|
|
debounce,
|
|
|
|
getIdFromString,
|
|
|
|
validTitle,
|
|
|
|
validURL,
|
|
|
|
} from "@utils/helpers";
|
|
|
|
import { isImage } from "@utils/media";
|
|
|
|
import { Choice } from "@utils/types";
|
2021-07-17 20:42:55 +00:00
|
|
|
import autosize from "autosize";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { Component, InfernoNode, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2023-06-14 12:20:40 +00:00
|
|
|
CommunityView,
|
2020-12-24 01:58:27 +00:00
|
|
|
CreatePost,
|
|
|
|
EditPost,
|
2023-06-14 12:20:40 +00:00
|
|
|
GetSiteMetadataResponse,
|
2022-09-22 15:14:58 +00:00
|
|
|
Language,
|
2021-07-17 20:42:55 +00:00
|
|
|
PostView,
|
2020-09-06 16:15:25 +00:00
|
|
|
SearchResponse,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-10-29 01:58:49 +00:00
|
|
|
archiveTodayUrl,
|
|
|
|
ghostArchiveUrl,
|
2022-02-24 15:31:44 +00:00
|
|
|
relTags,
|
2022-06-21 21:42:29 +00:00
|
|
|
trendingFetchLimit,
|
2021-10-29 01:58:49 +00:00
|
|
|
webArchiveUrl,
|
2023-06-21 22:28:24 +00:00
|
|
|
} from "../../config";
|
|
|
|
import { PostFormParams } from "../../interfaces";
|
2023-06-22 00:54:35 +00:00
|
|
|
import { I18NextService, UserService } from "../../services";
|
2023-06-21 22:28:24 +00:00
|
|
|
import { HttpService, RequestState } from "../../services/HttpService";
|
|
|
|
import { setupTippy } from "../../tippy";
|
|
|
|
import { toast } from "../../toast";
|
2021-07-17 20:42:55 +00:00
|
|
|
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-06-13 10:33:27 +00:00
|
|
|
import NavigationPrompt from "../common/navigation-prompt";
|
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
|
2023-06-14 12:20:40 +00:00
|
|
|
crossPosts?: PostView[];
|
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;
|
2023-06-14 12:20:40 +00:00
|
|
|
onCancel?(): void;
|
|
|
|
onCreate?(form: CreatePost): void;
|
|
|
|
onEdit?(form: EditPost): void;
|
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;
|
2023-06-14 12:20:40 +00:00
|
|
|
initialCommunities?: CommunityView[];
|
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;
|
|
|
|
};
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: boolean;
|
2023-06-14 12:20:40 +00:00
|
|
|
suggestedPostsRes: RequestState<SearchResponse>;
|
|
|
|
metadataRes: RequestState<GetSiteMetadataResponse>;
|
2020-09-06 16:15:25 +00:00
|
|
|
imageLoading: boolean;
|
2023-06-14 20:29:04 +00:00
|
|
|
imageDeleteUrl: string;
|
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;
|
2023-06-14 12:20:40 +00:00
|
|
|
submitted: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 11:52:32 +00:00
|
|
|
function handlePostSubmit(i: PostForm, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
// Coerce empty url string to undefined
|
|
|
|
if ((i.state.form.url ?? "") === "") {
|
|
|
|
i.setState(s => ((s.form.url = undefined), s));
|
|
|
|
}
|
|
|
|
i.setState({ loading: true, submitted: true });
|
|
|
|
const auth = myAuthRequired();
|
|
|
|
|
|
|
|
const pForm = i.state.form;
|
|
|
|
const pv = i.props.post_view;
|
|
|
|
|
|
|
|
if (pv) {
|
|
|
|
i.props.onEdit?.({
|
|
|
|
name: pForm.name,
|
|
|
|
url: pForm.url,
|
|
|
|
body: pForm.body,
|
|
|
|
nsfw: pForm.nsfw,
|
|
|
|
post_id: pv.post.id,
|
|
|
|
language_id: pForm.language_id,
|
|
|
|
auth,
|
|
|
|
});
|
|
|
|
} else if (pForm.name && pForm.community_id) {
|
|
|
|
i.props.onCreate?.({
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function copySuggestedTitle(d: { i: PostForm; suggestedTitle?: string }) {
|
|
|
|
const sTitle = d.suggestedTitle;
|
|
|
|
if (sTitle) {
|
|
|
|
d.i.setState(
|
|
|
|
s => ((s.form.name = sTitle?.substring(0, MAX_POST_TITLE_LENGTH)), s)
|
|
|
|
);
|
|
|
|
d.i.setState({ suggestedPostsRes: { state: "empty" } });
|
|
|
|
setTimeout(() => {
|
|
|
|
const textarea: any = document.getElementById("post-title");
|
|
|
|
autosize.update(textarea);
|
|
|
|
}, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlePostUrlChange(i: PostForm, event: any) {
|
|
|
|
const url = event.target.value;
|
|
|
|
|
|
|
|
i.setState(prev => ({
|
|
|
|
...prev,
|
|
|
|
form: {
|
|
|
|
...prev.form,
|
|
|
|
url,
|
|
|
|
},
|
|
|
|
imageDeleteUrl: "",
|
|
|
|
}));
|
|
|
|
|
|
|
|
i.fetchPageTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlePostNsfwChange(i: PostForm, event: any) {
|
|
|
|
i.setState(s => ((s.form.nsfw = event.target.checked), s));
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleHoneyPotChange(i: PostForm, event: any) {
|
|
|
|
i.setState(s => ((s.form.honeypot = event.target.value), s));
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleCancel(i: PostForm) {
|
|
|
|
i.props.onCancel?.();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleImageUploadPaste(i: PostForm, event: any) {
|
|
|
|
const image = event.clipboardData.files[0];
|
|
|
|
if (image) {
|
|
|
|
handleImageUpload(i, image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleImageUpload(i: PostForm, event: any) {
|
|
|
|
let file: any;
|
|
|
|
if (event.target) {
|
|
|
|
event.preventDefault();
|
|
|
|
file = event.target.files[0];
|
|
|
|
} else {
|
|
|
|
file = event;
|
|
|
|
}
|
|
|
|
|
|
|
|
i.setState({ imageLoading: true });
|
|
|
|
|
|
|
|
HttpService.client.uploadImage({ image: file }).then(res => {
|
|
|
|
console.log("pictrs upload:");
|
|
|
|
console.log(res);
|
|
|
|
if (res.state === "success") {
|
|
|
|
if (res.data.msg === "ok") {
|
|
|
|
i.state.form.url = res.data.url;
|
|
|
|
i.setState({
|
|
|
|
imageLoading: false,
|
|
|
|
imageDeleteUrl: res.data.delete_url as string,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
toast(JSON.stringify(res), "danger");
|
|
|
|
}
|
|
|
|
} else if (res.state === "failed") {
|
|
|
|
console.error(res.msg);
|
|
|
|
toast(res.msg, "danger");
|
|
|
|
i.setState({ imageLoading: false });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlePostNameChange(i: PostForm, event: any) {
|
|
|
|
i.setState(s => ((s.form.name = event.target.value), s));
|
|
|
|
i.fetchSimilarPosts();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleImageDelete(i: PostForm) {
|
|
|
|
const { imageDeleteUrl } = i.state;
|
|
|
|
|
|
|
|
fetch(imageDeleteUrl);
|
|
|
|
|
|
|
|
i.setState(prev => ({
|
|
|
|
...prev,
|
|
|
|
imageDeleteUrl: "",
|
|
|
|
imageLoading: false,
|
|
|
|
form: {
|
|
|
|
...prev.form,
|
|
|
|
url: "",
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
export class PostForm extends Component<PostFormProps, PostFormState> {
|
2023-01-04 16:56:24 +00:00
|
|
|
state: PostFormState = {
|
2023-06-14 12:20:40 +00:00
|
|
|
suggestedPostsRes: { state: "empty" },
|
|
|
|
metadataRes: { state: "empty" },
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {},
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: false,
|
|
|
|
imageLoading: false,
|
2023-06-14 20:29:04 +00:00
|
|
|
imageDeleteUrl: "",
|
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: [],
|
2023-06-14 12:20:40 +00:00
|
|
|
submitted: false,
|
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
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
const { post_view, selectedCommunityChoice, params } = this.props;
|
2022-09-22 15:03:35 +00:00
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
// Means its an edit
|
2023-06-14 12:20:40 +00:00
|
|
|
if (post_view) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
2023-06-14 12:20:40 +00:00
|
|
|
body: post_view.post.body,
|
|
|
|
name: post_view.post.name,
|
|
|
|
community_id: post_view.community.id,
|
|
|
|
url: post_view.post.url,
|
|
|
|
nsfw: post_view.post.nsfw,
|
|
|
|
language_id: post_view.post.language_id,
|
2023-01-04 16:56:24 +00:00
|
|
|
},
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
2023-06-14 12:20:40 +00:00
|
|
|
} else if (selectedCommunityChoice) {
|
2023-04-15 14:47:10 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
form: {
|
|
|
|
...this.state.form,
|
|
|
|
community_id: getIdFromString(selectedCommunityChoice.value),
|
|
|
|
},
|
2023-06-21 11:52:32 +00:00
|
|
|
communitySearchOptions: [selectedCommunityChoice].concat(
|
|
|
|
(
|
2023-06-14 12:20:40 +00:00
|
|
|
this.props.initialCommunities?.map(
|
|
|
|
({ community: { id, title } }) => ({
|
|
|
|
label: title,
|
|
|
|
value: id.toString(),
|
|
|
|
})
|
|
|
|
) ?? []
|
2023-06-21 11:52:32 +00:00
|
|
|
).filter(option => option.value !== selectedCommunityChoice.value)
|
|
|
|
),
|
2023-06-14 12:20:40 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
communitySearchOptions:
|
|
|
|
this.props.initialCommunities?.map(
|
|
|
|
({ community: { id, title } }) => ({
|
|
|
|
label: title,
|
|
|
|
value: id.toString(),
|
|
|
|
})
|
|
|
|
) ?? [],
|
2023-04-15 14:47:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
componentWillReceiveProps(
|
|
|
|
nextProps: Readonly<{ children?: InfernoNode } & PostFormProps>
|
|
|
|
): void {
|
|
|
|
if (this.props != nextProps) {
|
|
|
|
this.setState(
|
|
|
|
s => (
|
|
|
|
(s.form.community_id = getIdFromString(
|
|
|
|
nextProps.selectedCommunityChoice?.value
|
|
|
|
)),
|
|
|
|
s
|
|
|
|
)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const firstLang = this.state.form.language_id;
|
|
|
|
const selectedLangs = firstLang ? Array.of(firstLang) : undefined;
|
2022-09-22 15:14:58 +00:00
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const url = this.state.form.url;
|
2023-06-14 12:20:40 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
2023-06-21 11:52:32 +00:00
|
|
|
<form className="post-form" onSubmit={linkEvent(this, handlePostSubmit)}>
|
2023-06-13 10:33:27 +00:00
|
|
|
<NavigationPrompt
|
2020-09-06 16:15:25 +00:00
|
|
|
when={
|
2023-06-13 10:33:27 +00:00
|
|
|
!!(
|
|
|
|
this.state.form.name ||
|
2023-01-04 16:56:24 +00:00
|
|
|
this.state.form.url ||
|
2023-06-13 10:33:27 +00:00
|
|
|
this.state.form.body
|
2023-06-14 12:20:40 +00:00
|
|
|
) && !this.state.submitted
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
/>
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-14 12:20:40 +00:00
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="post-url">
|
2023-06-22 00:54:35 +00:00
|
|
|
{I18NextService.i18n.t("url")}
|
2023-06-14 12:20:40 +00:00
|
|
|
</label>
|
|
|
|
<div className="col-sm-10">
|
|
|
|
<input
|
|
|
|
type="url"
|
|
|
|
id="post-url"
|
|
|
|
className="form-control"
|
2023-06-21 11:52:32 +00:00
|
|
|
value={url}
|
|
|
|
onInput={linkEvent(this, handlePostUrlChange)}
|
|
|
|
onPaste={linkEvent(this, handleImageUploadPaste)}
|
2023-06-14 12:20:40 +00:00
|
|
|
/>
|
|
|
|
{this.renderSuggestedTitleCopy()}
|
|
|
|
<form>
|
|
|
|
<label
|
|
|
|
htmlFor="file-upload"
|
|
|
|
className={`${
|
|
|
|
UserService.Instance.myUserInfo && "pointer"
|
|
|
|
} d-inline-block float-right text-muted font-weight-bold`}
|
2023-06-22 00:54:35 +00:00
|
|
|
data-tippy-content={I18NextService.i18n.t("upload_image")}
|
2023-06-14 12:20:40 +00:00
|
|
|
>
|
|
|
|
<Icon icon="image" classes="icon-inline" />
|
|
|
|
</label>
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
2023-06-14 12:20:40 +00:00
|
|
|
id="file-upload"
|
|
|
|
type="file"
|
|
|
|
accept="image/*,video/*"
|
|
|
|
name="file"
|
|
|
|
className="d-none"
|
|
|
|
disabled={!UserService.Instance.myUserInfo}
|
2023-06-21 11:52:32 +00:00
|
|
|
onChange={linkEvent(this, handleImageUpload)}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
2023-06-14 12:20:40 +00:00
|
|
|
</form>
|
|
|
|
{url && validURL(url) && (
|
|
|
|
<div>
|
|
|
|
<a
|
|
|
|
href={`${webArchiveUrl}/save/${encodeURIComponent(url)}`}
|
2023-06-20 12:01:29 +00:00
|
|
|
className="me-2 d-inline-block float-right text-muted small font-weight-bold"
|
2023-06-14 12:20:40 +00:00
|
|
|
rel={relTags}
|
2023-01-04 16:56:24 +00:00
|
|
|
>
|
2023-06-22 00:54:35 +00:00
|
|
|
archive.org {I18NextService.i18n.t("archive_link")}
|
2023-06-14 12:20:40 +00:00
|
|
|
</a>
|
|
|
|
<a
|
|
|
|
href={`${ghostArchiveUrl}/search?term=${encodeURIComponent(
|
|
|
|
url
|
|
|
|
)}`}
|
2023-06-20 12:01:29 +00:00
|
|
|
className="me-2 d-inline-block float-right text-muted small font-weight-bold"
|
2023-06-14 12:20:40 +00:00
|
|
|
rel={relTags}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2023-06-22 00:54:35 +00:00
|
|
|
ghostarchive.org {I18NextService.i18n.t("archive_link")}
|
2023-06-14 12:20:40 +00:00
|
|
|
</a>
|
|
|
|
<a
|
|
|
|
href={`${archiveTodayUrl}/?run=1&url=${encodeURIComponent(
|
|
|
|
url
|
|
|
|
)}`}
|
2023-06-20 12:01:29 +00:00
|
|
|
className="me-2 d-inline-block float-right text-muted small font-weight-bold"
|
2023-06-14 12:20:40 +00:00
|
|
|
rel={relTags}
|
|
|
|
>
|
2023-06-22 00:54:35 +00:00
|
|
|
archive.today {I18NextService.i18n.t("archive_link")}
|
2023-06-14 12:20:40 +00:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{this.state.imageLoading && <Spinner />}
|
|
|
|
{url && isImage(url) && (
|
|
|
|
<img src={url} className="img-fluid" alt="" />
|
|
|
|
)}
|
2023-06-14 20:29:04 +00:00
|
|
|
{this.state.imageDeleteUrl && (
|
|
|
|
<button
|
2023-06-14 20:45:56 +00:00
|
|
|
className="btn btn-danger btn-sm mt-2"
|
2023-06-21 11:52:32 +00:00
|
|
|
onClick={linkEvent(this, handleImageDelete)}
|
2023-06-22 00:54:35 +00:00
|
|
|
aria-label={I18NextService.i18n.t("delete")}
|
|
|
|
data-tippy-content={I18NextService.i18n.t("delete")}
|
2023-06-14 20:29:04 +00:00
|
|
|
>
|
2023-06-20 12:01:29 +00:00
|
|
|
<Icon icon="x" classes="icon-inline me-1" />
|
2023-06-22 00:54:35 +00:00
|
|
|
{capitalizeFirstLetter(I18NextService.i18n.t("delete"))}
|
2023-06-14 20:29:04 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2023-06-14 12:20:40 +00:00
|
|
|
{this.props.crossPosts && this.props.crossPosts.length > 0 && (
|
|
|
|
<>
|
|
|
|
<div className="my-1 text-muted small font-weight-bold">
|
2023-06-22 00:54:35 +00:00
|
|
|
{I18NextService.i18n.t("cross_posts")}
|
2023-01-04 16:56:24 +00:00
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
<PostListings
|
|
|
|
showCommunity
|
|
|
|
posts={this.props.crossPosts}
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
viewOnly
|
|
|
|
// All of these are unused, since its view only
|
|
|
|
onPostEdit={() => {}}
|
|
|
|
onPostVote={() => {}}
|
|
|
|
onPostReport={() => {}}
|
|
|
|
onBlockPerson={() => {}}
|
|
|
|
onLockPost={() => {}}
|
|
|
|
onDeletePost={() => {}}
|
|
|
|
onRemovePost={() => {}}
|
|
|
|
onSavePost={() => {}}
|
|
|
|
onFeaturePost={() => {}}
|
|
|
|
onPurgePerson={() => {}}
|
|
|
|
onPurgePost={() => {}}
|
|
|
|
onBanPersonFromCommunity={() => {}}
|
|
|
|
onBanPerson={() => {}}
|
|
|
|
onAddModToCommunity={() => {}}
|
|
|
|
onAddAdmin={() => {}}
|
|
|
|
onTransferCommunity={() => {}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-14 12:20:40 +00:00
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="post-title">
|
2023-06-22 00:54:35 +00:00
|
|
|
{I18NextService.i18n.t("title")}
|
2023-06-14 12:20:40 +00:00
|
|
|
</label>
|
|
|
|
<div className="col-sm-10">
|
|
|
|
<textarea
|
|
|
|
value={this.state.form.name}
|
|
|
|
id="post-title"
|
2023-06-21 11:52:32 +00:00
|
|
|
onInput={linkEvent(this, handlePostNameChange)}
|
2023-06-14 12:20:40 +00:00
|
|
|
className={`form-control ${
|
|
|
|
!validTitle(this.state.form.name) && "is-invalid"
|
|
|
|
}`}
|
|
|
|
required
|
|
|
|
rows={1}
|
|
|
|
minLength={3}
|
|
|
|
maxLength={MAX_POST_TITLE_LENGTH}
|
|
|
|
/>
|
|
|
|
{!validTitle(this.state.form.name) && (
|
|
|
|
<div className="invalid-feedback">
|
2023-06-22 00:54:35 +00:00
|
|
|
{I18NextService.i18n.t("invalid_post_title")}
|
2023-06-14 12:20:40 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{this.renderSuggestedPosts()}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
</div>
|
|
|
|
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-22 00:54:35 +00:00
|
|
|
<label className="col-sm-2 col-form-label">
|
|
|
|
{I18NextService.i18n.t("body")}
|
|
|
|
</label>
|
2023-06-14 12:20:40 +00:00
|
|
|
<div className="col-sm-10">
|
|
|
|
<MarkdownTextArea
|
|
|
|
initialContent={this.state.form.body}
|
|
|
|
onContentChange={this.handlePostBodyChange}
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
hideNavigationWarnings
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-06-21 04:20:05 +00:00
|
|
|
<LanguageSelect
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
selectedLanguageIds={selectedLangs}
|
|
|
|
multiple={false}
|
|
|
|
onChange={this.handleLanguageChange}
|
|
|
|
/>
|
2023-06-14 12:20:40 +00:00
|
|
|
{!this.props.post_view && (
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-14 12:20:40 +00:00
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="post-community">
|
2023-06-22 00:54:35 +00:00
|
|
|
{I18NextService.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-06-14 12:20:40 +00:00
|
|
|
<SearchableSelect
|
|
|
|
id="post-community"
|
|
|
|
value={this.state.form.community_id}
|
|
|
|
options={[
|
|
|
|
{
|
2023-06-22 00:54:35 +00:00
|
|
|
label: I18NextService.i18n.t("select_a_community"),
|
2023-06-14 12:20:40 +00:00
|
|
|
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>
|
2023-06-14 12:20:40 +00:00
|
|
|
)}
|
|
|
|
{this.props.enableNsfw && (
|
2023-06-21 04:20:05 +00:00
|
|
|
<div className="form-check mb-3">
|
|
|
|
<input
|
|
|
|
className="form-check-input"
|
|
|
|
id="post-nsfw"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.form.nsfw}
|
2023-06-21 12:16:11 +00:00
|
|
|
onChange={linkEvent(this, handlePostNsfwChange)}
|
2023-06-21 04:20:05 +00:00
|
|
|
/>
|
2023-06-22 00:54:35 +00:00
|
|
|
<label className="form-check-label">
|
|
|
|
{I18NextService.i18n.t("nsfw")}
|
|
|
|
</label>
|
2023-06-14 12:20:40 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<input
|
|
|
|
tabIndex={-1}
|
|
|
|
autoComplete="false"
|
|
|
|
name="a_password"
|
|
|
|
type="text"
|
|
|
|
className="form-control honeypot"
|
|
|
|
id="register-honey"
|
|
|
|
value={this.state.form.honeypot}
|
2023-06-21 11:52:32 +00:00
|
|
|
onInput={linkEvent(this, handleHoneyPotChange)}
|
2023-06-14 12:20:40 +00:00
|
|
|
/>
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-14 12:20:40 +00:00
|
|
|
<div className="col-sm-10">
|
|
|
|
<button
|
|
|
|
disabled={!this.state.form.community_id || this.state.loading}
|
|
|
|
type="submit"
|
2023-06-20 12:01:29 +00:00
|
|
|
className="btn btn-secondary me-2"
|
2023-06-14 12:20:40 +00:00
|
|
|
>
|
|
|
|
{this.state.loading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : this.props.post_view ? (
|
2023-06-22 00:54:35 +00:00
|
|
|
capitalizeFirstLetter(I18NextService.i18n.t("save"))
|
2023-06-14 12:20:40 +00:00
|
|
|
) : (
|
2023-06-22 00:54:35 +00:00
|
|
|
capitalizeFirstLetter(I18NextService.i18n.t("create"))
|
2023-06-14 12:20:40 +00:00
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
{this.props.post_view && (
|
2020-09-06 16:15:25 +00:00
|
|
|
<button
|
2023-06-14 12:20:40 +00:00
|
|
|
type="button"
|
|
|
|
className="btn btn-secondary"
|
2023-06-21 11:52:32 +00:00
|
|
|
onClick={linkEvent(this, handleCancel)}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2023-06-22 00:54:35 +00:00
|
|
|
{I18NextService.i18n.t("cancel")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
2023-06-14 12:20:40 +00:00
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
2020-09-06 16:15:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
renderSuggestedTitleCopy() {
|
|
|
|
switch (this.state.metadataRes.state) {
|
|
|
|
case "loading":
|
|
|
|
return <Spinner />;
|
|
|
|
case "success": {
|
|
|
|
const suggestedTitle = this.state.metadataRes.data.metadata.title;
|
|
|
|
|
|
|
|
return (
|
|
|
|
suggestedTitle && (
|
|
|
|
<div
|
|
|
|
className="mt-1 text-muted small font-weight-bold pointer"
|
|
|
|
role="button"
|
|
|
|
onClick={linkEvent(
|
|
|
|
{ i: this, suggestedTitle },
|
2023-06-21 11:52:32 +00:00
|
|
|
copySuggestedTitle
|
2023-06-14 12:20:40 +00:00
|
|
|
)}
|
|
|
|
>
|
2023-06-22 00:54:35 +00:00
|
|
|
{I18NextService.i18n.t("copy_suggested_title", { title: "" })}{" "}
|
|
|
|
{suggestedTitle}
|
2023-06-14 12:20:40 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
renderSuggestedPosts() {
|
|
|
|
switch (this.state.suggestedPostsRes.state) {
|
|
|
|
case "loading":
|
|
|
|
return <Spinner />;
|
|
|
|
case "success": {
|
|
|
|
const suggestedPosts = this.state.suggestedPostsRes.data.posts;
|
|
|
|
|
|
|
|
return (
|
|
|
|
suggestedPosts &&
|
|
|
|
suggestedPosts.length > 0 && (
|
|
|
|
<>
|
|
|
|
<div className="my-1 text-muted small font-weight-bold">
|
2023-06-22 00:54:35 +00:00
|
|
|
{I18NextService.i18n.t("related_posts")}
|
2023-06-14 12:20:40 +00:00
|
|
|
</div>
|
|
|
|
<PostListings
|
|
|
|
showCommunity
|
|
|
|
posts={suggestedPosts}
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
viewOnly
|
|
|
|
// All of these are unused, since its view only
|
|
|
|
onPostEdit={() => {}}
|
|
|
|
onPostVote={() => {}}
|
|
|
|
onPostReport={() => {}}
|
|
|
|
onBlockPerson={() => {}}
|
|
|
|
onLockPost={() => {}}
|
|
|
|
onDeletePost={() => {}}
|
|
|
|
onRemovePost={() => {}}
|
|
|
|
onSavePost={() => {}}
|
|
|
|
onFeaturePost={() => {}}
|
|
|
|
onPurgePerson={() => {}}
|
|
|
|
onPurgePost={() => {}}
|
|
|
|
onBanPersonFromCommunity={() => {}}
|
|
|
|
onBanPerson={() => {}}
|
|
|
|
onAddModToCommunity={() => {}}
|
|
|
|
onAddAdmin={() => {}}
|
|
|
|
onTransferCommunity={() => {}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-22 15:03:35 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async fetchPageTitle() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const url = this.state.form.url;
|
2023-01-04 16:56:24 +00:00
|
|
|
if (url && validURL(url)) {
|
2023-06-14 12:20:40 +00:00
|
|
|
this.setState({ metadataRes: { state: "loading" } });
|
|
|
|
this.setState({
|
|
|
|
metadataRes: await HttpService.client.getSiteMetadata({ url }),
|
2023-01-04 16:56:24 +00:00
|
|
|
});
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async fetchSimilarPosts() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const q = this.state.form.name;
|
2023-01-04 16:56:24 +00:00
|
|
|
if (q && q !== "") {
|
2023-06-14 12:20:40 +00:00
|
|
|
this.setState({ suggestedPostsRes: { state: "loading" } });
|
|
|
|
this.setState({
|
|
|
|
suggestedPostsRes: await HttpService.client.search({
|
|
|
|
q,
|
|
|
|
type_: "Posts",
|
|
|
|
sort: "TopAll",
|
|
|
|
listing_type: "All",
|
|
|
|
community_id: this.state.form.community_id,
|
|
|
|
page: 1,
|
|
|
|
limit: trendingFetchLimit,
|
|
|
|
auth: myAuth(),
|
|
|
|
}),
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2023-06-14 12:20:40 +00:00
|
|
|
newOptions.push(...(await fetchCommunities(text)).map(communityToChoice));
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
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.props.onSelectCommunity(choice);
|
2020-09-24 13:42:20 +00:00
|
|
|
}
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|