use-project-output-files.js 1.5 KB

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