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)}
164 {this.state.suggestedTitle && (
166 class="mt-1 text-muted small font-weight-bold pointer"
167 onClick={linkEvent(this, this.copySuggestedTitle)}
170 i18nKey="copy_suggested_title"
171 interpolation={{ title: this.state.suggestedTitle }}
179 htmlFor="file-upload"
180 className={`${UserService.Instance.user &&
181 'pointer'} d-inline-block mr-2 float-right text-muted small font-weight-bold`}
183 <T i18nKey="upload_image">#</T>
188 accept="image/*,video/*"
191 disabled={!UserService.Instance.user}
192 onChange={linkEvent(this, this.handleImageUpload)}
195 {validURL(this.state.postForm.url) && (
197 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
198 this.state.postForm.url
201 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
203 <T i18nKey="archive_link">#</T>
206 {this.state.imageLoading && (
207 <svg class="icon icon-spinner spin">
208 <use xlinkHref="#icon-spinner"></use>
211 {isImage(this.state.postForm.url) && (
212 <img src={this.state.postForm.url} class="img-fluid" />
214 {this.state.crossPosts.length > 0 && (
216 <div class="my-1 text-muted small font-weight-bold">
217 <T i18nKey="cross_posts">#</T>
219 <PostListings showCommunity posts={this.state.crossPosts} />
224 <div class="form-group row">
225 <label class="col-sm-2 col-form-label">
226 <T i18nKey="title">#</T>
228 <div class="col-sm-10">
230 value={this.state.postForm.name}
231 onInput={linkEvent(this, this.handlePostNameChange)}
238 {this.state.suggestedPosts.length > 0 && (
240 <div class="my-1 text-muted small font-weight-bold">
241 <T i18nKey="related_posts">#</T>
243 <PostListings posts={this.state.suggestedPosts} />
248 <div class="form-group row">
249 <label class="col-sm-2 col-form-label">
250 <T i18nKey="body">#</T>
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)}
273 <T i18nKey="preview">#</T>
277 href={markdownHelpUrl}
279 class="d-inline-block float-right text-muted small font-weight-bold"
281 <T i18nKey="formatting_help">#</T>
285 {!this.props.post && (
286 <div class="form-group row">
287 <label class="col-sm-2 col-form-label">
288 <T i18nKey="community">#</T>
290 <div class="col-sm-10">
293 value={this.state.postForm.community_id}
294 onInput={linkEvent(this, this.handlePostCommunityChange)}
296 {this.state.communities.map(community => (
297 <option value={community.id}>{community.name}</option>
303 {this.state.enable_nsfw && (
304 <div class="form-group row">
305 <div class="col-sm-10">
306 <div class="form-check">
308 class="form-check-input"
310 checked={this.state.postForm.nsfw}
311 onChange={linkEvent(this, this.handlePostNsfwChange)}
313 <label class="form-check-label">
314 <T i18nKey="nsfw">#</T>
320 <div class="form-group row">
321 <div class="col-sm-10">
322 <button type="submit" class="btn btn-secondary mr-2">
323 {this.state.loading ? (
324 <svg class="icon icon-spinner spin">
325 <use xlinkHref="#icon-spinner"></use>
327 ) : this.props.post ? (
328 capitalizeFirstLetter(i18n.t('save'))
330 capitalizeFirstLetter(i18n.t('create'))
333 {this.props.post && (
336 class="btn btn-secondary"
337 onClick={linkEvent(this, this.handleCancel)}
339 <T i18nKey="cancel">#</T>
349 handlePostSubmit(i: PostForm, event: any) {
350 event.preventDefault();
352 WebSocketService.Instance.editPost(i.state.postForm);
354 WebSocketService.Instance.createPost(i.state.postForm);
356 i.state.loading = true;
360 copySuggestedTitle(i: PostForm) {
361 i.state.postForm.name = i.state.suggestedTitle;
362 i.state.suggestedTitle = undefined;
366 handlePostUrlChange(i: PostForm, event: any) {
367 i.state.postForm.url = event.target.value;
373 if (validURL(this.state.postForm.url)) {
374 let form: SearchForm = {
375 q: this.state.postForm.url,
376 type_: SearchType[SearchType.Url],
377 sort: SortType[SortType.TopAll],
382 WebSocketService.Instance.search(form);
384 // Fetch the page title
385 getPageTitle(this.state.postForm.url).then(d => {
386 this.state.suggestedTitle = d;
387 this.setState(this.state);
390 this.state.suggestedTitle = undefined;
391 this.state.crossPosts = [];
395 handlePostNameChange(i: PostForm, event: any) {
396 i.state.postForm.name = event.target.value;
398 i.fetchSimilarPosts();
401 fetchSimilarPosts() {
402 let form: SearchForm = {
403 q: this.state.postForm.name,
404 type_: SearchType[SearchType.Posts],
405 sort: SortType[SortType.TopAll],
406 community_id: this.state.postForm.community_id,
411 if (this.state.postForm.name !== '') {
412 WebSocketService.Instance.search(form);
414 this.state.suggestedPosts = [];
417 this.setState(this.state);
420 handlePostBodyChange(i: PostForm, event: any) {
421 i.state.postForm.body = event.target.value;
425 handlePostCommunityChange(i: PostForm, event: any) {
426 i.state.postForm.community_id = Number(event.target.value);
430 handlePostNsfwChange(i: PostForm, event: any) {
431 i.state.postForm.nsfw = event.target.checked;
435 handleCancel(i: PostForm) {
439 handlePreviewToggle(i: PostForm, event: any) {
440 event.preventDefault();
441 i.state.previewMode = !i.state.previewMode;
445 handleImageUpload(i: PostForm, event: any) {
446 event.preventDefault();
447 let file = event.target.files[0];
448 const imageUploadUrl = `/pictshare/api/upload.php`;
449 const formData = new FormData();
450 formData.append('file', file);
452 i.state.imageLoading = true;
455 fetch(imageUploadUrl, {
459 .then(res => res.json())
461 let url = `${window.location.origin}/pictshare/${res.url}`;
462 if (res.filetype == 'mp4') {
465 i.state.postForm.url = url;
466 i.state.imageLoading = false;
470 i.state.imageLoading = false;
472 toast(error, 'danger');
476 parseMessage(msg: WebSocketJsonResponse) {
477 let res = wsJsonToRes(msg);
479 toast(i18n.t(msg.error), 'danger');
480 this.state.loading = false;
481 this.setState(this.state);
483 } else if (res.op == UserOperation.ListCommunities) {
484 let data = res.data as ListCommunitiesResponse;
485 this.state.communities = data.communities;
486 if (this.props.post) {
487 this.state.postForm.community_id = this.props.post.community_id;
488 } else if (this.props.params && this.props.params.community) {
489 let foundCommunityId = data.communities.find(
490 r => r.name == this.props.params.community
492 this.state.postForm.community_id = foundCommunityId;
494 this.state.postForm.community_id = data.communities[0].id;
496 this.setState(this.state);
497 } else if (res.op == UserOperation.CreatePost) {
498 let data = res.data as PostResponse;
499 this.state.loading = false;
500 this.props.onCreate(data.post.id);
501 } else if (res.op == UserOperation.EditPost) {
502 let data = res.data as PostResponse;
503 this.state.loading = false;
504 this.props.onEdit(data.post);
505 } else if (res.op == UserOperation.Search) {
506 let data = res.data as SearchResponse;
508 if (data.type_ == SearchType[SearchType.Posts]) {
509 this.state.suggestedPosts = data.posts;
510 } else if (data.type_ == SearchType[SearchType.Url]) {
511 this.state.crossPosts = data.posts;
513 this.setState(this.state);
514 } else if (res.op == UserOperation.GetSite) {
515 let data = res.data as GetSiteResponse;
516 this.state.enable_nsfw = data.site.enable_nsfw;
517 this.setState(this.state);