From 6c1cc1d2831afa52bb23def983f9ac858ebb100c Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 20 Sep 2022 23:30:25 +0800 Subject: [PATCH] chore: use conditional rendering instead of OnlyWhen --- web/src/components/AboutSiteDialog.tsx | 5 ++- web/src/components/Editor/Editor.tsx | 9 ++---- web/src/components/Memo.tsx | 5 ++- web/src/components/MemoCardDialog.tsx | 9 +++--- web/src/components/MemoList.tsx | 14 ++++----- web/src/components/MemoResources.tsx | 5 ++- web/src/components/ShareMemoImageDialog.tsx | 9 ++---- web/src/components/Sidebar.tsx | 23 +++++++------- web/src/components/TagList.tsx | 5 +-- web/src/components/UserBanner.tsx | 35 +++++++++++---------- web/src/components/common/OnlyWhen.tsx | 15 --------- web/src/pages/Auth.tsx | 9 ++---- web/src/pages/Explore.tsx | 25 +++++++-------- web/src/pages/Home.tsx | 9 ++---- web/src/pages/MemoDetail.tsx | 25 ++++++++------- 15 files changed, 82 insertions(+), 120 deletions(-) delete mode 100644 web/src/components/common/OnlyWhen.tsx diff --git a/web/src/components/AboutSiteDialog.tsx b/web/src/components/AboutSiteDialog.tsx index dacca902..942fb129 100644 --- a/web/src/components/AboutSiteDialog.tsx +++ b/web/src/components/AboutSiteDialog.tsx @@ -1,7 +1,6 @@ import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import * as api from "../helpers/api"; -import Only from "./common/OnlyWhen"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import GitHubBadge from "./GitHubBadge"; @@ -50,7 +49,7 @@ const AboutSiteDialog: React.FC = ({ destroy }: Props) => {
- + {profile !== undefined && ( <> {t("common.version")}: @@ -58,7 +57,7 @@ const AboutSiteDialog: React.FC = ({ destroy }: Props) => { 🎉 - + )}
diff --git a/web/src/components/Editor/Editor.tsx b/web/src/components/Editor/Editor.tsx index ed2d9445..aa29e500 100644 --- a/web/src/components/Editor/Editor.tsx +++ b/web/src/components/Editor/Editor.tsx @@ -1,7 +1,6 @@ import { forwardRef, ReactNode, useCallback, useEffect, useImperativeHandle, useRef } from "react"; import { useTranslation } from "react-i18next"; import useRefresh from "../../hooks/useRefresh"; -import Only from "../common/OnlyWhen"; import "../../less/editor.less"; export interface EditorRefActions { @@ -125,15 +124,13 @@ const Editor = forwardRef((props: EditorProps, ref: React.ForwardedRef
-
- {props.tools} -
+
{props.tools !== undefined && props.tools}
- + {showConfirmBtn && ( - + )}
diff --git a/web/src/components/Memo.tsx b/web/src/components/Memo.tsx index 318b6209..ec990dee 100644 --- a/web/src/components/Memo.tsx +++ b/web/src/components/Memo.tsx @@ -8,7 +8,6 @@ import { UNKNOWN_ID } from "../helpers/consts"; import { DONE_BLOCK_REG, TODO_BLOCK_REG } from "../helpers/marked"; import { editorStateService, locationService, memoService, userService } from "../services"; import Icon from "./Icon"; -import Only from "./common/OnlyWhen"; import toastHelper from "./Toast"; import MemoContent from "./MemoContent"; import MemoResources from "./MemoResources"; @@ -168,9 +167,9 @@ const Memo: React.FC = (props: Props) => {
{createdAtStr} - + {memo.visibility !== "PRIVATE" && !isVisitorMode && ( {memo.visibility} - + )}
diff --git a/web/src/components/MemoCardDialog.tsx b/web/src/components/MemoCardDialog.tsx index 8800b8c3..2dde33c7 100644 --- a/web/src/components/MemoCardDialog.tsx +++ b/web/src/components/MemoCardDialog.tsx @@ -5,7 +5,6 @@ import { useAppSelector } from "../store"; import { UNKNOWN_ID, VISIBILITY_SELECTOR_ITEMS } from "../helpers/consts"; import * as utils from "../helpers/utils"; import { formatMemoContent, MEMO_LINK_REG, parseHtmlToRawText } from "../helpers/marked"; -import Only from "./common/OnlyWhen"; import toastHelper from "./Toast"; import { generateDialog } from "./Dialog"; import Icon from "./Icon"; @@ -146,7 +145,7 @@ const MemoCardDialog: React.FC = (props: Props) => { return ( <> - + {!userService.isVisitorMode() && (
@@ -158,14 +157,14 @@ const MemoCardDialog: React.FC = (props: Props) => { />
-
+ )}

{utils.getDateTimeString(memo.createdTs)}

- + {!userService.isVisitorMode() && ( <> / - + )} diff --git a/web/src/components/MemoList.tsx b/web/src/components/MemoList.tsx index c0d5e1a3..54b4f1d6 100644 --- a/web/src/components/MemoList.tsx +++ b/web/src/components/MemoList.tsx @@ -6,7 +6,6 @@ import { IMAGE_URL_REG, LINK_URL_REG, MEMO_LINK_REG, TAG_REG } from "../helpers/ import * as utils from "../helpers/utils"; import { checkShouldShowMemoWithFilters } from "../helpers/filter"; import toastHelper from "./Toast"; -import Only from "./common/OnlyWhen"; import Memo from "./Memo"; import "../less/memo-list.less"; @@ -98,19 +97,18 @@ const MemoList = () => { return (
- -
-

{t("memo-list.fetching-data")}

-
-
{sortedMemos.map((memo) => ( ))} - + {isFetching ? ( +
+

{t("memo-list.fetching-data")}

+
+ ) : (

{sortedMemos.length === 0 ? t("message.no-memos") : showMemoFilter ? "" : t("message.memos-ready")}

-
+ )}
); }; diff --git a/web/src/components/MemoResources.tsx b/web/src/components/MemoResources.tsx index 4f4f5a7f..a94027a1 100644 --- a/web/src/components/MemoResources.tsx +++ b/web/src/components/MemoResources.tsx @@ -1,5 +1,4 @@ import { IMAGE_URL_REG } from "../helpers/marked"; -import Only from "./common/OnlyWhen"; import Image from "./Image"; import "../less/memo-resources.less"; @@ -14,13 +13,13 @@ const MemoResources: React.FC = (props: Props) => { return (
- 0}> + {imageUrls.length > 0 && (
{imageUrls.map((imgUrl, idx) => ( ))}
-
+ )}
); }; diff --git a/web/src/components/ShareMemoImageDialog.tsx b/web/src/components/ShareMemoImageDialog.tsx index 8e063d1c..78520e1a 100644 --- a/web/src/components/ShareMemoImageDialog.tsx +++ b/web/src/components/ShareMemoImageDialog.tsx @@ -5,7 +5,6 @@ import toImage from "../labs/html2image"; import { ANIMATION_DURATION } from "../helpers/consts"; import * as utils from "../helpers/utils"; import { IMAGE_URL_REG } from "../helpers/marked"; -import Only from "./common/OnlyWhen"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import toastHelper from "./Toast"; @@ -88,18 +87,16 @@ const ShareMemoImageDialog: React.FC = (props: Props) => {

{shortcutImgUrl ? "Click to save the image 👇" : "Generating the screenshot..."}

- - - + {shortcutImgUrl !== "" && } {memo.createdAtStr} - 0}> + {imageUrls.length > 0 && (
{imageUrls.map((imgUrl, idx) => ( ))}
-
+ )}
✍️ by {userinfo?.name} diff --git a/web/src/components/Sidebar.tsx b/web/src/components/Sidebar.tsx index 96f1cbdf..fbf665c0 100644 --- a/web/src/components/Sidebar.tsx +++ b/web/src/components/Sidebar.tsx @@ -2,7 +2,6 @@ import { Link } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { userService } from "../services"; import Icon from "./Icon"; -import Only from "./common/OnlyWhen"; import showDailyReviewDialog from "./DailyReviewDialog"; import showSettingDialog from "./SettingDialog"; import UserBanner from "./UserBanner"; @@ -31,18 +30,18 @@ const Sidebar = () => { - - - 🏂 {t("common.explore")} - - - + {!userService.isVisitorMode() && ( + <> + + 🏂 {t("common.explore")} + + + + )}
- - - + {!userService.isVisitorMode() && } ); diff --git a/web/src/components/TagList.tsx b/web/src/components/TagList.tsx index 689a16fe..564456e2 100644 --- a/web/src/components/TagList.tsx +++ b/web/src/components/TagList.tsx @@ -4,7 +4,6 @@ import { useAppSelector } from "../store"; import { locationService, memoService, userService } from "../services"; import useToggle from "../hooks/useToggle"; import Icon from "./Icon"; -import Only from "./common/OnlyWhen"; import "../less/tag-list.less"; interface Tag { @@ -73,9 +72,7 @@ const TagList = () => { {tags.map((t, idx) => ( ))} - -

{t("tag-list.tip-text")}

-
+ {!userService.isVisitorMode() && tags.length === 0 &&

{t("tag-list.tip-text")}

}
); diff --git a/web/src/components/UserBanner.tsx b/web/src/components/UserBanner.tsx index 6afa0878..7632007c 100644 --- a/web/src/components/UserBanner.tsx +++ b/web/src/components/UserBanner.tsx @@ -7,7 +7,6 @@ import { locationService } from "../services"; import { useAppSelector } from "../store"; import Icon from "./Icon"; import Dropdown from "./common/Dropdown"; -import Only from "./common/OnlyWhen"; import showResourcesDialog from "./ResourcesDialog"; import showArchivedMemoDialog from "./ArchivedMemoDialog"; import showAboutSiteDialog from "./AboutSiteDialog"; @@ -74,31 +73,33 @@ const UserBanner = () => { actionsClassName="!w-36" actions={ <> - - - - + {!userService.isVisitorMode() && ( + <> + + + + )} - + {!userService.isVisitorMode() && ( - + )} } /> diff --git a/web/src/components/common/OnlyWhen.tsx b/web/src/components/common/OnlyWhen.tsx deleted file mode 100644 index c366fbea..00000000 --- a/web/src/components/common/OnlyWhen.tsx +++ /dev/null @@ -1,15 +0,0 @@ -import { ReactNode } from "react"; - -interface OnlyWhenProps { - children: ReactNode; - when: boolean; -} - -const OnlyWhen: React.FC = (props: OnlyWhenProps) => { - const { children, when } = props; - return when ? <>{children} : null; -}; - -const Only = OnlyWhen; - -export default Only; diff --git a/web/src/pages/Auth.tsx b/web/src/pages/Auth.tsx index 20ad1091..2899861e 100644 --- a/web/src/pages/Auth.tsx +++ b/web/src/pages/Auth.tsx @@ -6,7 +6,6 @@ import { validate, ValidatorConfig } from "../helpers/validator"; import useLoading from "../hooks/useLoading"; import { globalService, userService } from "../services"; import Icon from "../components/Icon"; -import Only from "../components/common/OnlyWhen"; import toastHelper from "../components/Toast"; import "../less/auth.less"; @@ -139,17 +138,15 @@ const Auth = () => {
- + {!pageLoadingState.isLoading && ( - + )}

{siteHost || pageLoadingState.isLoading ? t("auth.not-host-tip") : t("auth.host-tip")} diff --git a/web/src/pages/Explore.tsx b/web/src/pages/Explore.tsx index b8f9225a..08f87868 100644 --- a/web/src/pages/Explore.tsx +++ b/web/src/pages/Explore.tsx @@ -6,7 +6,6 @@ import { memoService, userService } from "../services"; import { isNullorUndefined } from "../helpers/utils"; import { useAppSelector } from "../store"; import useLoading from "../hooks/useLoading"; -import Only from "../components/common/OnlyWhen"; import MemoContent from "../components/MemoContent"; import MemoResources from "../components/MemoResources"; import "../less/explore.less"; @@ -49,20 +48,18 @@ const Explore = () => { Explore

- - {user ? ( - - 🏠 {t("common.back-to-home")} - - ) : ( - - 👉 {t("common.sign-in")} - - )} - + {!loadingState.isLoading && user ? ( + + 🏠 {t("common.back-to-home")} + + ) : ( + + 👉 {t("common.sign-in")} + + )}
- + {!loadingState.isLoading && (
{state.memos.length > 0 ? ( state.memos.map((memo) => { @@ -85,7 +82,7 @@ const Explore = () => {

{t("message.no-memos")}

)}
-
+ )} ); diff --git a/web/src/pages/Home.tsx b/web/src/pages/Home.tsx index f43fdaba..62ba70eb 100644 --- a/web/src/pages/Home.tsx +++ b/web/src/pages/Home.tsx @@ -4,7 +4,6 @@ import { useLocation, useNavigate } from "react-router-dom"; import { globalService, userService } from "../services"; import { useAppSelector } from "../store"; import { isNullorUndefined } from "../helpers/utils"; -import Only from "../components/common/OnlyWhen"; import toastHelper from "../components/Toast"; import Sidebar from "../components/Sidebar"; import MemosHeader from "../components/MemosHeader"; @@ -51,13 +50,11 @@ function Home() {
- - - + {!userService.isVisitorMode() && }
- + {userService.isVisitorMode() && (
{user ? ( )}
-
+ )}
diff --git a/web/src/pages/MemoDetail.tsx b/web/src/pages/MemoDetail.tsx index 9778fb66..115172b5 100644 --- a/web/src/pages/MemoDetail.tsx +++ b/web/src/pages/MemoDetail.tsx @@ -7,7 +7,6 @@ import { UNKNOWN_ID } from "../helpers/consts"; import { isNullorUndefined } from "../helpers/utils"; import { useAppSelector } from "../store"; import useLoading from "../hooks/useLoading"; -import Only from "../components/common/OnlyWhen"; import MemoContent from "../components/MemoContent"; import MemoResources from "../components/MemoResources"; import "../less/explore.less"; @@ -55,17 +54,19 @@ const MemoDetail = () => {
- - {user ? ( - - 🏠 {t("common.back-to-home")} - - ) : ( - - 👉 {t("common.sign-in")} - - )} - + {!loadingState.isLoading && ( + <> + {user ? ( + + 🏠 {t("common.back-to-home")} + + ) : ( + + 👉 {t("common.sign-in")} + + )} + + )}
{!loadingState.isLoading && (