overleaf/services/web/frontend/js/features/file-tree/hooks/use-project-output-files.js
Alf Eaton 1be43911b4 Merge pull request #3942 from overleaf/prettier-trailing-comma
Set Prettier's "trailingComma" setting to "es5"

GitOrigin-RevId: 9f14150511929a855b27467ad17be6ab262fe5d5
2021-04-28 02:10:01 +00:00

42 lines
1.2 KiB
JavaScript

import { useEffect, useState } from 'react'
import { postJSON } from '../../../infrastructure/fetch-json'
import { fileCollator } from '../util/file-collator'
const alphabetical = (a, b) => fileCollator.compare(a.path, b.path)
export function useProjectOutputFiles(projectId) {
const [loading, setLoading] = useState(false)
const [data, setData] = useState(null)
const [error, setError] = useState(false)
useEffect(() => {
if (projectId) {
setLoading(true)
setError(false)
setData(null)
postJSON(`/project/${projectId}/compile`, {
body: {
check: 'silent',
draft: false,
incrementalCompilesEnabled: false,
},
})
.then(data => {
if (data.status === 'success') {
const filteredFiles = data.outputFiles.filter(file =>
file.path.match(/.*\.(pdf|png|jpeg|jpg|gif)/)
)
setData(filteredFiles.sort(alphabetical))
} else {
setError(true)
}
})
.catch(error => setError(error))
.finally(() => setLoading(false))
}
}, [projectId])
return { loading, data, error }
}