1- import { useState , useCallback , useMemo } from 'react' ;
1+ import { useState , useCallback , useMemo , useRef } from 'react' ;
22import { useSelector , useDispatch } from 'react-redux' ;
33import { MediaViewProps } from '@/types/Media' ;
44import { selectCurrentViewIndex } from '@/features/imageSelectors' ;
@@ -8,14 +8,14 @@ import {
88 previousImage ,
99 closeImageView ,
1010} from '@/features/imageSlice' ;
11-
1211// Modular components
1312import { MediaViewControls } from './MediaViewControls' ;
1413import { ZoomControls } from './ZoomControls' ;
1514import { MediaThumbnails } from './MediaThumbnails' ;
1615import { MediaInfoPanel } from './MediaInfoPanel' ;
1716import { ImageViewer } from './ImageViewer' ;
1817import { NavigationButtons } from './NavigationButtons' ;
18+ import type { ImageViewerRef } from './ImageViewer' ;
1919
2020// Custom hooks
2121import { useImageViewControls } from '@/hooks/useImageViewControls' ;
@@ -29,17 +29,20 @@ export function MediaView({ onClose, images, type = 'image' }: MediaViewProps) {
2929 // Redux selectors
3030 const currentViewIndex = useSelector ( selectCurrentViewIndex ) ;
3131 const totalImages = images . length ;
32+
3233 const currentImage = useMemo ( ( ) => {
3334 if ( currentViewIndex >= 0 && currentViewIndex < images . length ) {
3435 return images [ currentViewIndex ] ;
3536 }
3637 return null ;
3738 } , [ images , currentViewIndex ] ) ;
38- console . log ( currentViewIndex ) ;
39+
40+ const imageViewerRef = useRef < ImageViewerRef > ( null ) ;
3941
4042 // Local UI state
4143 const [ showInfo , setShowInfo ] = useState ( false ) ;
4244 const [ showThumbnails , setShowThumbnails ] = useState ( false ) ;
45+ const [ resetSignal , setResetSignal ] = useState ( 0 ) ;
4346
4447 // Custom hooks
4548 const { viewState, handlers } = useImageViewControls ( ) ;
@@ -69,39 +72,54 @@ export function MediaView({ onClose, images, type = 'image' }: MediaViewProps) {
6972 [ dispatch , handlers ] ,
7073 ) ;
7174
72- // Slideshow functionality
73- const { isSlideshowActive, toggleSlideshow } = useSlideshow (
74- totalImages ,
75- handleNextImage ,
76- ) ;
77-
78- // Toggle functions
7975 const toggleInfo = useCallback ( ( ) => {
8076 setShowInfo ( ( prev ) => ! prev ) ;
8177 } , [ ] ) ;
8278
79+ // Hooks that depend on currentImage but always declared
8380 const handleToggleFavorite = useCallback ( ( ) => {
8481 if ( currentImage ) {
8582 toggleFavorite ( currentImage . path ) ;
8683 }
8784 } , [ currentImage , toggleFavorite ] ) ;
8885
86+ const handleZoomIn = useCallback ( ( ) => {
87+ imageViewerRef . current ?. zoomIn ( ) ;
88+ } , [ ] ) ;
89+
90+ const handleZoomOut = useCallback ( ( ) => {
91+ imageViewerRef . current ?. zoomOut ( ) ;
92+ } , [ ] ) ;
93+
94+ const handleResetZoom = useCallback ( ( ) => {
95+ imageViewerRef . current ?. reset ( ) ;
96+ handlers . resetZoom ( ) ;
97+ setResetSignal ( ( s ) => s + 1 ) ;
98+ } , [ handlers ] ) ;
99+
89100 // Keyboard navigation
90101 useKeyboardNavigation ( {
91102 onClose : handleClose ,
92103 onNext : handleNextImage ,
93104 onPrevious : handlePreviousImage ,
94- onZoomIn : handlers . handleZoomIn ,
95- onZoomOut : handlers . handleZoomOut ,
105+ onZoomIn : handleZoomIn ,
106+ onZoomOut : handleZoomOut ,
96107 onRotate : handlers . handleRotate ,
97108 onToggleInfo : toggleInfo ,
98109 } ) ;
99110
111+ // Slideshow functionality
112+ const { isSlideshowActive, toggleSlideshow } = useSlideshow (
113+ totalImages ,
114+ handleNextImage ,
115+ ) ;
116+
100117 // Early return if no images or invalid index
101118 if ( ! images . length || currentViewIndex === - 1 || ! currentImage ) {
102119 return null ;
103120 }
104121
122+ // Safe variables
105123 const currentImagePath = currentImage . path ;
106124 const currentImageAlt = `image-${ currentViewIndex } ` ;
107125
@@ -121,28 +139,18 @@ export function MediaView({ onClose, images, type = 'image' }: MediaViewProps) {
121139
122140 { /* Main viewer area */ }
123141 < div
124- className = "relative flex h-full w-full items-center justify-center"
142+ className = "relative flex h-full w-full items-center justify-center overflow-visible "
125143 onClick = { ( e ) => {
126144 if ( e . target === e . currentTarget ) handleClose ( ) ;
127145 } }
128146 >
129147 { type === 'image' && (
130148 < ImageViewer
149+ ref = { imageViewerRef }
131150 imagePath = { currentImagePath }
132151 alt = { currentImageAlt }
133- scale = { viewState . scale }
134- position = { viewState . position }
135152 rotation = { viewState . rotation }
136- isDragging = { viewState . isDragging }
137- onMouseDown = { handlers . handleMouseDown }
138- onMouseMove = { handlers . handleMouseMove }
139- onMouseUp = { handlers . handleMouseUp }
140- onMouseLeave = { handlers . handleMouseUp }
141- onClick = { ( e ) => {
142- if ( e . target === e . currentTarget ) {
143- handleClose ( ) ;
144- }
145- } }
153+ resetSignal = { resetSignal }
146154 />
147155 ) }
148156
@@ -156,10 +164,10 @@ export function MediaView({ onClose, images, type = 'image' }: MediaViewProps) {
156164 { /* Zoom controls */ }
157165 { type === 'image' && (
158166 < ZoomControls
159- onZoomIn = { handlers . handleZoomIn }
160- onZoomOut = { handlers . handleZoomOut }
167+ onZoomIn = { handleZoomIn }
168+ onZoomOut = { handleZoomOut }
161169 onRotate = { handlers . handleRotate }
162- onReset = { handlers . resetZoom }
170+ onReset = { handleResetZoom }
163171 showThumbnails = { showThumbnails }
164172 />
165173 ) }
0 commit comments