]> Untitled Git - lemmy-ui.git/blobdiff - src/shared/components/community/sidebar.tsx
Changing all bigints to numbers
[lemmy-ui.git] / src / shared / components / community / sidebar.tsx
index c723567789095b515368bf3a9241c63bc6ebe656..a5eef75c3f7c420d46f424e9f2f18622620e0ef9 100644 (file)
@@ -1,4 +1,3 @@
-import { None, Option, Some } from "@sniptt/monads";
 import { Component, linkEvent } from "inferno";
 import { Link } from "inferno-router";
 import {
@@ -8,11 +7,10 @@ import {
   CommunityView,
   DeleteCommunity,
   FollowCommunity,
-  PersonViewSafe,
+  Language,
+  PersonView,
   PurgeCommunity,
   RemoveCommunity,
-  SubscribedType,
-  toUndefined,
 } from "lemmy-js-client";
 import { i18n } from "../../i18next";
 import { UserService, WebSocketService } from "../../services";
@@ -20,9 +18,10 @@ import {
   amAdmin,
   amMod,
   amTopMod,
-  auth,
   getUnixTime,
+  hostname,
   mdToHtml,
+  myAuth,
   numToSI,
   wsClient,
 } from "../../utils";
@@ -35,38 +34,38 @@ import { PersonListing } from "../person/person-listing";
 interface SidebarProps {
   community_view: CommunityView;
   moderators: CommunityModeratorView[];
-  admins: PersonViewSafe[];
+  admins: PersonView[];
+  allLanguages: Language[];
+  siteLanguages: number[];
+  communityLanguages?: number[];
   online: number;
   enableNsfw?: boolean;
   showIcon?: boolean;
+  editable?: boolean;
 }
 
 interface SidebarState {
-  removeReason: Option<string>;
-  removeExpires: Option<string>;
+  removeReason?: string;
+  removeExpires?: string;
   showEdit: boolean;
   showRemoveDialog: boolean;
   showPurgeDialog: boolean;
-  purgeReason: Option<string>;
+  purgeReason?: string;
   purgeLoading: boolean;
   showConfirmLeaveModTeam: boolean;
 }
 
 export class Sidebar extends Component<SidebarProps, SidebarState> {
-  private emptyState: SidebarState = {
+  state: SidebarState = {
     showEdit: false,
     showRemoveDialog: false,
-    removeReason: None,
-    removeExpires: None,
     showPurgeDialog: false,
-    purgeReason: None,
     purgeLoading: false,
     showConfirmLeaveModTeam: false,
   };
 
   constructor(props: any, context: any) {
     super(props, context);
-    this.state = this.emptyState;
     this.handleEditCommunity = this.handleEditCommunity.bind(this);
     this.handleEditCancel = this.handleEditCancel.bind(this);
   }
@@ -78,7 +77,10 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
           this.sidebar()
         ) : (
           <CommunityForm
-            community_view={Some(this.props.community_view)}
+            community_view={this.props.community_view}
+            allLanguages={this.props.allLanguages}
+            siteLanguages={this.props.siteLanguages}
+            communityLanguages={this.props.communityLanguages}
             onEdit={this.handleEditCommunity}
             onCancel={this.handleEditCancel}
             enableNsfw={this.props.enableNsfw}
@@ -89,15 +91,25 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
   }
 
   sidebar() {
+    const myUSerInfo = UserService.Instance.myUserInfo;
+    const { name, actor_id } = this.props.community_view.community;
     return (
       <div>
         <div className="card border-secondary mb-3">
           <div className="card-body">
             {this.communityTitle()}
-            {this.adminButtons()}
-            {this.subscribe()}
+            {this.props.editable && this.adminButtons()}
+            {myUSerInfo && this.subscribe()}
             {this.canPost && this.createPost()}
-            {this.blockCommunity()}
+            {myUSerInfo && this.blockCommunity()}
+            {!myUSerInfo && (
+              <div className="alert alert-info" role="alert">
+                {i18n.t("community_not_logged_in_alert", {
+                  community: name,
+                  instance: hostname(actor_id),
+                })}
+              </div>
+            )}
           </div>
         </div>
         <div className="card border-secondary mb-3">
@@ -117,11 +129,11 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
     return (
       <div>
         <h5 className="mb-0">
-          {this.props.showIcon && (
+          {this.props.showIcon && !community.removed && (
             <BannerIconHeader icon={community.icon} banner={community.banner} />
           )}
           <span className="mr-2">{community.title}</span>
-          {subscribed == SubscribedType.Subscribed && (
+          {subscribed === "Subscribed" && (
             <button
               className="btn btn-secondary btn-sm mr-2"
               onClick={linkEvent(this, this.handleUnsubscribe)}
@@ -130,7 +142,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
               {i18n.t("joined")}
             </button>
           )}
-          {subscribed == SubscribedType.Pending && (
+          {subscribed === "Pending" && (
             <button
               className="btn btn-warning mr-2"
               onClick={linkEvent(this, this.handleUnsubscribe)}
@@ -179,12 +191,12 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
         <li
           className="list-inline-item badge badge-secondary pointer"
           data-tippy-content={i18n.t("active_users_in_the_last_day", {
-            count: counts.users_active_day,
-            formattedCount: counts.users_active_day,
+            count: Number(counts.users_active_day),
+            formattedCount: numToSI(counts.users_active_day),
           })}
         >
           {i18n.t("number_of_users", {
-            count: counts.users_active_day,
+            count: Number(counts.users_active_day),
             formattedCount: numToSI(counts.users_active_day),
           })}{" "}
           / {i18n.t("day")}
@@ -192,12 +204,12 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
         <li
           className="list-inline-item badge badge-secondary pointer"
           data-tippy-content={i18n.t("active_users_in_the_last_week", {
-            count: counts.users_active_week,
-            formattedCount: counts.users_active_week,
+            count: Number(counts.users_active_week),
+            formattedCount: numToSI(counts.users_active_week),
           })}
         >
           {i18n.t("number_of_users", {
-            count: counts.users_active_week,
+            count: Number(counts.users_active_week),
             formattedCount: numToSI(counts.users_active_week),
           })}{" "}
           / {i18n.t("week")}
@@ -205,12 +217,12 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
         <li
           className="list-inline-item badge badge-secondary pointer"
           data-tippy-content={i18n.t("active_users_in_the_last_month", {
-            count: counts.users_active_month,
-            formattedCount: counts.users_active_month,
+            count: Number(counts.users_active_month),
+            formattedCount: numToSI(counts.users_active_month),
           })}
         >
           {i18n.t("number_of_users", {
-            count: counts.users_active_month,
+            count: Number(counts.users_active_month),
             formattedCount: numToSI(counts.users_active_month),
           })}{" "}
           / {i18n.t("month")}
@@ -218,38 +230,38 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
         <li
           className="list-inline-item badge badge-secondary pointer"
           data-tippy-content={i18n.t("active_users_in_the_last_six_months", {
-            count: counts.users_active_half_year,
-            formattedCount: counts.users_active_half_year,
+            count: Number(counts.users_active_half_year),
+            formattedCount: numToSI(counts.users_active_half_year),
           })}
         >
           {i18n.t("number_of_users", {
-            count: counts.users_active_half_year,
+            count: Number(counts.users_active_half_year),
             formattedCount: numToSI(counts.users_active_half_year),
           })}{" "}
           / {i18n.t("number_of_months", { count: 6, formattedCount: 6 })}
         </li>
         <li className="list-inline-item badge badge-secondary">
           {i18n.t("number_of_subscribers", {
-            count: counts.subscribers,
+            count: Number(counts.subscribers),
             formattedCount: numToSI(counts.subscribers),
           })}
         </li>
         <li className="list-inline-item badge badge-secondary">
           {i18n.t("number_of_posts", {
-            count: counts.posts,
+            count: Number(counts.posts),
             formattedCount: numToSI(counts.posts),
           })}
         </li>
         <li className="list-inline-item badge badge-secondary">
           {i18n.t("number_of_comments", {
-            count: counts.comments,
+            count: Number(counts.comments),
             formattedCount: numToSI(counts.comments),
           })}
         </li>
         <li className="list-inline-item">
           <Link
             className="badge badge-primary"
-            to={`/modlog/community/${this.props.community_view.community.id}`}
+            to={`/modlog/${this.props.community_view.community.id}`}
           >
             {i18n.t("modlog")}
           </Link>
@@ -278,7 +290,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
         className={`btn btn-secondary btn-block mb-2 ${
           cv.community.deleted || cv.community.removed ? "no-click" : ""
         }`}
-        to={`/create_post?community_id=${cv.community.id}`}
+        to={`/create_post?communityId=${cv.community.id}`}
       >
         {i18n.t("create_a_post")}
       </Link>
@@ -289,7 +301,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
     let community_view = this.props.community_view;
     return (
       <div className="mb-2">
-        {community_view.subscribed == SubscribedType.NotSubscribed && (
+        {community_view.subscribed == "NotSubscribed" && (
           <button
             className="btn btn-secondary btn-block"
             onClick={linkEvent(this, this.handleSubscribe)}
@@ -307,7 +319,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
 
     return (
       <div className="mb-2">
-        {community_view.subscribed == SubscribedType.NotSubscribed &&
+        {community_view.subscribed == "NotSubscribed" &&
           (blocked ? (
             <button
               className="btn btn-danger btn-block"
@@ -328,13 +340,12 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
   }
 
   description() {
-    let description = this.props.community_view.community.description;
-    return description.match({
-      some: desc => (
+    let desc = this.props.community_view.community.description;
+    return (
+      desc && (
         <div className="md-div" dangerouslySetInnerHTML={mdToHtml(desc)} />
-      ),
-      none: <></>,
-    });
+      )
+    );
   }
 
   adminButtons() {
@@ -342,7 +353,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
     return (
       <>
         <ul className="list-inline mb-1 text-muted font-weight-bold">
-          {amMod(Some(this.props.moderators)) && (
+          {amMod(this.props.moderators) && (
             <>
               <li className="list-inline-item-action">
                 <button
@@ -354,7 +365,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
                   <Icon icon="edit" classes="icon-inline" />
                 </button>
               </li>
-              {!amTopMod(Some(this.props.moderators)) &&
+              {!amTopMod(this.props.moderators) &&
                 (!this.state.showConfirmLeaveModTeam ? (
                   <li className="list-inline-item-action">
                     <button
@@ -393,7 +404,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
                     </li>
                   </>
                 ))}
-              {amTopMod(Some(this.props.moderators)) && (
+              {amTopMod(this.props.moderators) && (
                 <li className="list-inline-item-action">
                   <button
                     className="btn btn-link text-muted d-inline-block"
@@ -420,7 +431,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
               )}
             </>
           )}
-          {amAdmin(Some(this.props.admins)) && (
+          {amAdmin() && (
             <li className="list-inline-item">
               {!this.props.community_view.community.removed ? (
                 <button
@@ -458,7 +469,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
                 id="remove-reason"
                 className="form-control mr-2"
                 placeholder={i18n.t("optional")}
-                value={toUndefined(this.state.removeReason)}
+                value={this.state.removeReason}
                 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
               />
             </div>
@@ -488,7 +499,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
                 id="purge-reason"
                 className="form-control mr-2"
                 placeholder={i18n.t("reason")}
-                value={toUndefined(this.state.purgeReason)}
+                value={this.state.purgeReason}
                 onInput={linkEvent(this, this.handlePurgeReasonChange)}
               />
             </div>
@@ -525,12 +536,15 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
 
   handleDeleteClick(i: Sidebar, event: any) {
     event.preventDefault();
-    let deleteForm = new DeleteCommunity({
-      community_id: i.props.community_view.community.id,
-      deleted: !i.props.community_view.community.deleted,
-      auth: auth().unwrap(),
-    });
-    WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
+    let auth = myAuth();
+    if (auth) {
+      let deleteForm: DeleteCommunity = {
+        community_id: i.props.community_view.community.id,
+        deleted: !i.props.community_view.community.deleted,
+        auth,
+      };
+      WebSocketService.Instance.send(wsClient.deleteCommunity(deleteForm));
+    }
   }
 
   handleShowConfirmLeaveModTeamClick(i: Sidebar) {
@@ -538,19 +552,18 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
   }
 
   handleLeaveModTeamClick(i: Sidebar) {
-    UserService.Instance.myUserInfo.match({
-      some: mui => {
-        let form = new AddModToCommunity({
-          person_id: mui.local_user_view.person.id,
-          community_id: i.props.community_view.community.id,
-          added: false,
-          auth: auth().unwrap(),
-        });
-        WebSocketService.Instance.send(wsClient.addModToCommunity(form));
-        i.setState({ showConfirmLeaveModTeam: false });
-      },
-      none: void 0,
-    });
+    let mui = UserService.Instance.myUserInfo;
+    let auth = myAuth();
+    if (auth && mui) {
+      let form: AddModToCommunity = {
+        person_id: mui.local_user_view.person.id,
+        community_id: i.props.community_view.community.id,
+        added: false,
+        auth,
+      };
+      WebSocketService.Instance.send(wsClient.addModToCommunity(form));
+      i.setState({ showConfirmLeaveModTeam: false });
+    }
   }
 
   handleCancelLeaveModTeamClick(i: Sidebar) {
@@ -560,47 +573,51 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
   handleUnsubscribe(i: Sidebar, event: any) {
     event.preventDefault();
     let community_id = i.props.community_view.community.id;
-    let form = new FollowCommunity({
-      community_id,
-      follow: false,
-      auth: auth().unwrap(),
-    });
-    WebSocketService.Instance.send(wsClient.followCommunity(form));
+    let auth = myAuth();
+    if (auth) {
+      let form: FollowCommunity = {
+        community_id,
+        follow: false,
+        auth,
+      };
+      WebSocketService.Instance.send(wsClient.followCommunity(form));
+    }
 
     // Update myUserInfo
-    UserService.Instance.myUserInfo.match({
-      some: mui =>
-        (mui.follows = mui.follows.filter(i => i.community.id != community_id)),
-      none: void 0,
-    });
+    let mui = UserService.Instance.myUserInfo;
+    if (mui) {
+      mui.follows = mui.follows.filter(i => i.community.id != community_id);
+    }
   }
 
   handleSubscribe(i: Sidebar, event: any) {
     event.preventDefault();
     let community_id = i.props.community_view.community.id;
-    let form = new FollowCommunity({
-      community_id,
-      follow: true,
-      auth: auth().unwrap(),
-    });
-    WebSocketService.Instance.send(wsClient.followCommunity(form));
+    let auth = myAuth();
+    if (auth) {
+      let form: FollowCommunity = {
+        community_id,
+        follow: true,
+        auth,
+      };
+      WebSocketService.Instance.send(wsClient.followCommunity(form));
+    }
 
     // Update myUserInfo
-    UserService.Instance.myUserInfo.match({
-      some: mui =>
-        mui.follows.push({
-          community: i.props.community_view.community,
-          follower: mui.local_user_view.person,
-        }),
-      none: void 0,
-    });
+    let mui = UserService.Instance.myUserInfo;
+    if (mui) {
+      mui.follows.push({
+        community: i.props.community_view.community,
+        follower: mui.local_user_view.person,
+      });
+    }
   }
 
   get canPost(): boolean {
     return (
       !this.props.community_view.community.posting_restricted_to_mods ||
-      amMod(Some(this.props.moderators)) ||
-      amAdmin(Some(this.props.admins))
+      amMod(this.props.moderators) ||
+      amAdmin()
     );
   }
 
@@ -609,25 +626,28 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
   }
 
   handleModRemoveReasonChange(i: Sidebar, event: any) {
-    i.setState({ removeReason: Some(event.target.value) });
+    i.setState({ removeReason: event.target.value });
   }
 
   handleModRemoveExpiresChange(i: Sidebar, event: any) {
-    i.setState({ removeExpires: Some(event.target.value) });
+    i.setState({ removeExpires: event.target.value });
   }
 
   handleModRemoveSubmit(i: Sidebar, event: any) {
     event.preventDefault();
-    let removeForm = new RemoveCommunity({
-      community_id: i.props.community_view.community.id,
-      removed: !i.props.community_view.community.removed,
-      reason: i.state.removeReason,
-      expires: i.state.removeExpires.map(getUnixTime),
-      auth: auth().unwrap(),
-    });
-    WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
-
-    i.setState({ showRemoveDialog: false });
+    let auth = myAuth();
+    if (auth) {
+      let removeForm: RemoveCommunity = {
+        community_id: i.props.community_view.community.id,
+        removed: !i.props.community_view.community.removed,
+        reason: i.state.removeReason,
+        expires: getUnixTime(i.state.removeExpires),
+        auth,
+      };
+      WebSocketService.Instance.send(wsClient.removeCommunity(removeForm));
+
+      i.setState({ showRemoveDialog: false });
+    }
   }
 
   handlePurgeCommunityShow(i: Sidebar) {
@@ -635,39 +655,51 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
   }
 
   handlePurgeReasonChange(i: Sidebar, event: any) {
-    i.setState({ purgeReason: Some(event.target.value) });
+    i.setState({ purgeReason: event.target.value });
   }
 
   handlePurgeSubmit(i: Sidebar, event: any) {
     event.preventDefault();
 
-    let form = new PurgeCommunity({
-      community_id: i.props.community_view.community.id,
-      reason: i.state.purgeReason,
-      auth: auth().unwrap(),
-    });
-    WebSocketService.Instance.send(wsClient.purgeCommunity(form));
-
-    i.setState({ purgeLoading: true });
+    let auth = myAuth();
+    if (auth) {
+      let form: PurgeCommunity = {
+        community_id: i.props.community_view.community.id,
+        reason: i.state.purgeReason,
+        auth,
+      };
+      WebSocketService.Instance.send(wsClient.purgeCommunity(form));
+      i.setState({ purgeLoading: true });
+    }
   }
 
   handleBlock(i: Sidebar, event: any) {
     event.preventDefault();
-    let blockCommunityForm = new BlockCommunity({
-      community_id: i.props.community_view.community.id,
-      block: true,
-      auth: auth().unwrap(),
-    });
-    WebSocketService.Instance.send(wsClient.blockCommunity(blockCommunityForm));
+    let auth = myAuth();
+    if (auth) {
+      let blockCommunityForm: BlockCommunity = {
+        community_id: i.props.community_view.community.id,
+        block: true,
+        auth,
+      };
+      WebSocketService.Instance.send(
+        wsClient.blockCommunity(blockCommunityForm)
+      );
+    }
   }
 
   handleUnblock(i: Sidebar, event: any) {
     event.preventDefault();
-    let blockCommunityForm = new BlockCommunity({
-      community_id: i.props.community_view.community.id,
-      block: false,
-      auth: auth().unwrap(),
-    });
-    WebSocketService.Instance.send(wsClient.blockCommunity(blockCommunityForm));
+    let auth = myAuth();
+    if (auth) {
+      let blockCommunityForm: BlockCommunity = {
+        community_id: i.props.community_view.community.id,
+        block: false,
+        auth,
+      };
+      WebSocketService.Instance.send(
+        wsClient.blockCommunity(blockCommunityForm)
+      );
+    }
   }
 }