From bb4f415ae125afe768d18cc501e1a85a7f0c46eb Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 9 Sep 2024 17:39:13 +0530 Subject: [PATCH] Inline --- .../Collections/CollectionListBar/index.tsx | 80 ++++++++++++++++--- .../shared/hooks/useComponentScroll.tsx | 66 --------------- 2 files changed, 69 insertions(+), 77 deletions(-) diff --git a/web/apps/photos/src/components/Collections/CollectionListBar/index.tsx b/web/apps/photos/src/components/Collections/CollectionListBar/index.tsx index f4a2e4a040..55f7231556 100644 --- a/web/apps/photos/src/components/Collections/CollectionListBar/index.tsx +++ b/web/apps/photos/src/components/Collections/CollectionListBar/index.tsx @@ -2,9 +2,6 @@ import { IconButtonWithBG, SpaceBetweenFlex, } from "@ente/shared/components/Container"; -import useComponentScroll, { - SCROLL_DIRECTION, -} from "@ente/shared/hooks/useComponentScroll"; import useWindowSize from "@ente/shared/hooks/useWindowSize"; import ExpandMore from "@mui/icons-material/ExpandMore"; import NavigateNextIcon from "@mui/icons-material/NavigateNext"; @@ -23,7 +20,7 @@ import { } from "components/Collections/styledComponents"; import { t } from "i18next"; import memoize from "memoize-one"; -import React, { useEffect } from "react"; +import React, { useEffect, useRef, useState } from "react"; import AutoSizer from "react-virtualized-auto-sizer"; import { FixedSizeList as List, @@ -210,7 +207,74 @@ const CollectionListBar = (props: IProps) => { export default CollectionListBar; -const Wrapper = styled("button")<{ direction: SCROLL_DIRECTION }>` +enum SCROLL_DIRECTION { + LEFT = -1, + RIGHT = +1, +} + +function useComponentScroll({ dependencies }: { dependencies: any[] }) { + const componentRef = useRef(null); + + const [scrollObj, setScrollObj] = useState<{ + scrollLeft?: number; + scrollWidth?: number; + clientWidth?: number; + }>({}); + + const updateScrollObj = () => { + if (!componentRef.current) { + return; + } + const { scrollLeft, scrollWidth, clientWidth } = componentRef.current; + setScrollObj({ scrollLeft, scrollWidth, clientWidth }); + }; + + useEffect(() => { + if (!componentRef.current) { + return; + } + // Add event listener + componentRef.current?.addEventListener("scroll", updateScrollObj); + + // Call handler right away so state gets updated with initial window size + updateScrollObj(); + // Remove event listener on cleanup + return () => + componentRef.current?.removeEventListener( + "resize", + updateScrollObj, + ); + }, [componentRef.current]); + + useEffect(() => { + updateScrollObj(); + }, [...dependencies]); + + const scrollComponent = (direction: SCROLL_DIRECTION) => () => { + componentRef.current.scrollBy(250 * direction, 0); + }; + + const hasScrollBar = scrollObj.scrollWidth > scrollObj.clientWidth; + const onFarLeft = scrollObj.scrollLeft === 0; + const onFarRight = + scrollObj.scrollLeft + scrollObj.clientWidth === scrollObj.scrollWidth; + + return { + hasScrollBar, + onFarLeft, + onFarRight, + scrollComponent, + componentRef, + }; +} + +const ScrollButton = ({ scrollDirection, ...rest }) => ( + + + +); + +const ScrollButtonWrapper = styled("button")<{ direction: SCROLL_DIRECTION }>` position: absolute; z-index: 2; top: 7px; @@ -246,9 +310,3 @@ const Wrapper = styled("button")<{ direction: SCROLL_DIRECTION }>` width: 30px; } `; - -const ScrollButton = ({ scrollDirection, ...rest }) => ( - - - -); diff --git a/web/packages/shared/hooks/useComponentScroll.tsx b/web/packages/shared/hooks/useComponentScroll.tsx index 9cdf264559..e69de29bb2 100644 --- a/web/packages/shared/hooks/useComponentScroll.tsx +++ b/web/packages/shared/hooks/useComponentScroll.tsx @@ -1,66 +0,0 @@ -import { useEffect, useRef, useState } from "react"; - -export enum SCROLL_DIRECTION { - LEFT = -1, - RIGHT = +1, -} - -export default function useComponentScroll({ - dependencies, -}: { - dependencies: any[]; -}) { - const componentRef = useRef(null); - - const [scrollObj, setScrollObj] = useState<{ - scrollLeft?: number; - scrollWidth?: number; - clientWidth?: number; - }>({}); - - const updateScrollObj = () => { - if (!componentRef.current) { - return; - } - const { scrollLeft, scrollWidth, clientWidth } = componentRef.current; - setScrollObj({ scrollLeft, scrollWidth, clientWidth }); - }; - - useEffect(() => { - if (!componentRef.current) { - return; - } - // Add event listener - componentRef.current?.addEventListener("scroll", updateScrollObj); - - // Call handler right away so state gets updated with initial window size - updateScrollObj(); - // Remove event listener on cleanup - return () => - componentRef.current?.removeEventListener( - "resize", - updateScrollObj, - ); - }, [componentRef.current]); - - useEffect(() => { - updateScrollObj(); - }, [...dependencies]); - - const scrollComponent = (direction: SCROLL_DIRECTION) => () => { - componentRef.current.scrollBy(250 * direction, 0); - }; - - const hasScrollBar = scrollObj.scrollWidth > scrollObj.clientWidth; - const onFarLeft = scrollObj.scrollLeft === 0; - const onFarRight = - scrollObj.scrollLeft + scrollObj.clientWidth === scrollObj.scrollWidth; - - return { - hasScrollBar, - onFarLeft, - onFarRight, - scrollComponent, - componentRef, - }; -}