@yogiswara/honcho-editor-ui 1.4.16 → 1.4.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/useGallerySwipe.js +57 -27
- package/package.json +1 -1
|
@@ -28,6 +28,29 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
28
28
|
// This prevents the hook from re-initializing when the same values are passed
|
|
29
29
|
const prevFirebaseUid = useRef(null);
|
|
30
30
|
const prevInitImageId = useRef(null);
|
|
31
|
+
// Refs to track current state values for use in callbacks
|
|
32
|
+
// This prevents stale closure issues with useCallback
|
|
33
|
+
const currentImageIdRef = useRef(currentImageId);
|
|
34
|
+
const currentImageListRef = useRef(currentImageList);
|
|
35
|
+
const currentEventIdRef = useRef(currentEventId);
|
|
36
|
+
const currentPageRef = useRef(currentPage);
|
|
37
|
+
const hasNextPageRef = useRef(hasNextPage);
|
|
38
|
+
// Update refs whenever state changes
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
currentImageIdRef.current = currentImageId;
|
|
41
|
+
}, [currentImageId]);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
currentImageListRef.current = currentImageList;
|
|
44
|
+
}, [currentImageList]);
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
currentEventIdRef.current = currentEventId;
|
|
47
|
+
}, [currentEventId]);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
currentPageRef.current = currentPage;
|
|
50
|
+
}, [currentPage]);
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
hasNextPageRef.current = hasNextPage;
|
|
53
|
+
}, [hasNextPage]);
|
|
31
54
|
/**
|
|
32
55
|
* Fetch image pages sequentially until the target image is found
|
|
33
56
|
* This is necessary because we don't know which page contains the initial image
|
|
@@ -81,12 +104,12 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
81
104
|
*/
|
|
82
105
|
const loadNextPage = useCallback(async () => {
|
|
83
106
|
// Check prerequisites before attempting to load next page
|
|
84
|
-
if (!
|
|
107
|
+
if (!hasNextPageRef.current || !currentEventIdRef.current || !controller || !firebaseUid) {
|
|
85
108
|
return [];
|
|
86
109
|
}
|
|
87
110
|
try {
|
|
88
|
-
const nextPageNum =
|
|
89
|
-
const response = await controller.getImageList(firebaseUid,
|
|
111
|
+
const nextPageNum = currentPageRef.current + 1;
|
|
112
|
+
const response = await controller.getImageList(firebaseUid, currentEventIdRef.current, nextPageNum);
|
|
90
113
|
if (response.gallery && response.gallery.length > 0) {
|
|
91
114
|
// Update pagination state with new page information
|
|
92
115
|
setCurrentPage(nextPageNum);
|
|
@@ -98,7 +121,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
98
121
|
console.error('Error loading next page:', err);
|
|
99
122
|
}
|
|
100
123
|
return [];
|
|
101
|
-
}, [
|
|
124
|
+
}, [controller, firebaseUid]);
|
|
102
125
|
/**
|
|
103
126
|
* Initialize or re-initialize the gallery when parameters change
|
|
104
127
|
* This function:
|
|
@@ -164,28 +187,34 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
164
187
|
*/
|
|
165
188
|
const onSwipeNext = useCallback(async () => {
|
|
166
189
|
// Prevent action if no current image or already loading
|
|
167
|
-
if (!
|
|
190
|
+
if (!currentImageIdRef.current || isLoading)
|
|
168
191
|
return;
|
|
169
192
|
setIsLoading(true);
|
|
170
193
|
setError(null);
|
|
171
194
|
try {
|
|
172
|
-
//
|
|
173
|
-
|
|
195
|
+
// Debug logging using ref values
|
|
196
|
+
console.log("=== SWIPE NEXT DEBUG ===");
|
|
197
|
+
console.log("currentImageId (ref):", currentImageIdRef.current);
|
|
198
|
+
console.log("currentImageList length (ref):", currentImageListRef.current.length);
|
|
199
|
+
console.log("currentImageList IDs (ref):", currentImageListRef.current.map(img => img.id).join(", "));
|
|
200
|
+
// Calculate current index using ref values
|
|
201
|
+
const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
|
|
174
202
|
console.log("Current index: ", currentIndex);
|
|
175
203
|
if (currentIndex === -1) {
|
|
176
204
|
throw new Error('Current image not found in list');
|
|
177
205
|
}
|
|
178
206
|
// Scenario 1: At the last image of current list
|
|
179
|
-
if (currentIndex ===
|
|
180
|
-
console.log("[SCENARIO 1] if last image: " +
|
|
207
|
+
if (currentIndex === currentImageListRef.current.length - 1) {
|
|
208
|
+
console.log("[SCENARIO 1] if last image: " + currentImageListRef.current.length);
|
|
181
209
|
// Try to load next page for more images
|
|
182
210
|
const newImages = await loadNextPage();
|
|
183
211
|
if (newImages.length > 0) {
|
|
184
212
|
// Extend current list with new images
|
|
185
|
-
const updatedList = [...
|
|
213
|
+
const updatedList = [...currentImageListRef.current, ...newImages];
|
|
186
214
|
setCurrentImageList(updatedList);
|
|
187
215
|
// Navigate to first image of the new page
|
|
188
216
|
const nextImage = newImages[0];
|
|
217
|
+
console.log("Setting currentImageId to:", nextImage.id);
|
|
189
218
|
setCurrentImageId(nextImage.id);
|
|
190
219
|
// Fetch complete data for the new current image
|
|
191
220
|
const nextImageData = await controller.onGetImage(firebaseUid, nextImage.id);
|
|
@@ -200,8 +229,9 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
200
229
|
}
|
|
201
230
|
else {
|
|
202
231
|
// Scenario 2: Navigate to next image in current list
|
|
203
|
-
const nextImage =
|
|
232
|
+
const nextImage = currentImageListRef.current[currentIndex + 1];
|
|
204
233
|
console.log("[SCENARIO 2] Navigating to next image:", nextImage.id);
|
|
234
|
+
console.log("Setting currentImageId from", currentImageIdRef.current, "to", nextImage.id);
|
|
205
235
|
setCurrentImageId(nextImage.id);
|
|
206
236
|
// Fetch complete data for the next image
|
|
207
237
|
const nextImageData = await controller.onGetImage(firebaseUid, nextImage.id);
|
|
@@ -218,7 +248,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
218
248
|
finally {
|
|
219
249
|
setIsLoading(false);
|
|
220
250
|
}
|
|
221
|
-
}, [
|
|
251
|
+
}, [isLoading, loadNextPage, controller, firebaseUid]);
|
|
222
252
|
/**
|
|
223
253
|
* Navigate to the previous image in the gallery
|
|
224
254
|
* Only works within the currently loaded image list
|
|
@@ -228,19 +258,19 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
228
258
|
*/
|
|
229
259
|
const onSwipePrev = useCallback(async () => {
|
|
230
260
|
// Prevent action if no current image or already loading
|
|
231
|
-
if (!
|
|
261
|
+
if (!currentImageIdRef.current || isLoading)
|
|
232
262
|
return;
|
|
233
263
|
setIsLoading(true);
|
|
234
264
|
setError(null);
|
|
235
265
|
try {
|
|
236
|
-
// Calculate current index
|
|
237
|
-
const currentIndex =
|
|
266
|
+
// Calculate current index using ref values
|
|
267
|
+
const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
|
|
238
268
|
if (currentIndex === -1) {
|
|
239
269
|
throw new Error('Current image not found in list');
|
|
240
270
|
}
|
|
241
271
|
if (currentIndex > 0) {
|
|
242
272
|
// Navigate to previous image in the current list
|
|
243
|
-
const prevImage =
|
|
273
|
+
const prevImage = currentImageListRef.current[currentIndex - 1];
|
|
244
274
|
setCurrentImageId(prevImage.id);
|
|
245
275
|
// Fetch complete data for the previous image
|
|
246
276
|
const prevImageData = await controller.onGetImage(firebaseUid, prevImage.id);
|
|
@@ -261,7 +291,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
261
291
|
finally {
|
|
262
292
|
setIsLoading(false);
|
|
263
293
|
}
|
|
264
|
-
}, [
|
|
294
|
+
}, [isLoading, controller, firebaseUid]);
|
|
265
295
|
/**
|
|
266
296
|
* Calculate if next image navigation is available
|
|
267
297
|
* Returns true if:
|
|
@@ -271,18 +301,18 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
271
301
|
* @returns Boolean indicating if next navigation is possible
|
|
272
302
|
*/
|
|
273
303
|
const isNextAvailable = useCallback(() => {
|
|
274
|
-
if (isLoading || !
|
|
304
|
+
if (isLoading || !currentImageIdRef.current)
|
|
275
305
|
return false;
|
|
276
|
-
// Calculate current index
|
|
277
|
-
const currentIndex =
|
|
306
|
+
// Calculate current index using ref values
|
|
307
|
+
const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
|
|
278
308
|
if (currentIndex === -1)
|
|
279
309
|
return false;
|
|
280
310
|
// If we're not at the last image, next is definitely available
|
|
281
|
-
if (currentIndex <
|
|
311
|
+
if (currentIndex < currentImageListRef.current.length - 1)
|
|
282
312
|
return true;
|
|
283
313
|
// If we're at the last image but there are more pages, next is still available
|
|
284
|
-
return
|
|
285
|
-
}, [isLoading
|
|
314
|
+
return hasNextPageRef.current;
|
|
315
|
+
}, [isLoading]);
|
|
286
316
|
/**
|
|
287
317
|
* Calculate if previous image navigation is available
|
|
288
318
|
* Returns true if there's a previous image in the currently loaded list
|
|
@@ -290,12 +320,12 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
290
320
|
* @returns Boolean indicating if previous navigation is possible
|
|
291
321
|
*/
|
|
292
322
|
const isPrevAvailable = useCallback(() => {
|
|
293
|
-
if (isLoading || !
|
|
323
|
+
if (isLoading || !currentImageIdRef.current)
|
|
294
324
|
return false;
|
|
295
|
-
// Calculate current index
|
|
296
|
-
const currentIndex =
|
|
325
|
+
// Calculate current index using ref values
|
|
326
|
+
const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
|
|
297
327
|
return currentIndex > 0;
|
|
298
|
-
}, [isLoading
|
|
328
|
+
}, [isLoading]);
|
|
299
329
|
// Initialize when dependencies change
|
|
300
330
|
useEffect(() => {
|
|
301
331
|
initializeGallery();
|