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';
39 import { T } from 'inferno-i18next';
41 interface PostFormProps {
42 post?: Post; // If a post is given, that means this is an edit
43 params?: PostFormParams;
45 onCreate?(id: number): any;
46 onEdit?(post: Post): any;
49 interface PostFormState {
51 communities: Array<Community>;
53 imageLoading: boolean;
55 suggestedTitle: string;
56 suggestedPosts: Array<Post>;
57 crossPosts: Array<Post>;
61 export class PostForm extends Component<PostFormProps, PostFormState> {
62 private id = `post-form-${randomStr()}`;
63 private tribute: Tribute;
64 private subscription: Subscription;
65 private emptyState: PostFormState = {
71 creator_id: UserService.Instance.user
72 ? UserService.Instance.user.id
79 suggestedTitle: undefined,
82 enable_nsfw: undefined,
85 constructor(props: any, context: any) {
86 super(props, context);
87 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
88 this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
90 this.tribute = setupTribute();
91 this.state = this.emptyState;
93 if (this.props.post) {
94 this.state.postForm = {
95 body: this.props.post.body,
96 // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
97 name: this.props.post.name,
98 community_id: this.props.post.community_id,
99 edit_id: this.props.post.id,
100 creator_id: this.props.post.creator_id,
101 url: this.props.post.url,
102 nsfw: this.props.post.nsfw,
107 if (this.props.params) {
108 this.state.postForm.name = this.props.params.name;
109 if (this.props.params.url) {
110 this.state.postForm.url = this.props.params.url;
112 if (this.props.params.body) {
113 this.state.postForm.body = this.props.params.body;
117 this.subscription = WebSocketService.Instance.subject
118 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
120 msg => this.parseMessage(msg),
121 err => console.error(err),
122 () => console.log('complete')
125 let listCommunitiesForm: ListCommunitiesForm = {
126 sort: SortType[SortType.TopAll],
130 WebSocketService.Instance.listCommunities(listCommunitiesForm);
131 WebSocketService.Instance.getSite();
134 componentDidMount() {
135 var textarea: any = document.getElementById(this.id);
137 this.tribute.attach(textarea);
138 textarea.addEventListener('tribute-replaced', () => {
139 this.state.postForm.body = textarea.value;
140 this.setState(this.state);
141 autosize.update(textarea);
145 componentWillUnmount() {
146 this.subscription.unsubscribe();
152 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
153 <div class="form-group row">
154 <label class="col-sm-2 col-form-label">
155 <T i18nKey="url">#</T>
157 <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)}
171 i18nKey="copy_suggested_title"
172 interpolation={{ title: this.state.suggestedTitle }}
180 htmlFor="file-upload"
181 className={`${UserService.Instance.user &&
182 'pointer'} d-inline-block mr-2 float-right text-muted small font-weight-bold`}
184 <T i18nKey="upload_image">#</T>
189 accept="image/*,video/*"
192 disabled={!UserService.Instance.user}
193 onChange={linkEvent(this, this.handleImageUpload)}
196 {validURL(this.state.postForm.url) && (
198 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
199 this.state.postForm.url
202 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
204 <T i18nKey="archive_link">#</T>
207 {this.state.imageLoading && (
208 <svg class="icon icon-spinner spin">
209 <use xlinkHref="#icon-spinner"></use>
212 {isImage(this.state.postForm.url) && (
213 <img src={this.state.postForm.url} class="img-fluid" />
215 {this.state.crossPosts.length > 0 && (
217 <div class="my-1 text-muted small font-weight-bold">
218 <T i18nKey="cross_posts">#</T>
220 <PostListings showCommunity posts={this.state.crossPosts} />
225 <div class="form-group row">
226 <label class="col-sm-2 col-form-label">
227 <T i18nKey="title">#</T>
229 <div class="col-sm-10">
231 value={this.state.postForm.name}
232 onInput={linkEvent(this, this.handlePostNameChange)}
239 {this.state.suggestedPosts.length > 0 && (
241 <div class="my-1 text-muted small font-weight-bold">
242 <T i18nKey="related_posts">#</T>
244 <PostListings posts={this.state.suggestedPosts} />
249 <div class="form-group row">
250 <label class="col-sm-2 col-form-label">
251 <T i18nKey="body">#</T>
253 <div class="col-sm-10">
256 value={this.state.postForm.body}
257 onInput={linkEvent(this, this.handlePostBodyChange)}
258 className={`form-control ${this.state.previewMode && 'd-none'}`}
262 {this.state.previewMode && (
265 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
268 {this.state.postForm.body && (
270 className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state
271 .previewMode && 'active'}`}
272 onClick={linkEvent(this, this.handlePreviewToggle)}
274 <T i18nKey="preview">#</T>
278 href={markdownHelpUrl}
280 class="d-inline-block float-right text-muted small font-weight-bold"
282 <T i18nKey="formatting_help">#</T>
286 {!this.props.post && (
287 <div class="form-group row">
288 <label class="col-sm-2 col-form-label">
289 <T i18nKey="community">#</T>
291 <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"
311 checked={this.state.postForm.nsfw}
312 onChange={linkEvent(this, this.handlePostNsfwChange)}
314 <label class="form-check-label">
315 <T i18nKey="nsfw">#</T>
321 <div class="form-group row">
322 <div class="col-sm-10">
323 <button type="submit" class="btn btn-secondary mr-2">
324 {this.state.loading ? (
325 <svg class="icon icon-spinner spin">
326 <use xlinkHref="#icon-spinner"></use>
328 ) : this.props.post ? (
329 capitalizeFirstLetter(i18n.t('save'))
331 capitalizeFirstLetter(i18n.t('create'))
334 {this.props.post && (
337 class="btn btn-secondary"
338 onClick={linkEvent(this, this.handleCancel)}
340 <T i18nKey="cancel">#</T>
350 handlePostSubmit(i: PostForm, event: any) {
351 event.preventDefault();
353 WebSocketService.Instance.editPost(i.state.postForm);
355 WebSocketService.Instance.createPost(i.state.postForm);
357 i.state.loading = true;
361 copySuggestedTitle(i: PostForm) {
362 i.state.postForm.name = i.state.suggestedTitle;
363 i.state.suggestedTitle = undefined;
367 handlePostUrlChange(i: PostForm, event: any) {
368 i.state.postForm.url = event.target.value;
374 if (validURL(this.state.postForm.url)) {
375 let form: SearchForm = {
376 q: this.state.postForm.url,
377 type_: SearchType[SearchType.Url],
378 sort: SortType[SortType.TopAll],
383 WebSocketService.Instance.search(form);
385 // Fetch the page title
386 getPageTitle(this.state.postForm.url).then(d => {
387 this.state.suggestedTitle = d;
388 this.setState(this.state);
391 this.state.suggestedTitle = undefined;
392 this.state.crossPosts = [];
396 handlePostNameChange(i: PostForm, event: any) {
397 i.state.postForm.name = event.target.value;
399 i.fetchSimilarPosts();
402 fetchSimilarPosts() {
403 let form: SearchForm = {
404 q: this.state.postForm.name,
405 type_: SearchType[SearchType.Posts],
406 sort: SortType[SortType.TopAll],
407 community_id: this.state.postForm.community_id,
412 if (this.state.postForm.name !== '') {
413 WebSocketService.Instance.search(form);
415 this.state.suggestedPosts = [];
418 this.setState(this.state);
421 handlePostBodyChange(i: PostForm, event: any) {
422 i.state.postForm.body = event.target.value;
426 handlePostCommunityChange(i: PostForm, event: any) {
427 i.state.postForm.community_id = Number(event.target.value);
431 handlePostNsfwChange(i: PostForm, event: any) {
432 i.state.postForm.nsfw = event.target.checked;
436 handleCancel(i: PostForm) {
440 handlePreviewToggle(i: PostForm, event: any) {
441 event.preventDefault();
442 i.state.previewMode = !i.state.previewMode;
446 handleImageUploadPaste(i: PostForm, event: any) {
447 let image = event.clipboardData.files[0];
449 i.handleImageUpload(i, image);
453 handleImageUpload(i: PostForm, event: any) {
456 event.preventDefault();
457 file = event.target.files[0];
462 const imageUploadUrl = `/pictshare/api/upload.php`;
463 const formData = new FormData();
464 formData.append('file', file);
466 i.state.imageLoading = true;
469 fetch(imageUploadUrl, {
473 .then(res => res.json())
475 let url = `${window.location.origin}/pictshare/${res.url}`;
476 if (res.filetype == 'mp4') {
479 i.state.postForm.url = url;
480 i.state.imageLoading = false;
484 i.state.imageLoading = false;
486 toast(error, 'danger');
490 parseMessage(msg: WebSocketJsonResponse) {
491 let res = wsJsonToRes(msg);
493 toast(i18n.t(msg.error), 'danger');
494 this.state.loading = false;
495 this.setState(this.state);
497 } else if (res.op == UserOperation.ListCommunities) {
498 let data = res.data as ListCommunitiesResponse;
499 this.state.communities = data.communities;
500 if (this.props.post) {
501 this.state.postForm.community_id = this.props.post.community_id;
502 } else if (this.props.params && this.props.params.community) {
503 let foundCommunityId = data.communities.find(
504 r => r.name == this.props.params.community
506 this.state.postForm.community_id = foundCommunityId;
508 this.state.postForm.community_id = data.communities[0].id;
510 this.setState(this.state);
511 } else if (res.op == UserOperation.CreatePost) {
512 let data = res.data as PostResponse;
513 this.state.loading = false;
514 this.props.onCreate(data.post.id);
515 } else if (res.op == UserOperation.EditPost) {
516 let data = res.data as PostResponse;
517 this.state.loading = false;
518 this.props.onEdit(data.post);
519 } else if (res.op == UserOperation.Search) {
520 let data = res.data as SearchResponse;
522 if (data.type_ == SearchType[SearchType.Posts]) {
523 this.state.suggestedPosts = data.posts;
524 } else if (data.type_ == SearchType[SearchType.Url]) {
525 this.state.crossPosts = data.posts;
527 this.setState(this.state);
528 } else if (res.op == UserOperation.GetSite) {
529 let data = res.data as GetSiteResponse;
530 this.state.enable_nsfw = data.site.enable_nsfw;
531 this.setState(this.state);