overleaf/services/web/frontend/js/shared/hooks/use-pdf-scroll-position.ts
Alf Eaton 7daebe2f42 Store the scroll position of the PDF per root doc (#20376)
GitOrigin-RevId: 561c38b7258df40f0e738fdc2575e23d9d15102a
2024-10-14 10:59:04 +00:00

56 lines
1.7 KiB
TypeScript

import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react'
import customLocalStorage from '@/infrastructure/local-storage'
import { debugConsole } from '@/utils/debugging'
export type PdfScrollPosition = Record<string, any> | undefined
export const usePdfScrollPosition = (
lastCompileRootDocId: string | null | undefined
): [PdfScrollPosition, Dispatch<SetStateAction<PdfScrollPosition>>] => {
// scroll position of the PDF
const [position, setPosition] = useState<PdfScrollPosition>()
const lastCompileRootDocIdRef = useRef<string | null | undefined>(
lastCompileRootDocId
)
useEffect(() => {
lastCompileRootDocIdRef.current = lastCompileRootDocId
}, [lastCompileRootDocId])
const initialScrollPositionRef = useRef<PdfScrollPosition | null>(null)
// load the stored PDF scroll position when the compiled root doc changes
useEffect(() => {
if (lastCompileRootDocId) {
const position = customLocalStorage.getItem(
`pdf.position.${lastCompileRootDocId}`
)
if (position) {
debugConsole.log('loaded position for', lastCompileRootDocId, position)
initialScrollPositionRef.current = position
setPosition(position)
}
}
}, [lastCompileRootDocId])
// store the current root doc's PDF scroll position when it changes
useEffect(() => {
if (
lastCompileRootDocIdRef.current &&
position &&
position !== initialScrollPositionRef.current
) {
debugConsole.log(
'storing position for',
lastCompileRootDocIdRef.current,
position
)
customLocalStorage.setItem(
`pdf.position.${lastCompileRootDocIdRef.current}`,
position
)
}
}, [position])
return [position, setPosition]
}