diff --git a/src/components/pages/gallery/Collections.tsx b/src/components/pages/gallery/Collections.tsx index 0a50e75f9..6d55c829f 100644 --- a/src/components/pages/gallery/Collections.tsx +++ b/src/components/pages/gallery/Collections.tsx @@ -74,7 +74,14 @@ export default function Collections(props: CollectionProps) { const { selected, collections, setActiveCollection } = props; const [selectedCollectionID, setSelectedCollectionID] = useState(null); - const collectionRef = useRef(null); + const collectionWrapperRef = useRef(null); + const collectionChipsRef = props.collections.reduce( + (refMap, collection) => { + refMap[collection.id] = React.createRef(); + return refMap; + }, + {} + ); const [collectionShareModalView, setCollectionShareModalView] = useState(false); const [scrollObj, setScrollObj] = useState<{ @@ -84,24 +91,30 @@ export default function Collections(props: CollectionProps) { }>({}); const updateScrollObj = () => { - if (collectionRef.current) { + if (collectionWrapperRef.current) { const { scrollLeft, scrollWidth, clientWidth } = - collectionRef.current; + collectionWrapperRef.current; setScrollObj({ scrollLeft, scrollWidth, clientWidth }); } }; useEffect(() => { updateScrollObj(); - }, [collectionRef.current]); + }, [collectionWrapperRef.current]); useEffect(() => { - if (!collectionRef?.current) { + if (!collectionWrapperRef?.current) { return; } - collectionRef.current.scrollLeft = 0; + collectionWrapperRef.current.scrollLeft = 0; }, [collections]); + useEffect(() => { + collectionChipsRef[selected]?.current.scrollIntoView({ + inline: 'center', + }); + }, [selected]); + const clickHandler = (collectionID?: number) => () => { setSelectedCollectionID(collectionID); setActiveCollection(collectionID ?? ALL_SECTION); @@ -125,7 +138,7 @@ export default function Collections(props: CollectionProps) { }); const scrollCollection = (direction: SCROLL_DIRECTION) => () => { - collectionRef.current.scrollBy(250 * direction, 0); + collectionWrapperRef.current.scrollBy(250 * direction, 0); }; const renderTooltip = (collectionID) => { const fileCount = props.collectionFilesCount?.get(collectionID) ?? 0; @@ -135,10 +148,8 @@ export default function Collections(props: CollectionProps) { padding: '0', paddingBottom: '5px', }} - id="button-tooltip" - {...props}> + id="button-tooltip">
)} - + @@ -191,6 +204,7 @@ export default function Collections(props: CollectionProps) { delay={{ show: 250, hide: 400 }} overlay={renderTooltip(item.id)}> {item.name}