From f8f1f49250dc26a2cb4b48bf707a684f0c75337a Mon Sep 17 00:00:00 2001 From: Jay Sitter Date: Fri, 16 Jun 2023 18:31:47 -0400 Subject: [PATCH 1/5] feat: Move Badges to common component --- src/shared/components/common/badges.tsx | 113 ++++++++++++++++++++ src/shared/components/community/sidebar.tsx | 101 ++--------------- src/shared/components/home/site-sidebar.tsx | 104 +----------------- 3 files changed, 124 insertions(+), 194 deletions(-) create mode 100644 src/shared/components/common/badges.tsx diff --git a/src/shared/components/common/badges.tsx b/src/shared/components/common/badges.tsx new file mode 100644 index 00000000..188d92ce --- /dev/null +++ b/src/shared/components/common/badges.tsx @@ -0,0 +1,113 @@ +import { Link } from "inferno-router"; +import { + CommunityAggregates, + CommunityView, + SiteAggregates, +} from "lemmy-js-client"; +import { i18n } from "../../i18next"; +import { numToSI } from "../../utils"; + +interface BadgesProps { + counts: CommunityAggregates | SiteAggregates; + online: number; + community_view?: CommunityView; +} + +const isCommunityAggregates = ( + counts: CommunityAggregates | SiteAggregates +): counts is CommunityAggregates => { + return "subscribers" in counts; +}; + +export const Badges = ({ counts, community_view, online }: BadgesProps) => { + return ( + + ); +}; diff --git a/src/shared/components/community/sidebar.tsx b/src/shared/components/community/sidebar.tsx index 57400f48..7023d133 100644 --- a/src/shared/components/community/sidebar.tsx +++ b/src/shared/components/community/sidebar.tsx @@ -24,8 +24,8 @@ import { hostname, mdToHtml, myAuthRequired, - numToSI, } from "../../utils"; +import { Badges } from "../common/badges"; import { BannerIconHeader } from "../common/banner-icon-header"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; import { CommunityForm } from "../community/community-form"; @@ -158,7 +158,11 @@ export class Sidebar extends Component {
{this.description()} - {this.badges()} + {this.mods()}
@@ -232,99 +236,6 @@ export class Sidebar extends Component { ); } - badges() { - const community_view = this.props.community_view; - const counts = community_view.counts; - return ( -
    -
  • - {i18n.t("number_online", { - count: this.props.online, - formattedCount: numToSI(this.props.online), - })} -
  • -
  • - {i18n.t("number_of_users", { - count: Number(counts.users_active_day), - formattedCount: numToSI(counts.users_active_day), - })}{" "} - / {i18n.t("day")} -
  • -
  • - {i18n.t("number_of_users", { - count: Number(counts.users_active_week), - formattedCount: numToSI(counts.users_active_week), - })}{" "} - / {i18n.t("week")} -
  • -
  • - {i18n.t("number_of_users", { - count: Number(counts.users_active_month), - formattedCount: numToSI(counts.users_active_month), - })}{" "} - / {i18n.t("month")} -
  • -
  • - {i18n.t("number_of_users", { - count: Number(counts.users_active_half_year), - formattedCount: numToSI(counts.users_active_half_year), - })}{" "} - / {i18n.t("number_of_months", { count: 6, formattedCount: 6 })} -
  • -
  • - {i18n.t("number_of_subscribers", { - count: Number(counts.subscribers), - formattedCount: numToSI(counts.subscribers), - })} -
  • -
  • - {i18n.t("number_of_posts", { - count: Number(counts.posts), - formattedCount: numToSI(counts.posts), - })} -
  • -
  • - {i18n.t("number_of_comments", { - count: Number(counts.comments), - formattedCount: numToSI(counts.comments), - })} -
  • -
  • - - {i18n.t("modlog")} - -
  • -
- ); - } - mods() { return (
    diff --git a/src/shared/components/home/site-sidebar.tsx b/src/shared/components/home/site-sidebar.tsx index 051c3afd..5f32e59e 100644 --- a/src/shared/components/home/site-sidebar.tsx +++ b/src/shared/components/home/site-sidebar.tsx @@ -1,8 +1,8 @@ import { Component, linkEvent } from "inferno"; -import { Link } from "inferno-router"; import { PersonView, Site, SiteAggregates } from "lemmy-js-client"; import { i18n } from "../../i18next"; -import { mdToHtml, numToSI } from "../../utils"; +import { mdToHtml } from "../../utils"; +import { Badges } from "../common/badges"; import { BannerIconHeader } from "../common/banner-icon-header"; import { Icon } from "../common/icon"; import { PersonListing } from "../person/person-listing"; @@ -72,7 +72,9 @@ export class SiteSidebar extends Component {
    {site.description &&
    {site.description}
    } {site.sidebar && this.siteSidebar(site.sidebar)} - {this.props.counts && this.badges(this.props.counts)} + {this.props.counts && ( + + )} {this.props.admins && this.admins(this.props.admins)}
    ); @@ -97,102 +99,6 @@ export class SiteSidebar extends Component { ); } - badges(siteAggregates: SiteAggregates) { - const counts = siteAggregates; - const online = this.props.online ?? 1; - return ( -
      -
    • - {i18n.t("number_online", { - count: online, - formattedCount: numToSI(online), - })} -
    • -
    • - {i18n.t("number_of_users", { - count: Number(counts.users_active_day), - formattedCount: numToSI(counts.users_active_day), - })}{" "} - / {i18n.t("day")} -
    • -
    • - {i18n.t("number_of_users", { - count: Number(counts.users_active_week), - formattedCount: numToSI(counts.users_active_week), - })}{" "} - / {i18n.t("week")} -
    • -
    • - {i18n.t("number_of_users", { - count: Number(counts.users_active_month), - formattedCount: numToSI(counts.users_active_month), - })}{" "} - / {i18n.t("month")} -
    • -
    • - {i18n.t("number_of_users", { - count: Number(counts.users_active_half_year), - formattedCount: numToSI(counts.users_active_half_year), - })}{" "} - / {i18n.t("number_of_months", { count: 6, formattedCount: 6 })} -
    • -
    • - {i18n.t("number_of_users", { - count: Number(counts.users), - formattedCount: numToSI(counts.users), - })} -
    • -
    • - {i18n.t("number_of_communities", { - count: Number(counts.communities), - formattedCount: numToSI(counts.communities), - })} -
    • -
    • - {i18n.t("number_of_posts", { - count: Number(counts.posts), - formattedCount: numToSI(counts.posts), - })} -
    • -
    • - {i18n.t("number_of_comments", { - count: Number(counts.comments), - formattedCount: numToSI(counts.comments), - })} -
    • -
    • - - {i18n.t("modlog")} - -
    • -
    - ); - } - handleCollapseSidebar(i: SiteSidebar) { i.setState({ collapsed: !i.state.collapsed }); } From cb72180f9728991a8f0b1937ef43735d96dee87e Mon Sep 17 00:00:00 2001 From: Jay Sitter Date: Fri, 16 Jun 2023 20:54:13 -0400 Subject: [PATCH 2/5] fix: Fix broken link --- src/shared/components/common/badges.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/shared/components/common/badges.tsx b/src/shared/components/common/badges.tsx index 44278950..2024ad2c 100644 --- a/src/shared/components/common/badges.tsx +++ b/src/shared/components/common/badges.tsx @@ -9,7 +9,7 @@ import { numToSI } from "../../utils"; interface BadgesProps { counts: CommunityAggregates | SiteAggregates; - community_view?: CommunityView; + community_view: CommunityView; } const isCommunityAggregates = ( @@ -96,7 +96,9 @@ export const Badges = ({ counts, community_view }: BadgesProps) => {
  • {i18n.t("modlog")} From fe64edd2fc26ec46fa5512f7ee13923b2ca6fae9 Mon Sep 17 00:00:00 2001 From: Jay Sitter Date: Fri, 16 Jun 2023 20:56:01 -0400 Subject: [PATCH 3/5] fix: Re-add 'number of communities' to site sidebar --- src/shared/components/common/badges.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/shared/components/common/badges.tsx b/src/shared/components/common/badges.tsx index 2024ad2c..56390342 100644 --- a/src/shared/components/common/badges.tsx +++ b/src/shared/components/common/badges.tsx @@ -18,6 +18,12 @@ const isCommunityAggregates = ( return "subscribers" in counts; }; +const isSiteAggregates = ( + counts: CommunityAggregates | SiteAggregates +): counts is SiteAggregates => { + return "communities" in counts; +}; + export const Badges = ({ counts, community_view }: BadgesProps) => { return (
      @@ -81,6 +87,14 @@ export const Badges = ({ counts, community_view }: BadgesProps) => { })} )} + {isSiteAggregates(counts) && ( +
    • + {i18n.t("number_of_communities", { + count: Number(counts.communities), + formattedCount: numToSI(counts.communities), + })} +
    • + )}
    • {i18n.t("number_of_posts", { count: Number(counts.posts), From f7f8ddcd7ca18ca4a1a6688c69766f275871a0a8 Mon Sep 17 00:00:00 2001 From: Jay Sitter Date: Fri, 16 Jun 2023 21:44:45 -0400 Subject: [PATCH 4/5] fix: Add total users in site sidebar --- src/shared/components/common/badges.tsx | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/shared/components/common/badges.tsx b/src/shared/components/common/badges.tsx index 56390342..858b9864 100644 --- a/src/shared/components/common/badges.tsx +++ b/src/shared/components/common/badges.tsx @@ -79,6 +79,22 @@ export const Badges = ({ counts, community_view }: BadgesProps) => { })}{" "} / {i18n.t("number_of_months", { count: 6, formattedCount: 6 })}
    • + {isSiteAggregates(counts) && ( + <> +
    • + {i18n.t("number_of_users", { + count: Number(counts.users), + formattedCount: numToSI(counts.users), + })} +
    • +
    • + {i18n.t("number_of_communities", { + count: Number(counts.communities), + formattedCount: numToSI(counts.communities), + })} +
    • + + )} {isCommunityAggregates(counts) && (
    • {i18n.t("number_of_subscribers", { @@ -87,14 +103,6 @@ export const Badges = ({ counts, community_view }: BadgesProps) => { })}
    • )} - {isSiteAggregates(counts) && ( -
    • - {i18n.t("number_of_communities", { - count: Number(counts.communities), - formattedCount: numToSI(counts.communities), - })} -
    • - )}
    • {i18n.t("number_of_posts", { count: Number(counts.posts), From d951fb57dab3e8e763f93ea4326d6fc4a4bd9e98 Mon Sep 17 00:00:00 2001 From: Jay Sitter Date: Fri, 16 Jun 2023 22:24:32 -0400 Subject: [PATCH 5/5] fix: Fix missing prop --- src/shared/components/common/badges.tsx | 10 ++++------ src/shared/components/community/sidebar.tsx | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/shared/components/common/badges.tsx b/src/shared/components/common/badges.tsx index 858b9864..49c0888b 100644 --- a/src/shared/components/common/badges.tsx +++ b/src/shared/components/common/badges.tsx @@ -1,7 +1,7 @@ import { Link } from "inferno-router"; import { CommunityAggregates, - CommunityView, + CommunityId, SiteAggregates, } from "lemmy-js-client"; import { i18n } from "../../i18next"; @@ -9,7 +9,7 @@ import { numToSI } from "../../utils"; interface BadgesProps { counts: CommunityAggregates | SiteAggregates; - community_view: CommunityView; + communityId?: CommunityId; } const isCommunityAggregates = ( @@ -24,7 +24,7 @@ const isSiteAggregates = ( return "communities" in counts; }; -export const Badges = ({ counts, community_view }: BadgesProps) => { +export const Badges = ({ counts, communityId }: BadgesProps) => { return (
      • {
      • {i18n.t("modlog")} diff --git a/src/shared/components/community/sidebar.tsx b/src/shared/components/community/sidebar.tsx index 711ab5e6..720e596f 100644 --- a/src/shared/components/community/sidebar.tsx +++ b/src/shared/components/community/sidebar.tsx @@ -159,7 +159,7 @@ export class Sidebar extends Component {
        {this.description()} {this.mods()}