Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Inline

Manav Rathic23bb13187be
diff --git a/web/apps/photos/src/components/Collections/CollectionListBar/index.tsx b/web/apps/photos/src/components/Collections/CollectionListBar/index.tsx
index 53b7bad9d4..3db5c8e979 100644
--- a/web/apps/photos/src/components/Collections/CollectionListBar/index.tsx
+++ b/web/apps/photos/src/components/Collections/CollectionListBar/index.tsx
@@ -30,7 +30,7 @@ import { CollectionSummary } from "types/collection";
import { ALL_SECTION, COLLECTION_LIST_SORT_BY } from "utils/collection";
import CollectionListSortBy from "../CollectionListSortBy";
-interface IProps {
+interface CollectionListBarProps {
activeCollectionID?: number;
isInHiddenSection: boolean;
setActiveCollectionID: (id?: number) => void;
@@ -40,73 +40,67 @@ interface IProps {
setCollectionListSortBy: (v: COLLECTION_LIST_SORT_BY) => void;
}
-interface ItemData {
- collectionSummaries: CollectionSummary[];
- activeCollectionID?: number;
- onCollectionClick: (id?: number) => void;
-}
-
-const CollectionListBarCardWidth = 94;
+export const CollectionListBar: React.FC<CollectionListBarProps> = ({
+ activeCollectionID,
+ setActiveCollectionID,
+ collectionSummaries,
+ showAllCollections,
+ isInHiddenSection,
+ setCollectionListSortBy,
+ collectionListSortBy,
+}) => {
+ const windowSize = useWindowSize();
+ const isMobile = useMediaQuery("(max-width: 428px)");
-const createItemData = memoize(
- (collectionSummaries, activeCollectionID, onCollectionClick) => ({
- collectionSummaries,
- activeCollectionID,
- onCollectionClick,
- }),
-);
+ const collectionListWrapperRef = useRef<HTMLDivElement>(null);
+ const collectionListRef = React.useRef(null);
-const CollectionCardContainer = React.memo(
- ({
- data,
- index,
- style,
- isScrolling,
- }: ListChildComponentProps<ItemData>) => {
- const { collectionSummaries, activeCollectionID, onCollectionClick } =
- data;
+ const [scrollObj, setScrollObj] = useState<{
+ scrollLeft?: number;
+ scrollWidth?: number;
+ clientWidth?: number;
+ }>({});
- const collectionSummary = collectionSummaries[index];
+ const updateScrollObj = () => {
+ if (!collectionListWrapperRef.current) {
+ return;
+ }
+ const { scrollLeft, scrollWidth, clientWidth } =
+ collectionListWrapperRef.current;
+ setScrollObj({ scrollLeft, scrollWidth, clientWidth });
+ };
- return (
- <div style={style}>
- <CollectionListBarCard
- key={collectionSummary.id}
- activeCollectionID={activeCollectionID}
- isScrolling={isScrolling}
- collectionSummary={collectionSummary}
- onCollectionClick={onCollectionClick}
- />
- </div>
+ useEffect(() => {
+ if (!collectionListWrapperRef.current) {
+ return;
+ }
+ // Add event listener
+ collectionListWrapperRef.current?.addEventListener(
+ "scroll",
+ updateScrollObj,
);
- },
- areEqual,
-);
-const getItemKey = (index: number, data: ItemData) => {
- return `${data.collectionSummaries[index].id}-${data.collectionSummaries[index].coverFile?.id}`;
-};
-
-const CollectionListBar = (props: IProps) => {
- const {
- activeCollectionID,
- setActiveCollectionID,
- collectionSummaries,
- showAllCollections,
- isInHiddenSection,
- } = props;
+ // Call handler right away so state gets updated with initial window size
+ updateScrollObj();
+ // Remove event listener on cleanup
+ return () =>
+ collectionListWrapperRef.current?.removeEventListener(
+ "resize",
+ updateScrollObj,
+ );
+ }, [collectionListWrapperRef.current]);
- const windowSize = useWindowSize();
- const isMobile = useMediaQuery("(max-width: 428px)");
+ useEffect(() => {
+ updateScrollObj();
+ }, [windowSize, collectionSummaries]);
- const {
- componentRef: collectionListWrapperRef,
- scrollComponent,
- onFarLeft,
- onFarRight,
- } = useComponentScroll([windowSize, collectionSummaries]);
+ const scrollComponent = (direction: number) => () => {
+ collectionListWrapperRef.current.scrollBy(250 * direction, 0);
+ };
- const collectionListRef = React.useRef(null);
+ const onFarLeft = scrollObj.scrollLeft === 0;
+ const onFarRight =
+ scrollObj.scrollLeft + scrollObj.clientWidth === scrollObj.scrollWidth;
useEffect(() => {
if (!collectionListRef.current) {
@@ -138,8 +132,8 @@ const CollectionListBar = (props: IProps) => {
{isMobile && (
<Box display="flex" alignItems={"center"} gap={1}>
<CollectionListSortBy
- setSortBy={props.setCollectionListSortBy}
- activeSortBy={props.collectionListSortBy}
+ setSortBy={setCollectionListSortBy}
+ activeSortBy={collectionListSortBy}
disableBG
/>
<IconButton onClick={showAllCollections}>
@@ -183,8 +177,8 @@ const CollectionListBar = (props: IProps) => {
height={"64px"}
>
<CollectionListSortBy
- setSortBy={props.setCollectionListSortBy}
- activeSortBy={props.collectionListSortBy}
+ setSortBy={setCollectionListSortBy}
+ activeSortBy={collectionListSortBy}
/>
<IconButtonWithBG onClick={showAllCollections}>
<ExpandMore />
@@ -196,62 +190,51 @@ const CollectionListBar = (props: IProps) => {
);
};
-export default CollectionListBar;
-
-const useComponentScroll = (deps: React.DependencyList) => {
- const componentRef = useRef<HTMLDivElement>(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 });
- };
+interface ItemData {
+ collectionSummaries: CollectionSummary[];
+ activeCollectionID?: number;
+ onCollectionClick: (id?: number) => void;
+}
- useEffect(() => {
- if (!componentRef.current) {
- return;
- }
- // Add event listener
- componentRef.current?.addEventListener("scroll", updateScrollObj);
+const CollectionListBarCardWidth = 94;
- // 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]);
+const createItemData = memoize(
+ (collectionSummaries, activeCollectionID, onCollectionClick) => ({
+ collectionSummaries,
+ activeCollectionID,
+ onCollectionClick,
+ }),
+);
- useEffect(() => {
- updateScrollObj();
- }, deps);
+const CollectionCardContainer = React.memo(
+ ({
+ data,
+ index,
+ style,
+ isScrolling,
+ }: ListChildComponentProps<ItemData>) => {
+ const { collectionSummaries, activeCollectionID, onCollectionClick } =
+ data;
- const scrollComponent = (direction: number) => () => {
- componentRef.current.scrollBy(250 * direction, 0);
- };
+ const collectionSummary = collectionSummaries[index];
- const hasScrollBar = scrollObj.scrollWidth > scrollObj.clientWidth;
- const onFarLeft = scrollObj.scrollLeft === 0;
- const onFarRight =
- scrollObj.scrollLeft + scrollObj.clientWidth === scrollObj.scrollWidth;
+ return (
+ <div style={style}>
+ <CollectionListBarCard
+ key={collectionSummary.id}
+ activeCollectionID={activeCollectionID}
+ isScrolling={isScrolling}
+ collectionSummary={collectionSummary}
+ onCollectionClick={onCollectionClick}
+ />
+ </div>
+ );
+ },
+ areEqual,
+);
- return {
- hasScrollBar,
- onFarLeft,
- onFarRight,
- scrollComponent,
- componentRef,
- };
+const getItemKey = (index: number, data: ItemData) => {
+ return `${data.collectionSummaries[index].id}-${data.collectionSummaries[index].coverFile?.id}`;
};
const ScrollButtonBase: React.FC<
diff --git a/web/apps/photos/src/components/Collections/index.tsx b/web/apps/photos/src/components/Collections/index.tsx
index 91c1a51082..60519984da 100644
--- a/web/apps/photos/src/components/Collections/index.tsx
+++ b/web/apps/photos/src/components/Collections/index.tsx
@@ -2,7 +2,7 @@ import { useLocalState } from "@ente/shared/hooks/useLocalState";
import { LS_KEYS } from "@ente/shared/storage/localStorage";
import AllCollections from "components/Collections/AllCollections";
import CollectionInfoWithOptions from "components/Collections/CollectionInfoWithOptions";
-import CollectionListBar from "components/Collections/CollectionListBar";
+import { CollectionListBar } from "components/Collections/CollectionListBar";
import { SetCollectionNamerAttributes } from "components/Collections/CollectionNamer";
import CollectionShare from "components/Collections/CollectionShare";
import { ITEM_TYPE, TimeStampListItem } from "components/PhotoList";