-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathImageGallery.js
More file actions
121 lines (116 loc) · 3.37 KB
/
Copy pathImageGallery.js
File metadata and controls
121 lines (116 loc) · 3.37 KB
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
import React, { useEffect, useState } from 'react';
import {
Box,
ActionButton,
Icon,
Button,
Throbber,
useTheme,
ReactPortal,
} from '@embeddedchat/ui-elements';
import { useRCContext } from '../../context/RCInstance';
import { Swiper, SwiperSlide } from './Swiper';
import getImageGalleryStyles from './ImageGallery.styles';
import AuthenticatedImage from '../AttachmentHandler/AuthenticatedImage';
const ImageGallery = ({ currentFileId, setShowGallery }) => {
const { theme } = useTheme();
const styles = getImageGalleryStyles(theme);
const { RCInstance } = useRCContext();
const [files, setFiles] = useState([]);
const [currentFileIndex, setCurrentFileIndex] = useState(-1);
const [loading, setLoading] = useState(true);
const [imgFetchErr, setImgFetchErr] = useState(false);
useEffect(() => {
const fetchAllImages = async () => {
const res = await RCInstance.getAllImages();
if (res) {
if (res?.files) {
setFiles(res.files);
const fileIndex = res.files.findIndex(
(file) => file._id === currentFileId
);
setCurrentFileIndex(fileIndex);
setLoading(false);
}
} else {
setLoading(false);
setImgFetchErr(true);
}
};
fetchAllImages();
}, [RCInstance, setFiles, setCurrentFileIndex, currentFileId]);
return (
<ReactPortal wrapperId="overlay-items">
<Box css={styles.overlay}>
<ActionButton
css={styles.exit}
onClick={() => setShowGallery(false)}
size="medium"
>
<Icon name="cross" />
</ActionButton>
{loading && (
<Box css={styles.throbberContainer}>
<Throbber />
</Box>
)}
{imgFetchErr || currentFileIndex === -1 ? (
<Box
css={styles.fetchErrorContainer}
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}}
>
<Icon
name="magnifier"
size="3rem"
style={{ padding: '0.5rem', color: theme.colors.destructive }}
/>
<Box
is="span"
style={{
fontSize: '1.2rem',
fontWeight: 'bold',
color: theme.colors.primaryForeground,
}}
>
Something went wrong
</Box>
<Button
type="primary"
onClick={() => setShowGallery(false)}
style={{
alignSelf: 'auto',
margin: '10px',
}}
>
Close
</Button>
</Box>
) : (
<Box css={styles.swiperContainer}>
<Swiper
navigation
pagination={{ clickable: true }}
keyboard
initialSlide={currentFileIndex}
injectStyles={[styles.swiperInject]}
>
{files.map(({ _id, url }) => (
<SwiperSlide key={_id}>
<Box css={styles.imageContainer}>
<AuthenticatedImage url={url} css={styles.image} />
</Box>
</SwiperSlide>
))}
</Swiper>
)
</Box>
)}
</Box>
</ReactPortal>
);
};
export default ImageGallery;