use-project-output-files.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { useEffect, useState } from 'react'
  2. import { postJSON } from '../../../infrastructure/fetch-json'
  3. import { fileCollator } from '../util/file-collator'
  4. const alphabetical = (a, b) => fileCollator.compare(a.path, b.path)
  5. export function useProjectOutputFiles(projectId) {
  6. const [loading, setLoading] = useState(false)
  7. const [data, setData] = useState(null)
  8. const [error, setError] = useState(false)
  9. useEffect(() => {
  10. if (projectId) {
  11. setLoading(true)
  12. setError(false)
  13. setData(null)
  14. postJSON(`/project/${projectId}/compile`, {
  15. body: {
  16. check: 'silent',
  17. draft: false,
  18. incrementalCompilesEnabled: false
  19. }
  20. })
  21. .then(data => {
  22. if (data.status === 'success') {
  23. const filteredFiles = data.outputFiles.filter(file =>
  24. file.path.match(/.*\.(pdf|png|jpeg|jpg|gif)/)
  25. )
  26. setData(filteredFiles.sort(alphabetical))
  27. } else {
  28. setError(true)
  29. }
  30. })
  31. .catch(error => setError(error))
  32. .finally(() => setLoading(false))
  33. }
  34. }, [projectId])
  35. return { loading, data, error }
  36. }