11'use client'
22
33import { Button } from '@mui/joy'
4+ import { useRouter } from 'next/navigation'
45import type { RefObject } from 'react'
56
67interface UseBookmarkProps < ItemType > {
78 refImageGallery ?: RefObject < any >
89 displayedItems : ItemType [ ]
910 pathname : string
11+ currentIndex ?: number
12+ selectById ?: ( id : string ) => void
1013}
1114
1215export default function useBookmark < ItemType > ( {
1316 refImageGallery,
1417 displayedItems,
1518 pathname,
19+ currentIndex,
20+ selectById,
1621} : UseBookmarkProps < ItemType > ) {
22+ const router = useRouter ( )
23+
1724 const handleBookmark = ( ) => {
1825 // Get current photo ID from displayed items (respects map filter)
19- const currentIndex = refImageGallery ?. current ?. getCurrentIndex ?.( ) ?? 0
20- const currentItem = displayedItems [ currentIndex ]
26+ const galleryIndex = refImageGallery ?. current ?. getCurrentIndex ?.( )
27+ const indexToUse = typeof galleryIndex === 'number' ? galleryIndex : ( currentIndex ?? 0 )
28+ const currentItem = displayedItems [ indexToUse ]
2129 if ( ! currentItem ) return
2230
2331 // Use filename as unique identifier (works across all albums)
@@ -26,9 +34,17 @@ export default function useBookmark<ItemType>({
2634 : ( currentItem as any ) . filename
2735 if ( ! identifier ) return
2836
37+ // Notify parent to track selection (updates memoryIndex to match ID)
38+ if ( selectById ) {
39+ selectById ( identifier )
40+ }
41+
2942 // Copy bookmark URL to clipboard
3043 const bookmarkUrl = `${ window . location . origin } ${ pathname } ?select=${ identifier } `
3144 navigator . clipboard . writeText ( bookmarkUrl )
45+
46+ // Update URL to reflect the selection
47+ router . replace ( `${ pathname } ?select=${ identifier } ` , { scroll : false } )
3248 }
3349
3450 const BookmarkButton = ( ) => (
0 commit comments