diff --git a/packages/client/src/hooks/use-dragable-width.ts b/packages/client/src/hooks/use-dragable-width.ts index 663a4597..5fc3ef86 100644 --- a/packages/client/src/hooks/use-dragable-width.ts +++ b/packages/client/src/hooks/use-dragable-width.ts @@ -1,9 +1,10 @@ import { clamp } from 'helpers/clamp'; import { getStorage, setStorage } from 'helpers/storage'; -import { useWindowSize } from 'hooks/use-window-size'; -import { useCallback, useEffect, useRef, useState } from 'react'; +import { useCallback, useMemo, useRef } from 'react'; import { useQuery } from 'react-query'; +import { IsOnMobile } from './use-on-mobile'; + const key = 'dragable-menu-width'; const DEFAULT_PC_MIN_WIDTH = 240; @@ -15,12 +16,13 @@ const DEFAULT_MOBILE_MAX_WIDTH = 240; // 收起宽度:24 const COLLAPSED_WIDTH = 24; -const PC_MOBILE_CRITICAL_WIDTH = 765; - export const useDragableWidth = () => { - const { width: windowWidth } = useWindowSize(); - const [minWidth, setMinWidth] = useState(DEFAULT_MOBILE_MIN_WIDTH); - const [maxWidth, setMaxWidth] = useState(DEFAULT_MOBILE_MAX_WIDTH); + const { isMobile } = IsOnMobile.useHook(); + const [minWidth, maxWidth] = useMemo( + () => + isMobile ? [DEFAULT_MOBILE_MIN_WIDTH, DEFAULT_MOBILE_MAX_WIDTH] : [DEFAULT_PC_MIN_WIDTH, DEFAULT_PC_MAX_WIDTH], + [isMobile] + ); const { data: currentWidth, refetch } = useQuery(key, () => { const nextWidth = getStorage(key, minWidth); @@ -34,8 +36,6 @@ export const useDragableWidth = () => { const updateWidth = useCallback( (size) => { - const isMobile = windowWidth <= PC_MOBILE_CRITICAL_WIDTH; - if (isMobile && size < maxWidth) { size = minWidth; } @@ -43,7 +43,7 @@ export const useDragableWidth = () => { prevWidthRef.current = size; refetch(); }, - [refetch, windowWidth, minWidth, maxWidth] + [isMobile, minWidth, maxWidth, refetch] ); const toggleCollapsed = useCallback(() => { @@ -68,13 +68,6 @@ export const useDragableWidth = () => { refetch(); }, [refetch, currentWidth, minWidth, maxWidth]); - useEffect(() => { - const min = windowWidth <= PC_MOBILE_CRITICAL_WIDTH ? DEFAULT_MOBILE_MIN_WIDTH : DEFAULT_PC_MIN_WIDTH; - const max = windowWidth <= PC_MOBILE_CRITICAL_WIDTH ? DEFAULT_MOBILE_MAX_WIDTH : DEFAULT_PC_MAX_WIDTH; - setMinWidth(min); - setMaxWidth(max); - }, [windowWidth, refetch, currentWidth]); - return { minWidth, maxWidth, diff --git a/packages/client/src/layouts/double-column/index.tsx b/packages/client/src/layouts/double-column/index.tsx index 39fca9be..51c34b4f 100644 --- a/packages/client/src/layouts/double-column/index.tsx +++ b/packages/client/src/layouts/double-column/index.tsx @@ -1,8 +1,9 @@ import { IconChevronLeft, IconChevronRight } from '@douyinfe/semi-icons'; import { Button, Layout as SemiLayout } from '@douyinfe/semi-ui'; import cls from 'classnames'; +import { debounce } from 'helpers/debounce'; import { useDragableWidth } from 'hooks/use-dragable-width'; -import React from 'react'; +import React, { useMemo } from 'react'; import SplitPane from 'react-split-pane'; import { RouterHeader } from '../router-header'; @@ -17,12 +18,13 @@ interface IProps { export const DoubleColumnLayout: React.FC = ({ leftNode, rightNode }) => { const { minWidth, maxWidth, width, isCollapsed, updateWidth, toggleCollapsed } = useDragableWidth(); + const debounceUpdate = useMemo(() => debounce(updateWidth, 200), [updateWidth]); return ( - +