Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139import {createContext, useContext, useEffect, useMemo, useState} from 'react'import {type SharedValue, useSharedValue} from 'react-native-reanimated'import {nanoid} from 'nanoid/non-secure'
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'import {useHotkeysContext} from '#/lib/hotkeys'import {measureThumb} from '#/components/Lightbox/measureThumb'import {type ImageSource} from '#/components/Lightbox/types'
export type LightboxMetricsContext = { layout: 'single' | 'grid' | 'carousel' postUri: string postAuthorDid: string feedDescriptor?: string}
export type Lightbox = { id: string images: ImageSource[] index: number // Set for post photo embeds so the lightbox can emit post:photoEmbed:lightboxSwipe. // Left unset for non-post contexts (e.g. profile avatar/banner lightbox). metricsContext?: LightboxMetricsContext /** * Called as the user pages through the images, so the source can keep the * matching thumbnail in view (e.g. by scrolling a carousel) for when the * lightbox closes. */ onIndexChange?: (index: number) => void}
const LightboxContext = createContext<{ activeLightbox: Lightbox | null}>({ activeLightbox: null,})LightboxContext.displayName = 'LightboxContext'
const LightboxControlContext = createContext<{ openLightbox: (lightbox: Omit<Lightbox, 'id'>) => void closeLightbox: () => boolean}>({ openLightbox: () => {}, closeLightbox: () => false,})LightboxControlContext.displayName = 'LightboxControlContext'
const LightboxIsOpenContext = createContext<SharedValue<boolean> | null>(null)LightboxIsOpenContext.displayName = 'LightboxIsOpenContext'
export function Provider({children}: React.PropsWithChildren<{}>) { const [activeLightbox, setActiveLightbox] = useState<Lightbox | null>(null) const isOpen = useSharedValue(false) const {disableScope, enableScope} = useHotkeysContext()
useEffect(() => { if (activeLightbox) { disableScope('global') } else { enableScope('global') } }, [activeLightbox, disableScope, enableScope])
const openLightbox = useNonReactiveCallback( (lightbox: Omit<Lightbox, 'id'>) => { const thumbRef = lightbox.images[lightbox.index]?.thumbRef if (thumbRef) { // Bake the tapped image's rect in so it's there from the first render. const thumbRect = measureThumb(thumbRef) lightbox = { ...lightbox, images: lightbox.images.map((img, i) => i === lightbox.index ? {...img, thumbRect} : img, ), } } setActiveLightbox(prevLightbox => { if (prevLightbox) { // Ignore duplicate open requests. If it's already open, // the user has to explicitly close the previous one first. return prevLightbox } else { return {...lightbox, id: nanoid()} } }) isOpen.set(true) }, )
const closeLightbox = useNonReactiveCallback(() => { let wasActive = !!activeLightbox setActiveLightbox(null) isOpen.set(false) return wasActive })
const state = useMemo( () => ({ activeLightbox, }), [activeLightbox], )
const methods = useMemo( () => ({ openLightbox, closeLightbox, }), [openLightbox, closeLightbox], )
return ( <LightboxContext.Provider value={state}> <LightboxControlContext.Provider value={methods}> <LightboxIsOpenContext.Provider value={isOpen}> {children} </LightboxIsOpenContext.Provider> </LightboxControlContext.Provider> </LightboxContext.Provider> )}
export function useLightbox() { return useContext(LightboxContext)}
export function useLightboxControls() { return useContext(LightboxControlContext)}
/** * Whether a lightbox is open, as a shared value for UI-thread readers. Unlike * `useLightbox()`, reading it doesn't re-render anything when the lightbox * opens or closes. */export function useLightboxIsOpenSharedValue() { return useContext(LightboxIsOpenContext)}