File size: 2,095 Bytes
523133d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { useEffect } from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import './ImageModal.css';

function ImageModal({
  modalState,
  setModalState,
  figures
}) {
  const { show, currentIndex } = modalState;

  const closeModal = () => setModalState({ ...modalState, show: false });
  const prevImage = () => setModalState({
    ...modalState,
    currentIndex: (currentIndex - 1 + figures.length) % figures.length
  });
  const nextImage = () => setModalState({
    ...modalState,
    currentIndex: (currentIndex + 1) % figures.length
  });

  useEffect(() => {
    const handleKeyDown = (e) => {
      if (!show) return;
      if (e.key === 'ArrowLeft') prevImage();
      if (e.key === 'ArrowRight') nextImage();
      if (e.key === 'Escape') closeModal();
    };
    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, [show, currentIndex, figures.length]);

  if (!show || figures.length === 0) return null;
  return (
    <>
      <div className="modal fade show d-block" tabIndex="-1" role="dialog" onClick={closeModal}>
        <div className="modal-dialog modal-lg modal-dialog-centered" role="document" onClick={(e) => e.stopPropagation()}>
          <div className="modal-content">
            <div className="modal-body text-center">
              <img
                className="img-fluid"
                src={`data:image/png;base64,${figures[currentIndex]}`}
                alt={`Fig-${currentIndex}`}
              />
            </div>
            <div className="modal-footer justify-content-between">
              <button className="btn btn-secondary" onClick={prevImage}>Prev</button>
              <span>Image {currentIndex + 1}/{figures.length}</span>
              <button className="btn btn-secondary" onClick={nextImage}>Next</button>
              <button className="btn btn-danger" onClick={closeModal}>Close</button>
            </div>
          </div>
        </div>
      </div>
      <div className="modal-backdrop fade show" />
    </>
  );
}

export default ImageModal;