diff --git a/src/components/ConditionalLink.tsx b/src/components/ConditionalLink.tsx new file mode 100644 index 0000000..8e07220 --- /dev/null +++ b/src/components/ConditionalLink.tsx @@ -0,0 +1,63 @@ +// Copyright (c) 2020-2021 Drew Lemmy +// This file is part of KristWeb 2 under GPL-3.0. +// Full details: https://github.com/tmpim/KristWeb2/blob/master/LICENSE.txt +import React, { FC } from "react"; + +import { Link, useRouteMatch } from "react-router-dom"; + +interface Props { + to: string; + condition?: boolean; + + replace?: boolean; + + matchTo?: boolean; + matchPath?: string; + matchExact?: boolean; + matchStrict?: boolean; + matchSensitive?: boolean; +} + +export const ConditionalLink: FC = ({ + to, + condition, + + replace, + + matchTo, + matchPath, + matchExact, + matchStrict, + matchSensitive, + + children, ...props +}): JSX.Element => { + // Disable the link if we're already on that route + const wantsCondition = condition !== undefined; + const wantsMatch = matchTo || !!matchPath; + + const match = useRouteMatch(wantsMatch ? { + path: matchTo && to ? to : matchPath, + exact: matchExact, + strict: matchStrict, + sensitive: matchSensitive + } : {}); + + const active = (!wantsCondition || !!condition) && (!wantsMatch || !match); + + return active && to + ? ( + + {children} + + ) + : ( + + {children} + + ); +}; diff --git a/src/components/addresses/ContextualAddress.less b/src/components/addresses/ContextualAddress.less index 256c71a..d6defe9 100644 --- a/src/components/addresses/ContextualAddress.less +++ b/src/components/addresses/ContextualAddress.less @@ -19,7 +19,7 @@ opacity: 0.8; } - &.contextual-address-non-existent a { + &.contextual-address-non-existent { color: @text-color-secondary; cursor: not-allowed; diff --git a/src/components/addresses/ContextualAddress.tsx b/src/components/addresses/ContextualAddress.tsx index ad82cb5..b3dfd21 100644 --- a/src/components/addresses/ContextualAddress.tsx +++ b/src/components/addresses/ContextualAddress.tsx @@ -17,6 +17,7 @@ import { useBooleanSetting } from "@utils/settings"; import { KristNameLink } from "../names/KristNameLink"; +import { ConditionalLink } from "@comp/ConditionalLink"; import "./ContextualAddress.less"; @@ -111,10 +112,10 @@ }); /** The label of the wallet, or the address itself (not a metaname) */ - function AddressContent(): JSX.Element { + function AddressContent(props: any): JSX.Element { return wallet && wallet.label - ? {wallet.label} - : {address}; + ? {wallet.label} + : {address}; } return @@ -137,13 +138,14 @@ /> ) : ( - nonExistent - ? - : ( - - - - ) + + + ) } diff --git a/src/layout/nav/AppHeader.tsx b/src/layout/nav/AppHeader.tsx index 09c9d44..f787e07 100644 --- a/src/layout/nav/AppHeader.tsx +++ b/src/layout/nav/AppHeader.tsx @@ -13,6 +13,8 @@ import { ConnectionIndicator } from "./ConnectionIndicator"; import { CymbalIndicator } from "./CymbalIndicator"; +import { ConditionalLink } from "@comp/ConditionalLink"; + const { useBreakpoint } = Grid; interface Props { @@ -58,7 +60,7 @@ {/* Settings button */} } title={t("nav.settings")}> - + ; diff --git a/src/layout/nav/Brand.tsx b/src/layout/nav/Brand.tsx index a7ec5ee..1026c05 100644 --- a/src/layout/nav/Brand.tsx +++ b/src/layout/nav/Brand.tsx @@ -2,8 +2,8 @@ // This file is part of KristWeb 2 under GPL-3.0. // Full details: https://github.com/tmpim/KristWeb2/blob/master/LICENSE.txt import React from "react"; +import { Tag } from "antd"; -import { Link } from "react-router-dom"; import { useTranslation } from "react-i18next"; import semverMajor from "semver/functions/major"; @@ -11,7 +11,7 @@ import semverPatch from "semver/functions/patch"; import semverPrerelease from "semver/functions/prerelease"; -import { Tag } from "antd"; +import { ConditionalLink } from "@comp/ConditionalLink"; import packageJson from "../../../package.json"; @@ -40,10 +40,10 @@ } return
- + {t("app.name")} v{major}.{minor}.{patch} {tag} - +
; } diff --git a/src/layout/sidebar/Sidebar.tsx b/src/layout/sidebar/Sidebar.tsx index 559e462..ba585f2 100644 --- a/src/layout/sidebar/Sidebar.tsx +++ b/src/layout/sidebar/Sidebar.tsx @@ -6,12 +6,14 @@ import { HomeOutlined, WalletOutlined, TeamOutlined, BankOutlined, TagsOutlined, SketchOutlined, BuildOutlined, StockOutlined } from "@ant-design/icons"; import { TFunction, useTranslation } from "react-i18next"; -import { Link, useLocation } from "react-router-dom"; +import { useLocation } from "react-router-dom"; import { ServiceWorkerCheck } from "./ServiceWorkerCheck"; import { SidebarTotalBalance } from "./SidebarTotalBalance"; import { SidebarFooter } from "./SidebarFooter"; +import { ConditionalLink } from "@comp/ConditionalLink"; + const { Sider } = Layout; type SidebarItemProps = MenuItemProps & { @@ -42,7 +44,9 @@ .filter(i => i.group === group) .map(i => ( - {t("sidebar." + i.name)} + + {t("sidebar." + i.name)} + )); } diff --git a/src/layout/sidebar/SidebarFooter.tsx b/src/layout/sidebar/SidebarFooter.tsx index aaad619..ac491c8 100644 --- a/src/layout/sidebar/SidebarFooter.tsx +++ b/src/layout/sidebar/SidebarFooter.tsx @@ -4,11 +4,11 @@ import React, { useState } from "react"; import { useTranslation, Trans } from "react-i18next"; -import { Link } from "react-router-dom"; - import { useMountEffect } from "@utils"; import { getAuthorInfo } from "@utils/credits"; +import { ConditionalLink } from "@comp/ConditionalLink"; + export function SidebarFooter(): JSX.Element { const { t } = useTranslation(); const [host, setHost] = useState<{ host: { name: string; url: string } } | undefined>(); @@ -41,7 +41,9 @@
{t("sidebar.github")}  –  - {t("sidebar.credits")} + + {t("sidebar.credits")} +
);