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