@yogiswara/honcho-editor-ui 1.4.17 → 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 +55 -32
- 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,30 +187,30 @@ 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
|
-
// Debug logging
|
|
195
|
+
// Debug logging using ref values
|
|
173
196
|
console.log("=== SWIPE NEXT DEBUG ===");
|
|
174
|
-
console.log("currentImageId:",
|
|
175
|
-
console.log("currentImageList length:",
|
|
176
|
-
console.log("currentImageList IDs:",
|
|
177
|
-
// Calculate current index
|
|
178
|
-
const currentIndex =
|
|
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);
|
|
179
202
|
console.log("Current index: ", currentIndex);
|
|
180
203
|
if (currentIndex === -1) {
|
|
181
204
|
throw new Error('Current image not found in list');
|
|
182
205
|
}
|
|
183
206
|
// Scenario 1: At the last image of current list
|
|
184
|
-
if (currentIndex ===
|
|
185
|
-
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);
|
|
186
209
|
// Try to load next page for more images
|
|
187
210
|
const newImages = await loadNextPage();
|
|
188
211
|
if (newImages.length > 0) {
|
|
189
212
|
// Extend current list with new images
|
|
190
|
-
const updatedList = [...
|
|
213
|
+
const updatedList = [...currentImageListRef.current, ...newImages];
|
|
191
214
|
setCurrentImageList(updatedList);
|
|
192
215
|
// Navigate to first image of the new page
|
|
193
216
|
const nextImage = newImages[0];
|
|
@@ -206,9 +229,9 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
206
229
|
}
|
|
207
230
|
else {
|
|
208
231
|
// Scenario 2: Navigate to next image in current list
|
|
209
|
-
const nextImage =
|
|
232
|
+
const nextImage = currentImageListRef.current[currentIndex + 1];
|
|
210
233
|
console.log("[SCENARIO 2] Navigating to next image:", nextImage.id);
|
|
211
|
-
console.log("Setting currentImageId from",
|
|
234
|
+
console.log("Setting currentImageId from", currentImageIdRef.current, "to", nextImage.id);
|
|
212
235
|
setCurrentImageId(nextImage.id);
|
|
213
236
|
// Fetch complete data for the next image
|
|
214
237
|
const nextImageData = await controller.onGetImage(firebaseUid, nextImage.id);
|
|
@@ -225,7 +248,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
225
248
|
finally {
|
|
226
249
|
setIsLoading(false);
|
|
227
250
|
}
|
|
228
|
-
}, [
|
|
251
|
+
}, [isLoading, loadNextPage, controller, firebaseUid]);
|
|
229
252
|
/**
|
|
230
253
|
* Navigate to the previous image in the gallery
|
|
231
254
|
* Only works within the currently loaded image list
|
|
@@ -235,19 +258,19 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
235
258
|
*/
|
|
236
259
|
const onSwipePrev = useCallback(async () => {
|
|
237
260
|
// Prevent action if no current image or already loading
|
|
238
|
-
if (!
|
|
261
|
+
if (!currentImageIdRef.current || isLoading)
|
|
239
262
|
return;
|
|
240
263
|
setIsLoading(true);
|
|
241
264
|
setError(null);
|
|
242
265
|
try {
|
|
243
|
-
// Calculate current index
|
|
244
|
-
const currentIndex =
|
|
266
|
+
// Calculate current index using ref values
|
|
267
|
+
const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
|
|
245
268
|
if (currentIndex === -1) {
|
|
246
269
|
throw new Error('Current image not found in list');
|
|
247
270
|
}
|
|
248
271
|
if (currentIndex > 0) {
|
|
249
272
|
// Navigate to previous image in the current list
|
|
250
|
-
const prevImage =
|
|
273
|
+
const prevImage = currentImageListRef.current[currentIndex - 1];
|
|
251
274
|
setCurrentImageId(prevImage.id);
|
|
252
275
|
// Fetch complete data for the previous image
|
|
253
276
|
const prevImageData = await controller.onGetImage(firebaseUid, prevImage.id);
|
|
@@ -268,7 +291,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
268
291
|
finally {
|
|
269
292
|
setIsLoading(false);
|
|
270
293
|
}
|
|
271
|
-
}, [
|
|
294
|
+
}, [isLoading, controller, firebaseUid]);
|
|
272
295
|
/**
|
|
273
296
|
* Calculate if next image navigation is available
|
|
274
297
|
* Returns true if:
|
|
@@ -278,18 +301,18 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
278
301
|
* @returns Boolean indicating if next navigation is possible
|
|
279
302
|
*/
|
|
280
303
|
const isNextAvailable = useCallback(() => {
|
|
281
|
-
if (isLoading || !
|
|
304
|
+
if (isLoading || !currentImageIdRef.current)
|
|
282
305
|
return false;
|
|
283
|
-
// Calculate current index
|
|
284
|
-
const currentIndex =
|
|
306
|
+
// Calculate current index using ref values
|
|
307
|
+
const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
|
|
285
308
|
if (currentIndex === -1)
|
|
286
309
|
return false;
|
|
287
310
|
// If we're not at the last image, next is definitely available
|
|
288
|
-
if (currentIndex <
|
|
311
|
+
if (currentIndex < currentImageListRef.current.length - 1)
|
|
289
312
|
return true;
|
|
290
313
|
// If we're at the last image but there are more pages, next is still available
|
|
291
|
-
return
|
|
292
|
-
}, [isLoading
|
|
314
|
+
return hasNextPageRef.current;
|
|
315
|
+
}, [isLoading]);
|
|
293
316
|
/**
|
|
294
317
|
* Calculate if previous image navigation is available
|
|
295
318
|
* Returns true if there's a previous image in the currently loaded list
|
|
@@ -297,12 +320,12 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
|
|
|
297
320
|
* @returns Boolean indicating if previous navigation is possible
|
|
298
321
|
*/
|
|
299
322
|
const isPrevAvailable = useCallback(() => {
|
|
300
|
-
if (isLoading || !
|
|
323
|
+
if (isLoading || !currentImageIdRef.current)
|
|
301
324
|
return false;
|
|
302
|
-
// Calculate current index
|
|
303
|
-
const currentIndex =
|
|
325
|
+
// Calculate current index using ref values
|
|
326
|
+
const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
|
|
304
327
|
return currentIndex > 0;
|
|
305
|
-
}, [isLoading
|
|
328
|
+
}, [isLoading]);
|
|
306
329
|
// Initialize when dependencies change
|
|
307
330
|
useEffect(() => {
|
|
308
331
|
initializeGallery();
|