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