Skip to content

Commit

Permalink
Experiment: Lightbox should use image cached iamge sources
Browse files Browse the repository at this point in the history
Pros:
- fixes slides not rendering correctly based on their shape
- fixes ability to zoom

Cons: might slow down app
  • Loading branch information
mrdjohnson committed Apr 8, 2024
1 parent cb7fde8 commit 3714e08
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/components/lightbox/LazyLightbox.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
import { useEffect, useState } from 'react'
import { observer } from 'mobx-react-lite'
import Lightbox from 'yet-another-react-lightbox'
import Lightbox, { Slide } from 'yet-another-react-lightbox'
import Thumbnails from 'yet-another-react-lightbox/plugins/thumbnails'
import Download from 'yet-another-react-lightbox/plugins/download'
import Captions from 'yet-another-react-lightbox/plugins/captions'
import Zoom from 'yet-another-react-lightbox/plugins/zoom'
import _ from 'lodash'

import { chatStore } from '~/models/ChatStore'
import CachedImage from '~/components/CachedImage'
import CachedStorage from '~/utils/CachedStorage'

import 'yet-another-react-lightbox/styles.css'
import 'yet-another-react-lightbox/plugins/thumbnails.css'
import 'yet-another-react-lightbox/plugins/captions.css'

const LazyLightbox = observer(() => {
const [slides, setSlides] = useState<Array<Slide & { uniqId: string }>>([])
const chat = chatStore.selectedChat

const getAllSlideImages = async () => {
if (!chat) return

const slides = []

for (const slide of chat.lightboxSlides) {
const src = await CachedStorage.get(slide.src)

if (src) {
slides.push({
...slide,
src,
})
}
}

setSlides(slides)
}

useEffect(() => {
getAllSlideImages()
}, [chat?.lightboxSlides?.length])

if (!chat) return null

const index = chat.lightboxMessageIndex
if (index === -1) return null

const slides = chat.lightboxSlides
if (index === -1 || _.isEmpty(slides)) return null

return (
<Lightbox
close={chat.closeLightbox}
plugins={[Thumbnails, Download, Captions, Zoom]}
download={{
download: ({ slide, saveAs }) => {
CachedStorage.get(slide.src).then(imageData => {
if (!imageData) return
let name: string | undefined

let name: string | undefined
if (_.isString(slide.description)) {
name = _.snakeCase(slide.description)
}

if (_.isString(slide.description)) {
name = _.snakeCase(slide.description)
}

return saveAs(imageData, name)
})
saveAs(slide.src, name)
},
}}
index={index}
Expand All @@ -49,10 +67,6 @@ const LazyLightbox = observer(() => {
view: ({ index }) => chat.setLightboxMessageById(slides[index].uniqId),
}}
slides={slides}
render={{
slide: ({ slide }) => <CachedImage src={slide.src} />,
thumbnail: ({ slide }) => <CachedImage src={slide.src} />,
}}
controller={{ closeOnBackdropClick: true }}
zoom={{ maxZoomPixelRatio: 7 }}
open
Expand Down

0 comments on commit 3714e08

Please sign in to comment.