Skip to content

Commit

Permalink
Drive-by lightbox refactors (#1659)
Browse files Browse the repository at this point in the history
* Remove dead code from lightbox

* Rename imageIndex prop to initialImageIndex

* Rename currentImageIndex to imageIndex
  • Loading branch information
gaearon authored Oct 10, 2023
1 parent bc2c44c commit d47ff54
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 40 deletions.
19 changes: 0 additions & 19 deletions src/view/com/lightbox/ImageViewing/hooks/useImageDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,10 @@ const useImageDimensions = (image: ImageSource): Dimensions | null => {
// eslint-disable-next-line @typescript-eslint/no-shadow
const getImageDimensions = (image: ImageSource): Promise<Dimensions> => {
return new Promise(resolve => {
if (typeof image === 'number') {
const cacheKey = `${image}`
let imageDimensions = imageDimensionsCache.get(cacheKey)

if (!imageDimensions) {
const {width, height} = Image.resolveAssetSource(image)
imageDimensions = {width, height}
imageDimensionsCache.set(cacheKey, imageDimensions)
}

resolve(imageDimensions)

return
}

// @ts-ignore
if (image.uri) {
const source = image as ImageURISource

const cacheKey = source.uri as string

const imageDimensions = imageDimensionsCache.get(cacheKey)

if (imageDimensions) {
resolve(imageDimensions)
} else {
Expand Down
32 changes: 13 additions & 19 deletions src/view/com/lightbox/ImageViewing/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ import {Edge, SafeAreaView} from 'react-native-safe-area-context'

type Props = {
images: ImageSource[]
keyExtractor?: (imageSrc: ImageSource, index: number) => string
imageIndex: number
initialImageIndex: number
visible: boolean
onRequestClose: () => void
presentationStyle?: ModalProps['presentationStyle']
Expand All @@ -60,8 +59,7 @@ const ANIMATION_CONFIG = {

function ImageViewing({
images,
keyExtractor,
imageIndex,
initialImageIndex,
visible,
onRequestClose,
backgroundColor = DEFAULT_BG_COLOR,
Expand All @@ -71,7 +69,7 @@ function ImageViewing({
const imageList = useRef<VirtualizedList<ImageSource>>(null)
const [isScaled, setIsScaled] = useState(false)
const [isDragging, setIsDragging] = useState(false)
const [currentImageIndex, setImageIndex] = useState(imageIndex)
const [imageIndex, setImageIndex] = useState(initialImageIndex)
const [headerTranslate] = useState(
() => new Animated.ValueXY(INITIAL_POSITION),
)
Expand Down Expand Up @@ -125,10 +123,13 @@ function ImageViewing({
}, [])

const onLayout = useCallback(() => {
if (imageIndex) {
imageList.current?.scrollToIndex({index: imageIndex, animated: false})
if (initialImageIndex) {
imageList.current?.scrollToIndex({
index: initialImageIndex,
animated: false,
})
}
}, [imageList, imageIndex])
}, [imageList, initialImageIndex])

// This is a hack.
// RNGH doesn't have an easy way to express that pinch of individual items
Expand Down Expand Up @@ -159,7 +160,7 @@ function ImageViewing({
<Animated.View style={[styles.header, {transform: headerTransform}]}>
{typeof HeaderComponent !== 'undefined' ? (
React.createElement(HeaderComponent, {
imageIndex: currentImageIndex,
imageIndex,
})
) : (
<ImageDefaultHeader onRequestClose={onRequestClose} />
Expand Down Expand Up @@ -205,19 +206,12 @@ function ImageViewing({
setIsScaled(false)
onScroll(e)
}}
//@ts-ignore
keyExtractor={(imageSrc, index) =>
keyExtractor
? keyExtractor(imageSrc, index)
: typeof imageSrc === 'number'
? `${imageSrc}`
: imageSrc.uri
}
keyExtractor={imageSrc => imageSrc.uri}
/>
{typeof FooterComponent !== 'undefined' && (
<Animated.View style={[styles.footer, {transform: footerTransform}]}>
{React.createElement(FooterComponent, {
imageIndex: currentImageIndex,
imageIndex,
})}
</Animated.View>
)}
Expand Down Expand Up @@ -250,7 +244,7 @@ const styles = StyleSheet.create({
})

const EnhancedImageViewing = (props: Props) => (
<ImageViewing key={props.imageIndex} {...props} />
<ImageViewing key={props.initialImageIndex} {...props} />
)

export default EnhancedImageViewing
4 changes: 2 additions & 2 deletions src/view/com/lightbox/Lightbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const Lightbox = observer(function Lightbox() {
return (
<ImageView
images={[{uri: opts.profileView.avatar || ''}]}
imageIndex={0}
initialImageIndex={0}
visible
onRequestClose={onClose}
FooterComponent={LightboxFooter}
Expand All @@ -37,7 +37,7 @@ export const Lightbox = observer(function Lightbox() {
return (
<ImageView
images={opts.images.map(img => ({...img}))}
imageIndex={opts.index}
initialImageIndex={opts.index}
visible
onRequestClose={onClose}
FooterComponent={LightboxFooter}
Expand Down

0 comments on commit d47ff54

Please sign in to comment.