]> Untitled Git - lemmy-ui.git/blob - src/shared/components/post/post-listing.tsx
fix(a11y): Add aria-controls for advanced button dropdown
[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()}
660       </div>
661     );
662   }
663
664   get hasAdvancedButtons() {
665     return (
666       this.myPost ||
667       (this.showBody && this.postView.post.body) ||
668       amMod(this.props.moderators) ||
669       amAdmin() ||
670       this.canMod_ ||
671       this.canAdmin_
672     );
673   }
674
675   postActions() {
676     // Possible enhancement: Priority+ pattern instead of just hard coding which get hidden behind the show more button.
677     // Possible enhancement: Make each button a component.
678     const post_view = this.postView;
679     return (
680       <>
681         {this.saveButton}
682         {this.crossPostButton}
683
684         {this.showBody && post_view.post.body && this.viewSourceButton}
685
686         {this.hasAdvancedButtons && (
687           <div className="dropdown">
688             <button
689               className="btn btn-link btn-animate text-muted py-0 dropdown-toggle"
690               onClick={linkEvent(this, this.handleShowAdvanced)}
691               data-tippy-content={i18n.t("more")}
692               data-bs-toggle="dropdown"
693               aria-expanded="false"
694               aria-controls="advancedButtonsDropdown"
695               aria-label={i18n.t("more")}
696             >
697               <Icon icon="more-vertical" inline />
698             </button>
699
700             <ul className="dropdown-menu" id="advancedButtonsDropdown">
701               {!this.myPost ? (
702                 <>
703                   <li>{this.reportButton}</li>
704                   <li>{this.blockButton}</li>
705                 </>
706               ) : (
707                 <>
708                   <li>{this.editButton}</li>
709                   <li>{this.deleteButton}</li>
710                 </>
711               )}
712
713               {/* Any mod can do these, not limited to hierarchy*/}
714               {(amMod(this.props.moderators) || amAdmin()) && (
715                 <>
716                   <li>
717                     <hr className="dropdown-divider" />
718                   </li>
719                   <li>{this.lockButton}</li>
720                   {this.featureButtons}
721                 </>
722               )}
723
724               {(this.canMod_ || this.canAdmin_) && (
725                 <li>{this.modRemoveButton}</li>
726               )}
727             </ul>
728           </div>
729         )}
730       </>
731     );
732   }
733
734   get commentsButton() {
735     const post_view = this.postView;
736     return (
737       <Link
738         className="btn btn-link text-muted py-0 pl-0 text-muted"
739         title={i18n.t("number_of_comments", {
740           count: Number(post_view.counts.comments),
741           formattedCount: Number(post_view.counts.comments),
742         })}
743         to={`/post/${post_view.post.id}?scrollToComments=true`}
744       >
745         <Icon icon="message-square" classes="mr-1" inline />
746         <span className="mr-2">
747           {i18n.t("number_of_comments", {
748             count: Number(post_view.counts.comments),
749             formattedCount: numToSI(post_view.counts.comments),
750           })}
751         </span>
752         {this.unreadCount && (
753           <span className="small text-warning">
754             ({this.unreadCount} {i18n.t("new")})
755           </span>
756         )}
757       </Link>
758     );
759   }
760
761   get unreadCount(): number | undefined {
762     const pv = this.postView;
763     return pv.unread_comments == pv.counts.comments || pv.unread_comments == 0
764       ? undefined
765       : pv.unread_comments;
766   }
767
768   get mobileVotes() {
769     // TODO: make nicer
770     const tippy = showScores()
771       ? { "data-tippy-content": this.pointsTippy }
772       : {};
773     return (
774       <>
775         <div>
776           <button
777             className={`btn-animate btn py-0 px-1 ${
778               this.postView.my_vote === 1 ? "text-info" : "text-muted"
779             }`}
780             {...tippy}
781             onClick={linkEvent(this, this.handleUpvote)}
782             aria-label={i18n.t("upvote")}
783             aria-pressed={this.postView.my_vote === 1}
784           >
785             {this.state.upvoteLoading ? (
786               <Spinner />
787             ) : (
788               <>
789                 <Icon icon="arrow-up1" classes="icon-inline small" />
790                 {showScores() && (
791                   <span className="ml-2">
792                     {numToSI(this.postView.counts.upvotes)}
793                   </span>
794                 )}
795               </>
796             )}
797           </button>
798           {this.props.enableDownvotes && (
799             <button
800               className={`ml-2 btn-animate btn py-0 px-1 ${
801                 this.postView.my_vote === -1 ? "text-danger" : "text-muted"
802               }`}
803               onClick={linkEvent(this, this.handleDownvote)}
804               {...tippy}
805               aria-label={i18n.t("downvote")}
806               aria-pressed={this.postView.my_vote === -1}
807             >
808               {this.state.downvoteLoading ? (
809                 <Spinner />
810               ) : (
811                 <>
812                   <Icon icon="arrow-down1" classes="icon-inline small" />
813                   {showScores() && (
814                     <span
815                       className={classNames("ml-2", {
816                         invisible: this.postView.counts.downvotes === 0,
817                       })}
818                     >
819                       {numToSI(this.postView.counts.downvotes)}
820                     </span>
821                   )}
822                 </>
823               )}
824             </button>
825           )}
826         </div>
827       </>
828     );
829   }
830
831   get saveButton() {
832     const saved = this.postView.saved;
833     const label = saved ? i18n.t("unsave") : i18n.t("save");
834     return (
835       <button
836         className="btn btn-link btn-animate text-muted py-0"
837         onClick={linkEvent(this, this.handleSavePostClick)}
838         data-tippy-content={label}
839         aria-label={label}
840       >
841         {this.state.saveLoading ? (
842           <Spinner />
843         ) : (
844           <Icon
845             icon="star"
846             classes={classNames({ "text-warning": saved })}
847             inline
848           />
849         )}
850       </button>
851     );
852   }
853
854   get crossPostButton() {
855     return (
856       <Link
857         className="btn btn-link btn-animate text-muted py-0"
858         to={{
859           /* Empty string properties are required to satisfy type*/
860           pathname: "/create_post",
861           state: { ...this.crossPostParams },
862           hash: "",
863           key: "",
864           search: "",
865         }}
866         title={i18n.t("cross_post")}
867         data-tippy-content={i18n.t("cross_post")}
868         aria-label={i18n.t("cross_post")}
869       >
870         <Icon icon="copy" inline />
871       </Link>
872     );
873   }
874
875   get reportButton() {
876     return (
877       <button
878         className="btn btn-link btn-animate text-muted py-0 dropdown-item"
879         onClick={linkEvent(this, this.handleShowReportDialog)}
880         data-tippy-content={i18n.t("show_report_dialog")}
881         aria-label={i18n.t("show_report_dialog")}
882       >
883         <Icon icon="flag" inline />
884       </button>
885     );
886   }
887
888   get blockButton() {
889     return (
890       <button
891         className="btn btn-link btn-animate text-muted py-0 dropdown-item"
892         onClick={linkEvent(this, this.handleBlockPersonClick)}
893         data-tippy-content={i18n.t("block_user")}
894         aria-label={i18n.t("block_user")}
895       >
896         {this.state.blockLoading ? <Spinner /> : <Icon icon="slash" inline />}
897       </button>
898     );
899   }
900
901   get editButton() {
902     return (
903       <button
904         className="btn btn-link text-muted dropdown-item"
905         onClick={linkEvent(this, this.handleEditClick)}
906         aria-label={i18n.t("edit")}
907       >
908         <Icon classes="mr-1" icon="edit" inline />
909         {i18n.t("edit")}
910       </button>
911     );
912   }
913
914   get deleteButton() {
915     const deleted = this.postView.post.deleted;
916     const label = !deleted ? i18n.t("delete") : i18n.t("restore");
917     return (
918       <button
919         className="btn btn-link text-muted dropdown-item"
920         onClick={linkEvent(this, this.handleDeleteClick)}
921         aria-label={label}
922       >
923         {this.state.deleteLoading ? (
924           <Spinner />
925         ) : (
926           <>
927             <Icon
928               icon="trash"
929               classes={classNames("mr-1", { "text-danger": deleted })}
930               inline
931             />
932             {label}
933           </>
934         )}
935       </button>
936     );
937   }
938
939   get viewSourceButton() {
940     return (
941       <button
942         className="btn btn-link btn-animate text-muted py-0"
943         onClick={linkEvent(this, this.handleViewSource)}
944         data-tippy-content={i18n.t("view_source")}
945         aria-label={i18n.t("view_source")}
946       >
947         <Icon
948           icon="file-text"
949           classes={classNames({ "text-success": this.state.viewSource })}
950           inline
951         />
952       </button>
953     );
954   }
955
956   get lockButton() {
957     const locked = this.postView.post.locked;
958     const label = locked ? i18n.t("unlock") : i18n.t("lock");
959     return (
960       <button
961         className="btn btn-link text-muted dropdown-item"
962         onClick={linkEvent(this, this.handleModLock)}
963         aria-label={label}
964       >
965         {this.state.lockLoading ? (
966           <Spinner />
967         ) : (
968           <>
969             <Icon
970               icon="lock"
971               classes={classNames("mr-1", { "text-danger": locked })}
972               inline
973             />
974             {label}
975           </>
976         )}
977       </button>
978     );
979   }
980
981   get featureButtons() {
982     const featuredCommunity = this.postView.post.featured_community;
983     const labelCommunity = featuredCommunity
984       ? i18n.t("unfeature_from_community")
985       : i18n.t("feature_in_community");
986
987     const featuredLocal = this.postView.post.featured_local;
988     const labelLocal = featuredLocal
989       ? i18n.t("unfeature_from_local")
990       : i18n.t("feature_in_local");
991     return (
992       <>
993         <li>
994           <button
995             className="btn btn-link text-muted dropdown-item"
996             onClick={linkEvent(this, this.handleModFeaturePostCommunity)}
997             data-tippy-content={labelCommunity}
998             aria-label={labelCommunity}
999           >
1000             {this.state.featureCommunityLoading ? (
1001               <Spinner />
1002             ) : (
1003               <>
1004                 <Icon
1005                   icon="pin"
1006                   classes={classNames("mr-1", {
1007                     "text-success": featuredCommunity,
1008                   })}
1009                   inline
1010                 />
1011                 {i18n.t("community")}
1012               </>
1013             )}
1014           </button>
1015         </li>
1016         <li>
1017           {amAdmin() && (
1018             <button
1019               className="btn btn-link text-muted dropdown-item"
1020               onClick={linkEvent(this, this.handleModFeaturePostLocal)}
1021               data-tippy-content={labelLocal}
1022               aria-label={labelLocal}
1023             >
1024               {this.state.featureLocalLoading ? (
1025                 <Spinner />
1026               ) : (
1027                 <>
1028                   <Icon
1029                     icon="pin"
1030                     classes={classNames("mr-1", {
1031                       "text-success": featuredLocal,
1032                     })}
1033                     inline
1034                   />
1035                   {i18n.t("local")}
1036                 </>
1037               )}
1038             </button>
1039           )}
1040         </li>
1041       </>
1042     );
1043   }
1044
1045   get modRemoveButton() {
1046     const removed = this.postView.post.removed;
1047     return (
1048       <button
1049         className="btn btn-link text-muted dropdown-item"
1050         onClick={linkEvent(
1051           this,
1052           !removed ? this.handleModRemoveShow : this.handleModRemoveSubmit
1053         )}
1054       >
1055         {/* TODO: Find an icon for this. */}
1056         {this.state.removeLoading ? (
1057           <Spinner />
1058         ) : !removed ? (
1059           i18n.t("remove")
1060         ) : (
1061           i18n.t("restore")
1062         )}
1063       </button>
1064     );
1065   }
1066
1067   /**
1068    * Mod/Admin actions to be taken against the author.
1069    */
1070   userActionsLine() {
1071     // TODO: make nicer
1072     const post_view = this.postView;
1073     return (
1074       this.state.showAdvanced && (
1075         <>
1076           {this.canMod_ && (
1077             <>
1078               {!this.creatorIsMod_ &&
1079                 (!post_view.creator_banned_from_community ? (
1080                   <button
1081                     className="btn btn-link btn-animate text-muted py-0"
1082                     onClick={linkEvent(
1083                       this,
1084                       this.handleModBanFromCommunityShow
1085                     )}
1086                     aria-label={i18n.t("ban_from_community")}
1087                   >
1088                     {i18n.t("ban_from_community")}
1089                   </button>
1090                 ) : (
1091                   <button
1092                     className="btn btn-link btn-animate text-muted py-0"
1093                     onClick={linkEvent(
1094                       this,
1095                       this.handleModBanFromCommunitySubmit
1096                     )}
1097                     aria-label={i18n.t("unban")}
1098                   >
1099                     {this.state.banLoading ? <Spinner /> : i18n.t("unban")}
1100                   </button>
1101                 ))}
1102               {!post_view.creator_banned_from_community && (
1103                 <button
1104                   className="btn btn-link btn-animate text-muted py-0"
1105                   onClick={linkEvent(this, this.handleAddModToCommunity)}
1106                   aria-label={
1107                     this.creatorIsMod_
1108                       ? i18n.t("remove_as_mod")
1109                       : i18n.t("appoint_as_mod")
1110                   }
1111                 >
1112                   {this.state.addModLoading ? (
1113                     <Spinner />
1114                   ) : this.creatorIsMod_ ? (
1115                     i18n.t("remove_as_mod")
1116                   ) : (
1117                     i18n.t("appoint_as_mod")
1118                   )}
1119                 </button>
1120               )}
1121             </>
1122           )}
1123           {/* Community creators and admins can transfer community to another mod */}
1124           {(amCommunityCreator(post_view.creator.id, this.props.moderators) ||
1125             this.canAdmin_) &&
1126             this.creatorIsMod_ &&
1127             (!this.state.showConfirmTransferCommunity ? (
1128               <button
1129                 className="btn btn-link btn-animate text-muted py-0"
1130                 onClick={linkEvent(
1131                   this,
1132                   this.handleShowConfirmTransferCommunity
1133                 )}
1134                 aria-label={i18n.t("transfer_community")}
1135               >
1136                 {i18n.t("transfer_community")}
1137               </button>
1138             ) : (
1139               <>
1140                 <button
1141                   className="d-inline-block mr-1 btn btn-link btn-animate text-muted py-0"
1142                   aria-label={i18n.t("are_you_sure")}
1143                 >
1144                   {i18n.t("are_you_sure")}
1145                 </button>
1146                 <button
1147                   className="btn btn-link btn-animate text-muted py-0 d-inline-block mr-1"
1148                   aria-label={i18n.t("yes")}
1149                   onClick={linkEvent(this, this.handleTransferCommunity)}
1150                 >
1151                   {this.state.transferLoading ? <Spinner /> : i18n.t("yes")}
1152                 </button>
1153                 <button
1154                   className="btn btn-link btn-animate text-muted py-0 d-inline-block"
1155                   onClick={linkEvent(
1156                     this,
1157                     this.handleCancelShowConfirmTransferCommunity
1158                   )}
1159                   aria-label={i18n.t("no")}
1160                 >
1161                   {i18n.t("no")}
1162                 </button>
1163               </>
1164             ))}
1165           {/* Admins can ban from all, and appoint other admins */}
1166           {this.canAdmin_ && (
1167             <>
1168               {!this.creatorIsAdmin_ && (
1169                 <>
1170                   {!isBanned(post_view.creator) ? (
1171                     <button
1172                       className="btn btn-link btn-animate text-muted py-0"
1173                       onClick={linkEvent(this, this.handleModBanShow)}
1174                       aria-label={i18n.t("ban_from_site")}
1175                     >
1176                       {i18n.t("ban_from_site")}
1177                     </button>
1178                   ) : (
1179                     <button
1180                       className="btn btn-link btn-animate text-muted py-0"
1181                       onClick={linkEvent(this, this.handleModBanSubmit)}
1182                       aria-label={i18n.t("unban_from_site")}
1183                     >
1184                       {this.state.banLoading ? (
1185                         <Spinner />
1186                       ) : (
1187                         i18n.t("unban_from_site")
1188                       )}
1189                     </button>
1190                   )}
1191                   <button
1192                     className="btn btn-link btn-animate text-muted py-0"
1193                     onClick={linkEvent(this, this.handlePurgePersonShow)}
1194                     aria-label={i18n.t("purge_user")}
1195                   >
1196                     {i18n.t("purge_user")}
1197                   </button>
1198                   <button
1199                     className="btn btn-link btn-animate text-muted py-0"
1200                     onClick={linkEvent(this, this.handlePurgePostShow)}
1201                     aria-label={i18n.t("purge_post")}
1202                   >
1203                     {i18n.t("purge_post")}
1204                   </button>
1205                 </>
1206               )}
1207               {!isBanned(post_view.creator) && post_view.creator.local && (
1208                 <button
1209                   className="btn btn-link btn-animate text-muted py-0"
1210                   onClick={linkEvent(this, this.handleAddAdmin)}
1211                   aria-label={
1212                     this.creatorIsAdmin_
1213                       ? i18n.t("remove_as_admin")
1214                       : i18n.t("appoint_as_admin")
1215                   }
1216                 >
1217                   {this.state.addAdminLoading ? (
1218                     <Spinner />
1219                   ) : this.creatorIsAdmin_ ? (
1220                     i18n.t("remove_as_admin")
1221                   ) : (
1222                     i18n.t("appoint_as_admin")
1223                   )}
1224                 </button>
1225               )}
1226             </>
1227           )}
1228         </>
1229       )
1230     );
1231   }
1232
1233   removeAndBanDialogs() {
1234     const post = this.postView;
1235     const purgeTypeText =
1236       this.state.purgeType == PurgeType.Post
1237         ? i18n.t("purge_post")
1238         : `${i18n.t("purge")} ${post.creator.name}`;
1239     return (
1240       <>
1241         {this.state.showRemoveDialog && (
1242           <form
1243             className="form-inline"
1244             onSubmit={linkEvent(this, this.handleModRemoveSubmit)}
1245           >
1246             <label className="sr-only" htmlFor="post-listing-remove-reason">
1247               {i18n.t("reason")}
1248             </label>
1249             <input
1250               type="text"
1251               id="post-listing-remove-reason"
1252               className="form-control mr-2"
1253               placeholder={i18n.t("reason")}
1254               value={this.state.removeReason}
1255               onInput={linkEvent(this, this.handleModRemoveReasonChange)}
1256             />
1257             <button
1258               type="submit"
1259               className="btn btn-secondary"
1260               aria-label={i18n.t("remove_post")}
1261             >
1262               {this.state.removeLoading ? <Spinner /> : i18n.t("remove_post")}
1263             </button>
1264           </form>
1265         )}
1266         {this.state.showBanDialog && (
1267           <form onSubmit={linkEvent(this, this.handleModBanBothSubmit)}>
1268             <div className="form-group row col-12">
1269               <label
1270                 className="col-form-label"
1271                 htmlFor="post-listing-ban-reason"
1272               >
1273                 {i18n.t("reason")}
1274               </label>
1275               <input
1276                 type="text"
1277                 id="post-listing-ban-reason"
1278                 className="form-control mr-2"
1279                 placeholder={i18n.t("reason")}
1280                 value={this.state.banReason}
1281                 onInput={linkEvent(this, this.handleModBanReasonChange)}
1282               />
1283               <label className="col-form-label" htmlFor={`mod-ban-expires`}>
1284                 {i18n.t("expires")}
1285               </label>
1286               <input
1287                 type="number"
1288                 id={`mod-ban-expires`}
1289                 className="form-control mr-2"
1290                 placeholder={i18n.t("number_of_days")}
1291                 value={this.state.banExpireDays}
1292                 onInput={linkEvent(this, this.handleModBanExpireDaysChange)}
1293               />
1294               <div className="form-group">
1295                 <div className="form-check">
1296                   <input
1297                     className="form-check-input"
1298                     id="mod-ban-remove-data"
1299                     type="checkbox"
1300                     checked={this.state.removeData}
1301                     onChange={linkEvent(this, this.handleModRemoveDataChange)}
1302                   />
1303                   <label
1304                     className="form-check-label"
1305                     htmlFor="mod-ban-remove-data"
1306                     title={i18n.t("remove_content_more")}
1307                   >
1308                     {i18n.t("remove_content")}
1309                   </label>
1310                 </div>
1311               </div>
1312             </div>
1313             {/* TODO hold off on expires until later */}
1314             {/* <div class="form-group row"> */}
1315             {/*   <label class="col-form-label">Expires</label> */}
1316             {/*   <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.banExpires} onInput={linkEvent(this, this.handleModBanExpiresChange)} /> */}
1317             {/* </div> */}
1318             <div className="form-group row">
1319               <button
1320                 type="submit"
1321                 className="btn btn-secondary"
1322                 aria-label={i18n.t("ban")}
1323               >
1324                 {this.state.banLoading ? (
1325                   <Spinner />
1326                 ) : (
1327                   <span>
1328                     {i18n.t("ban")} {post.creator.name}
1329                   </span>
1330                 )}
1331               </button>
1332             </div>
1333           </form>
1334         )}
1335         {this.state.showReportDialog && (
1336           <form
1337             className="form-inline"
1338             onSubmit={linkEvent(this, this.handleReportSubmit)}
1339           >
1340             <label className="sr-only" htmlFor="post-report-reason">
1341               {i18n.t("reason")}
1342             </label>
1343             <input
1344               type="text"
1345               id="post-report-reason"
1346               className="form-control mr-2"
1347               placeholder={i18n.t("reason")}
1348               required
1349               value={this.state.reportReason}
1350               onInput={linkEvent(this, this.handleReportReasonChange)}
1351             />
1352             <button
1353               type="submit"
1354               className="btn btn-secondary"
1355               aria-label={i18n.t("create_report")}
1356             >
1357               {this.state.reportLoading ? <Spinner /> : i18n.t("create_report")}
1358             </button>
1359           </form>
1360         )}
1361         {this.state.showPurgeDialog && (
1362           <form
1363             className="form-inline"
1364             onSubmit={linkEvent(this, this.handlePurgeSubmit)}
1365           >
1366             <PurgeWarning />
1367             <label className="sr-only" htmlFor="purge-reason">
1368               {i18n.t("reason")}
1369             </label>
1370             <input
1371               type="text"
1372               id="purge-reason"
1373               className="form-control mr-2"
1374               placeholder={i18n.t("reason")}
1375               value={this.state.purgeReason}
1376               onInput={linkEvent(this, this.handlePurgeReasonChange)}
1377             />
1378             {this.state.purgeLoading ? (
1379               <Spinner />
1380             ) : (
1381               <button
1382                 type="submit"
1383                 className="btn btn-secondary"
1384                 aria-label={purgeTypeText}
1385               >
1386                 {this.state.purgeLoading ? <Spinner /> : { purgeTypeText }}
1387               </button>
1388             )}
1389           </form>
1390         )}
1391       </>
1392     );
1393   }
1394
1395   mobileThumbnail() {
1396     const post = this.postView.post;
1397     return post.thumbnail_url || (post.url && isImage(post.url)) ? (
1398       <div className="row">
1399         <div className={`${this.state.imageExpanded ? "col-12" : "col-8"}`}>
1400           {this.postTitleLine()}
1401         </div>
1402         <div className="col-4">
1403           {/* Post body prev or thumbnail */}
1404           {!this.state.imageExpanded && this.thumbnail()}
1405         </div>
1406       </div>
1407     ) : (
1408       this.postTitleLine()
1409     );
1410   }
1411
1412   showMobilePreview() {
1413     const { body, id } = this.postView.post;
1414
1415     return !this.showBody && body ? (
1416       <Link className="text-body" to={`/post/${id}`}>
1417         <div className="md-div mb-1 preview-lines">{body}</div>
1418       </Link>
1419     ) : (
1420       <></>
1421     );
1422   }
1423
1424   listing() {
1425     return (
1426       <>
1427         {/* The mobile view*/}
1428         <div className="d-block d-sm-none">
1429           <article className="row post-container">
1430             <div className="col-12">
1431               {this.createdLine()}
1432
1433               {/* If it has a thumbnail, do a right aligned thumbnail */}
1434               {this.mobileThumbnail()}
1435
1436               {/* Show a preview of the post body */}
1437               {this.showMobilePreview()}
1438
1439               {this.commentsLine(true)}
1440               {this.userActionsLine()}
1441               {this.duplicatesLine()}
1442               {this.removeAndBanDialogs()}
1443             </div>
1444           </article>
1445         </div>
1446
1447         {/* The larger view*/}
1448         <div className="d-none d-sm-block">
1449           <article className="row post-container">
1450             {!this.props.viewOnly && this.voteBar()}
1451             <div className="col-sm-2 pr-0 post-media">
1452               <div className="">{this.thumbnail()}</div>
1453             </div>
1454             <div className="col-12 col-sm-9">
1455               <div className="row">
1456                 <div className="col-12">
1457                   {this.postTitleLine()}
1458                   {this.createdLine()}
1459                   {this.commentsLine()}
1460                   {this.duplicatesLine()}
1461                   {this.userActionsLine()}
1462                   {this.removeAndBanDialogs()}
1463                 </div>
1464               </div>
1465             </div>
1466           </article>
1467         </div>
1468       </>
1469     );
1470   }
1471
1472   private get myPost(): boolean {
1473     return (
1474       this.postView.creator.id ==
1475       UserService.Instance.myUserInfo?.local_user_view.person.id
1476     );
1477   }
1478   handleEditClick(i: PostListing) {
1479     i.setState({ showEdit: true });
1480   }
1481
1482   handleEditCancel() {
1483     this.setState({ showEdit: false });
1484   }
1485
1486   // The actual editing is done in the receive for post
1487   handleEditPost(form: EditPost) {
1488     this.setState({ showEdit: false });
1489     this.props.onPostEdit(form);
1490   }
1491
1492   handleShare(i: PostListing) {
1493     const { name, body, id } = i.props.post_view.post;
1494     share({
1495       title: name,
1496       text: body?.slice(0, 50),
1497       url: `${getHttpBase()}/post/${id}`,
1498     });
1499   }
1500
1501   handleShowReportDialog(i: PostListing) {
1502     i.setState({ showReportDialog: !i.state.showReportDialog });
1503   }
1504
1505   handleReportReasonChange(i: PostListing, event: any) {
1506     i.setState({ reportReason: event.target.value });
1507   }
1508
1509   handleReportSubmit(i: PostListing, event: any) {
1510     event.preventDefault();
1511     i.setState({ reportLoading: true });
1512     i.props.onPostReport({
1513       post_id: i.postView.post.id,
1514       reason: i.state.reportReason ?? "",
1515       auth: myAuthRequired(),
1516     });
1517   }
1518
1519   handleBlockPersonClick(i: PostListing) {
1520     i.setState({ blockLoading: true });
1521     i.props.onBlockPerson({
1522       person_id: i.postView.creator.id,
1523       block: true,
1524       auth: myAuthRequired(),
1525     });
1526   }
1527
1528   handleDeleteClick(i: PostListing) {
1529     i.setState({ deleteLoading: true });
1530     i.props.onDeletePost({
1531       post_id: i.postView.post.id,
1532       deleted: !i.postView.post.deleted,
1533       auth: myAuthRequired(),
1534     });
1535   }
1536
1537   handleSavePostClick(i: PostListing) {
1538     i.setState({ saveLoading: true });
1539     i.props.onSavePost({
1540       post_id: i.postView.post.id,
1541       save: !i.postView.saved,
1542       auth: myAuthRequired(),
1543     });
1544   }
1545
1546   get crossPostParams(): PostFormParams {
1547     const queryParams: PostFormParams = {};
1548     const { name, url } = this.postView.post;
1549
1550     queryParams.name = name;
1551
1552     if (url) {
1553       queryParams.url = url;
1554     }
1555
1556     const crossPostBody = this.crossPostBody();
1557     if (crossPostBody) {
1558       queryParams.body = crossPostBody;
1559     }
1560
1561     return queryParams;
1562   }
1563
1564   crossPostBody(): string | undefined {
1565     const post = this.postView.post;
1566     const body = post.body;
1567
1568     return body
1569       ? `${i18n.t("cross_posted_from")} ${post.ap_id}\n\n${body.replace(
1570           /^/gm,
1571           "> "
1572         )}`
1573       : undefined;
1574   }
1575
1576   get showBody(): boolean {
1577     return this.props.showBody || this.state.showBody;
1578   }
1579
1580   handleModRemoveShow(i: PostListing) {
1581     i.setState({
1582       showRemoveDialog: !i.state.showRemoveDialog,
1583       showBanDialog: false,
1584     });
1585   }
1586
1587   handleModRemoveReasonChange(i: PostListing, event: any) {
1588     i.setState({ removeReason: event.target.value });
1589   }
1590
1591   handleModRemoveDataChange(i: PostListing, event: any) {
1592     i.setState({ removeData: event.target.checked });
1593   }
1594
1595   handleModRemoveSubmit(i: PostListing, event: any) {
1596     event.preventDefault();
1597     i.setState({ removeLoading: true });
1598     i.props.onRemovePost({
1599       post_id: i.postView.post.id,
1600       removed: !i.postView.post.removed,
1601       auth: myAuthRequired(),
1602     });
1603   }
1604
1605   handleModLock(i: PostListing) {
1606     i.setState({ lockLoading: true });
1607     i.props.onLockPost({
1608       post_id: i.postView.post.id,
1609       locked: !i.postView.post.locked,
1610       auth: myAuthRequired(),
1611     });
1612   }
1613
1614   handleModFeaturePostLocal(i: PostListing) {
1615     i.setState({ featureLocalLoading: true });
1616     i.props.onFeaturePost({
1617       post_id: i.postView.post.id,
1618       featured: !i.postView.post.featured_local,
1619       feature_type: "Local",
1620       auth: myAuthRequired(),
1621     });
1622   }
1623
1624   handleModFeaturePostCommunity(i: PostListing) {
1625     i.setState({ featureCommunityLoading: true });
1626     i.props.onFeaturePost({
1627       post_id: i.postView.post.id,
1628       featured: !i.postView.post.featured_community,
1629       feature_type: "Community",
1630       auth: myAuthRequired(),
1631     });
1632   }
1633
1634   handleModBanFromCommunityShow(i: PostListing) {
1635     i.setState({
1636       showBanDialog: true,
1637       banType: BanType.Community,
1638       showRemoveDialog: false,
1639     });
1640   }
1641
1642   handleModBanShow(i: PostListing) {
1643     i.setState({
1644       showBanDialog: true,
1645       banType: BanType.Site,
1646       showRemoveDialog: false,
1647     });
1648   }
1649
1650   handlePurgePersonShow(i: PostListing) {
1651     i.setState({
1652       showPurgeDialog: true,
1653       purgeType: PurgeType.Person,
1654       showRemoveDialog: false,
1655     });
1656   }
1657
1658   handlePurgePostShow(i: PostListing) {
1659     i.setState({
1660       showPurgeDialog: true,
1661       purgeType: PurgeType.Post,
1662       showRemoveDialog: false,
1663     });
1664   }
1665
1666   handlePurgeReasonChange(i: PostListing, event: any) {
1667     i.setState({ purgeReason: event.target.value });
1668   }
1669
1670   handlePurgeSubmit(i: PostListing, event: any) {
1671     event.preventDefault();
1672     i.setState({ purgeLoading: true });
1673     if (i.state.purgeType == PurgeType.Person) {
1674       i.props.onPurgePerson({
1675         person_id: i.postView.creator.id,
1676         reason: i.state.purgeReason,
1677         auth: myAuthRequired(),
1678       });
1679     } else if (i.state.purgeType == PurgeType.Post) {
1680       i.props.onPurgePost({
1681         post_id: i.postView.post.id,
1682         reason: i.state.purgeReason,
1683         auth: myAuthRequired(),
1684       });
1685     }
1686   }
1687
1688   handleModBanReasonChange(i: PostListing, event: any) {
1689     i.setState({ banReason: event.target.value });
1690   }
1691
1692   handleModBanExpireDaysChange(i: PostListing, event: any) {
1693     i.setState({ banExpireDays: event.target.value });
1694   }
1695
1696   handleModBanFromCommunitySubmit(i: PostListing, event: any) {
1697     i.setState({ banType: BanType.Community });
1698     i.handleModBanBothSubmit(i, event);
1699   }
1700
1701   handleModBanSubmit(i: PostListing, event: any) {
1702     i.setState({ banType: BanType.Site });
1703     i.handleModBanBothSubmit(i, event);
1704   }
1705
1706   handleModBanBothSubmit(i: PostListing, event: any) {
1707     event.preventDefault();
1708     i.setState({ banLoading: true });
1709
1710     const ban = !i.props.post_view.creator_banned_from_community;
1711     // If its an unban, restore all their data
1712     if (ban == false) {
1713       i.setState({ removeData: false });
1714     }
1715     const person_id = i.props.post_view.creator.id;
1716     const remove_data = i.state.removeData;
1717     const reason = i.state.banReason;
1718     const expires = futureDaysToUnixTime(i.state.banExpireDays);
1719
1720     if (i.state.banType == BanType.Community) {
1721       const community_id = i.postView.community.id;
1722       i.props.onBanPersonFromCommunity({
1723         community_id,
1724         person_id,
1725         ban,
1726         remove_data,
1727         reason,
1728         expires,
1729         auth: myAuthRequired(),
1730       });
1731     } else {
1732       i.props.onBanPerson({
1733         person_id,
1734         ban,
1735         remove_data,
1736         reason,
1737         expires,
1738         auth: myAuthRequired(),
1739       });
1740     }
1741   }
1742
1743   handleAddModToCommunity(i: PostListing) {
1744     i.setState({ addModLoading: true });
1745     i.props.onAddModToCommunity({
1746       community_id: i.postView.community.id,
1747       person_id: i.postView.creator.id,
1748       added: !i.creatorIsMod_,
1749       auth: myAuthRequired(),
1750     });
1751   }
1752
1753   handleAddAdmin(i: PostListing) {
1754     i.setState({ addAdminLoading: true });
1755     i.props.onAddAdmin({
1756       person_id: i.postView.creator.id,
1757       added: !i.creatorIsAdmin_,
1758       auth: myAuthRequired(),
1759     });
1760   }
1761
1762   handleShowConfirmTransferCommunity(i: PostListing) {
1763     i.setState({ showConfirmTransferCommunity: true });
1764   }
1765
1766   handleCancelShowConfirmTransferCommunity(i: PostListing) {
1767     i.setState({ showConfirmTransferCommunity: false });
1768   }
1769
1770   handleTransferCommunity(i: PostListing) {
1771     i.setState({ transferLoading: true });
1772     i.props.onTransferCommunity({
1773       community_id: i.postView.community.id,
1774       person_id: i.postView.creator.id,
1775       auth: myAuthRequired(),
1776     });
1777   }
1778
1779   handleShowConfirmTransferSite(i: PostListing) {
1780     i.setState({ showConfirmTransferSite: true });
1781   }
1782
1783   handleCancelShowConfirmTransferSite(i: PostListing) {
1784     i.setState({ showConfirmTransferSite: false });
1785   }
1786
1787   handleImageExpandClick(i: PostListing, event: any) {
1788     event.preventDefault();
1789     i.setState({ imageExpanded: !i.state.imageExpanded });
1790     setupTippy();
1791   }
1792
1793   handleViewSource(i: PostListing) {
1794     i.setState({ viewSource: !i.state.viewSource });
1795   }
1796
1797   handleShowAdvanced(i: PostListing) {
1798     i.setState({ showAdvanced: !i.state.showAdvanced });
1799     setupTippy();
1800   }
1801
1802   handleShowMoreMobile(i: PostListing) {
1803     i.setState({
1804       showMoreMobile: !i.state.showMoreMobile,
1805       showAdvanced: !i.state.showAdvanced,
1806     });
1807     setupTippy();
1808   }
1809
1810   handleShowBody(i: PostListing) {
1811     i.setState({ showBody: !i.state.showBody });
1812     setupTippy();
1813   }
1814
1815   handleUpvote(i: PostListing) {
1816     i.setState({ upvoteLoading: true });
1817     i.props.onPostVote({
1818       post_id: i.postView.post.id,
1819       score: newVote(VoteType.Upvote, i.props.post_view.my_vote),
1820       auth: myAuthRequired(),
1821     });
1822   }
1823
1824   handleDownvote(i: PostListing) {
1825     i.setState({ downvoteLoading: true });
1826     i.props.onPostVote({
1827       post_id: i.postView.post.id,
1828       score: newVote(VoteType.Downvote, i.props.post_view.my_vote),
1829       auth: myAuthRequired(),
1830     });
1831   }
1832
1833   get pointsTippy(): string {
1834     const points = i18n.t("number_of_points", {
1835       count: Number(this.postView.counts.score),
1836       formattedCount: Number(this.postView.counts.score),
1837     });
1838
1839     const upvotes = i18n.t("number_of_upvotes", {
1840       count: Number(this.postView.counts.upvotes),
1841       formattedCount: Number(this.postView.counts.upvotes),
1842     });
1843
1844     const downvotes = i18n.t("number_of_downvotes", {
1845       count: Number(this.postView.counts.downvotes),
1846       formattedCount: Number(this.postView.counts.downvotes),
1847     });
1848
1849     return `${points} • ${upvotes} • ${downvotes}`;
1850   }
1851
1852   get canModOnSelf_(): boolean {
1853     return canMod(
1854       this.postView.creator.id,
1855       this.props.moderators,
1856       this.props.admins,
1857       undefined,
1858       true
1859     );
1860   }
1861
1862   get canMod_(): boolean {
1863     return canMod(
1864       this.postView.creator.id,
1865       this.props.moderators,
1866       this.props.admins
1867     );
1868   }
1869
1870   get canAdmin_(): boolean {
1871     return canAdmin(this.postView.creator.id, this.props.admins);
1872   }
1873
1874   get creatorIsMod_(): boolean {
1875     return isMod(this.postView.creator.id, this.props.moderators);
1876   }
1877
1878   get creatorIsAdmin_(): boolean {
1879     return isAdmin(this.postView.creator.id, this.props.admins);
1880   }
1881 }