1
- import { useEffect , useRef , useState } from 'react' ;
1
+ import { useEffect , useRef , useState , useMemo } from 'react' ;
2
2
import { AnimatePresence , motion } from 'framer-motion' ;
3
3
import cn from 'clsx' ;
4
4
import { useModal } from '@lib/hooks/useModal' ;
5
5
import { preventBubbling } from '@lib/utils' ;
6
+ import { combineBase64Images } from '@lib/utils/image-utils' ;
6
7
import { ImageModal } from '@components/modal/image-modal' ;
7
8
import { Modal } from '@components/modal/modal' ;
8
9
import { NextImage } from '@components/ui/next-image' ;
@@ -43,7 +44,7 @@ const postImageBorderRadius: Readonly<PostImageBorderRadius> = {
43
44
export function ImagePreview ( {
44
45
tweet,
45
46
viewTweet,
46
- previewCount,
47
+ previewCount : _previewCount , // Renamed to avoid unused variable warning
47
48
imagesPreview,
48
49
removeImage
49
50
} : ImagePreviewProps ) : JSX . Element {
@@ -54,11 +55,19 @@ export function ImagePreview({
54
55
55
56
const { open, openModal, closeModal } = useModal ( ) ;
56
57
58
+ // Combine accidentally separated base64 images
59
+ const processedImages = useMemo ( ( ) => {
60
+ return combineBase64Images ( imagesPreview ) ;
61
+ } , [ imagesPreview ] ) ;
62
+
63
+ // Update previewCount based on processed images
64
+ const actualPreviewCount = processedImages . length ;
65
+
57
66
useEffect ( ( ) => {
58
- const imageData = imagesPreview [ selectedIndex ] ;
67
+ const imageData = processedImages [ selectedIndex ] ;
59
68
setSelectedImage ( imageData ) ;
60
69
// eslint-disable-next-line react-hooks/exhaustive-deps
61
- } , [ selectedIndex ] ) ;
70
+ } , [ selectedIndex , processedImages ] ) ;
62
71
63
72
const handleVideoStop = ( ) : void => {
64
73
if ( videoRef . current ) videoRef . current . pause ( ) ;
@@ -75,9 +84,9 @@ export function ImagePreview({
75
84
const nextIndex =
76
85
type === 'prev'
77
86
? selectedIndex === 0
78
- ? previewCount - 1
87
+ ? actualPreviewCount - 1
79
88
: selectedIndex - 1
80
- : selectedIndex === previewCount - 1
89
+ : selectedIndex === actualPreviewCount - 1
81
90
? 0
82
91
: selectedIndex + 1 ;
83
92
@@ -108,29 +117,29 @@ export function ImagePreview({
108
117
< ImageModal
109
118
tweet = { isTweet }
110
119
imageData = { selectedImage as ImageData }
111
- previewCount = { previewCount }
120
+ previewCount = { actualPreviewCount }
112
121
selectedIndex = { selectedIndex }
113
122
handleNextIndex = { handleNextIndex }
114
123
/>
115
124
</ Modal >
116
125
< AnimatePresence mode = 'popLayout' >
117
- { imagesPreview . map ( ( { id, src, alt } , index ) => {
126
+ { processedImages . map ( ( { id, src, alt } , index ) => {
118
127
const isVideo =
119
- imagesPreview [ index ] . type ?. includes ( 'video' ) ;
128
+ processedImages [ index ] . type ?. includes ( 'video' ) ;
120
129
121
130
return (
122
131
< motion . button
123
132
type = 'button'
124
133
className = { cn (
125
134
'accent-tab group relative transition-shadow' ,
126
135
isTweet
127
- ? postImageBorderRadius [ previewCount ] [ index ]
136
+ ? postImageBorderRadius [ actualPreviewCount ] ?. [ index ] || 'rounded-2xl'
128
137
: 'rounded-2xl' ,
129
138
{
130
- 'col-span-2 row-span-2' : previewCount === 1 ,
139
+ 'col-span-2 row-span-2' : actualPreviewCount === 1 ,
131
140
'row-span-2' :
132
- previewCount === 2 ||
133
- ( index === 0 && previewCount === 3 )
141
+ actualPreviewCount === 2 ||
142
+ ( index === 0 && actualPreviewCount === 3 )
134
143
}
135
144
) }
136
145
{ ...variants }
@@ -159,8 +168,8 @@ export function ImagePreview({
159
168
hover:brightness-75 hover:duration-200` ,
160
169
isTweet
161
170
? postImageBorderRadius [
162
- previewCount
163
- ] [ index ]
171
+ actualPreviewCount
172
+ ] ?. [ index ] || 'rounded-2xl'
164
173
: 'rounded-2xl'
165
174
) }
166
175
src = { src }
@@ -175,11 +184,11 @@ export function ImagePreview({
175
184
imgClassName = { cn (
176
185
isTweet
177
186
? postImageBorderRadius [
178
- previewCount
179
- ] [ index ]
187
+ actualPreviewCount
188
+ ] ?. [ index ] || 'rounded-2xl'
180
189
: 'rounded-2xl'
181
190
) }
182
- previewCount = { previewCount }
191
+ previewCount = { actualPreviewCount }
183
192
layout = 'fill'
184
193
src = { src }
185
194
alt = { alt }
0 commit comments