1 import { None, Option, Some } from "@sniptt/monads";
2 import autosize from "autosize";
3 import { Component, linkEvent } from "inferno";
4 import { Prompt } from "inferno-router";
21 } from "lemmy-js-client";
22 import { Subscription } from "rxjs";
23 import { pictrsUri } from "../../env";
24 import { i18n } from "../../i18next";
25 import { PostFormParams } from "../../interfaces";
26 import { UserService, WebSocketService } from "../../services";
30 capitalizeFirstLetter,
40 myFirstDiscussionLanguageId,
52 import { Icon, Spinner } from "../common/icon";
53 import { LanguageSelect } from "../common/language-select";
54 import { MarkdownTextArea } from "../common/markdown-textarea";
55 import { PostListings } from "./post-listings";
59 Choices = require("choices.js");
62 const MAX_POST_TITLE_LENGTH = 200;
64 interface PostFormProps {
65 post_view: Option<PostView>; // If a post is given, that means this is an edit
66 allLanguages: Language[];
67 siteLanguages: number[];
68 communities: Option<CommunityView[]>;
69 params: Option<PostFormParams>;
71 onCreate?(post: PostView): any;
72 onEdit?(post: PostView): any;
74 enableDownvotes?: boolean;
77 interface PostFormState {
79 suggestedTitle: Option<string>;
80 suggestedPosts: Option<PostView[]>;
81 crossPosts: Option<PostView[]>;
83 imageLoading: boolean;
84 communitySearchLoading: boolean;
88 export class PostForm extends Component<PostFormProps, PostFormState> {
89 private subscription: Subscription;
91 private emptyState: PostFormState = {
92 postForm: new CreatePost({
93 community_id: undefined,
104 communitySearchLoading: false,
106 suggestedTitle: None,
107 suggestedPosts: None,
111 constructor(props: any, context: any) {
112 super(props, context);
113 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts.bind(this));
114 this.fetchPageTitle = debounce(this.fetchPageTitle.bind(this));
115 this.handlePostBodyChange = this.handlePostBodyChange.bind(this);
116 this.handleLanguageChange = this.handleLanguageChange.bind(this);
118 this.state = this.emptyState;
120 this.parseMessage = this.parseMessage.bind(this);
121 this.subscription = wsSubscribe(this.parseMessage);
124 if (this.props.post_view.isSome()) {
125 let pv = this.props.post_view.unwrap();
129 postForm: new CreatePost({
132 community_id: pv.community.id,
134 nsfw: Some(pv.post.nsfw),
136 language_id: Some(pv.post.language_id),
137 auth: auth().unwrap(),
142 if (this.props.params.isSome()) {
143 let params = this.props.params.unwrap();
147 ...this.state.postForm,
148 name: toUndefined(params.name),
156 componentDidMount() {
158 this.setupCommunities();
159 let textarea: any = document.getElementById("post-title");
165 componentDidUpdate() {
167 !this.state.loading &&
168 (this.state.postForm.name ||
169 this.state.postForm.url.isSome() ||
170 this.state.postForm.body.isSome())
172 window.onbeforeunload = () => true;
174 window.onbeforeunload = undefined;
178 componentWillUnmount() {
179 this.subscription.unsubscribe();
180 /* this.choices && this.choices.destroy(); */
181 window.onbeforeunload = null;
185 let selectedLangs = this.state.postForm.language_id
187 myFirstDiscussionLanguageId(
188 this.props.allLanguages,
189 this.props.siteLanguages,
190 UserService.Instance.myUserInfo
199 !this.state.loading &&
200 (this.state.postForm.name ||
201 this.state.postForm.url.isSome() ||
202 this.state.postForm.body.isSome())
204 message={i18n.t("block_leaving")}
206 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
207 <div className="form-group row">
208 <label className="col-sm-2 col-form-label" htmlFor="post-url">
211 <div className="col-sm-10">
215 className="form-control"
216 value={toUndefined(this.state.postForm.url)}
217 onInput={linkEvent(this, this.handlePostUrlChange)}
218 onPaste={linkEvent(this, this.handleImageUploadPaste)}
220 {this.state.suggestedTitle.match({
223 className="mt-1 text-muted small font-weight-bold pointer"
225 onClick={linkEvent(this, this.copySuggestedTitle)}
227 {i18n.t("copy_suggested_title", { title: "" })} {title}
234 htmlFor="file-upload"
236 UserService.Instance.myUserInfo.isSome() && "pointer"
237 } d-inline-block float-right text-muted font-weight-bold`}
238 data-tippy-content={i18n.t("upload_image")}
240 <Icon icon="image" classes="icon-inline" />
245 accept="image/*,video/*"
248 disabled={UserService.Instance.myUserInfo.isNone()}
249 onChange={linkEvent(this, this.handleImageUpload)}
252 {this.state.postForm.url.match({
257 href={`${webArchiveUrl}/save/${encodeURIComponent(
260 className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
263 archive.org {i18n.t("archive_link")}
266 href={`${ghostArchiveUrl}/search?term=${encodeURIComponent(
269 className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
272 ghostarchive.org {i18n.t("archive_link")}
275 href={`${archiveTodayUrl}/?run=1&url=${encodeURIComponent(
278 className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
281 archive.today {i18n.t("archive_link")}
287 {this.state.imageLoading && <Spinner />}
288 {this.state.postForm.url.match({
291 <img src={url} className="img-fluid" alt="" />
295 {this.state.crossPosts.match({
297 xPosts.length > 0 && (
299 <div className="my-1 text-muted small font-weight-bold">
300 {i18n.t("cross_posts")}
305 enableDownvotes={this.props.enableDownvotes}
306 enableNsfw={this.props.enableNsfw}
307 allLanguages={this.props.allLanguages}
308 siteLanguages={this.props.siteLanguages}
316 <div className="form-group row">
317 <label className="col-sm-2 col-form-label" htmlFor="post-title">
320 <div className="col-sm-10">
322 value={this.state.postForm.name}
324 onInput={linkEvent(this, this.handlePostNameChange)}
325 className={`form-control ${
326 !validTitle(this.state.postForm.name) && "is-invalid"
331 maxLength={MAX_POST_TITLE_LENGTH}
333 {!validTitle(this.state.postForm.name) && (
334 <div className="invalid-feedback">
335 {i18n.t("invalid_post_title")}
338 {this.state.suggestedPosts.match({
340 sPosts.length > 0 && (
342 <div className="my-1 text-muted small font-weight-bold">
343 {i18n.t("related_posts")}
348 enableDownvotes={this.props.enableDownvotes}
349 enableNsfw={this.props.enableNsfw}
350 allLanguages={this.props.allLanguages}
351 siteLanguages={this.props.siteLanguages}
360 <div className="form-group row">
361 <label className="col-sm-2 col-form-label">{i18n.t("body")}</label>
362 <div className="col-sm-10">
364 initialContent={this.state.postForm.body}
365 initialLanguageId={None}
366 onContentChange={this.handlePostBodyChange}
370 allLanguages={this.props.allLanguages}
371 siteLanguages={this.props.siteLanguages}
375 {this.props.post_view.isNone() && (
376 <div className="form-group row">
378 className="col-sm-2 col-form-label"
379 htmlFor="post-community"
381 {this.state.communitySearchLoading ? (
387 <div className="col-sm-10">
389 className="form-control"
391 value={this.state.postForm.community_id}
392 onInput={linkEvent(this, this.handlePostCommunityChange)}
394 <option>{i18n.t("select_a_community")}</option>
395 {this.props.communities.unwrapOr([]).map(cv => (
396 <option key={cv.community.id} value={cv.community.id}>
397 {communitySelectName(cv)}
404 {this.props.enableNsfw && (
405 <div className="form-group row">
406 <legend className="col-form-label col-sm-2 pt-0">
409 <div className="col-sm-10">
410 <div className="form-check">
412 className="form-check-input position-static"
415 checked={toUndefined(this.state.postForm.nsfw)}
416 onChange={linkEvent(this, this.handlePostNsfwChange)}
423 allLanguages={this.props.allLanguages}
424 siteLanguages={this.props.siteLanguages}
425 selectedLanguageIds={selectedLangs}
427 onChange={this.handleLanguageChange}
434 className="form-control honeypot"
436 value={toUndefined(this.state.postForm.honeypot)}
437 onInput={linkEvent(this, this.handleHoneyPotChange)}
439 <div className="form-group row">
440 <div className="col-sm-10">
443 !this.state.postForm.community_id || this.state.loading
446 className="btn btn-secondary mr-2"
448 {this.state.loading ? (
450 ) : this.props.post_view.isSome() ? (
451 capitalizeFirstLetter(i18n.t("save"))
453 capitalizeFirstLetter(i18n.t("create"))
456 {this.props.post_view.isSome() && (
459 className="btn btn-secondary"
460 onClick={linkEvent(this, this.handleCancel)}
472 handlePostSubmit(i: PostForm, event: any) {
473 event.preventDefault();
475 i.setState({ loading: true });
477 // Coerce empty url string to undefined
479 i.state.postForm.url.isSome() &&
480 i.state.postForm.url.unwrapOr("blank") === ""
482 i.setState(s => ((s.postForm.url = None), s));
485 let pForm = i.state.postForm;
486 i.props.post_view.match({
488 let form = new EditPost({
489 name: Some(pForm.name),
494 language_id: Some(pv.post.language_id),
495 auth: auth().unwrap(),
497 WebSocketService.Instance.send(wsClient.editPost(form));
500 i.setState(s => ((s.postForm.auth = auth().unwrap()), s));
501 let form = new CreatePost({ ...i.state.postForm });
502 WebSocketService.Instance.send(wsClient.createPost(form));
507 copySuggestedTitle(i: PostForm) {
508 i.state.suggestedTitle.match({
512 (s.postForm.name = sTitle.substring(0, MAX_POST_TITLE_LENGTH)), s
515 i.setState({ suggestedTitle: None });
517 let textarea: any = document.getElementById("post-title");
518 autosize.update(textarea);
525 handlePostUrlChange(i: PostForm, event: any) {
526 i.setState(s => ((s.postForm.url = Some(event.target.value)), s));
531 this.state.postForm.url.match({
534 let form = new Search({
537 community_name: None,
539 type_: Some(SearchType.Url),
540 sort: Some(SortType.TopAll),
541 listing_type: Some(ListingType.All),
543 limit: Some(trendingFetchLimit),
544 auth: auth(false).ok(),
547 WebSocketService.Instance.send(wsClient.search(form));
549 // Fetch the page title
550 getSiteMetadata(url).then(d => {
551 this.setState({ suggestedTitle: d.metadata.title });
554 this.setState({ suggestedTitle: None, crossPosts: None });
561 handlePostNameChange(i: PostForm, event: any) {
562 i.setState(s => ((s.postForm.name = event.target.value), s));
563 i.fetchSimilarPosts();
566 fetchSimilarPosts() {
567 let form = new Search({
568 q: this.state.postForm.name,
569 type_: Some(SearchType.Posts),
570 sort: Some(SortType.TopAll),
571 listing_type: Some(ListingType.All),
572 community_id: Some(this.state.postForm.community_id),
573 community_name: None,
576 limit: Some(trendingFetchLimit),
577 auth: auth(false).ok(),
580 if (this.state.postForm.name !== "") {
581 WebSocketService.Instance.send(wsClient.search(form));
583 this.setState({ suggestedPosts: None });
587 handlePostBodyChange(val: string) {
588 this.setState(s => ((s.postForm.body = Some(val)), s));
591 handlePostCommunityChange(i: PostForm, event: any) {
593 s => ((s.postForm.community_id = Number(event.target.value)), s)
597 handlePostNsfwChange(i: PostForm, event: any) {
598 i.setState(s => ((s.postForm.nsfw = Some(event.target.checked)), s));
601 handleLanguageChange(val: number[]) {
602 this.setState(s => ((s.postForm.language_id = Some(val[0])), s));
605 handleHoneyPotChange(i: PostForm, event: any) {
606 i.setState(s => ((s.postForm.honeypot = Some(event.target.value)), s));
609 handleCancel(i: PostForm) {
613 handlePreviewToggle(i: PostForm, event: any) {
614 event.preventDefault();
615 i.setState({ previewMode: !i.state.previewMode });
618 handleImageUploadPaste(i: PostForm, event: any) {
619 let image = event.clipboardData.files[0];
621 i.handleImageUpload(i, image);
625 handleImageUpload(i: PostForm, event: any) {
628 event.preventDefault();
629 file = event.target.files[0];
634 const formData = new FormData();
635 formData.append("images[]", file);
637 i.setState({ imageLoading: true });
643 .then(res => res.json())
645 console.log("pictrs upload:");
647 if (res.msg == "ok") {
648 let hash = res.files[0].file;
649 let url = `${pictrsUri}/${hash}`;
650 let deleteToken = res.files[0].delete_token;
651 let deleteUrl = `${pictrsUri}/delete/${deleteToken}/${hash}`;
652 i.state.postForm.url = Some(url);
653 i.setState({ imageLoading: false });
655 `${i18n.t("click_to_delete_picture")}: ${file.name}`,
656 `${i18n.t("picture_deleted")}: ${file.name}`,
657 `${i18n.t("failed_to_delete_picture")}: ${file.name}`,
661 i.setState({ imageLoading: false });
662 toast(JSON.stringify(res), "danger");
666 i.setState({ imageLoading: false });
667 console.error(error);
668 toast(error, "danger");
673 // Set up select searching
675 let selectId: any = document.getElementById("post-community");
677 this.choices = new Choices(selectId, choicesConfig);
678 this.choices.passedElement.element.addEventListener(
683 (s.postForm.community_id = Number(e.detail.choice.value)), s
689 this.choices.passedElement.element.addEventListener("search", () => {
690 this.setState({ communitySearchLoading: true });
692 this.choices.passedElement.element.addEventListener(
694 debounce(async (e: any) => {
696 let communities = (await fetchCommunities(e.detail.value))
698 this.choices.setChoices(
699 communities.map(cv => communityToChoice(cv)),
704 this.setState({ communitySearchLoading: false });
714 this.props.post_view.match({
716 this.setState(s => ((s.postForm.community_id = pv.community.id), s)),
719 this.props.params.match({
721 params.nameOrId.match({
725 let foundCommunityId = this.props.communities
727 .find(r => r.community.name == name).community.id;
729 s => ((s.postForm.community_id = foundCommunityId), s)
733 this.setState(s => ((s.postForm.community_id = id), s)),
740 if (isBrowser() && this.state.postForm.community_id) {
741 this.choices.setChoiceByValue(
742 this.state.postForm.community_id.toString()
745 this.setState(this.state);
748 parseMessage(msg: any) {
749 let op = wsUserOp(msg);
752 // Errors handled by top level pages
753 // toast(i18n.t(msg.error), "danger");
754 this.setState({ loading: false });
756 } else if (op == UserOperation.CreatePost) {
757 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
758 UserService.Instance.myUserInfo.match({
760 if (data.post_view.creator.id == mui.local_user_view.person.id) {
761 this.props.onCreate(data.post_view);
766 } else if (op == UserOperation.EditPost) {
767 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
768 UserService.Instance.myUserInfo.match({
770 if (data.post_view.creator.id == mui.local_user_view.person.id) {
771 this.setState({ loading: false });
772 this.props.onEdit(data.post_view);
777 } else if (op == UserOperation.Search) {
778 let data = wsJsonToRes<SearchResponse>(msg, SearchResponse);
780 if (data.type_ == SearchType[SearchType.Posts]) {
781 this.setState({ suggestedPosts: Some(data.posts) });
782 } else if (data.type_ == SearchType[SearchType.Url]) {
783 this.setState({ crossPosts: Some(data.posts) });