@yogiswara/honcho-editor-ui 1.4.22 → 1.4.24

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.
@@ -91,17 +91,6 @@ export function useHonchoEditor(controller, initImageId, firebaseUid) {
91
91
  const initialHeight = useRef(0);
92
92
  const panelRef = useRef(null);
93
93
  const contentRef = useRef(null);
94
- // Effect for measuring mobile panel content
95
- // useEffect(() => {
96
- // const timeoutId = setTimeout(() => {
97
- // if (contentRef.current) {
98
- // const height = contentRef.current.scrollHeight;
99
- // setContentHeight(height);
100
- // }
101
- // }, 50);
102
- // return () => clearTimeout(timeoutId);
103
- // }, [activeSubPanel ]);
104
- // isBulkEditing
105
94
  // Effect for keyboard shortcuts
106
95
  // MARK: - Core Editor Logic
107
96
  // MARK: Batch Edit logic
@@ -602,6 +591,15 @@ export function useHonchoEditor(controller, initImageId, firebaseUid) {
602
591
  const handleCloseCopyDialog = () => setCopyDialogOpen(false);
603
592
  const handleConfirmCopy = () => { handleCopyEdit(); handleCloseCopyDialog(); setShowCopyAlert(true); };
604
593
  // MARK: useEffect HERE!
594
+ useEffect(() => {
595
+ const timeoutId = setTimeout(() => {
596
+ if (contentRef.current) {
597
+ const height = contentRef.current.scrollHeight;
598
+ setContentHeight(height);
599
+ }
600
+ }, 50);
601
+ return () => clearTimeout(timeoutId);
602
+ }, [activePanel, activeSubPanel]);
605
603
  useEffect(() => {
606
604
  if (showCopyAlert) {
607
605
  const timer = setTimeout(() => setShowCopyAlert(false), 2000);
@@ -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,14 +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
- // Add a small delay to ensure state is fully updated
243
- await new Promise(resolve => setTimeout(resolve, 100));
244
222
  // Fetch complete data for the next image
245
223
  const nextImageData = await controller.onGetImage(firebaseUid, nextImage.id);
246
224
  if (nextImageData) {
@@ -256,7 +234,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
256
234
  finally {
257
235
  setIsLoading(false);
258
236
  }
259
- }, [isLoading, loadNextPage, controller, firebaseUid]);
237
+ }, [currentImageId, isLoading, getCurrentImageIndex, currentImageList, loadNextPage, controller, firebaseUid]);
260
238
  /**
261
239
  * Navigate to the previous image in the gallery
262
240
  * Only works within the currently loaded image list
@@ -266,22 +244,20 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
266
244
  */
267
245
  const onSwipePrev = useCallback(async () => {
268
246
  // Prevent action if no current image or already loading
269
- if (!currentImageIdRef.current || isLoading)
247
+ if (!currentImageId || isLoading)
270
248
  return;
271
249
  setIsLoading(true);
272
250
  setError(null);
273
251
  try {
274
- // Calculate current index using ref values
275
- const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
252
+ // Calculate current index
253
+ const currentIndex = getCurrentImageIndex();
276
254
  if (currentIndex === -1) {
277
255
  throw new Error('Current image not found in list');
278
256
  }
279
257
  if (currentIndex > 0) {
280
258
  // Navigate to previous image in the current list
281
- const prevImage = currentImageListRef.current[currentIndex - 1];
259
+ const prevImage = currentImageList[currentIndex - 1];
282
260
  setCurrentImageId(prevImage.id);
283
- // Immediately update the ref to prevent stale closure issues
284
- currentImageIdRef.current = prevImage.id;
285
261
  // Fetch complete data for the previous image
286
262
  const prevImageData = await controller.onGetImage(firebaseUid, prevImage.id);
287
263
  if (prevImageData) {
@@ -301,7 +277,7 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
301
277
  finally {
302
278
  setIsLoading(false);
303
279
  }
304
- }, [isLoading, controller, firebaseUid]);
280
+ }, [currentImageId, isLoading, getCurrentImageIndex, currentImageList, controller, firebaseUid]);
305
281
  /**
306
282
  * Calculate if next image navigation is available
307
283
  * Returns true if:
@@ -311,18 +287,18 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
311
287
  * @returns Boolean indicating if next navigation is possible
312
288
  */
313
289
  const isNextAvailable = useCallback(() => {
314
- if (isLoading || !currentImageIdRef.current)
290
+ if (isLoading || !currentImageId)
315
291
  return false;
316
- // Calculate current index using ref values
317
- const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
292
+ // Calculate current index
293
+ const currentIndex = getCurrentImageIndex();
318
294
  if (currentIndex === -1)
319
295
  return false;
320
296
  // If we're not at the last image, next is definitely available
321
- if (currentIndex < currentImageListRef.current.length - 1)
297
+ if (currentIndex < currentImageList.length - 1)
322
298
  return true;
323
299
  // If we're at the last image but there are more pages, next is still available
324
- return hasNextPageRef.current;
325
- }, [isLoading]);
300
+ return hasNextPage;
301
+ }, [isLoading, getCurrentImageIndex, currentImageList.length, hasNextPage]);
326
302
  /**
327
303
  * Calculate if previous image navigation is available
328
304
  * Returns true if there's a previous image in the currently loaded list
@@ -330,23 +306,31 @@ export function useGallerySwipe(firebaseUid, initImageId, controller) {
330
306
  * @returns Boolean indicating if previous navigation is possible
331
307
  */
332
308
  const isPrevAvailable = useCallback(() => {
333
- if (isLoading || !currentImageIdRef.current)
309
+ if (isLoading || !currentImageId)
334
310
  return false;
335
- // Calculate current index using ref values
336
- const currentIndex = currentImageListRef.current.findIndex(img => img.id === currentImageIdRef.current);
311
+ // Calculate current index
312
+ const currentIndex = getCurrentImageIndex();
337
313
  return currentIndex > 0;
338
- }, [isLoading]);
314
+ }, [isLoading, getCurrentImageIndex]);
339
315
  // Initialize when dependencies change
340
316
  useEffect(() => {
341
317
  initializeGallery();
342
318
  }, [initializeGallery]);
343
- // Update currentImageId when initImageId changes (but only if different)
319
+ // Update currentImageId when initImageId changes from external source
344
320
  // This allows the hook to respond to external changes to the initial image ID
321
+ // but should not interfere with internal navigation
345
322
  useEffect(() => {
346
- if (initImageId && initImageId !== currentImageId) {
347
- 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
+ }
348
332
  }
349
- }, [initImageId, currentImageId]);
333
+ }, [initImageId]);
350
334
  // Return all the functionality and state that components need
351
335
  return {
352
336
  currentImageData,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yogiswara/honcho-editor-ui",
3
- "version": "1.4.22",
3
+ "version": "1.4.24",
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",