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