]> Untitled Git - lemmy-ui.git/blob - src/shared/components/post/post-listing.tsx
fix(a11y): Add aria-label to fetaured pins
[lemmy-ui.git] / src / shared / components / post / post-listing.tsx
1 import classNames from "classnames";
2 import { Component, linkEvent } from "inferno";
3 import { Link } from "inferno-router";
4 import {
5   AddAdmin,
6   AddModToCommunity,
7   BanFromCommunity,
8   BanPerson,
9   BlockPerson,
10   CommunityModeratorView,
11   CreatePostLike,
12   CreatePostReport,
13   DeletePost,
14   EditPost,
15   FeaturePost,
16   Language,
17   LockPost,
18   PersonView,
19   PostView,
20   PurgePerson,
21   PurgePost,
22   RemovePost,
23   SavePost,
24   TransferCommunity,
25 } from "lemmy-js-client";
26 import { getExternalHost, getHttpBase } from "../../env";
27 import { i18n } from "../../i18next";
28 import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces";
29 import { UserService } from "../../services";
30 import {
31   amAdmin,
32   amCommunityCreator,
33   amMod,
34   canAdmin,
35   canMod,
36   canShare,
37   futureDaysToUnixTime,
38   hostname,
39   isAdmin,
40   isBanned,
41   isImage,
42   isMod,
43   isVideo,
44   mdNoImages,
45   mdToHtml,
46   mdToHtmlInline,
47   myAuthRequired,
48   newVote,
49   numToSI,
50   relTags,
51   setupTippy,
52   share,
53   showScores,
54 } from "../../utils";
55 import { Icon, PurgeWarning, Spinner } from "../common/icon";
56 import { MomentTime } from "../common/moment-time";
57 import { PictrsImage } from "../common/pictrs-image";
58 import { CommunityLink } from "../community/community-link";
59 import { PersonListing } from "../person/person-listing";
60 import { MetadataCard } from "./metadata-card";
61 import { PostForm } from "./post-form";
62
63 interface PostListingState {
64   showEdit: boolean;
65   showRemoveDialog: boolean;
66   showPurgeDialog: boolean;
67   purgeReason?: string;
68   purgeType?: PurgeType;
69   purgeLoading: boolean;
70   removeReason?: string;
71   showBanDialog: boolean;
72   banReason?: string;
73   banExpireDays?: number;
74   banType?: BanType;
75   removeData?: boolean;
76   showConfirmTransferSite: boolean;
77   showConfirmTransferCommunity: boolean;
78   imageExpanded: boolean;
79   viewSource: boolean;
80   showAdvanced: boolean;
81   showMoreMobile: boolean;
82   showBody: boolean;
83   showReportDialog: boolean;
84   reportReason?: string;
85   upvoteLoading: boolean;
86   downvoteLoading: boolean;
87   reportLoading: boolean;
88   blockLoading: boolean;
89   lockLoading: boolean;
90   deleteLoading: boolean;
91   removeLoading: boolean;
92   saveLoading: boolean;
93   featureCommunityLoading: boolean;
94   featureLocalLoading: boolean;
95   banLoading: boolean;
96   addModLoading: boolean;
97   addAdminLoading: boolean;
98   transferLoading: boolean;
99 }
100
101 interface PostListingProps {
102   post_view: PostView;
103   crossPosts?: PostView[];
104   moderators?: CommunityModeratorView[];
105   admins?: PersonView[];
106   allLanguages: Language[];
107   siteLanguages: number[];
108   showCommunity?: boolean;
109   showBody?: boolean;
110   hideImage?: boolean;
111   enableDownvotes?: boolean;
112   enableNsfw?: boolean;
113   viewOnly?: boolean;
114   onPostEdit(form: EditPost): void;
115   onPostVote(form: CreatePostLike): void;
116   onPostReport(form: CreatePostReport): void;
117   onBlockPerson(form: BlockPerson): void;
118   onLockPost(form: LockPost): void;
119   onDeletePost(form: DeletePost): void;
120   onRemovePost(form: RemovePost): void;
121   onSavePost(form: SavePost): void;
122   onFeaturePost(form: FeaturePost): void;
123   onPurgePerson(form: PurgePerson): void;
124   onPurgePost(form: PurgePost): void;
125   onBanPersonFromCommunity(form: BanFromCommunity): void;
126   onBanPerson(form: BanPerson): void;
127   onAddModToCommunity(form: AddModToCommunity): void;
128   onAddAdmin(form: AddAdmin): void;
129   onTransferCommunity(form: TransferCommunity): void;
130 }
131
132 export class PostListing extends Component<PostListingProps, PostListingState> {
133   state: PostListingState = {
134     showEdit: false,
135     showRemoveDialog: false,
136     showPurgeDialog: false,
137     purgeType: PurgeType.Person,
138     showBanDialog: false,
139     banType: BanType.Community,
140     removeData: false,
141     showConfirmTransferSite: false,
142     showConfirmTransferCommunity: false,
143     imageExpanded: false,
144     viewSource: false,
145     showAdvanced: false,
146     showMoreMobile: false,
147     showBody: false,
148     showReportDialog: false,
149     upvoteLoading: false,
150     downvoteLoading: false,
151     purgeLoading: false,
152     reportLoading: false,
153     blockLoading: false,
154     lockLoading: false,
155     deleteLoading: false,
156     removeLoading: false,
157     saveLoading: false,
158     featureCommunityLoading: false,
159     featureLocalLoading: false,
160     banLoading: false,
161     addModLoading: false,
162     addAdminLoading: false,
163     transferLoading: false,
164   };
165
166   constructor(props: any, context: any) {
167     super(props, context);
168
169     this.handleEditPost = this.handleEditPost.bind(this);
170     this.handleEditCancel = this.handleEditCancel.bind(this);
171   }
172
173   componentWillReceiveProps(nextProps: PostListingProps) {
174     if (this.props !== nextProps) {
175       this.setState({
176         upvoteLoading: false,
177         downvoteLoading: false,
178         purgeLoading: false,
179         reportLoading: false,
180         blockLoading: false,
181         lockLoading: false,
182         deleteLoading: false,
183         removeLoading: false,
184         saveLoading: false,
185         featureCommunityLoading: false,
186         featureLocalLoading: false,
187         banLoading: false,
188         addModLoading: false,
189         addAdminLoading: false,
190         transferLoading: false,
191         imageExpanded: false,
192       });
193     }
194   }
195
196   get postView(): PostView {
197     return this.props.post_view;
198   }
199
200   render() {
201     const post = this.postView.post;
202
203     return (
204       <div className="post-listing">
205         {!this.state.showEdit ? (
206           <>
207             {this.listing()}
208             {this.state.imageExpanded && !this.props.hideImage && this.img}
209             {post.url && this.state.showBody && post.embed_title && (
210               <MetadataCard post={post} />
211             )}
212             {this.showBody && this.body()}
213           </>
214         ) : (
215           <PostForm
216             post_view={this.postView}
217             crossPosts={this.props.crossPosts}
218             onEdit={this.handleEditPost}
219             onCancel={this.handleEditCancel}
220             enableNsfw={this.props.enableNsfw}
221             enableDownvotes={this.props.enableDownvotes}
222             allLanguages={this.props.allLanguages}
223             siteLanguages={this.props.siteLanguages}
224           />
225         )}
226       </div>
227     );
228   }
229
230   body() {
231     const body = this.postView.post.body;
232     return body ? (
233       <article id="postContent" className="col-12 card my-2 p-2">
234         {this.state.viewSource ? (
235           <pre>{body}</pre>
236         ) : (
237           <div className="md-div" dangerouslySetInnerHTML={mdToHtml(body)} />
238         )}
239       </article>
240     ) : (
241       <></>
242     );
243   }
244
245   get img() {
246     return this.imageSrc ? (
247       <>
248         <div className="offset-sm-3 my-2 d-none d-sm-block">
249           <a href={this.imageSrc} className="d-inline-block">
250             <PictrsImage src={this.imageSrc} />
251           </a>
252         </div>
253         <div className="my-2 d-block d-sm-none">
254           <a
255             className="d-inline-block"
256             onClick={linkEvent(this, this.handleImageExpandClick)}
257           >
258             <PictrsImage src={this.imageSrc} />
259           </a>
260         </div>
261       </>
262     ) : (
263       <></>
264     );
265   }
266
267   imgThumb(src: string) {
268     const post_view = this.postView;
269     return (
270       <PictrsImage
271         src={src}
272         thumbnail
273         alt=""
274         nsfw={post_view.post.nsfw || post_view.community.nsfw}
275       />
276     );
277   }
278
279   get imageSrc(): string | undefined {
280     const post = this.postView.post;
281     const url = post.url;
282     const thumbnail = post.thumbnail_url;
283
284     if (url && isImage(url)) {
285       if (url.includes("pictrs")) {
286         return url;
287       } else if (thumbnail) {
288         return thumbnail;
289       } else {
290         return url;
291       }
292     } else if (thumbnail) {
293       return thumbnail;
294     } else {
295       return undefined;
296     }
297   }
298
299   thumbnail() {
300     const post = this.postView.post;
301     const url = post.url;
302     const thumbnail = post.thumbnail_url;
303
304     if (!this.props.hideImage && url && isImage(url) && this.imageSrc) {
305       return (
306         <a
307           href={this.imageSrc}
308           className="text-body d-inline-block position-relative mb-2"
309           data-tippy-content={i18n.t("expand_here")}
310           onClick={linkEvent(this, this.handleImageExpandClick)}
311           aria-label={i18n.t("expand_here")}
312         >
313           {this.imgThumb(this.imageSrc)}
314           <Icon icon="image" classes="mini-overlay" />
315         </a>
316       );
317     } else if (!this.props.hideImage && url && thumbnail && this.imageSrc) {
318       return (
319         <a
320           className="text-body d-inline-block position-relative mb-2"
321           href={url}
322           rel={relTags}
323           title={url}
324         >
325           {this.imgThumb(this.imageSrc)}
326           <Icon icon="external-link" classes="mini-overlay" />
327         </a>
328       );
329     } else if (url) {
330       if (!this.props.hideImage && isVideo(url)) {
331         return (
332           <div className="embed-responsive embed-responsive-16by9">
333             <video
334               playsInline
335               muted
336               loop
337               controls
338               className="embed-responsive-item"
339             >
340               <source src={url} type="video/mp4" />
341             </video>
342           </div>
343         );
344       } else {
345         return (
346           <a className="text-body" href={url} title={url} rel={relTags}>
347             <div className="thumbnail rounded bg-light d-flex justify-content-center">
348               <Icon icon="external-link" classes="d-flex align-items-center" />
349             </div>
350           </a>
351         );
352       }
353     } else {
354       return (
355         <Link
356           className="text-body"
357           to={`/post/${post.id}`}
358           title={i18n.t("comments")}
359         >
360           <div className="thumbnail rounded bg-light d-flex justify-content-center">
361             <Icon icon="message-square" classes="d-flex align-items-center" />
362           </div>
363         </Link>
364       );
365     }
366   }
367
368   createdLine() {
369     const post_view = this.postView;
370     const url = post_view.post.url;
371     const body = post_view.post.body;
372     return (
373       <ul className="list-inline mb-1 text-muted small">
374         <li className="list-inline-item">
375           <PersonListing person={post_view.creator} />
376
377           {this.creatorIsMod_ && (
378             <span className="mx-1 badge">{i18n.t("mod")}</span>
379           )}
380           {this.creatorIsAdmin_ && (
381             <span className="mx-1 badge">{i18n.t("admin")}</span>
382           )}
383           {post_view.creator.bot_account && (
384             <span className="mx-1 badge">
385               {i18n.t("bot_account").toLowerCase()}
386             </span>
387           )}
388           {this.props.showCommunity && (
389             <span>
390               <span className="mx-1"> {i18n.t("to")} </span>
391               <CommunityLink community={post_view.community} />
392             </span>
393           )}
394         </li>
395         {post_view.post.language_id !== 0 && (
396           <span className="mx-1 badge">
397             {
398               this.props.allLanguages.find(
399                 lang => lang.id === post_view.post.language_id
400               )?.name
401             }
402           </span>
403         )}
404         <li className="list-inline-item">•</li>
405         {url && !(hostname(url) === getExternalHost()) && (
406           <>
407             <li className="list-inline-item">
408               <a
409                 className="text-muted font-italic"
410                 href={url}
411                 title={url}
412                 rel={relTags}
413               >
414                 {hostname(url)}
415               </a>
416             </li>
417             <li className="list-inline-item">•</li>
418           </>
419         )}
420         <li className="list-inline-item">
421           <span>
422             <MomentTime
423               published={post_view.post.published}
424               updated={post_view.post.updated}
425             />
426           </span>
427         </li>
428         {body && (
429           <>
430             <li className="list-inline-item">•</li>
431             <li className="list-inline-item">
432               <button
433                 className="text-muted btn btn-sm btn-link p-0"
434                 data-tippy-content={mdNoImages.render(body)}
435                 data-tippy-allowHtml={true}
436                 onClick={linkEvent(this, this.handleShowBody)}
437               >
438                 <Icon icon="book-open" classes="icon-inline mr-1" />
439               </button>
440             </li>
441           </>
442         )}
443       </ul>
444     );
445   }
446
447   voteBar() {
448     return (
449       <div className={`vote-bar col-1 pr-0 small text-center`}>
450         <button
451           className={`btn-animate btn btn-link p-0 ${
452             this.postView.my_vote == 1 ? "text-info" : "text-muted"
453           }`}
454           onClick={linkEvent(this, this.handleUpvote)}
455           data-tippy-content={i18n.t("upvote")}
456           aria-label={i18n.t("upvote")}
457           aria-pressed={this.postView.my_vote === 1}
458         >
459           {this.state.upvoteLoading ? (
460             <Spinner />
461           ) : (
462             <Icon icon="arrow-up1" classes="upvote" />
463           )}
464         </button>
465         {showScores() ? (
466           <div
467             className={`unselectable pointer font-weight-bold text-muted px-1 post-score`}
468             data-tippy-content={this.pointsTippy}
469           >
470             {numToSI(this.postView.counts.score)}
471           </div>
472         ) : (
473           <div className="p-1"></div>
474         )}
475         {this.props.enableDownvotes && (
476           <button
477             className={`btn-animate btn btn-link p-0 ${
478               this.postView.my_vote == -1 ? "text-danger" : "text-muted"
479             }`}
480             onClick={linkEvent(this, this.handleDownvote)}
481             data-tippy-content={i18n.t("downvote")}
482             aria-label={i18n.t("downvote")}
483             aria-pressed={this.postView.my_vote === -1}
484           >
485             {this.state.downvoteLoading ? (
486               <Spinner />
487             ) : (
488               <Icon icon="arrow-down1" classes="downvote" />
489             )}
490           </button>
491         )}
492       </div>
493     );
494   }
495
496   get postLink() {
497     const post = this.postView.post;
498     return (
499       <Link
500         className={`d-inline-block ${
501           !post.featured_community && !post.featured_local
502             ? "text-body"
503             : "text-primary"
504         }`}
505         to={`/post/${post.id}`}
506         title={i18n.t("comments")}
507       >
508         <div
509           className="d-inline-block"
510           dangerouslySetInnerHTML={mdToHtmlInline(post.name)}
511         />
512       </Link>
513     );
514   }
515
516   postTitleLine() {
517     const post = this.postView.post;
518     const url = post.url;
519
520     return (
521       <div className="post-title overflow-hidden">
522         <h5>
523           {url ? (
524             this.props.showBody ? (
525               <a
526                 className={`d-inline-block ${
527                   !post.featured_community && !post.featured_local
528                     ? "text-body"
529                     : "text-primary"
530                 }`}
531                 href={url}
532                 title={url}
533                 rel={relTags}
534               >
535                 <div
536                   className="d-inline-block"
537                   dangerouslySetInnerHTML={mdToHtmlInline(post.name)}
538                 />
539               </a>
540             ) : (
541               this.postLink
542             )
543           ) : (
544             this.postLink
545           )}
546           {(url && isImage(url)) ||
547             (post.thumbnail_url && (
548               <button
549                 className="btn btn-link text-monospace text-muted small d-inline-block"
550                 data-tippy-content={i18n.t("expand_here")}
551                 onClick={linkEvent(this, this.handleImageExpandClick)}
552               >
553                 <Icon
554                   icon={
555                     !this.state.imageExpanded ? "plus-square" : "minus-square"
556                   }
557                   classes="icon-inline"
558                 />
559               </button>
560             ))}
561           {post.removed && (
562             <small className="ml-2 text-muted font-italic">
563               {i18n.t("removed")}
564             </small>
565           )}
566           {post.deleted && (
567             <small
568               className="unselectable pointer ml-2 text-muted font-italic"
569               data-tippy-content={i18n.t("deleted")}
570             >
571               <Icon icon="trash" classes="icon-inline text-danger" />
572             </small>
573           )}
574           {post.locked && (
575             <small
576               className="unselectable pointer ml-2 text-muted font-italic"
577               data-tippy-content={i18n.t("locked")}
578             >
579               <Icon icon="lock" classes="icon-inline text-danger" />
580             </small>
581           )}
582           {post.featured_community && (
583             <small
584               className="unselectable pointer ml-2 text-muted font-italic"
585               aria-label={i18n.t("featured_in_community")}
586               data-tippy-content={i18n.t("featured_in_community")}
587             >
588               <Icon icon="pin" classes="icon-inline text-primary" />
589             </small>
590           )}
591           {post.featured_local && (
592             <small
593               className="unselectable pointer ml-2 text-muted font-italic"
594               aria-label={i18n.t("featured_in_local")}
595               data-tippy-content={i18n.t("featured_in_local")}
596             >
597               <Icon icon="pin" classes="icon-inline text-secondary" />
598             </small>
599           )}
600           {post.nsfw && (
601             <small className="ml-2 text-muted font-italic">
602               {i18n.t("nsfw")}
603             </small>
604           )}
605         </h5>
606       </div>
607     );
608   }
609
610   duplicatesLine() {
611     const dupes = this.props.crossPosts;
612     return dupes && dupes.length > 0 ? (
613       <ul className="list-inline mb-1 small text-muted">
614         <>
615           <li className="list-inline-item mr-2">{i18n.t("cross_posted_to")}</li>
616           {dupes.map(pv => (
617             <li key={pv.post.id} className="list-inline-item mr-2">
618               <Link to={`/post/${pv.post.id}`}>
619                 {pv.community.local
620                   ? pv.community.name
621                   : `${pv.community.name}@${hostname(pv.community.actor_id)}`}
622               </Link>
623             </li>
624           ))}
625         </>
626       </ul>
627     ) : (
628       <></>
629     );
630   }
631
632   commentsLine(mobile = false) {
633     const post = this.postView.post;
634
635     return (
636       <div className="d-flex align-items-center justify-content-start flex-wrap text-muted font-weight-bold mb-1">
637         {this.commentsButton}
638         {canShare() && (
639           <button
640             className="btn btn-link"
641             onClick={linkEvent(this, this.handleShare)}
642             type="button"
643           >
644             <Icon icon="share" inline />
645           </button>
646         )}
647         {!post.local && (
648           <a
649             className="btn btn-link btn-animate text-muted py-0"
650             title={i18n.t("link")}
651             href={post.ap_id}
652           >
653             <Icon icon="fedilink" inline />
654           </a>
655         )}
656         {mobile && !this.props.viewOnly && this.mobileVotes}
657         {UserService.Instance.myUserInfo &&
658           !this.props.viewOnly &&
659           this.postActions(mobile)}
660       </div>
661     );
662   }
663
664   postActions(mobile = false) {
665     // Possible enhancement: Priority+ pattern instead of just hard coding which get hidden behind the show more button.
666     // Possible enhancement: Make each button a component.
667     const post_view = this.postView;
668     return (
669       <>
670         {this.saveButton}
671         {this.crossPostButton}
672         {mobile && this.showMoreButton}
673         {(!mobile || this.state.showAdvanced) && (
674           <>
675             {!this.myPost && (
676               <>
677                 {this.reportButton}
678                 {this.blockButton}
679               </>
680             )}
681             {this.myPost && (this.showBody || this.state.showAdvanced) && (
682               <>
683                 {this.editButton}
684                 {this.deleteButton}
685               </>
686             )}
687           </>
688         )}
689         {this.state.showAdvanced && (
690           <>
691             {this.showBody && post_view.post.body && this.viewSourceButton}
692             {/* Any mod can do these, not limited to hierarchy*/}
693             {(amMod(this.props.moderators) || amAdmin()) && (
694               <>
695                 {this.lockButton}
696                 {this.featureButton}
697               </>
698             )}
699             {(this.canMod_ || this.canAdmin_) && <>{this.modRemoveButton}</>}
700           </>
701         )}
702         {!mobile && this.showMoreButton}
703       </>
704     );
705   }
706
707   get commentsButton() {
708     const post_view = this.postView;
709     return (
710       <Link
711         className="btn btn-link text-muted py-0 pl-0 text-muted"
712         title={i18n.t("number_of_comments", {
713           count: Number(post_view.counts.comments),
714           formattedCount: Number(post_view.counts.comments),
715         })}
716         to={`/post/${post_view.post.id}?scrollToComments=true`}
717       >
718         <Icon icon="message-square" classes="mr-1" inline />
719         <span className="mr-2">
720           {i18n.t("number_of_comments", {
721             count: Number(post_view.counts.comments),
722             formattedCount: numToSI(post_view.counts.comments),
723           })}
724         </span>
725         {this.unreadCount && (
726           <span className="small text-warning">
727             ({this.unreadCount} {i18n.t("new")})
728           </span>
729         )}
730       </Link>
731     );
732   }
733
734   get unreadCount(): number | undefined {
735     const pv = this.postView;
736     return pv.unread_comments == pv.counts.comments || pv.unread_comments == 0
737       ? undefined
738       : pv.unread_comments;
739   }
740
741   get mobileVotes() {
742     // TODO: make nicer
743     const tippy = showScores()
744       ? { "data-tippy-content": this.pointsTippy }
745       : {};
746     return (
747       <>
748         <div>
749           <button
750             className={`btn-animate btn py-0 px-1 ${
751               this.postView.my_vote === 1 ? "text-info" : "text-muted"
752             }`}
753             {...tippy}
754             onClick={linkEvent(this, this.handleUpvote)}
755             aria-label={i18n.t("upvote")}
756             aria-pressed={this.postView.my_vote === 1}
757           >
758             {this.state.upvoteLoading ? (
759               <Spinner />
760             ) : (
761               <>
762                 <Icon icon="arrow-up1" classes="icon-inline small" />
763                 {showScores() && (
764                   <span className="ml-2">
765                     {numToSI(this.postView.counts.upvotes)}
766                   </span>
767                 )}
768               </>
769             )}
770           </button>
771           {this.props.enableDownvotes && (
772             <button
773               className={`ml-2 btn-animate btn py-0 px-1 ${
774                 this.postView.my_vote === -1 ? "text-danger" : "text-muted"
775               }`}
776               onClick={linkEvent(this, this.handleDownvote)}
777               {...tippy}
778               aria-label={i18n.t("downvote")}
779               aria-pressed={this.postView.my_vote === -1}
780             >
781               {this.state.downvoteLoading ? (
782                 <Spinner />
783               ) : (
784                 <>
785                   <Icon icon="arrow-down1" classes="icon-inline small" />
786                   {showScores() && (
787                     <span
788                       className={classNames("ml-2", {
789                         invisible: this.postView.counts.downvotes === 0,
790                       })}
791                     >
792                       {numToSI(this.postView.counts.downvotes)}
793                     </span>
794                   )}
795                 </>
796               )}
797             </button>
798           )}
799         </div>
800       </>
801     );
802   }
803
804   get saveButton() {
805     const saved = this.postView.saved;
806     const label = saved ? i18n.t("unsave") : i18n.t("save");
807     return (
808       <button
809         className="btn btn-link btn-animate text-muted py-0"
810         onClick={linkEvent(this, this.handleSavePostClick)}
811         data-tippy-content={label}
812         aria-label={label}
813       >
814         {this.state.saveLoading ? (
815           <Spinner />
816         ) : (
817           <Icon
818             icon="star"
819             classes={classNames({ "text-warning": saved })}
820             inline
821           />
822         )}
823       </button>
824     );
825   }
826
827   get crossPostButton() {
828     return (
829       <Link
830         className="btn btn-link btn-animate text-muted py-0"
831         to={{
832           /* Empty string properties are required to satisfy type*/
833           pathname: "/create_post",
834           state: { ...this.crossPostParams },
835           hash: "",
836           key: "",
837           search: "",
838         }}
839         title={i18n.t("cross_post")}
840         data-tippy-content={i18n.t("cross_post")}
841         aria-label={i18n.t("cross_post")}
842       >
843         <Icon icon="copy" inline />
844       </Link>
845     );
846   }
847
848   get reportButton() {
849     return (
850       <button
851         className="btn btn-link btn-animate text-muted py-0"
852         onClick={linkEvent(this, this.handleShowReportDialog)}
853         data-tippy-content={i18n.t("show_report_dialog")}
854         aria-label={i18n.t("show_report_dialog")}
855       >
856         <Icon icon="flag" inline />
857       </button>
858     );
859   }
860
861   get blockButton() {
862     return (
863       <button
864         className="btn btn-link btn-animate text-muted py-0"
865         onClick={linkEvent(this, this.handleBlockPersonClick)}
866         data-tippy-content={i18n.t("block_user")}
867         aria-label={i18n.t("block_user")}
868       >
869         {this.state.blockLoading ? <Spinner /> : <Icon icon="slash" inline />}
870       </button>
871     );
872   }
873
874   get editButton() {
875     return (
876       <button
877         className="btn btn-link btn-animate text-muted py-0"
878         onClick={linkEvent(this, this.handleEditClick)}
879         data-tippy-content={i18n.t("edit")}
880         aria-label={i18n.t("edit")}
881       >
882         <Icon icon="edit" inline />
883       </button>
884     );
885   }
886
887   get deleteButton() {
888     const deleted = this.postView.post.deleted;
889     const label = !deleted ? i18n.t("delete") : i18n.t("restore");
890     return (
891       <button
892         className="btn btn-link btn-animate text-muted py-0"
893         onClick={linkEvent(this, this.handleDeleteClick)}
894         data-tippy-content={label}
895         aria-label={label}
896       >
897         {this.state.deleteLoading ? (
898           <Spinner />
899         ) : (
900           <Icon
901             icon="trash"
902             classes={classNames({ "text-danger": deleted })}
903             inline
904           />
905         )}
906       </button>
907     );
908   }
909
910   get showMoreButton() {
911     return (
912       <button
913         className="btn btn-link btn-animate text-muted py-0"
914         onClick={linkEvent(this, this.handleShowAdvanced)}
915         data-tippy-content={i18n.t("more")}
916         aria-label={i18n.t("more")}
917       >
918         <Icon icon="more-vertical" inline />
919       </button>
920     );
921   }
922
923   get viewSourceButton() {
924     return (
925       <button
926         className="btn btn-link btn-animate text-muted py-0"
927         onClick={linkEvent(this, this.handleViewSource)}
928         data-tippy-content={i18n.t("view_source")}
929         aria-label={i18n.t("view_source")}
930       >
931         <Icon
932           icon="file-text"
933           classes={classNames({ "text-success": this.state.viewSource })}
934           inline
935         />
936       </button>
937     );
938   }
939
940   get lockButton() {
941     const locked = this.postView.post.locked;
942     const label = locked ? i18n.t("unlock") : i18n.t("lock");
943     return (
944       <button
945         className="btn btn-link btn-animate text-muted py-0"
946         onClick={linkEvent(this, this.handleModLock)}
947         data-tippy-content={label}
948         aria-label={label}
949       >
950         {this.state.lockLoading ? (
951           <Spinner />
952         ) : (
953           <Icon
954             icon="lock"
955             classes={classNames({ "text-danger": locked })}
956             inline
957           />
958         )}
959       </button>
960     );
961   }
962
963   get featureButton() {
964     const featuredCommunity = this.postView.post.featured_community;
965     const labelCommunity = featuredCommunity
966       ? i18n.t("unfeature_from_community")
967       : i18n.t("feature_in_community");
968
969     const featuredLocal = this.postView.post.featured_local;
970     const labelLocal = featuredLocal
971       ? i18n.t("unfeature_from_local")
972       : i18n.t("feature_in_local");
973     return (
974       <span>
975         <button
976           className="btn btn-link btn-animate text-muted py-0 pl-0"
977           onClick={linkEvent(this, this.handleModFeaturePostCommunity)}
978           data-tippy-content={labelCommunity}
979           aria-label={labelCommunity}
980         >
981           {this.state.featureCommunityLoading ? (
982             <Spinner />
983           ) : (
984             <span>
985               <Icon
986                 icon="pin"
987                 classes={classNames({ "text-success": featuredCommunity })}
988                 inline
989               />
990               {i18n.t("community")}
991             </span>
992           )}
993         </button>
994         {amAdmin() && (
995           <button
996             className="btn btn-link btn-animate text-muted py-0"
997             onClick={linkEvent(this, this.handleModFeaturePostLocal)}
998             data-tippy-content={labelLocal}
999             aria-label={labelLocal}
1000           >
1001             {this.state.featureLocalLoading ? (
1002               <Spinner />
1003             ) : (
1004               <span>
1005                 <Icon
1006                   icon="pin"
1007                   classes={classNames({ "text-success": featuredLocal })}
1008                   inline
1009                 />
1010                 {i18n.t("local")}
1011               </span>
1012             )}
1013           </button>
1014         )}
1015       </span>
1016     );
1017   }
1018
1019   get modRemoveButton() {
1020     const removed = this.postView.post.removed;
1021     return (
1022       <button
1023         className="btn btn-link btn-animate text-muted py-0"
1024         onClick={linkEvent(
1025           this,
1026           !removed ? this.handleModRemoveShow : this.handleModRemoveSubmit
1027         )}
1028       >
1029         {/* TODO: Find an icon for this. */}
1030         {this.state.removeLoading ? (
1031           <Spinner />
1032         ) : !removed ? (
1033           i18n.t("remove")
1034         ) : (
1035           i18n.t("restore")
1036         )}
1037       </button>
1038     );
1039   }
1040
1041   /**
1042    * Mod/Admin actions to be taken against the author.
1043    */
1044   userActionsLine() {
1045     // TODO: make nicer
1046     const post_view = this.postView;
1047     return (
1048       this.state.showAdvanced && (
1049         <>
1050           {this.canMod_ && (
1051             <>
1052               {!this.creatorIsMod_ &&
1053                 (!post_view.creator_banned_from_community ? (
1054                   <button
1055                     className="btn btn-link btn-animate text-muted py-0"
1056                     onClick={linkEvent(
1057                       this,
1058                       this.handleModBanFromCommunityShow
1059                     )}
1060                     aria-label={i18n.t("ban_from_community")}
1061                   >
1062                     {i18n.t("ban_from_community")}
1063                   </button>
1064                 ) : (
1065                   <button
1066                     className="btn btn-link btn-animate text-muted py-0"
1067                     onClick={linkEvent(
1068                       this,
1069                       this.handleModBanFromCommunitySubmit
1070                     )}
1071                     aria-label={i18n.t("unban")}
1072                   >
1073                     {this.state.banLoading ? <Spinner /> : i18n.t("unban")}
1074                   </button>
1075                 ))}
1076               {!post_view.creator_banned_from_community && (
1077                 <button
1078                   className="btn btn-link btn-animate text-muted py-0"
1079                   onClick={linkEvent(this, this.handleAddModToCommunity)}
1080                   aria-label={
1081                     this.creatorIsMod_
1082                       ? i18n.t("remove_as_mod")
1083                       : i18n.t("appoint_as_mod")
1084                   }
1085                 >
1086                   {this.state.addModLoading ? (
1087                     <Spinner />
1088                   ) : this.creatorIsMod_ ? (
1089                     i18n.t("remove_as_mod")
1090                   ) : (
1091                     i18n.t("appoint_as_mod")
1092                   )}
1093                 </button>
1094               )}
1095             </>
1096           )}
1097           {/* Community creators and admins can transfer community to another mod */}
1098           {(amCommunityCreator(post_view.creator.id, this.props.moderators) ||
1099             this.canAdmin_) &&
1100             this.creatorIsMod_ &&
1101             (!this.state.showConfirmTransferCommunity ? (
1102               <button
1103                 className="btn btn-link btn-animate text-muted py-0"
1104                 onClick={linkEvent(
1105                   this,
1106                   this.handleShowConfirmTransferCommunity
1107                 )}
1108                 aria-label={i18n.t("transfer_community")}
1109               >
1110                 {i18n.t("transfer_community")}
1111               </button>
1112             ) : (
1113               <>
1114                 <button
1115                   className="d-inline-block mr-1 btn btn-link btn-animate text-muted py-0"
1116                   aria-label={i18n.t("are_you_sure")}
1117                 >
1118                   {i18n.t("are_you_sure")}
1119                 </button>
1120                 <button
1121                   className="btn btn-link btn-animate text-muted py-0 d-inline-block mr-1"
1122                   aria-label={i18n.t("yes")}
1123                   onClick={linkEvent(this, this.handleTransferCommunity)}
1124                 >
1125                   {this.state.transferLoading ? <Spinner /> : i18n.t("yes")}
1126                 </button>
1127                 <button
1128                   className="btn btn-link btn-animate text-muted py-0 d-inline-block"
1129                   onClick={linkEvent(
1130                     this,
1131                     this.handleCancelShowConfirmTransferCommunity
1132                   )}
1133                   aria-label={i18n.t("no")}
1134                 >
1135                   {i18n.t("no")}
1136                 </button>
1137               </>
1138             ))}
1139           {/* Admins can ban from all, and appoint other admins */}
1140           {this.canAdmin_ && (
1141             <>
1142               {!this.creatorIsAdmin_ && (
1143                 <>
1144                   {!isBanned(post_view.creator) ? (
1145                     <button
1146                       className="btn btn-link btn-animate text-muted py-0"
1147                       onClick={linkEvent(this, this.handleModBanShow)}
1148                       aria-label={i18n.t("ban_from_site")}
1149                     >
1150                       {i18n.t("ban_from_site")}
1151                     </button>
1152                   ) : (
1153                     <button
1154                       className="btn btn-link btn-animate text-muted py-0"
1155                       onClick={linkEvent(this, this.handleModBanSubmit)}
1156                       aria-label={i18n.t("unban_from_site")}
1157                     >
1158                       {this.state.banLoading ? (
1159                         <Spinner />
1160                       ) : (
1161                         i18n.t("unban_from_site")
1162                       )}
1163                     </button>
1164                   )}
1165                   <button
1166                     className="btn btn-link btn-animate text-muted py-0"
1167                     onClick={linkEvent(this, this.handlePurgePersonShow)}
1168                     aria-label={i18n.t("purge_user")}
1169                   >
1170                     {i18n.t("purge_user")}
1171                   </button>
1172                   <button
1173                     className="btn btn-link btn-animate text-muted py-0"
1174                     onClick={linkEvent(this, this.handlePurgePostShow)}
1175                     aria-label={i18n.t("purge_post")}
1176                   >
1177                     {i18n.t("purge_post")}
1178                   </button>
1179                 </>
1180               )}
1181               {!isBanned(post_view.creator) && post_view.creator.local && (
1182                 <button
1183                   className="btn btn-link btn-animate text-muted py-0"
1184                   onClick={linkEvent(this, this.handleAddAdmin)}
1185                   aria-label={
1186                     this.creatorIsAdmin_
1187                       ? i18n.t("remove_as_admin")
1188                       : i18n.t("appoint_as_admin")
1189                   }
1190                 >
1191                   {this.state.addAdminLoading ? (
1192                     <Spinner />
1193                   ) : this.creatorIsAdmin_ ? (
1194                     i18n.t("remove_as_admin")
1195                   ) : (
1196                     i18n.t("appoint_as_admin")
1197                   )}
1198                 </button>
1199               )}
1200             </>
1201           )}
1202         </>
1203       )
1204     );
1205   }
1206
1207   removeAndBanDialogs() {
1208     const post = this.postView;
1209     const purgeTypeText =
1210       this.state.purgeType == PurgeType.Post
1211         ? i18n.t("purge_post")
1212         : `${i18n.t("purge")} ${post.creator.name}`;
1213     return (
1214       <>
1215         {this.state.showRemoveDialog && (
1216           <form
1217             className="form-inline"
1218             onSubmit={linkEvent(this, this.handleModRemoveSubmit)}
1219           >
1220             <label className="sr-only" htmlFor="post-listing-remove-reason">
1221               {i18n.t("reason")}
1222             </label>
1223             <input
1224               type="text"
1225               id="post-listing-remove-reason"
1226               className="form-control mr-2"
1227               placeholder={i18n.t("reason")}
1228               value={this.state.removeReason}
1229               onInput={linkEvent(this, this.handleModRemoveReasonChange)}
1230             />
1231             <button
1232               type="submit"
1233               className="btn btn-secondary"
1234               aria-label={i18n.t("remove_post")}
1235             >
1236               {this.state.removeLoading ? <Spinner /> : i18n.t("remove_post")}
1237             </button>
1238           </form>
1239         )}
1240         {this.state.showBanDialog && (
1241           <form onSubmit={linkEvent(this, this.handleModBanBothSubmit)}>
1242             <div className="form-group row col-12">
1243               <label
1244                 className="col-form-label"
1245                 htmlFor="post-listing-ban-reason"
1246               >
1247                 {i18n.t("reason")}
1248               </label>
1249               <input
1250                 type="text"
1251                 id="post-listing-ban-reason"
1252                 className="form-control mr-2"
1253                 placeholder={i18n.t("reason")}
1254                 value={this.state.banReason}
1255                 onInput={linkEvent(this, this.handleModBanReasonChange)}
1256               />
1257               <label className="col-form-label" htmlFor={`mod-ban-expires`}>
1258                 {i18n.t("expires")}
1259               </label>
1260               <input
1261                 type="number"
1262                 id={`mod-ban-expires`}
1263                 className="form-control mr-2"
1264                 placeholder={i18n.t("number_of_days")}
1265                 value={this.state.banExpireDays}
1266                 onInput={linkEvent(this, this.handleModBanExpireDaysChange)}
1267               />
1268               <div className="form-group">
1269                 <div className="form-check">
1270                   <input
1271                     className="form-check-input"
1272                     id="mod-ban-remove-data"
1273                     type="checkbox"
1274                     checked={this.state.removeData}
1275                     onChange={linkEvent(this, this.handleModRemoveDataChange)}
1276                   />
1277                   <label
1278                     className="form-check-label"
1279                     htmlFor="mod-ban-remove-data"
1280                     title={i18n.t("remove_content_more")}
1281                   >
1282                     {i18n.t("remove_content")}
1283                   </label>
1284                 </div>
1285               </div>
1286             </div>
1287             {/* TODO hold off on expires until later */}
1288             {/* <div class="form-group row"> */}
1289             {/*   <label class="col-form-label">Expires</label> */}
1290             {/*   <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.banExpires} onInput={linkEvent(this, this.handleModBanExpiresChange)} /> */}
1291             {/* </div> */}
1292             <div className="form-group row">
1293               <button
1294                 type="submit"
1295                 className="btn btn-secondary"
1296                 aria-label={i18n.t("ban")}
1297               >
1298                 {this.state.banLoading ? (
1299                   <Spinner />
1300                 ) : (
1301                   <span>
1302                     {i18n.t("ban")} {post.creator.name}
1303                   </span>
1304                 )}
1305               </button>
1306             </div>
1307           </form>
1308         )}
1309         {this.state.showReportDialog && (
1310           <form
1311             className="form-inline"
1312             onSubmit={linkEvent(this, this.handleReportSubmit)}
1313           >
1314             <label className="sr-only" htmlFor="post-report-reason">
1315               {i18n.t("reason")}
1316             </label>
1317             <input
1318               type="text"
1319               id="post-report-reason"
1320               className="form-control mr-2"
1321               placeholder={i18n.t("reason")}
1322               required
1323               value={this.state.reportReason}
1324               onInput={linkEvent(this, this.handleReportReasonChange)}
1325             />
1326             <button
1327               type="submit"
1328               className="btn btn-secondary"
1329               aria-label={i18n.t("create_report")}
1330             >
1331               {this.state.reportLoading ? <Spinner /> : i18n.t("create_report")}
1332             </button>
1333           </form>
1334         )}
1335         {this.state.showPurgeDialog && (
1336           <form
1337             className="form-inline"
1338             onSubmit={linkEvent(this, this.handlePurgeSubmit)}
1339           >
1340             <PurgeWarning />
1341             <label className="sr-only" htmlFor="purge-reason">
1342               {i18n.t("reason")}
1343             </label>
1344             <input
1345               type="text"
1346               id="purge-reason"
1347               className="form-control mr-2"
1348               placeholder={i18n.t("reason")}
1349               value={this.state.purgeReason}
1350               onInput={linkEvent(this, this.handlePurgeReasonChange)}
1351             />
1352             {this.state.purgeLoading ? (
1353               <Spinner />
1354             ) : (
1355               <button
1356                 type="submit"
1357                 className="btn btn-secondary"
1358                 aria-label={purgeTypeText}
1359               >
1360                 {this.state.purgeLoading ? <Spinner /> : { purgeTypeText }}
1361               </button>
1362             )}
1363           </form>
1364         )}
1365       </>
1366     );
1367   }
1368
1369   mobileThumbnail() {
1370     const post = this.postView.post;
1371     return post.thumbnail_url || (post.url && isImage(post.url)) ? (
1372       <div className="row">
1373         <div className={`${this.state.imageExpanded ? "col-12" : "col-8"}`}>
1374           {this.postTitleLine()}
1375         </div>
1376         <div className="col-4">
1377           {/* Post body prev or thumbnail */}
1378           {!this.state.imageExpanded && this.thumbnail()}
1379         </div>
1380       </div>
1381     ) : (
1382       this.postTitleLine()
1383     );
1384   }
1385
1386   showMobilePreview() {
1387     const { body, id } = this.postView.post;
1388
1389     return !this.showBody && body ? (
1390       <Link className="text-body" to={`/post/${id}`}>
1391         <div className="md-div mb-1 preview-lines">{body}</div>
1392       </Link>
1393     ) : (
1394       <></>
1395     );
1396   }
1397
1398   listing() {
1399     return (
1400       <>
1401         {/* The mobile view*/}
1402         <div className="d-block d-sm-none">
1403           <article className="row post-container">
1404             <div className="col-12">
1405               {this.createdLine()}
1406
1407               {/* If it has a thumbnail, do a right aligned thumbnail */}
1408               {this.mobileThumbnail()}
1409
1410               {/* Show a preview of the post body */}
1411               {this.showMobilePreview()}
1412
1413               {this.commentsLine(true)}
1414               {this.userActionsLine()}
1415               {this.duplicatesLine()}
1416               {this.removeAndBanDialogs()}
1417             </div>
1418           </article>
1419         </div>
1420
1421         {/* The larger view*/}
1422         <div className="d-none d-sm-block">
1423           <article className="row post-container">
1424             {!this.props.viewOnly && this.voteBar()}
1425             <div className="col-sm-2 pr-0 post-media">
1426               <div className="">{this.thumbnail()}</div>
1427             </div>
1428             <div className="col-12 col-sm-9">
1429               <div className="row">
1430                 <div className="col-12">
1431                   {this.postTitleLine()}
1432                   {this.createdLine()}
1433                   {this.commentsLine()}
1434                   {this.duplicatesLine()}
1435                   {this.userActionsLine()}
1436                   {this.removeAndBanDialogs()}
1437                 </div>
1438               </div>
1439             </div>
1440           </article>
1441         </div>
1442       </>
1443     );
1444   }
1445
1446   private get myPost(): boolean {
1447     return (
1448       this.postView.creator.id ==
1449       UserService.Instance.myUserInfo?.local_user_view.person.id
1450     );
1451   }
1452   handleEditClick(i: PostListing) {
1453     i.setState({ showEdit: true });
1454   }
1455
1456   handleEditCancel() {
1457     this.setState({ showEdit: false });
1458   }
1459
1460   // The actual editing is done in the receive for post
1461   handleEditPost(form: EditPost) {
1462     this.setState({ showEdit: false });
1463     this.props.onPostEdit(form);
1464   }
1465
1466   handleShare(i: PostListing) {
1467     const { name, body, id } = i.props.post_view.post;
1468     share({
1469       title: name,
1470       text: body?.slice(0, 50),
1471       url: `${getHttpBase()}/post/${id}`,
1472     });
1473   }
1474
1475   handleShowReportDialog(i: PostListing) {
1476     i.setState({ showReportDialog: !i.state.showReportDialog });
1477   }
1478
1479   handleReportReasonChange(i: PostListing, event: any) {
1480     i.setState({ reportReason: event.target.value });
1481   }
1482
1483   handleReportSubmit(i: PostListing, event: any) {
1484     event.preventDefault();
1485     i.setState({ reportLoading: true });
1486     i.props.onPostReport({
1487       post_id: i.postView.post.id,
1488       reason: i.state.reportReason ?? "",
1489       auth: myAuthRequired(),
1490     });
1491   }
1492
1493   handleBlockPersonClick(i: PostListing) {
1494     i.setState({ blockLoading: true });
1495     i.props.onBlockPerson({
1496       person_id: i.postView.creator.id,
1497       block: true,
1498       auth: myAuthRequired(),
1499     });
1500   }
1501
1502   handleDeleteClick(i: PostListing) {
1503     i.setState({ deleteLoading: true });
1504     i.props.onDeletePost({
1505       post_id: i.postView.post.id,
1506       deleted: !i.postView.post.deleted,
1507       auth: myAuthRequired(),
1508     });
1509   }
1510
1511   handleSavePostClick(i: PostListing) {
1512     i.setState({ saveLoading: true });
1513     i.props.onSavePost({
1514       post_id: i.postView.post.id,
1515       save: !i.postView.saved,
1516       auth: myAuthRequired(),
1517     });
1518   }
1519
1520   get crossPostParams(): PostFormParams {
1521     const queryParams: PostFormParams = {};
1522     const { name, url } = this.postView.post;
1523
1524     queryParams.name = name;
1525
1526     if (url) {
1527       queryParams.url = url;
1528     }
1529
1530     const crossPostBody = this.crossPostBody();
1531     if (crossPostBody) {
1532       queryParams.body = crossPostBody;
1533     }
1534
1535     return queryParams;
1536   }
1537
1538   crossPostBody(): string | undefined {
1539     const post = this.postView.post;
1540     const body = post.body;
1541
1542     return body
1543       ? `${i18n.t("cross_posted_from")} ${post.ap_id}\n\n${body.replace(
1544           /^/gm,
1545           "> "
1546         )}`
1547       : undefined;
1548   }
1549
1550   get showBody(): boolean {
1551     return this.props.showBody || this.state.showBody;
1552   }
1553
1554   handleModRemoveShow(i: PostListing) {
1555     i.setState({
1556       showRemoveDialog: !i.state.showRemoveDialog,
1557       showBanDialog: false,
1558     });
1559   }
1560
1561   handleModRemoveReasonChange(i: PostListing, event: any) {
1562     i.setState({ removeReason: event.target.value });
1563   }
1564
1565   handleModRemoveDataChange(i: PostListing, event: any) {
1566     i.setState({ removeData: event.target.checked });
1567   }
1568
1569   handleModRemoveSubmit(i: PostListing, event: any) {
1570     event.preventDefault();
1571     i.setState({ removeLoading: true });
1572     i.props.onRemovePost({
1573       post_id: i.postView.post.id,
1574       removed: !i.postView.post.removed,
1575       auth: myAuthRequired(),
1576     });
1577   }
1578
1579   handleModLock(i: PostListing) {
1580     i.setState({ lockLoading: true });
1581     i.props.onLockPost({
1582       post_id: i.postView.post.id,
1583       locked: !i.postView.post.locked,
1584       auth: myAuthRequired(),
1585     });
1586   }
1587
1588   handleModFeaturePostLocal(i: PostListing) {
1589     i.setState({ featureLocalLoading: true });
1590     i.props.onFeaturePost({
1591       post_id: i.postView.post.id,
1592       featured: !i.postView.post.featured_local,
1593       feature_type: "Local",
1594       auth: myAuthRequired(),
1595     });
1596   }
1597
1598   handleModFeaturePostCommunity(i: PostListing) {
1599     i.setState({ featureCommunityLoading: true });
1600     i.props.onFeaturePost({
1601       post_id: i.postView.post.id,
1602       featured: !i.postView.post.featured_community,
1603       feature_type: "Community",
1604       auth: myAuthRequired(),
1605     });
1606   }
1607
1608   handleModBanFromCommunityShow(i: PostListing) {
1609     i.setState({
1610       showBanDialog: true,
1611       banType: BanType.Community,
1612       showRemoveDialog: false,
1613     });
1614   }
1615
1616   handleModBanShow(i: PostListing) {
1617     i.setState({
1618       showBanDialog: true,
1619       banType: BanType.Site,
1620       showRemoveDialog: false,
1621     });
1622   }
1623
1624   handlePurgePersonShow(i: PostListing) {
1625     i.setState({
1626       showPurgeDialog: true,
1627       purgeType: PurgeType.Person,
1628       showRemoveDialog: false,
1629     });
1630   }
1631
1632   handlePurgePostShow(i: PostListing) {
1633     i.setState({
1634       showPurgeDialog: true,
1635       purgeType: PurgeType.Post,
1636       showRemoveDialog: false,
1637     });
1638   }
1639
1640   handlePurgeReasonChange(i: PostListing, event: any) {
1641     i.setState({ purgeReason: event.target.value });
1642   }
1643
1644   handlePurgeSubmit(i: PostListing, event: any) {
1645     event.preventDefault();
1646     i.setState({ purgeLoading: true });
1647     if (i.state.purgeType == PurgeType.Person) {
1648       i.props.onPurgePerson({
1649         person_id: i.postView.creator.id,
1650         reason: i.state.purgeReason,
1651         auth: myAuthRequired(),
1652       });
1653     } else if (i.state.purgeType == PurgeType.Post) {
1654       i.props.onPurgePost({
1655         post_id: i.postView.post.id,
1656         reason: i.state.purgeReason,
1657         auth: myAuthRequired(),
1658       });
1659     }
1660   }
1661
1662   handleModBanReasonChange(i: PostListing, event: any) {
1663     i.setState({ banReason: event.target.value });
1664   }
1665
1666   handleModBanExpireDaysChange(i: PostListing, event: any) {
1667     i.setState({ banExpireDays: event.target.value });
1668   }
1669
1670   handleModBanFromCommunitySubmit(i: PostListing, event: any) {
1671     i.setState({ banType: BanType.Community });
1672     i.handleModBanBothSubmit(i, event);
1673   }
1674
1675   handleModBanSubmit(i: PostListing, event: any) {
1676     i.setState({ banType: BanType.Site });
1677     i.handleModBanBothSubmit(i, event);
1678   }
1679
1680   handleModBanBothSubmit(i: PostListing, event: any) {
1681     event.preventDefault();
1682     i.setState({ banLoading: true });
1683
1684     const ban = !i.props.post_view.creator_banned_from_community;
1685     // If its an unban, restore all their data
1686     if (ban == false) {
1687       i.setState({ removeData: false });
1688     }
1689     const person_id = i.props.post_view.creator.id;
1690     const remove_data = i.state.removeData;
1691     const reason = i.state.banReason;
1692     const expires = futureDaysToUnixTime(i.state.banExpireDays);
1693
1694     if (i.state.banType == BanType.Community) {
1695       const community_id = i.postView.community.id;
1696       i.props.onBanPersonFromCommunity({
1697         community_id,
1698         person_id,
1699         ban,
1700         remove_data,
1701         reason,
1702         expires,
1703         auth: myAuthRequired(),
1704       });
1705     } else {
1706       i.props.onBanPerson({
1707         person_id,
1708         ban,
1709         remove_data,
1710         reason,
1711         expires,
1712         auth: myAuthRequired(),
1713       });
1714     }
1715   }
1716
1717   handleAddModToCommunity(i: PostListing) {
1718     i.setState({ addModLoading: true });
1719     i.props.onAddModToCommunity({
1720       community_id: i.postView.community.id,
1721       person_id: i.postView.creator.id,
1722       added: !i.creatorIsMod_,
1723       auth: myAuthRequired(),
1724     });
1725   }
1726
1727   handleAddAdmin(i: PostListing) {
1728     i.setState({ addAdminLoading: true });
1729     i.props.onAddAdmin({
1730       person_id: i.postView.creator.id,
1731       added: !i.creatorIsAdmin_,
1732       auth: myAuthRequired(),
1733     });
1734   }
1735
1736   handleShowConfirmTransferCommunity(i: PostListing) {
1737     i.setState({ showConfirmTransferCommunity: true });
1738   }
1739
1740   handleCancelShowConfirmTransferCommunity(i: PostListing) {
1741     i.setState({ showConfirmTransferCommunity: false });
1742   }
1743
1744   handleTransferCommunity(i: PostListing) {
1745     i.setState({ transferLoading: true });
1746     i.props.onTransferCommunity({
1747       community_id: i.postView.community.id,
1748       person_id: i.postView.creator.id,
1749       auth: myAuthRequired(),
1750     });
1751   }
1752
1753   handleShowConfirmTransferSite(i: PostListing) {
1754     i.setState({ showConfirmTransferSite: true });
1755   }
1756
1757   handleCancelShowConfirmTransferSite(i: PostListing) {
1758     i.setState({ showConfirmTransferSite: false });
1759   }
1760
1761   handleImageExpandClick(i: PostListing, event: any) {
1762     event.preventDefault();
1763     i.setState({ imageExpanded: !i.state.imageExpanded });
1764     setupTippy();
1765   }
1766
1767   handleViewSource(i: PostListing) {
1768     i.setState({ viewSource: !i.state.viewSource });
1769   }
1770
1771   handleShowAdvanced(i: PostListing) {
1772     i.setState({ showAdvanced: !i.state.showAdvanced });
1773     setupTippy();
1774   }
1775
1776   handleShowMoreMobile(i: PostListing) {
1777     i.setState({
1778       showMoreMobile: !i.state.showMoreMobile,
1779       showAdvanced: !i.state.showAdvanced,
1780     });
1781     setupTippy();
1782   }
1783
1784   handleShowBody(i: PostListing) {
1785     i.setState({ showBody: !i.state.showBody });
1786     setupTippy();
1787   }
1788
1789   handleUpvote(i: PostListing) {
1790     i.setState({ upvoteLoading: true });
1791     i.props.onPostVote({
1792       post_id: i.postView.post.id,
1793       score: newVote(VoteType.Upvote, i.props.post_view.my_vote),
1794       auth: myAuthRequired(),
1795     });
1796   }
1797
1798   handleDownvote(i: PostListing) {
1799     i.setState({ downvoteLoading: true });
1800     i.props.onPostVote({
1801       post_id: i.postView.post.id,
1802       score: newVote(VoteType.Downvote, i.props.post_view.my_vote),
1803       auth: myAuthRequired(),
1804     });
1805   }
1806
1807   get pointsTippy(): string {
1808     const points = i18n.t("number_of_points", {
1809       count: Number(this.postView.counts.score),
1810       formattedCount: Number(this.postView.counts.score),
1811     });
1812
1813     const upvotes = i18n.t("number_of_upvotes", {
1814       count: Number(this.postView.counts.upvotes),
1815       formattedCount: Number(this.postView.counts.upvotes),
1816     });
1817
1818     const downvotes = i18n.t("number_of_downvotes", {
1819       count: Number(this.postView.counts.downvotes),
1820       formattedCount: Number(this.postView.counts.downvotes),
1821     });
1822
1823     return `${points} • ${upvotes} • ${downvotes}`;
1824   }
1825
1826   get canModOnSelf_(): boolean {
1827     return canMod(
1828       this.postView.creator.id,
1829       this.props.moderators,
1830       this.props.admins,
1831       undefined,
1832       true
1833     );
1834   }
1835
1836   get canMod_(): boolean {
1837     return canMod(
1838       this.postView.creator.id,
1839       this.props.moderators,
1840       this.props.admins
1841     );
1842   }
1843
1844   get canAdmin_(): boolean {
1845     return canAdmin(this.postView.creator.id, this.props.admins);
1846   }
1847
1848   get creatorIsMod_(): boolean {
1849     return isMod(this.postView.creator.id, this.props.moderators);
1850   }
1851
1852   get creatorIsAdmin_(): boolean {
1853     return isAdmin(this.postView.creator.id, this.props.admins);
1854   }
1855 }