mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-04 01:46:52 -05:00
ba4300d9e1
Migrate "Add Files" modal to React GitOrigin-RevId: fc5235108ee65294e3176da9c327791c34aa5b3c
42 lines
1.1 KiB
JavaScript
42 lines
1.1 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 }
|
|
}
|