-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMediaPreview.js
118 lines (108 loc) · 3.66 KB
/
MediaPreview.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
import { memo, useEffect, useState, useMemo, useCallback } from "react";
import { useSwipeable } from "react-swipeable";
import CloseRoundedIcon from '@mui/icons-material/CloseRounded';
import ArrowCircleLeftRoundedIcon from '@mui/icons-material/ArrowCircleLeftRounded';
import ArrowCircleRightRoundedIcon from '@mui/icons-material/ArrowCircleRightRounded';
import InsertDriveFileIcon from '@mui/icons-material/InsertDriveFile';
import CircularProgress from '@mui/material/CircularProgress';
import "./MediaPreview.css";
export default memo(function MediaPreview({ imageSRC, mediaPreview, close, setRatio, activeElement, setActiveElement, isMedia, files }) {
const [height, setHeight] = useState("");
const [swipeIsActive, setSwipeIsActive] = useState(false);
useEffect(() => {
setHeight(document.querySelector('.chat').offsetHeight);
document.querySelectorAll(".carousel__img").forEach((img, index) => {
img.onload = () => {
URL.revokeObjectURL(img.src);
setRatio(prevRatios => {
const newRatios = [...prevRatios];
newRatios[index] = img.naturalHeight / img.naturalWidth;
console.log("new ratios: ", newRatios);
return newRatios;
});
};
});
}, [imageSRC]);
useEffect(() => {
setActiveElement(isMedia === "images" ? 0 : imageSRC.length - 1);
}, [imageSRC, isMedia])
const elementPercentage = useMemo(() => 100 / imageSRC.length, [imageSRC]);
const next = useCallback(() => {
if (!swipeIsActive) {
setSwipeIsActive(true);
setActiveElement(previousActive => {
if (previousActive === imageSRC.length - 1) {
return 0;
};
return previousActive + 1;
});
setTimeout(() => {
setSwipeIsActive(false);
}, [260]);
};
}, [imageSRC, swipeIsActive]);
const back = useCallback(() => {
if (!swipeIsActive) {
setSwipeIsActive(true);
setActiveElement(previousActive => {
if (previousActive === 0) {
return imageSRC.length - 1;
};
return previousActive - 1;
});
setTimeout(() => {
setSwipeIsActive(false);
}, [260])
};
}, [imageSRC, swipeIsActive]);
const swipeHandler = useSwipeable({
onSwipedLeft: next,
onSwipedRight: back
});
const closeWithoutMessage = useCallback(() => close(false), []);
return (
<div
ref={mediaPreview}
className="mediaPreview"
style={{
height: height,
}}
>
{isMedia === "loading" ?
<CircularProgress size={80} />
:
<>
<CloseRoundedIcon onClick={closeWithoutMessage} className="close__arrow" />
{imageSRC.length > 1 && <ArrowCircleLeftRoundedIcon onClick={back} className="left__arrow arrow" />}
<div className="carousel" {...swipeHandler}>
<div className="inner"
style={{
width: 100 * imageSRC.length + "%",
transform: `translateX(-${activeElement * elementPercentage}%)`
}}
>
{isMedia === "images" || isMedia === "images_dropped" ?
imageSRC.map((image, index) => (
<div className="carousel__item" key={image + index}>
<img draggable={false} className="carousel__img" src={image} alt="" />
</div>
))
:
imageSRC.map((image, index) => (
<div className="carousel__item" key={image + index}>
<div className="media__details">
<InsertDriveFileIcon />
<h3>{files[index].name}</h3>
</div>
<img draggable={false} className="carousel__img" src={image} alt="" />
</div>
))
}
</div>
</div>
{imageSRC.length > 1 && <ArrowCircleRightRoundedIcon onClick={next} className="right__arrow arrow" />}
</>
}
</div>
)
});