13 } from "@utils/helpers";
14 import { isImage } from "@utils/media";
15 import { Choice } from "@utils/types";
16 import autosize from "autosize";
17 import { Component, InfernoNode, linkEvent } from "inferno";
22 GetSiteMetadataResponse,
26 } from "lemmy-js-client";
33 } from "../../config";
34 import { i18n } from "../../i18next";
35 import { PostFormParams } from "../../interfaces";
36 import { UserService } from "../../services";
37 import { HttpService, RequestState } from "../../services/HttpService";
38 import { setupTippy } from "../../tippy";
39 import { toast } from "../../toast";
40 import { Icon, Spinner } from "../common/icon";
41 import { LanguageSelect } from "../common/language-select";
42 import { MarkdownTextArea } from "../common/markdown-textarea";
43 import NavigationPrompt from "../common/navigation-prompt";
44 import { SearchableSelect } from "../common/searchable-select";
45 import { PostListings } from "./post-listings";
47 const MAX_POST_TITLE_LENGTH = 200;
49 interface PostFormProps {
50 post_view?: PostView; // If a post is given, that means this is an edit
51 crossPosts?: PostView[];
52 allLanguages: Language[];
53 siteLanguages: number[];
54 params?: PostFormParams;
56 onCreate?(form: CreatePost): void;
57 onEdit?(form: EditPost): void;
59 enableDownvotes?: boolean;
60 selectedCommunityChoice?: Choice;
61 onSelectCommunity?: (choice: Choice) => void;
62 initialCommunities?: CommunityView[];
65 interface PostFormState {
72 community_id?: number;
76 suggestedPostsRes: RequestState<SearchResponse>;
77 metadataRes: RequestState<GetSiteMetadataResponse>;
78 imageLoading: boolean;
79 imageDeleteUrl: string;
80 communitySearchLoading: boolean;
81 communitySearchOptions: Choice[];
86 function handlePostSubmit(i: PostForm, event: any) {
87 event.preventDefault();
88 // Coerce empty url string to undefined
89 if ((i.state.form.url ?? "") === "") {
90 i.setState(s => ((s.form.url = undefined), s));
92 i.setState({ loading: true, submitted: true });
93 const auth = myAuthRequired();
95 const pForm = i.state.form;
96 const pv = i.props.post_view;
105 language_id: pForm.language_id,
108 } else if (pForm.name && pForm.community_id) {
111 community_id: pForm.community_id,
115 language_id: pForm.language_id,
116 honeypot: pForm.honeypot,
122 function copySuggestedTitle(d: { i: PostForm; suggestedTitle?: string }) {
123 const sTitle = d.suggestedTitle;
126 s => ((s.form.name = sTitle?.substring(0, MAX_POST_TITLE_LENGTH)), s)
128 d.i.setState({ suggestedPostsRes: { state: "empty" } });
130 const textarea: any = document.getElementById("post-title");
131 autosize.update(textarea);
136 function handlePostUrlChange(i: PostForm, event: any) {
137 const url = event.target.value;
139 i.setState(prev => ({
151 function handlePostNsfwChange(i: PostForm, event: any) {
152 i.setState(s => ((s.form.nsfw = event.target.checked), s));
155 function handleHoneyPotChange(i: PostForm, event: any) {
156 i.setState(s => ((s.form.honeypot = event.target.value), s));
159 function handleCancel(i: PostForm) {
160 i.props.onCancel?.();
163 function handleImageUploadPaste(i: PostForm, event: any) {
164 const image = event.clipboardData.files[0];
166 handleImageUpload(i, image);
170 function handleImageUpload(i: PostForm, event: any) {
173 event.preventDefault();
174 file = event.target.files[0];
179 i.setState({ imageLoading: true });
181 HttpService.client.uploadImage({ image: file }).then(res => {
182 console.log("pictrs upload:");
184 if (res.state === "success") {
185 if (res.data.msg === "ok") {
186 i.state.form.url = res.data.url;
189 imageDeleteUrl: res.data.delete_url as string,
192 toast(JSON.stringify(res), "danger");
194 } else if (res.state === "failed") {
195 console.error(res.msg);
196 toast(res.msg, "danger");
197 i.setState({ imageLoading: false });
202 function handlePostNameChange(i: PostForm, event: any) {
203 i.setState(s => ((s.form.name = event.target.value), s));
204 i.fetchSimilarPosts();
207 function handleImageDelete(i: PostForm) {
208 const { imageDeleteUrl } = i.state;
210 fetch(imageDeleteUrl);
212 i.setState(prev => ({
223 export class PostForm extends Component<PostFormProps, PostFormState> {
224 state: PostFormState = {
225 suggestedPostsRes: { state: "empty" },
226 metadataRes: { state: "empty" },
231 communitySearchLoading: false,
233 communitySearchOptions: [],
237 constructor(props: PostFormProps, context: any) {
238 super(props, context);
239 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts.bind(this));
240 this.fetchPageTitle = debounce(this.fetchPageTitle.bind(this));
241 this.handlePostBodyChange = this.handlePostBodyChange.bind(this);
242 this.handleLanguageChange = this.handleLanguageChange.bind(this);
243 this.handleCommunitySelect = this.handleCommunitySelect.bind(this);
245 const { post_view, selectedCommunityChoice, params } = this.props;
252 body: post_view.post.body,
253 name: post_view.post.name,
254 community_id: post_view.community.id,
255 url: post_view.post.url,
256 nsfw: post_view.post.nsfw,
257 language_id: post_view.post.language_id,
260 } else if (selectedCommunityChoice) {
265 community_id: getIdFromString(selectedCommunityChoice.value),
267 communitySearchOptions: [selectedCommunityChoice].concat(
269 this.props.initialCommunities?.map(
270 ({ community: { id, title } }) => ({
272 value: id.toString(),
275 ).filter(option => option.value !== selectedCommunityChoice.value)
281 communitySearchOptions:
282 this.props.initialCommunities?.map(
283 ({ community: { id, title } }) => ({
285 value: id.toString(),
302 componentDidMount() {
304 const textarea: any = document.getElementById("post-title");
311 componentWillReceiveProps(
312 nextProps: Readonly<{ children?: InfernoNode } & PostFormProps>
314 if (this.props != nextProps) {
317 (s.form.community_id = getIdFromString(
318 nextProps.selectedCommunityChoice?.value
327 const firstLang = this.state.form.language_id;
328 const selectedLangs = firstLang ? Array.of(firstLang) : undefined;
330 const url = this.state.form.url;
333 <form className="post-form" onSubmit={linkEvent(this, handlePostSubmit)}>
337 this.state.form.name ||
338 this.state.form.url ||
340 ) && !this.state.submitted
343 <div className="mb-3 row">
344 <label className="col-sm-2 col-form-label" htmlFor="post-url">
347 <div className="col-sm-10">
351 className="form-control"
353 onInput={linkEvent(this, handlePostUrlChange)}
354 onPaste={linkEvent(this, handleImageUploadPaste)}
356 {this.renderSuggestedTitleCopy()}
359 htmlFor="file-upload"
361 UserService.Instance.myUserInfo && "pointer"
362 } d-inline-block float-right text-muted font-weight-bold`}
363 data-tippy-content={i18n.t("upload_image")}
365 <Icon icon="image" classes="icon-inline" />
370 accept="image/*,video/*"
373 disabled={!UserService.Instance.myUserInfo}
374 onChange={linkEvent(this, handleImageUpload)}
377 {url && validURL(url) && (
380 href={`${webArchiveUrl}/save/${encodeURIComponent(url)}`}
381 className="me-2 d-inline-block float-right text-muted small font-weight-bold"
384 archive.org {i18n.t("archive_link")}
387 href={`${ghostArchiveUrl}/search?term=${encodeURIComponent(
390 className="me-2 d-inline-block float-right text-muted small font-weight-bold"
393 ghostarchive.org {i18n.t("archive_link")}
396 href={`${archiveTodayUrl}/?run=1&url=${encodeURIComponent(
399 className="me-2 d-inline-block float-right text-muted small font-weight-bold"
402 archive.today {i18n.t("archive_link")}
406 {this.state.imageLoading && <Spinner />}
407 {url && isImage(url) && (
408 <img src={url} className="img-fluid" alt="" />
410 {this.state.imageDeleteUrl && (
412 className="btn btn-danger btn-sm mt-2"
413 onClick={linkEvent(this, handleImageDelete)}
414 aria-label={i18n.t("delete")}
415 data-tippy-content={i18n.t("delete")}
417 <Icon icon="x" classes="icon-inline me-1" />
418 {capitalizeFirstLetter(i18n.t("delete"))}
421 {this.props.crossPosts && this.props.crossPosts.length > 0 && (
423 <div className="my-1 text-muted small font-weight-bold">
424 {i18n.t("cross_posts")}
428 posts={this.props.crossPosts}
429 enableDownvotes={this.props.enableDownvotes}
430 enableNsfw={this.props.enableNsfw}
431 allLanguages={this.props.allLanguages}
432 siteLanguages={this.props.siteLanguages}
434 // All of these are unused, since its view only
435 onPostEdit={() => {}}
436 onPostVote={() => {}}
437 onPostReport={() => {}}
438 onBlockPerson={() => {}}
439 onLockPost={() => {}}
440 onDeletePost={() => {}}
441 onRemovePost={() => {}}
442 onSavePost={() => {}}
443 onFeaturePost={() => {}}
444 onPurgePerson={() => {}}
445 onPurgePost={() => {}}
446 onBanPersonFromCommunity={() => {}}
447 onBanPerson={() => {}}
448 onAddModToCommunity={() => {}}
449 onAddAdmin={() => {}}
450 onTransferCommunity={() => {}}
456 <div className="mb-3 row">
457 <label className="col-sm-2 col-form-label" htmlFor="post-title">
460 <div className="col-sm-10">
462 value={this.state.form.name}
464 onInput={linkEvent(this, handlePostNameChange)}
465 className={`form-control ${
466 !validTitle(this.state.form.name) && "is-invalid"
471 maxLength={MAX_POST_TITLE_LENGTH}
473 {!validTitle(this.state.form.name) && (
474 <div className="invalid-feedback">
475 {i18n.t("invalid_post_title")}
478 {this.renderSuggestedPosts()}
482 <div className="mb-3 row">
483 <label className="col-sm-2 col-form-label">{i18n.t("body")}</label>
484 <div className="col-sm-10">
486 initialContent={this.state.form.body}
487 onContentChange={this.handlePostBodyChange}
488 allLanguages={this.props.allLanguages}
489 siteLanguages={this.props.siteLanguages}
490 hideNavigationWarnings
495 allLanguages={this.props.allLanguages}
496 siteLanguages={this.props.siteLanguages}
497 selectedLanguageIds={selectedLangs}
499 onChange={this.handleLanguageChange}
501 {!this.props.post_view && (
502 <div className="mb-3 row">
503 <label className="col-sm-2 col-form-label" htmlFor="post-community">
504 {i18n.t("community")}
506 <div className="col-sm-10">
509 value={this.state.form.community_id}
512 label: i18n.t("select_a_community"),
516 ].concat(this.state.communitySearchOptions)}
517 loading={this.state.communitySearchLoading}
518 onChange={this.handleCommunitySelect}
519 onSearch={this.handleCommunitySearch}
524 {this.props.enableNsfw && (
525 <div className="form-check mb-3">
527 className="form-check-input"
530 checked={this.state.form.nsfw}
531 onChange={linkEvent(this, handlePostNsfwChange)}
533 <label className="form-check-label">{i18n.t("nsfw")}</label>
541 className="form-control honeypot"
543 value={this.state.form.honeypot}
544 onInput={linkEvent(this, handleHoneyPotChange)}
546 <div className="mb-3 row">
547 <div className="col-sm-10">
549 disabled={!this.state.form.community_id || this.state.loading}
551 className="btn btn-secondary me-2"
553 {this.state.loading ? (
555 ) : this.props.post_view ? (
556 capitalizeFirstLetter(i18n.t("save"))
558 capitalizeFirstLetter(i18n.t("create"))
561 {this.props.post_view && (
564 className="btn btn-secondary"
565 onClick={linkEvent(this, handleCancel)}
576 renderSuggestedTitleCopy() {
577 switch (this.state.metadataRes.state) {
581 const suggestedTitle = this.state.metadataRes.data.metadata.title;
586 className="mt-1 text-muted small font-weight-bold pointer"
589 { i: this, suggestedTitle },
593 {i18n.t("copy_suggested_title", { title: "" })} {suggestedTitle}
601 renderSuggestedPosts() {
602 switch (this.state.suggestedPostsRes.state) {
606 const suggestedPosts = this.state.suggestedPostsRes.data.posts;
610 suggestedPosts.length > 0 && (
612 <div className="my-1 text-muted small font-weight-bold">
613 {i18n.t("related_posts")}
617 posts={suggestedPosts}
618 enableDownvotes={this.props.enableDownvotes}
619 enableNsfw={this.props.enableNsfw}
620 allLanguages={this.props.allLanguages}
621 siteLanguages={this.props.siteLanguages}
623 // All of these are unused, since its view only
624 onPostEdit={() => {}}
625 onPostVote={() => {}}
626 onPostReport={() => {}}
627 onBlockPerson={() => {}}
628 onLockPost={() => {}}
629 onDeletePost={() => {}}
630 onRemovePost={() => {}}
631 onSavePost={() => {}}
632 onFeaturePost={() => {}}
633 onPurgePerson={() => {}}
634 onPurgePost={() => {}}
635 onBanPersonFromCommunity={() => {}}
636 onBanPerson={() => {}}
637 onAddModToCommunity={() => {}}
638 onAddAdmin={() => {}}
639 onTransferCommunity={() => {}}
648 async fetchPageTitle() {
649 const url = this.state.form.url;
650 if (url && validURL(url)) {
651 this.setState({ metadataRes: { state: "loading" } });
653 metadataRes: await HttpService.client.getSiteMetadata({ url }),
658 async fetchSimilarPosts() {
659 const q = this.state.form.name;
661 this.setState({ suggestedPostsRes: { state: "loading" } });
663 suggestedPostsRes: await HttpService.client.search({
668 community_id: this.state.form.community_id,
670 limit: trendingFetchLimit,
677 handlePostBodyChange(val: string) {
678 this.setState(s => ((s.form.body = val), s));
681 handleLanguageChange(val: number[]) {
682 this.setState(s => ((s.form.language_id = val.at(0)), s));
685 handleCommunitySearch = debounce(async (text: string) => {
686 const { selectedCommunityChoice } = this.props;
687 this.setState({ communitySearchLoading: true });
689 const newOptions: Choice[] = [];
691 if (selectedCommunityChoice) {
692 newOptions.push(selectedCommunityChoice);
695 if (text.length > 0) {
696 newOptions.push(...(await fetchCommunities(text)).map(communityToChoice));
699 communitySearchOptions: newOptions,
704 communitySearchLoading: false,
708 handleCommunitySelect(choice: Choice) {
709 if (this.props.onSelectCommunity) {
710 this.props.onSelectCommunity(choice);