overleaf/services/web/frontend/js/features/file-tree/hooks/use-project-output-files.js
Jakob Ackermann 52073a13a9 Merge pull request #7867 from overleaf/jpa-compile-group
[web] pass compileGroup to clsi-lb

GitOrigin-RevId: c15adbff27e702b3e0f29be5b57f7a9520d8d02f
2022-05-16 08:03:36 +00:00

49 lines
1.5 KiB
JavaScript

import { useEffect, useState } from 'react'
import { postJSON } from '../../../infrastructure/fetch-json'
import { fileCollator } from '../util/file-collator'
import useAbortController from '../../../shared/hooks/use-abort-controller'
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)
const { signal } = useAbortController()
useEffect(() => {
if (projectId) {
setLoading(true)
setError(false)
setData(null)
postJSON(`/project/${projectId}/compile`, {
body: {
check: 'silent',
draft: false,
incrementalCompilesEnabled: false,
},
signal,
})
.then(data => {
if (data.status === 'success') {
const filteredFiles = data.outputFiles.filter(file =>
file.path.match(/.*\.(pdf|png|jpeg|jpg|gif)/)
)
data.outputFiles.forEach(file => {
file.clsiServerId = data.clsiServerId
file.compileGroup = data.compileGroup
})
setData(filteredFiles.sort(alphabetical))
} else {
setError('linked-project-compile-error')
}
})
.catch(error => setError(error))
.finally(() => setLoading(false))
}
}, [projectId, signal])
return { loading, data, error }
}