1 import autosize from "autosize";
2 import { Component, linkEvent } from "inferno";
3 import { Prompt } from "inferno-router";
18 } from "lemmy-js-client";
19 import { Subscription } from "rxjs";
20 import { i18n } from "../../i18next";
21 import { PostFormParams } from "../../interfaces";
22 import { UserService, WebSocketService } from "../../services";
26 capitalizeFirstLetter,
35 myFirstDiscussionLanguageId,
48 import { Icon, Spinner } from "../common/icon";
49 import { LanguageSelect } from "../common/language-select";
50 import { MarkdownTextArea } from "../common/markdown-textarea";
51 import { SearchableSelect } from "../common/searchable-select";
52 import { PostListings } from "./post-listings";
54 const MAX_POST_TITLE_LENGTH = 200;
56 interface PostFormProps {
57 post_view?: PostView; // If a post is given, that means this is an edit
58 allLanguages: Language[];
59 siteLanguages: number[];
60 params?: PostFormParams;
62 onCreate?(post: PostView): any;
63 onEdit?(post: PostView): any;
65 enableDownvotes?: boolean;
66 selectedCommunityChoice?: Choice;
67 onSelectCommunity?: (choice: Choice) => void;
70 interface PostFormState {
77 community_id?: number;
80 suggestedTitle?: string;
81 suggestedPosts?: PostView[];
82 crossPosts?: PostView[];
84 imageLoading: boolean;
85 communitySearchLoading: boolean;
86 communitySearchOptions: Choice[];
90 export class PostForm extends Component<PostFormProps, PostFormState> {
91 private subscription?: Subscription;
92 state: PostFormState = {
96 communitySearchLoading: false,
98 communitySearchOptions: [],
101 constructor(props: PostFormProps, context: any) {
102 super(props, context);
103 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts.bind(this));
104 this.fetchPageTitle = debounce(this.fetchPageTitle.bind(this));
105 this.handlePostBodyChange = this.handlePostBodyChange.bind(this);
106 this.handleLanguageChange = this.handleLanguageChange.bind(this);
107 this.handleCommunitySelect = this.handleCommunitySelect.bind(this);
109 this.parseMessage = this.parseMessage.bind(this);
110 this.subscription = wsSubscribe(this.parseMessage);
113 const pv = this.props.post_view;
120 community_id: pv.community.id,
123 language_id: pv.post.language_id,
128 const selectedCommunityChoice = this.props.selectedCommunityChoice;
130 if (selectedCommunityChoice) {
135 community_id: getIdFromString(selectedCommunityChoice.value),
137 communitySearchOptions: [selectedCommunityChoice],
141 const params = this.props.params;
153 componentDidMount() {
155 const textarea: any = document.getElementById("post-title");
162 componentDidUpdate() {
164 !this.state.loading &&
165 (this.state.form.name || this.state.form.url || this.state.form.body)
167 window.onbeforeunload = () => true;
169 window.onbeforeunload = null;
173 componentWillUnmount() {
174 this.subscription?.unsubscribe();
175 /* this.choices && this.choices.destroy(); */
176 window.onbeforeunload = null;
179 static getDerivedStateFromProps(
180 { selectedCommunityChoice }: PostFormProps,
181 { form, ...restState }: PostFormState
187 community_id: getIdFromString(selectedCommunityChoice?.value),
194 this.state.form.language_id ??
195 myFirstDiscussionLanguageId(
196 this.props.allLanguages,
197 this.props.siteLanguages,
198 UserService.Instance.myUserInfo
200 let selectedLangs = firstLang ? Array.of(firstLang) : undefined;
202 let url = this.state.form.url;
207 !this.state.loading &&
208 (this.state.form.name ||
209 this.state.form.url ||
210 this.state.form.body)
212 message={i18n.t("block_leaving")}
214 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
215 <div className="form-group row">
216 <label className="col-sm-2 col-form-label" htmlFor="post-url">
219 <div className="col-sm-10">
223 className="form-control"
224 value={this.state.form.url}
225 onInput={linkEvent(this, this.handlePostUrlChange)}
226 onPaste={linkEvent(this, this.handleImageUploadPaste)}
228 {this.state.suggestedTitle && (
230 className="mt-1 text-muted small font-weight-bold pointer"
232 onClick={linkEvent(this, this.copySuggestedTitle)}
234 {i18n.t("copy_suggested_title", { title: "" })}{" "}
235 {this.state.suggestedTitle}
240 htmlFor="file-upload"
242 UserService.Instance.myUserInfo && "pointer"
243 } d-inline-block float-right text-muted font-weight-bold`}
244 data-tippy-content={i18n.t("upload_image")}
246 <Icon icon="image" classes="icon-inline" />
251 accept="image/*,video/*"
254 disabled={!UserService.Instance.myUserInfo}
255 onChange={linkEvent(this, this.handleImageUpload)}
258 {url && validURL(url) && (
261 href={`${webArchiveUrl}/save/${encodeURIComponent(url)}`}
262 className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
265 archive.org {i18n.t("archive_link")}
268 href={`${ghostArchiveUrl}/search?term=${encodeURIComponent(
271 className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
274 ghostarchive.org {i18n.t("archive_link")}
277 href={`${archiveTodayUrl}/?run=1&url=${encodeURIComponent(
280 className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
283 archive.today {i18n.t("archive_link")}
287 {this.state.imageLoading && <Spinner />}
288 {url && isImage(url) && (
289 <img src={url} className="img-fluid" alt="" />
291 {this.state.crossPosts && this.state.crossPosts.length > 0 && (
293 <div className="my-1 text-muted small font-weight-bold">
294 {i18n.t("cross_posts")}
298 posts={this.state.crossPosts}
299 enableDownvotes={this.props.enableDownvotes}
300 enableNsfw={this.props.enableNsfw}
301 allLanguages={this.props.allLanguages}
302 siteLanguages={this.props.siteLanguages}
308 <div className="form-group row">
309 <label className="col-sm-2 col-form-label" htmlFor="post-title">
312 <div className="col-sm-10">
314 value={this.state.form.name}
316 onInput={linkEvent(this, this.handlePostNameChange)}
317 className={`form-control ${
318 !validTitle(this.state.form.name) && "is-invalid"
323 maxLength={MAX_POST_TITLE_LENGTH}
325 {!validTitle(this.state.form.name) && (
326 <div className="invalid-feedback">
327 {i18n.t("invalid_post_title")}
330 {this.state.suggestedPosts &&
331 this.state.suggestedPosts.length > 0 && (
333 <div className="my-1 text-muted small font-weight-bold">
334 {i18n.t("related_posts")}
338 posts={this.state.suggestedPosts}
339 enableDownvotes={this.props.enableDownvotes}
340 enableNsfw={this.props.enableNsfw}
341 allLanguages={this.props.allLanguages}
342 siteLanguages={this.props.siteLanguages}
349 <div className="form-group row">
350 <label className="col-sm-2 col-form-label">{i18n.t("body")}</label>
351 <div className="col-sm-10">
353 initialContent={this.state.form.body}
354 onContentChange={this.handlePostBodyChange}
355 allLanguages={this.props.allLanguages}
356 siteLanguages={this.props.siteLanguages}
360 {!this.props.post_view && (
361 <div className="form-group row">
363 className="col-sm-2 col-form-label"
364 htmlFor="post-community"
366 {i18n.t("community")}
368 <div className="col-sm-10">
371 value={this.state.form.community_id}
374 label: i18n.t("select_a_community"),
378 ].concat(this.state.communitySearchOptions)}
379 loading={this.state.communitySearchLoading}
380 onChange={this.handleCommunitySelect}
381 onSearch={this.handleCommunitySearch}
386 {this.props.enableNsfw && (
387 <div className="form-group row">
388 <legend className="col-form-label col-sm-2 pt-0">
391 <div className="col-sm-10">
392 <div className="form-check">
394 className="form-check-input position-static"
397 checked={this.state.form.nsfw}
398 onChange={linkEvent(this, this.handlePostNsfwChange)}
405 allLanguages={this.props.allLanguages}
406 siteLanguages={this.props.siteLanguages}
407 selectedLanguageIds={selectedLangs}
409 onChange={this.handleLanguageChange}
416 className="form-control honeypot"
418 value={this.state.form.honeypot}
419 onInput={linkEvent(this, this.handleHoneyPotChange)}
421 <div className="form-group row">
422 <div className="col-sm-10">
424 disabled={!this.state.form.community_id || this.state.loading}
426 className="btn btn-secondary mr-2"
428 {this.state.loading ? (
430 ) : this.props.post_view ? (
431 capitalizeFirstLetter(i18n.t("save"))
433 capitalizeFirstLetter(i18n.t("create"))
436 {this.props.post_view && (
439 className="btn btn-secondary"
440 onClick={linkEvent(this, this.handleCancel)}
452 handlePostSubmit(i: PostForm, event: any) {
453 event.preventDefault();
455 i.setState({ loading: true });
457 // Coerce empty url string to undefined
458 if ((i.state.form.url ?? "blank") === "") {
459 i.setState(s => ((s.form.url = undefined), s));
462 let pForm = i.state.form;
463 let pv = i.props.post_view;
467 let form: EditPost = {
473 language_id: pv.post.language_id,
476 WebSocketService.Instance.send(wsClient.editPost(form));
478 if (pForm.name && pForm.community_id) {
479 let form: CreatePost = {
481 community_id: pForm.community_id,
485 language_id: pForm.language_id,
486 honeypot: pForm.honeypot,
489 WebSocketService.Instance.send(wsClient.createPost(form));
495 copySuggestedTitle(i: PostForm) {
496 let sTitle = i.state.suggestedTitle;
499 s => ((s.form.name = sTitle?.substring(0, MAX_POST_TITLE_LENGTH)), s)
501 i.setState({ suggestedTitle: undefined });
503 let textarea: any = document.getElementById("post-title");
504 autosize.update(textarea);
509 handlePostUrlChange(i: PostForm, event: any) {
510 i.setState(s => ((s.form.url = event.target.value), s));
515 let url = this.state.form.url;
516 if (url && validURL(url)) {
519 type_: SearchType.Url,
520 sort: SortType.TopAll,
521 listing_type: ListingType.All,
523 limit: trendingFetchLimit,
527 WebSocketService.Instance.send(wsClient.search(form));
529 // Fetch the page title
530 getSiteMetadata(url).then(d => {
531 this.setState({ suggestedTitle: d.metadata.title });
534 this.setState({ suggestedTitle: undefined, crossPosts: undefined });
538 handlePostNameChange(i: PostForm, event: any) {
539 i.setState(s => ((s.form.name = event.target.value), s));
540 i.fetchSimilarPosts();
543 fetchSimilarPosts() {
544 let q = this.state.form.name;
548 type_: SearchType.Posts,
549 sort: SortType.TopAll,
550 listing_type: ListingType.All,
551 community_id: this.state.form.community_id,
553 limit: trendingFetchLimit,
557 WebSocketService.Instance.send(wsClient.search(form));
559 this.setState({ suggestedPosts: undefined });
563 handlePostBodyChange(val: string) {
564 this.setState(s => ((s.form.body = val), s));
567 handlePostCommunityChange(i: PostForm, event: any) {
568 i.setState(s => ((s.form.community_id = Number(event.target.value)), s));
571 handlePostNsfwChange(i: PostForm, event: any) {
572 i.setState(s => ((s.form.nsfw = event.target.checked), s));
575 handleLanguageChange(val: number[]) {
576 this.setState(s => ((s.form.language_id = val.at(0)), s));
579 handleHoneyPotChange(i: PostForm, event: any) {
580 i.setState(s => ((s.form.honeypot = event.target.value), s));
583 handleCancel(i: PostForm) {
584 i.props.onCancel?.();
587 handlePreviewToggle(i: PostForm, event: any) {
588 event.preventDefault();
589 i.setState({ previewMode: !i.state.previewMode });
592 handleImageUploadPaste(i: PostForm, event: any) {
593 let image = event.clipboardData.files[0];
595 i.handleImageUpload(i, image);
599 handleImageUpload(i: PostForm, event: any) {
602 event.preventDefault();
603 file = event.target.files[0];
608 i.setState({ imageLoading: true });
612 console.log("pictrs upload:");
614 if (res.msg === "ok") {
615 i.state.form.url = res.url;
616 i.setState({ imageLoading: false });
617 pictrsDeleteToast(file.name, res.delete_url as string);
619 i.setState({ imageLoading: false });
620 toast(JSON.stringify(res), "danger");
624 i.setState({ imageLoading: false });
625 console.error(error);
626 toast(error, "danger");
630 handleCommunitySearch = debounce(async (text: string) => {
631 const { selectedCommunityChoice } = this.props;
632 this.setState({ communitySearchLoading: true });
634 const newOptions: Choice[] = [];
636 if (selectedCommunityChoice) {
637 newOptions.push(selectedCommunityChoice);
640 if (text.length > 0) {
642 ...(await fetchCommunities(text)).communities.map(communityToChoice)
646 communitySearchOptions: newOptions,
651 communitySearchLoading: false,
655 handleCommunitySelect(choice: Choice) {
656 if (this.props.onSelectCommunity) {
661 this.props.onSelectCommunity(choice);
663 this.setState({ loading: false });
667 parseMessage(msg: any) {
668 let mui = UserService.Instance.myUserInfo;
669 let op = wsUserOp(msg);
672 // Errors handled by top level pages
673 // toast(i18n.t(msg.error), "danger");
674 this.setState({ loading: false });
676 } else if (op == UserOperation.CreatePost) {
677 let data = wsJsonToRes<PostResponse>(msg);
678 if (data.post_view.creator.id == mui?.local_user_view.person.id) {
679 this.props.onCreate?.(data.post_view);
681 } else if (op == UserOperation.EditPost) {
682 let data = wsJsonToRes<PostResponse>(msg);
683 if (data.post_view.creator.id == mui?.local_user_view.person.id) {
684 this.setState({ loading: false });
685 this.props.onEdit?.(data.post_view);
687 } else if (op == UserOperation.Search) {
688 let data = wsJsonToRes<SearchResponse>(msg);
690 if (data.type_ == SearchType[SearchType.Posts]) {
691 this.setState({ suggestedPosts: data.posts });
692 } else if (data.type_ == SearchType[SearchType.Url]) {
693 this.setState({ crossPosts: data.posts });