Skip to content

Commit 3b575f1

Browse files
committed
feat(carousel): use shallow query param to manage modal open/close
1 parent b0f01c1 commit 3b575f1

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

components/carousel.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ArrowRight from '@/svgs/arrow-right-line.svg'
55
import styles from './carousel.module.css'
66
import { useShowModal } from './modal'
77
import { Dropdown } from 'react-bootstrap'
8+
import { useRouter } from 'next/router'
89

910
function useSwiping ({ moveLeft, moveRight }) {
1011
const [touchStartX, setTouchStartX] = useState(null)
@@ -114,17 +115,29 @@ function CarouselOverflow ({ originalSrc, rel }) {
114115
export function CarouselProvider ({ children }) {
115116
const media = useRef(new Map())
116117
const showModal = useShowModal()
118+
const router = useRouter()
119+
const isCarouselOpenRef = useRef(false)
117120

118-
const showCarousel = useCallback(({ src }) => {
121+
const showCarousel = useCallback(async ({ src }) => {
119122
// only show confirmed entries
120123
const confirmedEntries = Array.from(media.current.entries())
121124
.filter(([, entry]) => entry.confirmed)
122125

126+
const index = confirmedEntries.findIndex(([key]) => key === src)
127+
const { nodata, ...rest } = router.query
128+
const query = { ...rest, imageId: String(index >= 0 ? index : 0) }
129+
await router.push({ pathname: router.pathname, query }, undefined, { shallow: true })
130+
isCarouselOpenRef.current = true
123131
showModal((close, setOptions) => {
124132
return <Carousel close={close} mediaArr={confirmedEntries} src={src} setOptions={setOptions} />
125133
}, {
126134
fullScreen: true,
127-
overflow: <CarouselOverflow {...media.current.get(src)} />
135+
overflow: <CarouselOverflow {...media.current.get(src)} />,
136+
onClose: async () => {
137+
const { imageId, nodata, ...rest } = router.query
138+
isCarouselOpenRef.current = false
139+
await router.replace({ pathname: router.pathname, query: rest }, undefined, { shallow: true })
140+
}
128141
})
129142
}, [showModal])
130143

@@ -144,6 +157,20 @@ export function CarouselProvider ({ children }) {
144157
media.current.delete(src)
145158
}, [])
146159

160+
useEffect(() => {
161+
const { imageId } = router.query
162+
if (!imageId || isCarouselOpenRef.current) return
163+
164+
const confirmedEntries = Array.from(media.current.entries())
165+
.filter(([, entry]) => entry.confirmed)
166+
167+
const index = Number(imageId)
168+
if (Number.isInteger(index) && index >= 0 && index < confirmedEntries.length) {
169+
const srcAtIndex = confirmedEntries[index][0]
170+
showCarousel({ src: srcAtIndex })
171+
}
172+
}, [router.query.imageId, showCarousel])
173+
147174
const value = useMemo(
148175
() => ({ showCarousel, addMedia, confirmMedia, removeMedia }),
149176
[showCarousel, addMedia, confirmMedia, removeMedia]

0 commit comments

Comments
 (0)