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 communities: Option<CommunityView[]>;
68 params: Option<PostFormParams>;
70 onCreate?(post: PostView): any;
71 onEdit?(post: PostView): any;
73 enableDownvotes?: boolean;
76 interface PostFormState {
78 suggestedTitle: Option<string>;
79 suggestedPosts: Option<PostView[]>;
80 crossPosts: Option<PostView[]>;
82 imageLoading: boolean;
83 communitySearchLoading: boolean;
87 export class PostForm extends Component<PostFormProps, PostFormState> {
88 private subscription: Subscription;
90 private emptyState: PostFormState = {
91 postForm: new CreatePost({
92 community_id: undefined,
103 communitySearchLoading: false,
105 suggestedTitle: None,
106 suggestedPosts: None,
110 constructor(props: any, context: any) {
111 super(props, context);
112 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts.bind(this));
113 this.fetchPageTitle = debounce(this.fetchPageTitle.bind(this));
114 this.handlePostBodyChange = this.handlePostBodyChange.bind(this);
115 this.handleLanguageChange = this.handleLanguageChange.bind(this);
117 this.state = this.emptyState;
119 this.parseMessage = this.parseMessage.bind(this);
120 this.subscription = wsSubscribe(this.parseMessage);
123 if (this.props.post_view.isSome()) {
124 let pv = this.props.post_view.unwrap();
128 postForm: new CreatePost({
131 community_id: pv.community.id,
133 nsfw: Some(pv.post.nsfw),
135 language_id: Some(pv.post.language_id),
136 auth: auth().unwrap(),
141 if (this.props.params.isSome()) {
142 let params = this.props.params.unwrap();
146 ...this.state.postForm,
147 name: toUndefined(params.name),
155 componentDidMount() {
157 this.setupCommunities();
158 let textarea: any = document.getElementById("post-title");
164 componentDidUpdate() {
166 !this.state.loading &&
167 (this.state.postForm.name ||
168 this.state.postForm.url.isSome() ||
169 this.state.postForm.body.isSome())
171 window.onbeforeunload = () => true;
173 window.onbeforeunload = undefined;
177 componentWillUnmount() {
178 this.subscription.unsubscribe();
179 /* this.choices && this.choices.destroy(); */
180 window.onbeforeunload = null;
184 let selectedLangs = this.state.postForm.language_id
185 .or(myFirstDiscussionLanguageId(UserService.Instance.myUserInfo))
192 !this.state.loading &&
193 (this.state.postForm.name ||
194 this.state.postForm.url.isSome() ||
195 this.state.postForm.body.isSome())
197 message={i18n.t("block_leaving")}
199 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
200 <div className="form-group row">
201 <label className="col-sm-2 col-form-label" htmlFor="post-url">
204 <div className="col-sm-10">
208 className="form-control"
209 value={toUndefined(this.state.postForm.url)}
210 onInput={linkEvent(this, this.handlePostUrlChange)}
211 onPaste={linkEvent(this, this.handleImageUploadPaste)}
213 {this.state.suggestedTitle.match({
216 className="mt-1 text-muted small font-weight-bold pointer"
218 onClick={linkEvent(this, this.copySuggestedTitle)}
220 {i18n.t("copy_suggested_title", { title: "" })} {title}
227 htmlFor="file-upload"
229 UserService.Instance.myUserInfo.isSome() && "pointer"
230 } d-inline-block float-right text-muted font-weight-bold`}
231 data-tippy-content={i18n.t("upload_image")}
233 <Icon icon="image" classes="icon-inline" />
238 accept="image/*,video/*"
241 disabled={UserService.Instance.myUserInfo.isNone()}
242 onChange={linkEvent(this, this.handleImageUpload)}
245 {this.state.postForm.url.match({
250 href={`${webArchiveUrl}/save/${encodeURIComponent(
253 className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
256 archive.org {i18n.t("archive_link")}
259 href={`${ghostArchiveUrl}/search?term=${encodeURIComponent(
262 className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
265 ghostarchive.org {i18n.t("archive_link")}
268 href={`${archiveTodayUrl}/?run=1&url=${encodeURIComponent(
271 className="mr-2 d-inline-block float-right text-muted small font-weight-bold"
274 archive.today {i18n.t("archive_link")}
280 {this.state.imageLoading && <Spinner />}
281 {this.state.postForm.url.match({
284 <img src={url} className="img-fluid" alt="" />
288 {this.state.crossPosts.match({
290 xPosts.length > 0 && (
292 <div className="my-1 text-muted small font-weight-bold">
293 {i18n.t("cross_posts")}
298 enableDownvotes={this.props.enableDownvotes}
299 enableNsfw={this.props.enableNsfw}
300 allLanguages={this.props.allLanguages}
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.postForm.name}
316 onInput={linkEvent(this, this.handlePostNameChange)}
317 className={`form-control ${
318 !validTitle(this.state.postForm.name) && "is-invalid"
323 maxLength={MAX_POST_TITLE_LENGTH}
325 {!validTitle(this.state.postForm.name) && (
326 <div className="invalid-feedback">
327 {i18n.t("invalid_post_title")}
330 {this.state.suggestedPosts.match({
332 sPosts.length > 0 && (
334 <div className="my-1 text-muted small font-weight-bold">
335 {i18n.t("related_posts")}
340 enableDownvotes={this.props.enableDownvotes}
341 enableNsfw={this.props.enableNsfw}
342 allLanguages={this.props.allLanguages}
351 <div className="form-group row">
352 <label className="col-sm-2 col-form-label">{i18n.t("body")}</label>
353 <div className="col-sm-10">
355 initialContent={this.state.postForm.body}
356 initialLanguageId={None}
357 onContentChange={this.handlePostBodyChange}
361 allLanguages={this.props.allLanguages}
365 {this.props.post_view.isNone() && (
366 <div className="form-group row">
368 className="col-sm-2 col-form-label"
369 htmlFor="post-community"
371 {this.state.communitySearchLoading ? (
377 <div className="col-sm-10">
379 className="form-control"
381 value={this.state.postForm.community_id}
382 onInput={linkEvent(this, this.handlePostCommunityChange)}
384 <option>{i18n.t("select_a_community")}</option>
385 {this.props.communities.unwrapOr([]).map(cv => (
386 <option key={cv.community.id} value={cv.community.id}>
387 {communitySelectName(cv)}
394 {this.props.enableNsfw && (
395 <div className="form-group row">
396 <legend className="col-form-label col-sm-2 pt-0">
399 <div className="col-sm-10">
400 <div className="form-check">
402 className="form-check-input position-static"
405 checked={toUndefined(this.state.postForm.nsfw)}
406 onChange={linkEvent(this, this.handlePostNsfwChange)}
413 allLanguages={this.props.allLanguages}
414 selectedLanguageIds={selectedLangs}
416 onChange={this.handleLanguageChange}
423 className="form-control honeypot"
425 value={toUndefined(this.state.postForm.honeypot)}
426 onInput={linkEvent(this, this.handleHoneyPotChange)}
428 <div className="form-group row">
429 <div className="col-sm-10">
432 !this.state.postForm.community_id || this.state.loading
435 className="btn btn-secondary mr-2"
437 {this.state.loading ? (
439 ) : this.props.post_view.isSome() ? (
440 capitalizeFirstLetter(i18n.t("save"))
442 capitalizeFirstLetter(i18n.t("create"))
445 {this.props.post_view.isSome() && (
448 className="btn btn-secondary"
449 onClick={linkEvent(this, this.handleCancel)}
461 handlePostSubmit(i: PostForm, event: any) {
462 event.preventDefault();
464 i.setState({ loading: true });
466 // Coerce empty url string to undefined
468 i.state.postForm.url.isSome() &&
469 i.state.postForm.url.unwrapOr("blank") === ""
471 i.setState(s => ((s.postForm.url = None), s));
474 let pForm = i.state.postForm;
475 i.props.post_view.match({
477 let form = new EditPost({
478 name: Some(pForm.name),
483 language_id: Some(pv.post.language_id),
484 auth: auth().unwrap(),
486 WebSocketService.Instance.send(wsClient.editPost(form));
489 i.setState(s => ((s.postForm.auth = auth().unwrap()), s));
490 let form = new CreatePost({ ...i.state.postForm });
491 WebSocketService.Instance.send(wsClient.createPost(form));
496 copySuggestedTitle(i: PostForm) {
497 i.state.suggestedTitle.match({
501 (s.postForm.name = sTitle.substring(0, MAX_POST_TITLE_LENGTH)), s
504 i.setState({ suggestedTitle: None });
506 let textarea: any = document.getElementById("post-title");
507 autosize.update(textarea);
514 handlePostUrlChange(i: PostForm, event: any) {
515 i.setState(s => ((s.postForm.url = Some(event.target.value)), s));
520 this.state.postForm.url.match({
523 let form = new Search({
526 community_name: None,
528 type_: Some(SearchType.Url),
529 sort: Some(SortType.TopAll),
530 listing_type: Some(ListingType.All),
532 limit: Some(trendingFetchLimit),
533 auth: auth(false).ok(),
536 WebSocketService.Instance.send(wsClient.search(form));
538 // Fetch the page title
539 getSiteMetadata(url).then(d => {
540 this.setState({ suggestedTitle: d.metadata.title });
543 this.setState({ suggestedTitle: None, crossPosts: None });
550 handlePostNameChange(i: PostForm, event: any) {
551 i.setState(s => ((s.postForm.name = event.target.value), s));
552 i.fetchSimilarPosts();
555 fetchSimilarPosts() {
556 let form = new Search({
557 q: this.state.postForm.name,
558 type_: Some(SearchType.Posts),
559 sort: Some(SortType.TopAll),
560 listing_type: Some(ListingType.All),
561 community_id: Some(this.state.postForm.community_id),
562 community_name: None,
565 limit: Some(trendingFetchLimit),
566 auth: auth(false).ok(),
569 if (this.state.postForm.name !== "") {
570 WebSocketService.Instance.send(wsClient.search(form));
572 this.setState({ suggestedPosts: None });
576 handlePostBodyChange(val: string) {
577 this.setState(s => ((s.postForm.body = Some(val)), s));
580 handlePostCommunityChange(i: PostForm, event: any) {
582 s => ((s.postForm.community_id = Number(event.target.value)), s)
586 handlePostNsfwChange(i: PostForm, event: any) {
587 i.setState(s => ((s.postForm.nsfw = Some(event.target.checked)), s));
590 handleLanguageChange(val: number[]) {
591 this.setState(s => ((s.postForm.language_id = Some(val[0])), s));
594 handleHoneyPotChange(i: PostForm, event: any) {
595 i.setState(s => ((s.postForm.honeypot = Some(event.target.value)), s));
598 handleCancel(i: PostForm) {
602 handlePreviewToggle(i: PostForm, event: any) {
603 event.preventDefault();
604 i.setState({ previewMode: !i.state.previewMode });
607 handleImageUploadPaste(i: PostForm, event: any) {
608 let image = event.clipboardData.files[0];
610 i.handleImageUpload(i, image);
614 handleImageUpload(i: PostForm, event: any) {
617 event.preventDefault();
618 file = event.target.files[0];
623 const formData = new FormData();
624 formData.append("images[]", file);
626 i.setState({ imageLoading: true });
632 .then(res => res.json())
634 console.log("pictrs upload:");
636 if (res.msg == "ok") {
637 let hash = res.files[0].file;
638 let url = `${pictrsUri}/${hash}`;
639 let deleteToken = res.files[0].delete_token;
640 let deleteUrl = `${pictrsUri}/delete/${deleteToken}/${hash}`;
641 i.state.postForm.url = Some(url);
642 i.setState({ imageLoading: false });
644 i18n.t("click_to_delete_picture"),
645 i18n.t("picture_deleted"),
649 i.setState({ imageLoading: false });
650 toast(JSON.stringify(res), "danger");
654 i.setState({ imageLoading: false });
655 console.error(error);
656 toast(error, "danger");
661 // Set up select searching
663 let selectId: any = document.getElementById("post-community");
665 this.choices = new Choices(selectId, choicesConfig);
666 this.choices.passedElement.element.addEventListener(
671 (s.postForm.community_id = Number(e.detail.choice.value)), s
677 this.choices.passedElement.element.addEventListener("search", () => {
678 this.setState({ communitySearchLoading: true });
680 this.choices.passedElement.element.addEventListener(
682 debounce(async (e: any) => {
684 let communities = (await fetchCommunities(e.detail.value))
686 this.choices.setChoices(
687 communities.map(cv => communityToChoice(cv)),
692 this.setState({ communitySearchLoading: false });
702 this.props.post_view.match({
704 this.setState(s => ((s.postForm.community_id = pv.community.id), s)),
707 this.props.params.match({
709 params.nameOrId.match({
713 let foundCommunityId = this.props.communities
715 .find(r => r.community.name == name).community.id;
717 s => ((s.postForm.community_id = foundCommunityId), s)
721 this.setState(s => ((s.postForm.community_id = id), s)),
728 if (isBrowser() && this.state.postForm.community_id) {
729 this.choices.setChoiceByValue(
730 this.state.postForm.community_id.toString()
733 this.setState(this.state);
736 parseMessage(msg: any) {
737 let op = wsUserOp(msg);
740 // Errors handled by top level pages
741 // toast(i18n.t(msg.error), "danger");
742 this.setState({ loading: false });
744 } else if (op == UserOperation.CreatePost) {
745 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
746 UserService.Instance.myUserInfo.match({
748 if (data.post_view.creator.id == mui.local_user_view.person.id) {
749 this.props.onCreate(data.post_view);
754 } else if (op == UserOperation.EditPost) {
755 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
756 UserService.Instance.myUserInfo.match({
758 if (data.post_view.creator.id == mui.local_user_view.person.id) {
759 this.setState({ loading: false });
760 this.props.onEdit(data.post_view);
765 } else if (op == UserOperation.Search) {
766 let data = wsJsonToRes<SearchResponse>(msg, SearchResponse);
768 if (data.type_ == SearchType[SearchType.Posts]) {
769 this.setState({ suggestedPosts: Some(data.posts) });
770 } else if (data.type_ == SearchType[SearchType.Url]) {
771 this.setState({ crossPosts: Some(data.posts) });