@yogiswara/honcho-editor-ui 1.4.21 → 1.4.23

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.
@@ -28,29 +28,15 @@ 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
+ /**
32
+ * Get the index of the current image in the loaded image list
33
+ * Returns -1 if the current image is not found in the list
34
+ */
35
+ const getCurrentImageIndex = useCallback(() => {
36
+ if (!currentImageId || currentImageList.length === 0)
37
+ return -1;
38
+ return currentImageList.findIndex(img => img.id === currentImageId);
39
+ }, [currentImageId, currentImageList]);
54
40
  /**
55
41
  * Fetch image pages sequentially until the target image is found
56
42
  * This is necessary because we don't know which page contains the initial image
@@ -104,12 +90,12 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
104
90
  */
105
91
  const loadNextPage = useCallback(async () => {
106
92
  // Check prerequisites before attempting to load next page
107
- if (!hasNextPageRef.current || !currentEventIdRef.current || !controller || !firebaseUid) {
93
+ if (!hasNextPage || !currentEventId || !controller || !firebaseUid) {
108
94
  return [];
109
95
  }
110
96
  try {
111
- const nextPageNum = currentPageRef.current + 1;
112
- const response = await controller.getImageList(firebaseUid, currentEventIdRef.current, nextPageNum);
97
+ const nextPageNum = currentPage + 1;
98
+ const response = await controller.getImageList(firebaseUid, currentEventId, nextPageNum);
113
99
  if (response.gallery && response.gallery.length > 0) {
114
100
  // Update pagination state with new page information
115
101
  setCurrentPage(nextPageNum);
@@ -121,7 +107,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
121
107
  console.error('Error loading next page:', err);
122
108
  }
123
109
  return [];
124
- }, [controller, firebaseUid]);
110
+ }, [hasNextPage, currentEventId, controller, firebaseUid, currentPage]);
125
111
  /**
126
112
  * Initialize or re-initialize the gallery when parameters change
127
113
  * This function:
@@ -187,39 +173,35 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
187
173
  */
188
174
  const onSwipeNext = useCallback(async () => {
189
175
  // Prevent action if no current image or already loading
190
- if (!currentImageIdRef.current || isLoading)
176
+ if (!currentImageId || isLoading)
191
177
  return;
192
178
  setIsLoading(true);
193
179
  setError(null);
194
180
  try {
195
- // Debug logging using ref values
181
+ // Debug logging
196
182
  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);
183
+ console.log("currentImageId:", currentImageId);
184
+ console.log("currentImageList length:", currentImageList.length);
185
+ console.log("currentImageList IDs:", currentImageList.map(img => img.id).join(", "));
186
+ // Calculate current index
187
+ const currentIndex = getCurrentImageIndex();
202
188
  console.log("Current index: ", currentIndex);
203
189
  if (currentIndex === -1) {
204
190
  throw new Error('Current image not found in list');
205
191
  }
206
192
  // Scenario 1: At the last image of current list
207
- if (currentIndex === currentImageListRef.current.length - 1) {
208
- console.log("[SCENARIO 1] if last image: " + currentImageListRef.current.length);
193
+ if (currentIndex === currentImageList.length - 1) {
194
+ console.log("[SCENARIO 1] if last image: " + currentImageList.length);
209
195
  // Try to load next page for more images
210
196
  const newImages = await loadNextPage();
211
197
  if (newImages.length > 0) {
212
198
  // Extend current list with new images
213
- const updatedList = [...currentImageListRef.current, ...newImages];
199
+ const updatedList = [...currentImageList, ...newImages];
214
200
  setCurrentImageList(updatedList);
215
- // Immediately update the ref to prevent stale closure issues
216
- currentImageListRef.current = updatedList;
217
201
  // Navigate to first image of the new page
218
202
  const nextImage = newImages[0];
219
203
  console.log("Setting currentImageId to:", nextImage.id);
220
204
  setCurrentImageId(nextImage.id);
221
- // Immediately update the ref to prevent stale closure issues
222
- currentImageIdRef.current = nextImage.id;
223
205
  // Fetch complete data for the new current image
224
206
  const nextImageData = await controller.onGetImage(firebaseUid, nextImage.id);
225
207
  if (nextImageData) {
@@ -233,12 +215,10 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
233
215
  }
234
216
  else {
235
217
  // Scenario 2: Navigate to next image in current list
236
- const nextImage = currentImageListRef.current[currentIndex + 1];
218
+ const nextImage = currentImageList[currentIndex + 1];
237
219
  console.log("[SCENARIO 2] Navigating to next image:", nextImage.id);
238
- console.log("Setting currentImageId from", currentImageIdRef.current, "to", nextImage.id);
220
+ console.log("Setting currentImageId from", currentImageId, "to", nextImage.id);
239
221
  setCurrentImageId(nextImage.id);
240
- // Immediately update the ref to prevent stale closure issues
241
- currentImageIdRef.current = nextImage.id;
242
222
  // Fetch complete data for the next image
243
223
  const nextImageData = await controller.onGetImage(firebaseUid, nextImage.id);
244
224
  if (nextImageData) {
@@ -254,7 +234,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
254
234
  finally {
255
235
  setIsLoading(false);
256
236
  }
257
- }, [isLoading, loadNextPage, controller, firebaseUid]);
237
+ }, [currentImageId, isLoading, getCurrentImageIndex, currentImageList, loadNextPage, controller, firebaseUid]);
258
238
  /**
259
239
  * Navigate to the previous image in the gallery
260
240
  * Only works within the currently loaded image list
@@ -264,22 +244,20 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
264
244
  */
265
245
  const onSwipePrev = useCallback(async () => {
266
246
  // Prevent action if no current image or already loading
267
- if (!currentImageIdRef.current || isLoading)
247
+ if (!currentImageId || isLoading)
268
248
  return;
269
249
  setIsLoading(true);
270
250
  setError(null);
271
251
  try {
272
- // Calculate current index using ref values
273
- const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
252
+ // Calculate current index
253
+ const currentIndex = getCurrentImageIndex();
274
254
  if (currentIndex === -1) {
275
255
  throw new Error('Current image not found in list');
276
256
  }
277
257
  if (currentIndex > 0) {
278
258
  // Navigate to previous image in the current list
279
- const prevImage = currentImageListRef.current[currentIndex - 1];
259
+ const prevImage = currentImageList[currentIndex - 1];
280
260
  setCurrentImageId(prevImage.id);
281
- // Immediately update the ref to prevent stale closure issues
282
- currentImageIdRef.current = prevImage.id;
283
261
  // Fetch complete data for the previous image
284
262
  const prevImageData = await controller.onGetImage(firebaseUid, prevImage.id);
285
263
  if (prevImageData) {
@@ -299,7 +277,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
299
277
  finally {
300
278
  setIsLoading(false);
301
279
  }
302
- }, [isLoading, controller, firebaseUid]);
280
+ }, [currentImageId, isLoading, getCurrentImageIndex, currentImageList, controller, firebaseUid]);
303
281
  /**
304
282
  * Calculate if next image navigation is available
305
283
  * Returns true if:
@@ -309,18 +287,18 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
309
287
  * @returns Boolean indicating if next navigation is possible
310
288
  */
311
289
  const isNextAvailable = useCallback(() => {
312
- if (isLoading || !currentImageIdRef.current)
290
+ if (isLoading || !currentImageId)
313
291
  return false;
314
- // Calculate current index using ref values
315
- const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
292
+ // Calculate current index
293
+ const currentIndex = getCurrentImageIndex();
316
294
  if (currentIndex === -1)
317
295
  return false;
318
296
  // If we're not at the last image, next is definitely available
319
- if (currentIndex < currentImageListRef.current.length - 1)
297
+ if (currentIndex < currentImageList.length - 1)
320
298
  return true;
321
299
  // If we're at the last image but there are more pages, next is still available
322
- return hasNextPageRef.current;
323
- }, [isLoading]);
300
+ return hasNextPage;
301
+ }, [isLoading, getCurrentImageIndex, currentImageList.length, hasNextPage]);
324
302
  /**
325
303
  * Calculate if previous image navigation is available
326
304
  * Returns true if there's a previous image in the currently loaded list
@@ -328,23 +306,31 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
328
306
  * @returns Boolean indicating if previous navigation is possible
329
307
  */
330
308
  const isPrevAvailable = useCallback(() => {
331
- if (isLoading || !currentImageIdRef.current)
309
+ if (isLoading || !currentImageId)
332
310
  return false;
333
- // Calculate current index using ref values
334
- const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
311
+ // Calculate current index
312
+ const currentIndex = getCurrentImageIndex();
335
313
  return currentIndex > 0;
336
- }, [isLoading]);
314
+ }, [isLoading, getCurrentImageIndex]);
337
315
  // Initialize when dependencies change
338
316
  useEffect(() => {
339
317
  initializeGallery();
340
318
  }, [initializeGallery]);
341
- // Update currentImageId when initImageId changes (but only if different)
319
+ // Update currentImageId when initImageId changes from external source
342
320
  // This allows the hook to respond to external changes to the initial image ID
321
+ // but should not interfere with internal navigation
343
322
  useEffect(() => {
344
- if (initImageId && initImageId !== currentImageId) {
345
- setCurrentImageId(initImageId);
323
+ // Only update if initImageId actually changed and we're not currently loading
324
+ // (loading indicates we're in the middle of navigation)
325
+ if (initImageId && initImageId !== currentImageId && !isLoading) {
326
+ // Additional check: only update if this is truly an external change
327
+ // If prevInitImageId is different from initImageId, it's an external change
328
+ if (prevInitImageId.current !== initImageId) {
329
+ console.log("External initImageId change detected, updating currentImageId");
330
+ setCurrentImageId(initImageId);
331
+ }
346
332
  }
347
- }, [initImageId, currentImageId]);
333
+ }, [initImageId]);
348
334
  // Return all the functionality and state that components need
349
335
  return {
350
336
  currentImageData,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yogiswara/honcho-editor-ui",
3
- "version": "1.4.21",
3
+ "version": "1.4.23",
4
4
  "description": "A complete UI component library for the Honcho photo editor.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",