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