From a44fc51007a566ce73b27ecb858c7c70611c46e3 Mon Sep 17 00:00:00 2001 From: t0ng7u Date: Wed, 16 Jul 2025 02:54:58 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=B1=20refactor(web):=20remove=20legacy?= =?UTF-8?q?=20isMobile=20util=20and=20migrate=20to=20useIsMobile=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BREAKING CHANGE: helpers/utils.js no longer exports `isMobile()`. Any external code that relied on this function must switch to the `useIsMobile` React hook. Summary ------- 1. Deleted the obsolete `isMobile()` function from helpers/utils.js. 2. Introduced `MOBILE_BREAKPOINT` constant and `matchMedia`-based detection for non-React contexts. 3. Reworked toast positioning logic in utils.js to rely on `matchMedia`. 4. Updated render.js: • Removed isMobile import. • Added MOBILE_BREAKPOINT detection in `truncateText`. 5. Migrated every page/component to the `useIsMobile` hook: • Layout: HeaderBar, PageLayout, SiderBar • Pages: Home, Detail, Playground, User (Add/Edit), Token, Channel, Redemption, Ratio Sync • Components: ChannelsTable, ChannelSelectorModal, ConflictConfirmModal 6. Purged all remaining `isMobile()` calls and legacy imports. 7. Added missing `const isMobile = useIsMobile()` declarations where required. Benefits -------- • Unifies mobile detection with a React-friendly hook. • Eliminates duplicated logic and improves maintainability. • Keeps non-React helpers lightweight by using `matchMedia` directly. --- web/src/components/layout/HeaderBar.js | 22 +- web/src/components/layout/PageLayout.js | 43 ++-- web/src/components/layout/SiderBar.js | 60 ++--- .../settings/ChannelSelectorModal.js | 5 +- web/src/components/table/ChannelsTable.js | 6 +- web/src/context/Style/index.js | 227 ------------------ web/src/helpers/render.js | 6 +- web/src/helpers/utils.js | 8 +- web/src/hooks/useIsMobile.js | 15 ++ web/src/hooks/useSidebarCollapsed.js | 22 ++ web/src/index.css | 16 ++ web/src/index.js | 5 +- web/src/pages/Channel/EditChannel.js | 5 +- web/src/pages/Detail/index.js | 8 +- web/src/pages/Home/index.js | 14 +- web/src/pages/Playground/index.js | 39 ++- web/src/pages/Redemption/EditRedemption.js | 5 +- .../pages/Setting/Ratio/UpstreamRatioSync.js | 7 +- web/src/pages/Token/EditToken.js | 5 +- web/src/pages/User/AddUser.js | 6 +- web/src/pages/User/EditUser.js | 5 +- 21 files changed, 176 insertions(+), 353 deletions(-) delete mode 100644 web/src/context/Style/index.js create mode 100644 web/src/hooks/useIsMobile.js create mode 100644 web/src/hooks/useSidebarCollapsed.js diff --git a/web/src/components/layout/HeaderBar.js b/web/src/components/layout/HeaderBar.js index 98a7e17b..9aeb4a79 100644 --- a/web/src/components/layout/HeaderBar.js +++ b/web/src/components/layout/HeaderBar.js @@ -31,13 +31,15 @@ import { Badge, } from '@douyinfe/semi-ui'; import { StatusContext } from '../../context/Status/index.js'; -import { useStyle, styleActions } from '../../context/Style/index.js'; +import { useIsMobile } from '../../hooks/useIsMobile.js'; +import { useSidebarCollapsed } from '../../hooks/useSidebarCollapsed.js'; -const HeaderBar = () => { +const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => { const { t, i18n } = useTranslation(); const [userState, userDispatch] = useContext(UserContext); const [statusState, statusDispatch] = useContext(StatusContext); - const { state: styleState, dispatch: styleDispatch } = useStyle(); + const isMobile = useIsMobile(); + const [collapsed, toggleCollapsed] = useSidebarCollapsed(); const [isLoading, setIsLoading] = useState(true); let navigate = useNavigate(); const [currentLang, setCurrentLang] = useState(i18n.language); @@ -207,7 +209,7 @@ const HeaderBar = () => { const handleNavLinkClick = (itemKey) => { if (itemKey === 'home') { - styleDispatch(styleActions.setSider(false)); + // styleDispatch(styleActions.setSider(false)); // This line is removed } setMobileMenuOpen(false); }; @@ -293,7 +295,7 @@ const HeaderBar = () => { placeholder={ } /> @@ -388,7 +390,7 @@ const HeaderBar = () => { const registerButtonTextSpanClass = "!text-xs !text-white !p-1.5"; if (showRegisterButton) { - if (styleState.isMobile) { + if (isMobile) { loginButtonClasses += " !rounded-full"; } else { loginButtonClasses += " !rounded-l-full !rounded-r-none"; @@ -436,7 +438,7 @@ const HeaderBar = () => { 0 ? 'system' : 'inApp'} unreadKeys={getUnreadKeys()} /> @@ -447,18 +449,18 @@ const HeaderBar = () => { {isDemoSiteMode && statusState?.status?.version ? (