1 import { Component, linkEvent } from 'inferno';
2 import { PostListings } from './post-listings';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
12 ListCommunitiesResponse,
19 WebSocketJsonResponse,
20 } from '../interfaces';
21 import { WebSocketService, UserService } from '../services';
26 capitalizeFirstLetter,
36 import autosize from 'autosize';
37 import Tribute from 'tributejs/src/Tribute.js';
38 import { i18n } from '../i18next';
40 interface PostFormProps {
41 post?: Post; // If a post is given, that means this is an edit
42 params?: PostFormParams;
44 onCreate?(id: number): any;
45 onEdit?(post: Post): any;
48 interface PostFormState {
50 communities: Array<Community>;
52 imageLoading: boolean;
54 suggestedTitle: string;
55 suggestedPosts: Array<Post>;
56 crossPosts: Array<Post>;
60 export class PostForm extends Component<PostFormProps, PostFormState> {
61 private id = `post-form-${randomStr()}`;
62 private tribute: Tribute;
63 private subscription: Subscription;
64 private emptyState: PostFormState = {
70 creator_id: UserService.Instance.user
71 ? UserService.Instance.user.id
78 suggestedTitle: undefined,
81 enable_nsfw: undefined,
84 constructor(props: any, context: any) {
85 super(props, context);
86 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
87 this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
89 this.tribute = setupTribute();
90 this.state = this.emptyState;
92 if (this.props.post) {
93 this.state.postForm = {
94 body: this.props.post.body,
95 // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
96 name: this.props.post.name,
97 community_id: this.props.post.community_id,
98 edit_id: this.props.post.id,
99 creator_id: this.props.post.creator_id,
100 url: this.props.post.url,
101 nsfw: this.props.post.nsfw,
106 if (this.props.params) {
107 this.state.postForm.name = this.props.params.name;
108 if (this.props.params.url) {
109 this.state.postForm.url = this.props.params.url;
111 if (this.props.params.body) {
112 this.state.postForm.body = this.props.params.body;
116 this.subscription = WebSocketService.Instance.subject
117 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
119 msg => this.parseMessage(msg),
120 err => console.error(err),
121 () => console.log('complete')
124 let listCommunitiesForm: ListCommunitiesForm = {
125 sort: SortType[SortType.TopAll],
129 WebSocketService.Instance.listCommunities(listCommunitiesForm);
130 WebSocketService.Instance.getSite();
133 componentDidMount() {
134 var textarea: any = document.getElementById(this.id);
136 this.tribute.attach(textarea);
137 textarea.addEventListener('tribute-replaced', () => {
138 this.state.postForm.body = textarea.value;
139 this.setState(this.state);
140 autosize.update(textarea);
144 componentWillUnmount() {
145 this.subscription.unsubscribe();
151 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
152 <div class="form-group row">
153 <label class="col-sm-2 col-form-label" htmlFor="post-url">
156 <div class="col-sm-10">
161 value={this.state.postForm.url}
162 onInput={linkEvent(this, this.handlePostUrlChange)}
163 onPaste={linkEvent(this, this.handleImageUploadPaste)}
165 {this.state.suggestedTitle && (
167 class="mt-1 text-muted small font-weight-bold pointer"
168 onClick={linkEvent(this, this.copySuggestedTitle)}
170 {i18n.t('copy_suggested_title', {
171 title: this.state.suggestedTitle,
177 htmlFor="file-upload"
178 className={`${UserService.Instance.user &&
179 'pointer'} d-inline-block mr-2 float-right text-muted small font-weight-bold`}
181 {i18n.t('upload_image')}
186 accept="image/*,video/*"
189 disabled={!UserService.Instance.user}
190 onChange={linkEvent(this, this.handleImageUpload)}
193 {validURL(this.state.postForm.url) && (
195 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
196 this.state.postForm.url
199 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
201 {i18n.t('archive_link')}
204 {this.state.imageLoading && (
205 <svg class="icon icon-spinner spin">
206 <use xlinkHref="#icon-spinner"></use>
209 {isImage(this.state.postForm.url) && (
210 <img src={this.state.postForm.url} class="img-fluid" />
212 {this.state.crossPosts.length > 0 && (
214 <div class="my-1 text-muted small font-weight-bold">
215 {i18n.t('cross_posts')}
217 <PostListings showCommunity posts={this.state.crossPosts} />
222 <div class="form-group row">
223 <label class="col-sm-2 col-form-label" htmlFor="post-title">
226 <div class="col-sm-10">
228 value={this.state.postForm.name}
230 onInput={linkEvent(this, this.handlePostNameChange)}
237 {this.state.suggestedPosts.length > 0 && (
239 <div class="my-1 text-muted small font-weight-bold">
240 {i18n.t('related_posts')}
242 <PostListings posts={this.state.suggestedPosts} />
248 <div class="form-group row">
249 <label class="col-sm-2 col-form-label" htmlFor={this.id}>
252 <div class="col-sm-10">
255 value={this.state.postForm.body}
256 onInput={linkEvent(this, this.handlePostBodyChange)}
257 className={`form-control ${this.state.previewMode && 'd-none'}`}
261 {this.state.previewMode && (
264 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
267 {this.state.postForm.body && (
269 className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state
270 .previewMode && 'active'}`}
271 onClick={linkEvent(this, this.handlePreviewToggle)}
277 href={markdownHelpUrl}
279 class="d-inline-block float-right text-muted small font-weight-bold"
281 {i18n.t('formatting_help')}
285 {!this.props.post && (
286 <div class="form-group row">
287 <label class="col-sm-2 col-form-label" htmlFor="post-community">
288 {i18n.t('community')}
290 <div class="col-sm-10">
294 value={this.state.postForm.community_id}
295 onInput={linkEvent(this, this.handlePostCommunityChange)}
297 {this.state.communities.map(community => (
298 <option value={community.id}>{community.name}</option>
304 {this.state.enable_nsfw && (
305 <div class="form-group row">
306 <div class="col-sm-10">
307 <div class="form-check">
309 class="form-check-input"
312 checked={this.state.postForm.nsfw}
313 onChange={linkEvent(this, this.handlePostNsfwChange)}
315 <label class="form-check-label" htmlFor="post-nsfw">
322 <div class="form-group row">
323 <div class="col-sm-10">
324 <button type="submit" class="btn btn-secondary mr-2">
325 {this.state.loading ? (
326 <svg class="icon icon-spinner spin">
327 <use xlinkHref="#icon-spinner"></use>
329 ) : this.props.post ? (
330 capitalizeFirstLetter(i18n.t('save'))
332 capitalizeFirstLetter(i18n.t('create'))
335 {this.props.post && (
338 class="btn btn-secondary"
339 onClick={linkEvent(this, this.handleCancel)}
351 handlePostSubmit(i: PostForm, event: any) {
352 event.preventDefault();
354 WebSocketService.Instance.editPost(i.state.postForm);
356 WebSocketService.Instance.createPost(i.state.postForm);
358 i.state.loading = true;
362 copySuggestedTitle(i: PostForm) {
363 i.state.postForm.name = i.state.suggestedTitle;
364 i.state.suggestedTitle = undefined;
368 handlePostUrlChange(i: PostForm, event: any) {
369 i.state.postForm.url = event.target.value;
375 if (validURL(this.state.postForm.url)) {
376 let form: SearchForm = {
377 q: this.state.postForm.url,
378 type_: SearchType[SearchType.Url],
379 sort: SortType[SortType.TopAll],
384 WebSocketService.Instance.search(form);
386 // Fetch the page title
387 getPageTitle(this.state.postForm.url).then(d => {
388 this.state.suggestedTitle = d;
389 this.setState(this.state);
392 this.state.suggestedTitle = undefined;
393 this.state.crossPosts = [];
397 handlePostNameChange(i: PostForm, event: any) {
398 i.state.postForm.name = event.target.value;
400 i.fetchSimilarPosts();
403 fetchSimilarPosts() {
404 let form: SearchForm = {
405 q: this.state.postForm.name,
406 type_: SearchType[SearchType.Posts],
407 sort: SortType[SortType.TopAll],
408 community_id: this.state.postForm.community_id,
413 if (this.state.postForm.name !== '') {
414 WebSocketService.Instance.search(form);
416 this.state.suggestedPosts = [];
419 this.setState(this.state);
422 handlePostBodyChange(i: PostForm, event: any) {
423 i.state.postForm.body = event.target.value;
427 handlePostCommunityChange(i: PostForm, event: any) {
428 i.state.postForm.community_id = Number(event.target.value);
432 handlePostNsfwChange(i: PostForm, event: any) {
433 i.state.postForm.nsfw = event.target.checked;
437 handleCancel(i: PostForm) {
441 handlePreviewToggle(i: PostForm, event: any) {
442 event.preventDefault();
443 i.state.previewMode = !i.state.previewMode;
447 handleImageUploadPaste(i: PostForm, event: any) {
448 let image = event.clipboardData.files[0];
450 i.handleImageUpload(i, image);
454 handleImageUpload(i: PostForm, event: any) {
457 event.preventDefault();
458 file = event.target.files[0];
463 const imageUploadUrl = `/pictshare/api/upload.php`;
464 const formData = new FormData();
465 formData.append('file', file);
467 i.state.imageLoading = true;
470 fetch(imageUploadUrl, {
474 .then(res => res.json())
476 let url = `${window.location.origin}/pictshare/${res.url}`;
477 if (res.filetype == 'mp4') {
480 i.state.postForm.url = url;
481 i.state.imageLoading = false;
485 i.state.imageLoading = false;
487 toast(error, 'danger');
491 parseMessage(msg: WebSocketJsonResponse) {
492 let res = wsJsonToRes(msg);
494 toast(i18n.t(msg.error), 'danger');
495 this.state.loading = false;
496 this.setState(this.state);
498 } else if (res.op == UserOperation.ListCommunities) {
499 let data = res.data as ListCommunitiesResponse;
500 this.state.communities = data.communities;
501 if (this.props.post) {
502 this.state.postForm.community_id = this.props.post.community_id;
503 } else if (this.props.params && this.props.params.community) {
504 let foundCommunityId = data.communities.find(
505 r => r.name == this.props.params.community
507 this.state.postForm.community_id = foundCommunityId;
509 this.state.postForm.community_id = data.communities[0].id;
511 this.setState(this.state);
512 } else if (res.op == UserOperation.CreatePost) {
513 let data = res.data as PostResponse;
514 this.state.loading = false;
515 this.props.onCreate(data.post.id);
516 } else if (res.op == UserOperation.EditPost) {
517 let data = res.data as PostResponse;
518 this.state.loading = false;
519 this.props.onEdit(data.post);
520 } else if (res.op == UserOperation.Search) {
521 let data = res.data as SearchResponse;
523 if (data.type_ == SearchType[SearchType.Posts]) {
524 this.state.suggestedPosts = data.posts;
525 } else if (data.type_ == SearchType[SearchType.Url]) {
526 this.state.crossPosts = data.posts;
528 this.setState(this.state);
529 } else if (res.op == UserOperation.GetSite) {
530 let data = res.data as GetSiteResponse;
531 this.state.enable_nsfw = data.site.enable_nsfw;
532 this.setState(this.state);