From 004713d4cd1c196d59d7e21aed4864a99c970cee Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 20 Sep 2022 21:11:33 +0800 Subject: [PATCH] chore: update dropdown component --- web/src/components/MenuBtnsPopup.tsx | 82 ------------------- web/src/components/ResourcesDialog.tsx | 32 ++++++-- web/src/components/Settings/MemberSection.tsx | 38 ++++++--- web/src/components/UserBanner.tsx | 69 ++++++++++++++-- web/src/components/common/Dropdown.tsx | 35 +++++--- web/src/less/common/dropdown.less | 21 ----- web/src/less/menu-btns-popup.less | 13 --- web/src/less/resources-dialog.less | 6 -- web/src/less/settings/member-section.less | 6 -- 9 files changed, 139 insertions(+), 163 deletions(-) delete mode 100644 web/src/components/MenuBtnsPopup.tsx delete mode 100644 web/src/less/common/dropdown.less delete mode 100644 web/src/less/menu-btns-popup.less diff --git a/web/src/components/MenuBtnsPopup.tsx b/web/src/components/MenuBtnsPopup.tsx deleted file mode 100644 index 69b56486..00000000 --- a/web/src/components/MenuBtnsPopup.tsx +++ /dev/null @@ -1,82 +0,0 @@ -import { useEffect, useRef } from "react"; -import { useTranslation } from "react-i18next"; -import { useNavigate } from "react-router-dom"; -import { userService } from "../services"; -import Only from "./common/OnlyWhen"; -import showAboutSiteDialog from "./AboutSiteDialog"; -import showArchivedMemoDialog from "./ArchivedMemoDialog"; -import showResourcesDialog from "./ResourcesDialog"; -import "../less/menu-btns-popup.less"; - -interface Props { - shownStatus: boolean; - setShownStatus: (status: boolean) => void; -} - -const MenuBtnsPopup: React.FC = (props: Props) => { - const { shownStatus, setShownStatus } = props; - const { t } = useTranslation(); - const navigate = useNavigate(); - const popupElRef = useRef(null); - - useEffect(() => { - if (shownStatus) { - const handleClickOutside = (event: MouseEvent) => { - if (!popupElRef.current?.contains(event.target as Node)) { - event.stopPropagation(); - } - setShownStatus(false); - }; - window.addEventListener("click", handleClickOutside, { - capture: true, - once: true, - }); - } - }, [shownStatus]); - - const handleResourcesBtnClick = () => { - showResourcesDialog(); - }; - - const handleArchivedBtnClick = () => { - showArchivedMemoDialog(); - }; - - const handleAboutBtnClick = () => { - showAboutSiteDialog(); - }; - - const handleSignOutBtnClick = async () => { - userService - .doSignOut() - .then(() => { - navigate("/auth"); - }) - .catch(() => { - // do nth - }); - }; - - return ( -
- - - - - - - - -
- ); -}; - -export default MenuBtnsPopup; diff --git a/web/src/components/ResourcesDialog.tsx b/web/src/components/ResourcesDialog.tsx index e4f2878c..b8b54e6c 100644 --- a/web/src/components/ResourcesDialog.tsx +++ b/web/src/components/ResourcesDialog.tsx @@ -154,13 +154,31 @@ const ResourcesDialog: React.FC = (props: Props) => { {resource.filename} {resource.type}
- - - - - + + + + + + } + />
)) diff --git a/web/src/components/Settings/MemberSection.tsx b/web/src/components/Settings/MemberSection.tsx index 592a536f..635025b2 100644 --- a/web/src/components/Settings/MemberSection.tsx +++ b/web/src/components/Settings/MemberSection.tsx @@ -139,18 +139,36 @@ const PreferencesSection = () => { {currentUser?.id === user.id ? ( {t("common.yourself")} ) : ( - - {user.rowStatus === "NORMAL" ? ( - - ) : ( + - - + {user.rowStatus === "NORMAL" ? ( + + ) : ( + <> + + + + )} - )} - + } + /> )} diff --git a/web/src/components/UserBanner.tsx b/web/src/components/UserBanner.tsx index a97547d1..6afa0878 100644 --- a/web/src/components/UserBanner.tsx +++ b/web/src/components/UserBanner.tsx @@ -1,18 +1,23 @@ import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; +import { useNavigate } from "react-router-dom"; import * as utils from "../helpers/utils"; import userService from "../services/userService"; import { locationService } from "../services"; import { useAppSelector } from "../store"; import Icon from "./Icon"; -import MenuBtnsPopup from "./MenuBtnsPopup"; +import Dropdown from "./common/Dropdown"; +import Only from "./common/OnlyWhen"; +import showResourcesDialog from "./ResourcesDialog"; +import showArchivedMemoDialog from "./ArchivedMemoDialog"; +import showAboutSiteDialog from "./AboutSiteDialog"; import "../less/user-banner.less"; const UserBanner = () => { const { t } = useTranslation(); + const navigate = useNavigate(); const { user, owner } = useAppSelector((state) => state.user); const { memos, tags } = useAppSelector((state) => state.memo); - const [shouldShowPopupBtns, setShouldShowPopupBtns] = useState(false); const [username, setUsername] = useState("Memos"); const [createdDays, setCreatedDays] = useState(0); const isVisitorMode = userService.isVisitorMode(); @@ -34,8 +39,27 @@ const UserBanner = () => { locationService.clearQuery(); }, []); - const handlePopupBtnClick = () => { - setShouldShowPopupBtns(true); + const handleResourcesBtnClick = () => { + showResourcesDialog(); + }; + + const handleArchivedBtnClick = () => { + showArchivedMemoDialog(); + }; + + const handleAboutBtnClick = () => { + showAboutSiteDialog(); + }; + + const handleSignOutBtnClick = async () => { + userService + .doSignOut() + .then(() => { + navigate("/auth"); + }) + .catch(() => { + // do nth + }); }; return ( @@ -45,10 +69,39 @@ const UserBanner = () => { {username} {!isVisitorMode && user?.role === "HOST" ? MOD : null} - - + } + actionsClassName="!w-36" + actions={ + <> + + + + + + + + + + } + />
diff --git a/web/src/components/common/Dropdown.tsx b/web/src/components/common/Dropdown.tsx index e90db05d..477e1fec 100644 --- a/web/src/components/common/Dropdown.tsx +++ b/web/src/components/common/Dropdown.tsx @@ -1,15 +1,16 @@ import { ReactNode, useEffect, useRef } from "react"; import useToggle from "../../hooks/useToggle"; import Icon from "../Icon"; -import "../../less/common/dropdown.less"; -interface DropdownProps { - children?: ReactNode; +interface Props { + trigger?: ReactNode; + actions?: ReactNode; className?: string; + actionsClassName?: string; } -const Dropdown: React.FC = (props: DropdownProps) => { - const { children, className } = props; +const Dropdown: React.FC = (props: Props) => { + const { trigger, actions, className, actionsClassName } = props; const [dropdownStatus, toggleDropdownStatus] = useToggle(false); const dropdownWrapperRef = useRef(null); @@ -28,11 +29,25 @@ const Dropdown: React.FC = (props: DropdownProps) => { }, [dropdownStatus]); return ( -
toggleDropdownStatus()}> - - - -
{children}
+
toggleDropdownStatus()} + > + {trigger ? ( + trigger + ) : ( + + )} +
+ {actions} +
); }; diff --git a/web/src/less/common/dropdown.less b/web/src/less/common/dropdown.less deleted file mode 100644 index 02de7b3d..00000000 --- a/web/src/less/common/dropdown.less +++ /dev/null @@ -1,21 +0,0 @@ -@import "../mixin.less"; - -.dropdown-wrapper { - @apply relative flex flex-col justify-start items-start select-none; - - > .trigger-button { - @apply flex flex-row justify-center items-center border p-1 rounded shadow text-gray-600 cursor-pointer hover:opacity-80; - - > .icon-img { - @apply w-4 h-auto; - } - } - - > .action-buttons-container { - @apply w-28 mt-1 absolute top-full right-0 flex flex-col justify-start items-start bg-white z-1 border p-1 rounded shadow; - - > button { - @apply w-full text-left px-2 text-sm leading-7 rounded hover:bg-gray-100; - } - } -} diff --git a/web/src/less/menu-btns-popup.less b/web/src/less/menu-btns-popup.less deleted file mode 100644 index 265ce707..00000000 --- a/web/src/less/menu-btns-popup.less +++ /dev/null @@ -1,13 +0,0 @@ -@import "./mixin.less"; - -.menu-btns-popup { - @apply absolute right-2 top-6 flex flex-col justify-start items-start mt-4 p-1 w-36 rounded-lg z-10 shadow bg-white; - - > .btn { - @apply flex flex-row justify-start items-center w-full py-2 px-3 text-base rounded text-left hover:bg-gray-100; - - > .icon { - @apply block w-6 text-center mr-2 text-base; - } - } -} diff --git a/web/src/less/resources-dialog.less b/web/src/less/resources-dialog.less index c5e2e7c1..bf3256a7 100644 --- a/web/src/less/resources-dialog.less +++ b/web/src/less/resources-dialog.less @@ -45,12 +45,6 @@ > .buttons-container { @apply w-full flex flex-row justify-end items-center; - - > .actions-dropdown { - .delete-btn { - @apply text-red-600; - } - } } } diff --git a/web/src/less/settings/member-section.less b/web/src/less/settings/member-section.less index 7edf7ad3..7328fad7 100644 --- a/web/src/less/settings/member-section.less +++ b/web/src/less/settings/member-section.less @@ -52,12 +52,6 @@ > .tip-text { @apply text-gray-400; } - - > .actions-dropdown { - .delete { - @apply text-red-600; - } - } } } }