|
| 1 | +import type { ReactNode } from 'react'; |
| 2 | +import React, { useCallback, useEffect, useRef, useState } from 'react'; |
| 3 | +import { Box, Icon, IconButton, Icons, Scroll } from 'folds'; |
| 4 | +import type { IContent } from 'matrix-js-sdk'; |
| 5 | +import type { IGalleryContent, IGalleryItem } from '$types/matrix/common'; |
| 6 | +import { |
| 7 | + getIntersectionObserverEntry, |
| 8 | + useIntersectionObserver, |
| 9 | +} from '$hooks/useIntersectionObserver'; |
| 10 | +import * as css from './MGallery.css'; |
| 11 | + |
| 12 | +function galleryItemToContent(item: IGalleryItem): IContent { |
| 13 | + const { itemtype, ...rest } = item; |
| 14 | + return { ...rest, msgtype: itemtype } as IContent; |
| 15 | +} |
| 16 | + |
| 17 | +type MGalleryProps = { |
| 18 | + content: IGalleryContent; |
| 19 | + renderItem: (content: IContent, index: number) => ReactNode; |
| 20 | + renderCaption?: () => ReactNode; |
| 21 | +}; |
| 22 | + |
| 23 | +export function MGallery({ content, renderItem, renderCaption }: MGalleryProps) { |
| 24 | + const scrollRef = useRef<HTMLDivElement>(null); |
| 25 | + const backAnchorRef = useRef<HTMLDivElement>(null); |
| 26 | + const frontAnchorRef = useRef<HTMLDivElement>(null); |
| 27 | + const [backVisible, setBackVisible] = useState(true); |
| 28 | + const [frontVisible, setFrontVisible] = useState(true); |
| 29 | + |
| 30 | + const intersectionObserver = useIntersectionObserver( |
| 31 | + useCallback((entries) => { |
| 32 | + const backAnchor = backAnchorRef.current; |
| 33 | + const frontAnchor = frontAnchorRef.current; |
| 34 | + const backEntry = backAnchor && getIntersectionObserverEntry(backAnchor, entries); |
| 35 | + const frontEntry = frontAnchor && getIntersectionObserverEntry(frontAnchor, entries); |
| 36 | + if (backEntry) { |
| 37 | + setBackVisible(backEntry.isIntersecting); |
| 38 | + } |
| 39 | + if (frontEntry) { |
| 40 | + setFrontVisible(frontEntry.isIntersecting); |
| 41 | + } |
| 42 | + }, []), |
| 43 | + useCallback( |
| 44 | + () => ({ |
| 45 | + root: scrollRef.current, |
| 46 | + rootMargin: '10px', |
| 47 | + }), |
| 48 | + [] |
| 49 | + ) |
| 50 | + ); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + const backAnchor = backAnchorRef.current; |
| 54 | + const frontAnchor = frontAnchorRef.current; |
| 55 | + if (backAnchor) intersectionObserver?.observe(backAnchor); |
| 56 | + if (frontAnchor) intersectionObserver?.observe(frontAnchor); |
| 57 | + return () => { |
| 58 | + if (backAnchor) intersectionObserver?.unobserve(backAnchor); |
| 59 | + if (frontAnchor) intersectionObserver?.unobserve(frontAnchor); |
| 60 | + }; |
| 61 | + }, [intersectionObserver]); |
| 62 | + |
| 63 | + const handleScrollBack = () => { |
| 64 | + const scroll = scrollRef.current; |
| 65 | + if (!scroll) return; |
| 66 | + const { offsetWidth, scrollLeft } = scroll; |
| 67 | + scroll.scrollTo({ |
| 68 | + left: scrollLeft - offsetWidth / 1.3, |
| 69 | + behavior: 'smooth', |
| 70 | + }); |
| 71 | + }; |
| 72 | + const handleScrollFront = () => { |
| 73 | + const scroll = scrollRef.current; |
| 74 | + if (!scroll) return; |
| 75 | + const { offsetWidth, scrollLeft } = scroll; |
| 76 | + scroll.scrollTo({ |
| 77 | + left: scrollLeft + offsetWidth / 1.3, |
| 78 | + behavior: 'smooth', |
| 79 | + }); |
| 80 | + }; |
| 81 | + |
| 82 | + const items = content.itemtypes; |
| 83 | + |
| 84 | + return ( |
| 85 | + <Box direction="Column"> |
| 86 | + <Box className={css.GalleryHolder} direction="Column" style={{ position: 'relative' }}> |
| 87 | + <Scroll ref={scrollRef} direction="Horizontal" size="0" visibility="Hover" hideTrack> |
| 88 | + <Box shrink="No" alignItems="Center"> |
| 89 | + <div ref={backAnchorRef} /> |
| 90 | + {!backVisible && ( |
| 91 | + <> |
| 92 | + <div className={css.GalleryHolderGradient({ position: 'Left' })} /> |
| 93 | + <IconButton |
| 94 | + className={css.GalleryHolderBtn({ position: 'Left' })} |
| 95 | + variant="Secondary" |
| 96 | + radii="Pill" |
| 97 | + size="300" |
| 98 | + outlined |
| 99 | + onClick={handleScrollBack} |
| 100 | + > |
| 101 | + <Icon size="300" src={Icons.ArrowLeft} /> |
| 102 | + </IconButton> |
| 103 | + </> |
| 104 | + )} |
| 105 | + <Box alignItems="Inherit" gap="200"> |
| 106 | + {items.map((item, index) => ( |
| 107 | + <div key={item.url ?? item.file?.url ?? index} className={css.GalleryItem}> |
| 108 | + {renderItem(galleryItemToContent(item), index)} |
| 109 | + </div> |
| 110 | + ))} |
| 111 | + {!frontVisible && ( |
| 112 | + <> |
| 113 | + <div className={css.GalleryHolderGradient({ position: 'Right' })} /> |
| 114 | + <IconButton |
| 115 | + className={css.GalleryHolderBtn({ position: 'Right' })} |
| 116 | + variant="Primary" |
| 117 | + radii="Pill" |
| 118 | + size="300" |
| 119 | + outlined |
| 120 | + onClick={handleScrollFront} |
| 121 | + > |
| 122 | + <Icon size="300" src={Icons.ArrowRight} /> |
| 123 | + </IconButton> |
| 124 | + </> |
| 125 | + )} |
| 126 | + <div ref={frontAnchorRef} /> |
| 127 | + </Box> |
| 128 | + </Box> |
| 129 | + </Scroll> |
| 130 | + </Box> |
| 131 | + {renderCaption?.()} |
| 132 | + </Box> |
| 133 | + ); |
| 134 | +} |
0 commit comments