Skip to content

Commit

Permalink
fix: Load more files if the current id is not already loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Nov 5, 2024
1 parent bbcc70c commit 9a14a2e
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/modules/views/Public/PublicFileViewer.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCurrentFolderId } from 'hooks'
import React, { useMemo } from 'react'
import React, { useMemo, useEffect, useState } from 'react'
import { useParams, useNavigate } from 'react-router-dom'

import FooterActionButtons from 'cozy-ui/transpiled/react/Viewer/Footer/FooterActionButtons'
Expand All @@ -13,6 +13,8 @@ const PublicFileViewer = () => {
const { fileId } = useParams()
const navigate = useNavigate()

const [fetchingMore, setFetchingMore] = useState(false)

const currentFolderId = useCurrentFolderId()

const filesResult = usePublicFilesQuery(currentFolderId)
Expand All @@ -27,6 +29,33 @@ const PublicFileViewer = () => {
[hasCurrentIndex, currentIndex]
)

useEffect(() => {
let isMounted = true

// If we can found the current file but we know there is more file inside the folder
const fetchMoreIfNecessary = async () => {
if (fetchingMore) {
return
}

setFetchingMore(true)
try {
const currentIndex = viewableFiles.findIndex(f => f.id === fileId)
if (currentIndex === -1 && filesResult.hasMore && isMounted) {
await filesResult.fetchMore()
}
} finally {
setFetchingMore(false)
}
}

fetchMoreIfNecessary()

return () => {
isMounted = false
}
}, [fetchingMore, filesResult, fileId, viewableFiles])

const handleChange = ({ _id }) => {
navigate(`../${_id}`, {
relative: 'path'
Expand Down

0 comments on commit 9a14a2e

Please sign in to comment.