-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathAttachmentCarouselPage.js
156 lines (133 loc) · 7.16 KB
/
AttachmentCarouselPage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* eslint-disable es/no-optional-chaining */
import React, {useContext, useEffect, useState} from 'react';
import {ActivityIndicator, PixelRatio, StyleSheet, View} from 'react-native';
import PropTypes from 'prop-types';
import Image from '../../../Image';
import AttachmentCarouselPagerContext from './AttachmentCarouselPagerContext';
import ImageTransformer from './ImageTransformer';
import ImageWrapper from './ImageWrapper';
import * as AttachmentsPropTypes from '../../propTypes';
function getCanvasFitScale({canvasWidth, canvasHeight, imageWidth, imageHeight}) {
const imageScaleX = canvasWidth / imageWidth;
const imageScaleY = canvasHeight / imageHeight;
return {imageScaleX, imageScaleY};
}
const cachedDimensions = new Map();
const pagePropTypes = {
/** Whether source url requires authentication */
isAuthTokenRequired: PropTypes.bool,
/** URL to full-sized attachment or SVG function */
source: AttachmentsPropTypes.attachmentSourcePropType.isRequired,
isActive: PropTypes.bool.isRequired,
};
const defaultProps = {
isAuthTokenRequired: false,
};
function AttachmentCarouselPage({source, isAuthTokenRequired, isActive: initialIsActive}) {
const {canvasWidth, canvasHeight} = useContext(AttachmentCarouselPagerContext);
const dimensions = cachedDimensions.get(source);
const [isActive, setIsActive] = useState(initialIsActive);
// We delay setting a page to active state by a (few) millisecond(s),
// to prevent the image transformer from flashing while still rendering
// Instead, we show the fallback image while the image transformer is loading the image
useEffect(() => {
if (initialIsActive) setTimeout(() => setIsActive(true), 1);
else setIsActive(false);
}, [initialIsActive]);
const [initialActivePageLoad, setInitialActivePageLoad] = useState(isActive);
const [isImageLoading, setIsImageLoading] = useState(true);
const [showFallback, setShowFallback] = useState(isImageLoading);
// We delay hiding the fallback image while image transformer is still rendering
useEffect(() => {
if (isImageLoading) setShowFallback(true);
else setTimeout(() => setShowFallback(false), 100);
}, [isImageLoading]);
return (
<>
{isActive && (
<View style={StyleSheet.absoluteFill}>
<ImageTransformer
isActive
imageWidth={dimensions?.imageWidth}
imageHeight={dimensions?.imageHeight}
scaledImageWidth={dimensions?.scaledImageWidth}
scaledImageHeight={dimensions?.scaledImageHeight}
minImageScale={dimensions?.minImageScale}
imageScaleX={dimensions?.imageScaleX}
imageScaleY={dimensions?.imageScaleY}
>
<Image
source={{uri: source}}
style={dimensions == null ? undefined : {width: dimensions.imageWidth, height: dimensions.imageHeight}}
isAuthTokenRequired={isAuthTokenRequired}
onLoadStart={() => setIsImageLoading(true)}
onLoadEnd={() => setIsImageLoading(false)}
onLoad={(evt) => {
const imageWidth = (evt.nativeEvent?.width || 0) / PixelRatio.get();
const imageHeight = (evt.nativeEvent?.height || 0) / PixelRatio.get();
const {imageScaleX, imageScaleY} = getCanvasFitScale({canvasWidth, canvasHeight, imageWidth, imageHeight});
// Don't update the dimensions if they are already set
if (
dimensions?.imageWidth !== imageWidth ||
dimensions?.imageHeight !== imageHeight ||
dimensions?.imageScaleX !== imageScaleX ||
dimensions?.imageScaleY !== imageScaleY
) {
cachedDimensions.set(source, {
...dimensions,
imageWidth,
imageHeight,
imageScaleX,
imageScaleY,
});
}
// On the initial render of the active page, the onLoadEnd event is never fired.
// That's why we instead set isImageLoading to false in the onLoad event.
if (initialActivePageLoad) {
setIsImageLoading(false);
setInitialActivePageLoad(false);
}
}}
/>
</ImageTransformer>
</View>
)}
{/* Keep rendering the image without gestures as fallback while ImageTransformer is loading the image */}
{(!isActive || showFallback) && (
<ImageWrapper>
<Image
source={{uri: source}}
isAuthTokenRequired={isAuthTokenRequired}
onLoadStart={() => setIsImageLoading(true)}
onLoad={(evt) => {
const imageWidth = evt.nativeEvent.width;
const imageHeight = evt.nativeEvent.height;
const {imageScaleX, imageScaleY} = getCanvasFitScale({canvasWidth, canvasHeight, imageWidth, imageHeight});
const minImageScale = Math.min(imageScaleX, imageScaleY);
const scaledImageWidth = imageWidth * minImageScale;
const scaledImageHeight = imageHeight * minImageScale;
// Don't update the dimensions if they are already set
if (dimensions?.scaledImageWidth === scaledImageWidth && dimensions?.scaledImageHeight === scaledImageHeight) return;
cachedDimensions.set(source, {
...dimensions,
scaledImageWidth,
scaledImageHeight,
});
}}
style={dimensions == null ? undefined : {width: dimensions.scaledImageWidth, height: dimensions.scaledImageHeight}}
/>
</ImageWrapper>
)}
{/* Show activity indicator while ImageTransfomer is still loading the image. */}
{isActive && isImageLoading && (
<ActivityIndicator
size="large"
style={StyleSheet.absoluteFill}
/>
)}
</>
);
}
AttachmentCarouselPage.propTypes = pagePropTypes;
AttachmentCarouselPage.defaultProps = defaultProps;
export default AttachmentCarouselPage;