@yogiswara/honcho-editor-ui 1.4.17 → 1.4.19

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,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 (!hasNextPage || !currentEventId || !controller || !firebaseUid) {
107
+ if (!hasNextPageRef.current || !currentEventIdRef.current || !controller || !firebaseUid) {
85
108
  return [];
86
109
  }
87
110
  try {
88
- const nextPageNum = currentPage + 1;
89
- const response = await controller.getImageList(firebaseUid, currentEventId, nextPageNum);
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
- }, [hasNextPage, currentEventId, controller, firebaseUid, currentPage]);
124
+ }, [controller, firebaseUid]);
102
125
  /**
103
126
  * Initialize or re-initialize the gallery when parameters change
104
127
  * This function:
@@ -164,35 +187,39 @@ 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 (!currentImageId || isLoading)
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:", currentImageId);
175
- console.log("currentImageList length:", currentImageList.length);
176
- console.log("currentImageList IDs:", currentImageList.map(img => img.id).join(", "));
177
- // Calculate current index directly to avoid stale closure issues
178
- const currentIndex = currentImageList.findIndex(img => img.id === currentImageId);
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 === currentImageList.length - 1) {
185
- console.log("[SCENARIO 1] if last image: " + currentImageList.length);
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 = [...currentImageList, ...newImages];
213
+ const updatedList = [...currentImageListRef.current, ...newImages];
191
214
  setCurrentImageList(updatedList);
215
+ // Immediately update the ref to prevent stale closure issues
216
+ currentImageListRef.current = updatedList;
192
217
  // Navigate to first image of the new page
193
218
  const nextImage = newImages[0];
194
219
  console.log("Setting currentImageId to:", nextImage.id);
195
220
  setCurrentImageId(nextImage.id);
221
+ // Immediately update the ref to prevent stale closure issues
222
+ currentImageIdRef.current = nextImage.id;
196
223
  // Fetch complete data for the new current image
197
224
  const nextImageData = await controller.onGetImage(firebaseUid, nextImage.id);
198
225
  if (nextImageData) {
@@ -206,10 +233,12 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
206
233
  }
207
234
  else {
208
235
  // Scenario 2: Navigate to next image in current list
209
- const nextImage = currentImageList[currentIndex + 1];
236
+ const nextImage = currentImageListRef.current[currentIndex + 1];
210
237
  console.log("[SCENARIO 2] Navigating to next image:", nextImage.id);
211
- console.log("Setting currentImageId from", currentImageId, "to", nextImage.id);
238
+ console.log("Setting currentImageId from", currentImageIdRef.current, "to", nextImage.id);
212
239
  setCurrentImageId(nextImage.id);
240
+ // Immediately update the ref to prevent stale closure issues
241
+ currentImageIdRef.current = nextImage.id;
213
242
  // Fetch complete data for the next image
214
243
  const nextImageData = await controller.onGetImage(firebaseUid, nextImage.id);
215
244
  if (nextImageData) {
@@ -225,7 +254,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
225
254
  finally {
226
255
  setIsLoading(false);
227
256
  }
228
- }, [currentImageId, isLoading, currentImageList, loadNextPage, controller, firebaseUid]);
257
+ }, [isLoading, loadNextPage, controller, firebaseUid]);
229
258
  /**
230
259
  * Navigate to the previous image in the gallery
231
260
  * Only works within the currently loaded image list
@@ -235,20 +264,22 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
235
264
  */
236
265
  const onSwipePrev = useCallback(async () => {
237
266
  // Prevent action if no current image or already loading
238
- if (!currentImageId || isLoading)
267
+ if (!currentImageIdRef.current || isLoading)
239
268
  return;
240
269
  setIsLoading(true);
241
270
  setError(null);
242
271
  try {
243
- // Calculate current index directly to avoid stale closure issues
244
- const currentIndex = currentImageList.findIndex(img => img.id === currentImageId);
272
+ // Calculate current index using ref values
273
+ const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
245
274
  if (currentIndex === -1) {
246
275
  throw new Error('Current image not found in list');
247
276
  }
248
277
  if (currentIndex > 0) {
249
278
  // Navigate to previous image in the current list
250
- const prevImage = currentImageList[currentIndex - 1];
279
+ const prevImage = currentImageListRef.current[currentIndex - 1];
251
280
  setCurrentImageId(prevImage.id);
281
+ // Immediately update the ref to prevent stale closure issues
282
+ currentImageIdRef.current = prevImage.id;
252
283
  // Fetch complete data for the previous image
253
284
  const prevImageData = await controller.onGetImage(firebaseUid, prevImage.id);
254
285
  if (prevImageData) {
@@ -268,7 +299,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
268
299
  finally {
269
300
  setIsLoading(false);
270
301
  }
271
- }, [currentImageId, isLoading, currentImageList, controller, firebaseUid]);
302
+ }, [isLoading, controller, firebaseUid]);
272
303
  /**
273
304
  * Calculate if next image navigation is available
274
305
  * Returns true if:
@@ -278,18 +309,18 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
278
309
  * @returns Boolean indicating if next navigation is possible
279
310
  */
280
311
  const isNextAvailable = useCallback(() => {
281
- if (isLoading || !currentImageId)
312
+ if (isLoading || !currentImageIdRef.current)
282
313
  return false;
283
- // Calculate current index directly to avoid stale closure issues
284
- const currentIndex = currentImageList.findIndex(img => img.id === currentImageId);
314
+ // Calculate current index using ref values
315
+ const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
285
316
  if (currentIndex === -1)
286
317
  return false;
287
318
  // If we're not at the last image, next is definitely available
288
- if (currentIndex < currentImageList.length - 1)
319
+ if (currentIndex < currentImageListRef.current.length - 1)
289
320
  return true;
290
321
  // If we're at the last image but there are more pages, next is still available
291
- return hasNextPage;
292
- }, [isLoading, currentImageId, currentImageList, hasNextPage]);
322
+ return hasNextPageRef.current;
323
+ }, [isLoading]);
293
324
  /**
294
325
  * Calculate if previous image navigation is available
295
326
  * Returns true if there's a previous image in the currently loaded list
@@ -297,12 +328,12 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
297
328
  * @returns Boolean indicating if previous navigation is possible
298
329
  */
299
330
  const isPrevAvailable = useCallback(() => {
300
- if (isLoading || !currentImageId)
331
+ if (isLoading || !currentImageIdRef.current)
301
332
  return false;
302
- // Calculate current index directly to avoid stale closure issues
303
- const currentIndex = currentImageList.findIndex(img => img.id === currentImageId);
333
+ // Calculate current index using ref values
334
+ const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
304
335
  return currentIndex > 0;
305
- }, [isLoading, currentImageId, currentImageList]);
336
+ }, [isLoading]);
306
337
  // Initialize when dependencies change
307
338
  useEffect(() => {
308
339
  initializeGallery();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yogiswara/honcho-editor-ui",
3
- "version": "1.4.17",
3
+ "version": "1.4.19",
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",