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